yolov5翻转数据增强

import cv2
from PIL import Image
import numpy as np
import os

for file in os.listdir('/home/lixuan/Downloads/payactions/scanandpay/images/train'):
    imgpath = '/home/lixuan/Downloads/payactions/scanandpay/images/train/{}'.format(file)
    txtpath = '/home/lixuan/Downloads/payactions/scanandpay/labels/train/{}'.format(file.replace('jpg','txt'))

    img = cv2.imread(imgpath)
    f = open(txtpath)
    datas = f.readlines()
    f.close()
    fw = open('/home/lixuan/Downloads/payactions/scanandpay/rotatelabel/rotate_{}'.format(file.replace('jpg','txt')),'w')
    for data in datas:
        data = data.strip().split(' ')
        lab = data[0]
        data = data[1:]
        data = [float(d) for d in data]
        data[0] *= img.shape[1]
        data[1] *= img.shape[0]
        data[2] *= img.shape[1]
        data[3] *= img.shape[0]
        x = img.shape[1] - data[0]
        y = img.shape[0] - data[1]
        w = data[2]
        h = data[3]
        fw.write(lab + ' ' + str(x / img.shape[1]) + ' ' + str(y / img.shape[0]) + ' ' + str(w / img.shape[1]) + ' ' + str(h / img.shape[0]) + '\n')
    fw.close()
    img = Image.fromarray(img)
    img = img.transpose(Image.ROTATE_180)
    img.save('/home/lixuan/Downloads/payactions/scanandpay/roteteimg/rotate_{}'.format(file))
import cv2
from PIL import Image
import numpy as np
import os

for file in os.listdir('/home/lixuan/fromLX/coco/images/train'):
    imgpath = '/home/lixuan/fromLX/coco/images/train/{}'.format(file)
    txtpath = '/home/lixuan/fromLX/coco/labels/train/{}'.format(file.replace('jpg', 'txt').replace('JPG', 'txt').replace('jpeg', 'txt'))

    img = cv2.imread(imgpath)
    DS = open(txtpath)
    datas = DS.readlines()
    for data in datas:
        data = data.strip().split(' ')
        data = [float(x) for x in data]
        label = str(int(data[0]))

        data[1] *= img.shape[1]
        data[2] *= img.shape[0]
        data[3] *= img.shape[1]
        data[4] *= img.shape[0]
        x = data[1]
        y = data[2]
        w = data[3]
        h = data[4]
        x1 = int(x - w / 2)
        y1 = int(y - h / 2)
        x2 = int(x1 + w)
        y2 = int(y1 + h)
        if label == '0':
            cv2.rectangle(img, (x1, y1), (x2, y2), (0, 255, 0), 5)
        else:
            cv2.rectangle(img, (x1, y1), (x2, y2), (0, 0, 255), 5)
    cv2.imwrite('/home/lixuan/fromLX/coco/show/{}'.format(file),img)
    # cv2.namedWindow('img', 0)
    # cv2.imshow('img', img)
    # cv2.waitKey(0)

 


版权声明:本文为weixin_38241876原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。