仅作笔记整理搬运使用,没有任何程序原创部分,主要给自己提个醒,避免重复搜索工作
不会编程,只能拼凑
一、数据增强(亮度、对比度)
UAV里面是单目标标注,其他目标需要自己手动标注,每隔20张取一张标注,再数据增强扩增数据集
import os
import numpy as np
import cv2
import random
from PIL import Image
import matplotlib.pyplot as plt
from matplotlib import cm as CM
def flip_img(image, img_name):
img1 = cv2.flip(image, 1)
cv2.imwrite(path_name + '/' + img_name + '_flip.jpg', img1)
# def contrast_img(img_name, image, label, h_image, h_label, c, d, e, f):
def contrast_img(img_name, image, c, d, e, f):
img2 = np.uint8(np.clip((c * image + random.randint(-30, 30)), 0, 255))
img3 = np.uint8(np.clip((d * image + random.randint(-30, 30)), 0, 255))
img4 = np.uint8(np.clip((e * image + random.randint(-30, 30)), 0, 255))
img5 = np.uint8(np.clip((f * image + random.randint(-30, 30)), 0, 255))
# h_img2 = np.uint8(np.clip((c * h_image + random.randint(-30, 30)), 0, 255))
# h_img3 = np.uint8(np.clip((d * h_image + random.randint(-30, 30)), 0, 255))
# h_img4 = np.uint8(np.clip((e * h_image + random.randint(-30, 30)), 0, 255))
# h_img5 = np.uint8(np.clip((f * h_image + random.randint(-30, 30)), 0, 255))
cv2.imwrite(save_name + '/' + img_name + '_LD1.jpg', img2)
# label.save(save_label + '/' + img_name + '_LD1.png')
cv2.imwrite(save_name + '/' + img_name + '_LD2.jpg', img3)
# label.save(save_label + '/' + img_name + '_LD2.png')
cv2.imwrite(save_name + '/' + img_name + '_LD3.jpg', img4)
# label.save(save_label + '/' + img_name + '_LD3.png')
cv2.imwrite(save_name + '/' + img_name + '_LD4.jpg', img5)
# label.save(save_label + '/' + img_name + '_LD4.png')
# cv2.imwrite(save_name + '/' + img_name + '_flip_LD1.jpg', h_img2)
# h_label.save(save_label + '/' + img_name + '_flip_LD1.png')
#
# cv2.imwrite(save_name + '/' + img_name + '_flip_LD2.jpg', h_img3)
# h_label.save(save_label + '/' + img_name + '_flip_LD2.png')
#
# cv2.imwrite(save_name + '/' + img_name + '_flip_LD3.jpg', h_img4)
# h_label.save(save_label + '/' + img_name + '_flip_LD3.png')
#
# cv2.imwrite(save_name + '/' + img_name + '_flip_LD4.jpg', h_img5)
# h_label.save(save_label + '/' + img_name + '_flip_LD4.png')
#
#
# cv2.imwrite(save_name + '/' + img_name + '_flip_LD0.jpg', h_image)
# h_label.save(save_label + '/' + img_name + '_flip_LD0.png')
if __name__ == "__main__":
path_name = 'C:/Users/SHS/Desktop/img1' #图片地址
# label_dir = 'data_ROAD/new_Labels_40_15'
save_name = 'C:/Users/SHS/Desktop/img_pro' #增强后图片地址
# save_label = 'data_ROAD/new_imp_Labels_40_15'
num = 0
color = (0, 255, 0)
for dir_item in os.listdir(path_name):
# 从初始路径开始叠加,合并成可识别的操作路径
if dir_item.endswith('.jpg'):
img_name = os.path.splitext(dir_item)[0]
# full_path = os.path.abspath(os.path.join(path_name, dir_item))
image = cv2.imread(os.path.abspath(os.path.join(path_name, dir_item)))
# label = cv2.imread(os.path.abspath(os.path.join(label_dir, img_name+'.png')))
# label = Image.open(os.path.abspath(os.path.join(label_dir, img_name+'.png')))
# print(label)
# print(origin)
# origin = np.array(origin)
# plt.imshow(origin, cmap=CM.jet)
# plt.show()
# h_image = cv2.flip(image, 1) # 图像水平翻转
# h_label = label.transpose(Image.FLIP_LEFT_RIGHT)
# contrast_img(img_name, image, label, h_image, h_label, 0.5, 0.8, 1.3, 1.6)
contrast_img(img_name, image, 0.5, 0.8, 1.3, 1.6)
print(img_name)
需要图片整体批量重命名
然后把xml复制四遍,批量重命名后需要把xml中的filename换成本身的文件名
二、图片翻转
img和xml同时翻转
import cv2
from PIL import Image
import xml.etree.ElementTree as ET
from tqdm import tqdm
import os
def flip_horizontal(jpg_file, new_jpg):
im = Image.open(jpg_file)
# out = im.transpose(Image.ROTATE_180)
out = im.transpose(Image.FLIP_LEFT_RIGHT)
out.save(new_jpg)
def flip_xml(xml_file, new_xml):
tree = ET.parse(xml_file)
objs = tree.findall('object')
for ix, obj in enumerate(objs):
name = obj.find('name').text
if name == 'car&#