当前位置:网站首页>目标检测带标签数据增强代码
目标检测带标签数据增强代码
2022-07-24 05:20:00 【河海CV小菜鸡】
注意:我的数据集名字从1开始的,所以是for i in range(1,num),所以倒数第三句 str(i + num-1) 如果是从零开始就不用-1。另外文件名以及图片大小(1280,1024)也要根据自己的数据集做修改 图片平移:
import cv2 as cv
import numpy as np
import random
import xml.etree.ElementTree as et
# 图片平移 i + num
def photo_move(name, num):
for i in range(1, num):
a = random.choice([-1, 1])
b = random.choice([-1, 1])
c = random.randint(30, 50)
d = random.randint(30, 50)
e1 = random.randint(0, 255)
f1 = random.randint(0, 255)
g1 = random.randint(0, 255)
# 定义位移矩阵,x方向移动a*c,y方向移动b*d,并且使用random库实现随机偏移
matshift = np.float32([[1, 0, a*c], [0, 1, b*d]])
# C:\Users\lx\Desktop\dataset\ban\photo_raw
# 生成图片并保存
img = cv.imread('C:/Users/86188/Desktop/dataset/' + name + '/photo_raw/' + name + str(i) + '.jpg')
dst = cv.warpAffine(img, matshift, (1280,1024), borderValue = (e1, f1, g1))
cv.imwrite('C:/Users/86188/Desktop/dataset/' + name + '/photo_enhancement/' + name + str(i + num-1) + '.jpg', dst)
# 获得二维矩阵
xml_path = 'C:/Users/86188/Desktop/dataset/' + name + '/label_raw/' + name + str(i) + '.xml'
xml_file = et.parse(xml_path)
root = xml_file.getroot()
objs = root.findall('object')
for obj in objs:
bnd = obj.find('bndbox')
# if obj.find('name').text != name:
# print('wrong')
# break
xmin = float(bnd.find('xmin').text)
ymin = float(bnd.find('ymin').text)
xmax = float(bnd.find('xmax').text)
ymax = float(bnd.find('ymax').text)
ju_zhen = np.array([[xmin, xmax],
[ymin, ymax],
[1, 1]])
# 保存变换后坐标点文件
juzhen_new = np.dot(matshift, ju_zhen) # 矩阵乘法
if juzhen_new[0][0] + juzhen_new[1][0] > juzhen_new[0][1] + juzhen_new[1][1]:
break
bnd.find('xmin').text = str(int(juzhen_new[0][0]))
bnd.find('ymin').text = str(int(juzhen_new[1][0]))
bnd.find('xmax').text = str(int(juzhen_new[0][1]))
bnd.find('ymax').text = str(int(juzhen_new[1][1]))
xml_path_new = 'C:/Users/86188/Desktop/dataset/' + name + '/label_enhancement/' + name + str(i + num-1) + '.xml'
xml_file.write(xml_path_new) # 更新xml文件
print('平移', i+num)图片缩放 :
def photo_zoom_in_and_out(name, num):
for i in range(1, num):
a = random.choice([0.8, 0.9, 0.95, 1.05, 1.08])
b = random.choice([0.8, 0.9, 0.98, 1.05, 1.08])
d = random.randint(0, 255)
e = random.randint(0, 255)
f = random.randint(0, 255)
# 定义缩放矩阵
matshift = np.float32([[a, 0, 0], [0, b, 0]]) # 定义缩放矩阵,长宽比为随机数
# 生成并保存图片
img = cv.imread('C:/Users/86188/Desktop/dataset/' + name + '/photo_raw/' + name + str(i) + '.jpg')
dst = cv.warpAffine(img, matshift, (1280,1024), borderValue=(d, e, f)) # 画布尺寸是和原来一样
cv.imwrite('C:/Users/86188/Desktop/dataset/' + name + '/photo_enhancement/' + name + str(i + 2* num-1) + '.jpg', dst)
# 获得二维矩阵
xml_path = 'C:/Users/86188/Desktop/dataset/' + name + '/label_raw/' + name + str(i) + '.xml'
xml_file = et.parse(xml_path)
root = xml_file.getroot()
objs = root.findall('object')
for obj in objs:
bnd = obj.find('bndbox')
xmin = float(bnd.find('xmin').text)
ymin = float(bnd.find('ymin').text)
xmax = float(bnd.find('xmax').text)
ymax = float(bnd.find('ymax').text)
ju_zhen = np.array([[xmin, xmax],
[ymin, ymax],
[1, 1]])
# 保存变换后坐标点文件
juzhen_new = np.dot(matshift, ju_zhen)
if juzhen_new[0][0] + juzhen_new[1][0] > juzhen_new[0][1] + juzhen_new[1][1]:
break
bnd.find('xmin').text = str(int(juzhen_new[0][0]))
bnd.find('ymin').text = str(int(juzhen_new[1][0]))
bnd.find('xmax').text = str(int(juzhen_new[0][1]))
bnd.find('ymax').text = str(int(juzhen_new[1][1]))
xml_path_new = 'C:/Users/86188/Desktop/dataset/' + name + '/label_enhancement/' + name + str(i + 2* num-1) + '.xml'
xml_file.write(xml_path_new) # 更新xml文件
print('缩放', i + 2 * num)
水平翻转:
def shuiping_photo_fan_zhuan(name, num):
# 参数2 必选参数。用于指定镜像翻转的类型,0表示绕×轴正直翻转,即垂直镜像翻转;1表示绕y轴翻转,即水平镜像翻转;-1表示绕×轴、y轴两个轴翻转,即对角镜像翻转。
for i in range(1, num):
img = cv.imread('C:/Users/86188/Desktop/dataset/' + name + '/photo_raw/' + name + str(i) + '.jpg')
dst = cv.flip(img, 1)
cv.imwrite('C:/Users/86188/Desktop/dataset/' + name + '/photo_enhancement/' + name + str(i + 3* num-1) + '.jpg', dst)
# 获得坐标值
xml_path = 'C:/Users/86188/Desktop/dataset/' + name + '/label_raw/' + name + str(i) + '.xml'
xml_file = et.parse(xml_path)
root = xml_file.getroot()
objs = root.findall('object')
for obj in objs:
bnd = obj.find('bndbox')
x1 = float(bnd.find('xmin').text)
# y1 = float(bnd.find('ymin').text)
x2 = float(bnd.find('xmax').text)
# y2 = float(bnd.find('ymax').text)
# x 值 变化
xmin = 1280 - x2
xmax = 1280 - x1
bnd.find('xmin').text = str(int(xmin))
bnd.find('xmax').text = str(int(xmax))
xml_path_new = 'C:/Users/86188/Desktop/dataset/' + name + '/label_enhancement/' + name + str(i +3* num-1) + '.xml'
xml_file.write(xml_path_new) # 更新xml文件
print('水平翻转', i + 3 * num)垂直翻转:
def chuizhi_photo_fan_zhuan(name, num):
# 参数2 必选参数。用于指定镜像翻转的类型,0表示绕×轴正直翻转,即垂直镜像翻转;1表示绕y轴翻转,即水平镜像翻转;-1表示绕×轴、y轴两个轴翻转,即对角镜像翻转。
for i in range(1, num):
img = cv.imread('C:/Users/86188/Desktop/dataset/' + name + '/photo_raw/' + name + str(i) + '.jpg')
dst = cv.flip(img, 0)
cv.imwrite('C:/Users/86188/Desktop/dataset/' + name + '/photo_enhancement/' + name + str(i + 4 * num-1) + '.jpg', dst)
# 获得坐标值
xml_path = 'C:/Users/86188/Desktop/dataset/' + name + '/label_raw/' + name + str(i) + '.xml'
xml_file = et.parse(xml_path)
root = xml_file.getroot()
objs = root.findall('object')
for obj in objs:
bnd = obj.find('bndbox')
x1 = float(bnd.find('xmin').text)
y1 = float(bnd.find('ymin').text)
x2 = float(bnd.find('xmax').text)
y2 = float(bnd.find('ymax').text)
# y 值 变化
ymin = 1024 - y2
ymax = 1024 - y1
bnd.find('ymin').text = str(int(ymin))
bnd.find('ymax').text = str(int(ymax))
bnd.find('ymin').text = str(int(ymin))
bnd.find('ymax').text = str(int(ymax))
xml_path_new = 'C:/Users/86188/Desktop/dataset/' + name + '/label_enhancement/' + name + str(i + 4 * num-1) + '.xml'
xml_file.write(xml_path_new) # 更新xml文件
print('垂直翻转', i + 4 * num)垂直水平翻转:
def chuizhi_shuiping_photo_fan_zhuan(name, num):
# 参数2 必选参数。用于指定镜像翻转的类型,0表示绕×轴正直翻转,即垂直镜像翻转;1表示绕y轴翻转,即水平镜像翻转;-1表示绕×轴、y轴两个轴翻转,即对角镜像翻转。
for i in range(1, num):
img = cv.imread('C:/Users/86188/Desktop/dataset/' + name + '/photo_raw/' + name + str(i) + '.jpg')
dst = cv.flip(img, -1)
cv.imwrite('C:/Users/86188/Desktop/dataset/' + name + '/photo_enhancement/' + name + str(i + 5 * num-1) + '.jpg', dst)
# 获得坐标值
xml_path = 'C:/Users/86188/Desktop/dataset/' + name + '/label_raw/' + name + str(i) + '.xml'
xml_file = et.parse(xml_path)
root = xml_file.getroot()
obj = root.find('object')
bnd = obj.find('bndbox')
x1 = float(bnd.find('xmin').text)
y1 = float(bnd.find('ymin').text)
x2 = float(bnd.find('xmax').text)
y2 = float(bnd.find('ymax').text)
# y 值 变化
xmin = 1280 - x2
xmax = 1280 - x1
ymin = 1024 - y2
ymax = 1024 - y1
bnd.find('xmin').text = str(int(xmin))
bnd.find('xmax').text = str(int(xmax))
bnd.find('ymin').text = str(int(ymin))
bnd.find('ymax').text = str(int(ymax))
xml_path_new = 'C:/Users/86188/Desktop/dataset/' + name + '/label_enhancement/' + name + str(i + 5 * num-1) + '.xml'
xml_file.write(xml_path_new) # 更新xml文件
print('垂直水平翻转', i + 5 * num)亮度和对比度改变:
def adjust_brightness_and_contrast(name, num):
for i in range(1, num):
a = random.choice([0.8,0.9, 1.05, 1.08])
b = random.randint(-30, 30)
img = cv.imread('C:/Users/86188/Desktop/dataset/' + name + '/photo_raw/' + name + str(i) + '.jpg')
h, w, ch = img.shape
blank = np.zeros([h, w, ch], img.dtype)
dst = cv.addWeighted(img, a, blank, 1-a, b)
cv.imwrite('C:/Users/86188/Desktop/dataset/' + name + '/photo_enhancement/' + name + str(i + 6*num-1) + '.jpg', dst)
xml_path = 'C:/Users/86188/Desktop/dataset/' + name + '/label_raw/' + name + str(i) + '.xml'
xml_file = et.parse(xml_path)
xml_path_new = 'C:/Users/86188/Desktop/dataset/' + name + '/label_enhancement/' + name + str(i + 6*num-1) + '.xml'
xml_file.write(xml_path_new) # 更新xml文件
print('亮度对比度',i + num)最后:
num_表示的是你数据集每个类别的个数
list_lingjian = ['TB', 'D71', 'TB20K']
num_ = 45
k=0
photo_move(name=list_lingjian[k], num=num_)
photo_zoom_in_and_out(name=list_lingjian[k], num=num_)
#fang_she_change(name=list_lingjian[k], num=num_)
shuiping_photo_fan_zhuan(name=list_lingjian[k], num=num_)
chuizhi_photo_fan_zhuan(name=list_lingjian[k], num=num_)
chuizhi_shuiping_photo_fan_zhuan(name=list_lingjian[k], num=num_)
adjust_brightness_and_contrast(list_lingjian[k], num=num_ )边栏推荐
- 数据仓库与数仓建模
- 【mycat】mycat安装
- [data mining] zero foundation entry decision tree
- 达梦数据库_支持的表类型,用法,特性
- 【vsphere高可用】主机出现故障或隔离后的处理
- Small operation of statistical signal processing -- detection of deterministic DC signal in Rayleigh distributed noise
- flink checkpoint配置详解
- Flink函数(2):CheckpointedFunction
- SqlServer 完全删除
- Principle of fusdt liquidity pledge mining development logic system
猜你喜欢

Flink 生产环境配置建议

Multi merchant mall system function disassembly lecture 04 - platform side merchants settling in

西瓜书/南瓜书--第1,2章总结

多商户商城系统功能拆解12讲-平台端商品评价

Likeshop100% open source encryption free B2B2C multi merchant mall system

Sunset: noontide target penetration vulnhub

【activiti】组任务

【activiti】activiti环境配置

Flink watermark mechanism

Penetration testing knowledge - industry terminology
随机推荐
达梦数据库_LENGTH_IN_CHAR和CHARSET的影响情况
The repetition detection function of PHP multi line text content and count the number of repetitions
多商户商城系统功能拆解11讲-平台端商品栏目
Wechat applet returns parameters or trigger events
ThreadLocal stores the current login user information
【mycat】mycat介绍
如何快速打通CRM系统和ERP系统,实现业务流程自动化流转
《统计学习方法(第2版)》李航 第15章 奇异值分解 SVD 思维导图笔记 及 课后习题答案(步骤详细)SVD 矩阵奇异值 十五章
《统计学习方法(第2版)》李航 第22章 无监督学习方法总结 思维导图笔记
【activiti】组任务
Flink state使用
[Baidu map API] the version of the map JS API you are using is too low and no longer maintained. In order to ensure the normal use of the basic functions of the map, please upgrade to the latest versi
《机器学习》(周志华)第2章 模型选择与评估 笔记 学习心得
[virtualization] how to convert virtual machines from workstation to esxi
达梦数据库_dmfldr工具使用说明
The SaaS mall system of likeshop single merchant is built, and the code is open source without encryption.
【activiti】流程实例
MySQL queries the last four digits of the mobile phone number. How to write the first few digits?
Likeshop100%开源无加密-B2B2C多商户商城系统
flink checkpoint配置详解