一、前言
记录一个用于yolo 目标检测的数据增强代码,当数据很少时,可对数据做 亮度对比度随机变化、裁减、上下左右翻转。
二、上代码
import PIL.Image as Image
import os
from torchvision import transforms as transforms
import random
from PIL import ImageDraw
import cv2
import numpy as np
def getBoxTxt( txt_path , h,w):
with open(txt_path, 'r') as f:
list1 = f.readlines()
info = []
for i in list1:
s = i.rstrip('\n').strip(" ")
if s =="":continue
tmp_list = s.split(" ")
class_n = int(tmp_list[0])
tt = [float(e) for e in tmp_list[1:] ]
t_0,t_1,t_2,t_3 = tt
tt[0] = int(t_0*w-t_2*w/2 ) #x0
tt[1] = int(t_1*h-t_3*h/2 ) #y0
tt[2] = int(t_0*w+t_2*w/2 ) #x1
tt[3] = int(t_1*h+t_3*h/2 ) #y1
info