针对警告标志进行模拟数据的生成
从标准库中截取标准图案,去掉白色背景
# 将空白处设置为零
img_dir = './all_moni_img'
save_dir = './all_moni_img_bgblack'
imgs_name = os.listdir(img_dir)
for img_name in imgs_name:
img = cv2.imread(os.path.join(img_dir,img_name))
img = np.array(img)
bool_ar = img<250
img =img*bool_ar
img = cv2.resize(img,(48,48))
cv2.imwrite(os.path.join(save_dir,img_name),img)
对上面得到的结果进行数据增广
import cv2,os
import imgaug as ia
from imgaug import augmenters as iaa
import numpy as np
ia.seed(1)
seq = iaa.Sequential([
# iaa.Fliplr(1.0), # 按概率水平翻转
iaa.Crop(percent=(0, 0.1)), # 随机裁剪
iaa.GaussianBlur(sigma=(1., 1.0)), # 模糊处理
# iaa.ContrastNormalization((1., 1.5)), # 进行对比度调整
# iaa.AdditiveGaussianNoise(loc=0, scale=(0.0, 0.005*255)), # 高斯噪声,对每个图片都在N(0,s)(均值为零,方差在0
# 到0.005*255之间随机取)的正太分布上进行一次采样。
iaa.Multiply((0.1,1.5)), # 亮度调节
iaa.Affine(
scale={"x": (0.8, 1.2), "y": (0.8, 1.2)},
translate_percent={"x": (-0.1, 0.1), "y": (-0.1, 0.1)},
rotate=(-5, 5),
# shear=(-8, 8)
) # 仿射变换 scale表示尺度,translate_percent: 平移比例,0表示不平移,0.5表示平移50%。
# rotate: 平移角度,0~360度之间,正负也可以表示方向,shear: 错切的程度,0~360度之间,正负表示方向。
# 详细参考:https://blog.csdn.net/a8039974/article/details/82791732
], random_order=False) # apply augmenters in random order
def mk_dir(pa):
if not os.path.exists(pa):
os.mkdir(pa)
if __name__ == '__main__':
imgs_name = os.listdir('/home/tf/China_TSR_test_new_net/test_teacher_idea/all_moni_img_bgblack')
for img_name in imgs_name:
mk_dir('/home/tf/China_TSR_test_new_net/test_teacher_idea/aug_sou_img/'+img_name[:-4])
img = cv2.imread('/home/tf/China_TSR_test_new_net/test_teacher_idea/all_moni_img_bgblack/'+img_name)
for i in range(50):
img_aug = seq.augment_image(img)
cv2.imwrite('/home/tf/China_TSR_test_new_net/test_teacher_idea/aug_sou_img/%s/au_%d'%(img_name[:-4],i)+img_name,img_aug)
从三张图片中随机截取背景,将交通标志贴上去
# 从实际场景中裁背景,将图片贴上去。
imgs_name = ['000028.jpg','000029.jpg','000030.jpg']
for img_name in imgs_name:
img = cv2.imread(img_name)
fils_name = os.listdir('./aug_sou_img')
for fil_name in fils_name:
print('img_name:',img_name,'fil_name:',fil_name)
mk_dir('./res/'+fil_name)
ts_imgs_name = os.listdir('./aug_sou_img/'+fil_name)
for ts_img_name in ts_imgs_name:
ts_img = cv2.imread('./aug_sou_img/'+fil_name+'/'+ts_img_name)
for gh in range(5):
ran = int(random.random()*120)
while ran<60:
ran = int(random.random()*120)
x1 = int(np.random.random()*1000)
y1 = int(np.random.random()*700)
bg = img[y1:y1+ran,x1:x1+ran,:]
bg = cv2.resize(bg,(52,52))
sou_bg = np.copy(bg)
bg[int(bg.shape[0]/2-24):int(bg.shape[0]/2+24),int(bg.shape[0]/2-24):int(bg.shape[0]/2+24),:] = ts_img
h,w,c = bg.shape
for i in range(h):
for j in range(w):
for k in range(c):
if(bg[i][j][k]==0):
bg[i][j][k] = sou_bg[i][j][k]
cv2.imwrite('./res/'+fil_name+'/%s_%s_%d.png'%(img_name[:-4],ts_img_name[:-4],gh),bg)