安装命令:pip install imgaug
代码实现:
import cv2
import random
import matplotlib.pyplot as pltfrom imgaug import augmenters as iaa
# 数据增强——缩放效果
def zoom_img(img):# 获取一个1-1.3倍的线性图像处理器,scale参数是缩放范围zoom = iaa.Affine(scale=(0.5, 1.3)) # 将图片进行增强并返回return zoom.augment_image(img)# 数据增强——平移效果
def trans_img(img):trans = iaa.Affine(translate_percent={"x":(-0.1,-0.1),"y":(-0.1,-0.1)})# 将图片进行增强并返回return trans.augment_image(img)# 数据增强——亮度变化
def img_random_brightness(img):# 亮度增加1.1-1.5倍brightness = iaa.Multiply((1.1,1.5))# 将图片进行增强并返回return brightness.augment_image(img)# 数据增强——镜像变化(按照业务需求,角度也要变化)
def img_flip(img,steering_angle):# 0-水平翻转,1-垂直翻转img = cv2.flip(img,1)steering_angle = -steering_anglereturn img,steering_angle# 随机图片增强器
def random_agument(img_path,steering_angle):index = random.randint(0,10)img = plt.imread(img_path)if index <=3:img = zoom_img(img)elif 3< index <6:img = trans_img(img)elif 6<=index<8:img = img_random_brightness(img)else:img,steering_angle = img_flip(img,steering_angle)return img,steering_angle