当前位置:网站首页>Target detection tagged data enhancement code
Target detection tagged data enhancement code
2022-07-24 05:49:00 【Hehai CV chicken with vegetables】
Be careful : My dataset name is from 1 At the beginning , So it is for i in range(1,num), So the penultimate sentence str(i + num-1) If you start from scratch, don't -1. In addition, the file name and image size (1280,1024) You should also make modifications according to your own data set Picture translation :
import cv2 as cv
import numpy as np
import random
import xml.etree.ElementTree as et
# Picture translation 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)
# Define the displacement matrix ,x direction a*c,y direction b*d, And use random The library implements random offset
matshift = np.float32([[1, 0, a*c], [0, 1, b*d]])
# C:\Users\lx\Desktop\dataset\ban\photo_raw
# Generate pictures and save
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)
# Obtain a two-dimensional matrix
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]])
# Save the transformed coordinate point file
juzhen_new = np.dot(matshift, ju_zhen) # Matrix multiplication
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) # to update xml file
print(' translation ', i+num)Picture zoom :
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)
# Define the scaling matrix
matshift = np.float32([[a, 0, 0], [0, b, 0]]) # Define the scaling matrix , The aspect ratio is a random number
# Generate and save images
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)) # The canvas size is the same as the original
cv.imwrite('C:/Users/86188/Desktop/dataset/' + name + '/photo_enhancement/' + name + str(i + 2* num-1) + '.jpg', dst)
# Obtain a two-dimensional matrix
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]])
# Save the transformed coordinate point file
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) # to update xml file
print(' The zoom ', i + 2 * num)
Flip horizontal :
def shuiping_photo_fan_zhuan(name, num):
# Parameters 2 Required parameters . Used to specify the type of mirror flip ,0 It means around × The shaft just flips , That is, flip the vertical mirror ;1 It means around y The axis turns , That is, the horizontal mirror flips ;-1 It means around × Axis 、y Axis two axes flip , That is, diagonal mirror flip .
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)
# Get coordinate values
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 value change
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) # to update xml file
print(' Flip horizontal ', i + 3 * num)Flip vertically :
def chuizhi_photo_fan_zhuan(name, num):
# Parameters 2 Required parameters . Used to specify the type of mirror flip ,0 It means around × The shaft just flips , That is, flip the vertical mirror ;1 It means around y The axis turns , That is, the horizontal mirror flips ;-1 It means around × Axis 、y Axis two axes flip , That is, diagonal mirror flip .
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)
# Get coordinate values
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 value change
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) # to update xml file
print(' Flip vertically ', i + 4 * num)Flip vertically and horizontally :
def chuizhi_shuiping_photo_fan_zhuan(name, num):
# Parameters 2 Required parameters . Used to specify the type of mirror flip ,0 It means around × The shaft just flips , That is, flip the vertical mirror ;1 It means around y The axis turns , That is, the horizontal mirror flips ;-1 It means around × Axis 、y Axis two axes flip , That is, diagonal mirror flip .
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)
# Get coordinate values
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 value change
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) # to update xml file
print(' Flip vertically and horizontally ', i + 5 * num)Brightness and contrast change :
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) # to update xml file
print(' Brightness, contrast ',i + num)Last :
num_ It represents the number of each category in your data set
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_ )边栏推荐
- Multi merchant mall system function disassembly lecture 04 - platform side merchants settling in
- OpenWRT快速配置Samba
- Multi merchant mall system function disassembly lecture 13 - platform side member management
- 达梦数据库_触发器、视图、物化视图、序列、同义词、自增列、外部链接等基本的操作
- [activiti] activiti introduction
- ThreadLocal存储当前登录用户信息
- Multi merchant mall system function disassembly lesson 03 - platform side merchant management
- Authorized access to MySQL database
- Multi merchant mall system function disassembly lecture 09 - platform end commodity brands
- The female colleague of the company asked me to go to her residence to repair the computer at 11 o'clock at night. It turned out that disk C was popular. Look at my move to fix the female colleague's
猜你喜欢

The SaaS mall system of likeshop single merchant is built, and the code is open source without encryption.

Multi merchant mall system function disassembly lecture 07 - platform side commodity management

【mycat】mycat搭建读写分离

多商户商城系统功能拆解11讲-平台端商品栏目

达梦数据库_逻辑架构基础

Similarities and differences of ODS, data mart and data warehouse

【activiti】activiti介绍

Likeshop100%开源无加密-B2B2C多商户商城系统

Multi merchant mall system function disassembly Lecture 14 - platform side member level

《统计学习方法(第2版)》李航 第15章 奇异值分解 SVD 思维导图笔记 及 课后习题答案(步骤详细)SVD 矩阵奇异值 十五章
随机推荐
删除分类网络预训练权重的的head部分的权重以及修改权重名称
【activiti】流程变量
达梦数据库_常用的用户管理命令
‘Results do not correspond to current coco set‘
MySQL batch insert demo
Numpy cheatsheet
Flink watermark mechanism
Detailed discussion on data synchronization tools ETL, ELT, reverse ETL
likeshop单商户SAAS商城系统搭建,代码开源无加密。
数据库连接数过大
关于卷积神经网络中的“输入通道”和“输出通道”的概念
达梦数据库_逻辑架构基础
Likeshop | single merchant mall system code open source no encryption -php
[activiti] activiti introduction
Flink 时间流处理
多商户商城系统功能拆解10讲-平台端商品单位
Highcharts use custom vector maps
多商户商城系统功能拆解05讲-平台端商家主营类目
likeshop单商户商城系统搭建,代码开源无加密
达梦数据库_用户口令策略