当前位置:网站首页>Target detection XML file to achieve mixup data enhancement (modifying the file path can be used directly, which is very convenient)
Target detection XML file to achieve mixup data enhancement (modifying the file path can be used directly, which is very convenient)
2022-07-23 10:15:00 【RooKiChen】
ad locum , First give the code of the original blogger :https://blog.csdn.net/qq_36756866/article/details/106299705
This code has some bug, And the amount of generated data cannot be changed arbitrarily , So I modified part of the code myself , Realize the function of generating any amount of data . You only need to modify the file path , Very convenient , There won't be any bug. The code is as follows :
# coding:utf-8
import cv2
import os
import random
import numpy as np
import xml.etree.ElementTree as ET
import xml.dom.minidom
img_path = '/root/wu_datasets/ReCurrentPapper/data/DOTA/DOTAship/VOCdevkit/VOC2012/JPEGImages/' # Original picture folder path
save_path = '/root/wu_datasets/ReCurrentPapper/data/DOTA_mixup/VOCdevkit/VOC2012/JPEGImages/' # mixup The path to your picture folder
xml_path = '/root/wu_datasets/ReCurrentPapper/data/DOTA/DOTAship/VOCdevkit/VOC2012/Annotations/' # The annotation file corresponding to the original picture xml Path to folder
save_xml = '/root/wu_datasets/ReCurrentPapper/data/DOTA_mixup/VOCdevkit/VOC2012//Annotations/' # mixup The label file corresponding to the picture of xml Folder path for
img_names = os.listdir(img_path)
img_num = len(img_names)
#print(img_names)
# sum Represents the sum of generated data
sum = []
# It means more than the original data set 10 times
for i in range(10):
for i in range(0, img_num):
sum.append(i)
# print(sum)
cnt = 0
for k in sum:
cnt += 1
print(cnt)
imgname = img_names[k]
imgpath = img_path + imgname
img = cv2.imread(imgpath)
img_h, img_w = img.shape[0], img.shape[1]
# print(img_h,img_w)
i = random.randint(0, img_num - 1)
add_path = img_path + img_names[i]
addimg = cv2.imread(add_path)
add_h, add_w = addimg.shape[0], addimg.shape[1]
if add_h != img_h or add_w != img_w:
print('resize!')
addimg = cv2.resize(addimg, (img_w, img_h), interpolation=cv2.INTER_LINEAR)
scale_h, scale_w = img_h / add_h, img_w / add_w
lam = np.random.beta(1.5, 1.5)
# print(lam)
mixed_img = lam * img + (1 - lam) * addimg
save_img = save_path + str(cnt) + '.jpg'
cv2.imwrite(save_img, mixed_img)
# print(save_img)
# print(imgname, img_names[i])
if imgname != img_names[i]:
xmlfile1 = xml_path + imgname[:-4] + '.xml'
xmlfile2 = xml_path + img_names[i][:-4] + '.xml'
# print(xmlfile1,xmlfile2)
tree1 = ET.parse(xmlfile1)
tree2 = ET.parse(xmlfile2)
doc = xml.dom.minidom.Document()
root = doc.createElement("annotation")
doc.appendChild(root)
for folds in tree1.findall("folder"):
folder = doc.createElement("folder")
folder.appendChild(doc.createTextNode(str(folds.text)))
root.appendChild(folder)
for filenames in tree1.findall("filename"):
filename = doc.createElement("filename")
filename.appendChild(doc.createTextNode("{}.jpg".format(cnt)))
root.appendChild(filename)
for paths in tree1.findall("path"):
path = doc.createElement("path")
path.appendChild(doc.createTextNode(str(paths.text)))
root.appendChild(path)
for sources in tree1.findall("source"):
source = doc.createElement("source")
database = doc.createElement("database")
database.appendChild(doc.createTextNode(str("Unknow")))
source.appendChild(database)
root.appendChild(source)
for sizes in tree1.findall("size"):
size = doc.createElement("size")
width = doc.createElement("width")
height = doc.createElement("height")
depth = doc.createElement("depth")
width.appendChild(doc.createTextNode(str(img_w)))
height.appendChild(doc.createTextNode(str(img_h)))
depth.appendChild(doc.createTextNode(str(3)))
size.appendChild(width)
size.appendChild(height)
size.appendChild(depth)
root.appendChild(size)
nodeframe = doc.createElement("frame")
nodeframe.appendChild(doc.createTextNode(imgname[:-4]))
objects = []
for obj in tree1.findall("object"):
obj_struct = {
}
obj_struct["name"] = obj.find("name").text
obj_struct["pose"] = obj.find("pose").text
obj_struct["truncated"] = obj.find("truncated").text
obj_struct["difficult"] = obj.find("difficult").text
bbox = obj.find("bndbox")
obj_struct["bbox"] = [int(bbox.find("xmin").text),
int(bbox.find("ymin").text),
int(bbox.find("xmax").text),
int(bbox.find("ymax").text)]
objects.append(obj_struct)
for obj in tree2.findall("object"):
obj_struct = {
}
obj_struct["name"] = obj.find("name").text
obj_struct["pose"] = obj.find("pose").text
obj_struct["truncated"] = obj.find("truncated").text
obj_struct["difficult"] = obj.find("difficult").text # There are versions of labelImg Change the parameter to lowercase difficult
bbox = obj.find("bndbox")
obj_struct["bbox"] = [int(int(bbox.find("xmin").text) * scale_w),
int(int(bbox.find("ymin").text) * scale_h),
int(int(bbox.find("xmax").text) * scale_w),
int(int(bbox.find("ymax").text) * scale_h)]
objects.append(obj_struct)
for obj in objects:
nodeobject = doc.createElement("object")
nodename = doc.createElement("name")
nodepose = doc.createElement("pose")
nodetruncated = doc.createElement("truncated")
nodedifficult = doc.createElement("difficult")
nodebndbox = doc.createElement("bndbox")
nodexmin = doc.createElement("xmin")
nodeymin = doc.createElement("ymin")
nodexmax = doc.createElement("xmax")
nodeymax = doc.createElement("ymax")
nodename.appendChild(doc.createTextNode(obj["name"]))
nodepose.appendChild(doc.createTextNode(obj["pose"]))
nodetruncated.appendChild(doc.createTextNode(obj["truncated"]))
nodedifficult.appendChild(doc.createTextNode(obj["difficult"]))
nodexmin.appendChild(doc.createTextNode(str(obj["bbox"][0])))
nodeymin.appendChild(doc.createTextNode(str(obj["bbox"][1])))
nodexmax.appendChild(doc.createTextNode(str(obj["bbox"][2])))
nodeymax.appendChild(doc.createTextNode(str(obj["bbox"][3])))
nodebndbox.appendChild(nodexmin)
nodebndbox.appendChild(nodeymin)
nodebndbox.appendChild(nodexmax)
nodebndbox.appendChild(nodeymax)
nodeobject.appendChild(nodename)
nodeobject.appendChild(nodepose)
nodeobject.appendChild(nodetruncated)
nodeobject.appendChild(nodedifficult)
nodeobject.appendChild(nodebndbox)
root.appendChild(nodeobject)
fp = open(save_xml + str(cnt) + ".xml", "w")
doc.writexml(fp, indent='\t', addindent='\t', newl='\n', encoding="utf-8")
fp.close()
else:
xmlfile1 = xml_path + imgname[:-4] + '.xml'
# print(xmlfile1)
tree1 = ET.parse(xmlfile1)
doc = xml.dom.minidom.Document()
root = doc.createElement("annotation")
doc.appendChild(root)
for folds in tree1.findall("folder"):
folder=doc.createElement("folder")
folder.appendChild(doc.createTextNode(str(folds.text)))
root.appendChild(folder)
for filenames in tree1.findall("filename"):
filename=doc.createElement("filename")
filename.appendChild(doc.createTextNode("{}.jpg".format(cnt)))
root.appendChild(filename)
for paths in tree1.findall("path"):
path = doc.createElement("path")
path.appendChild(doc.createTextNode(str(paths.text)))
root.appendChild(path)
for sources in tree1.findall("source"):
source = doc.createElement("source")
database = doc.createElement("database")
database.appendChild(doc.createTextNode(str("Unknow")))
source.appendChild(database)
root.appendChild(source)
for sizes in tree1.findall("size"):
size = doc.createElement("size")
width = doc.createElement("width")
height = doc.createElement("height")
depth = doc.createElement("depth")
width.appendChild(doc.createTextNode(str(img_w)))
height.appendChild(doc.createTextNode(str(img_h)))
depth.appendChild(doc.createTextNode(str(3)))
size.appendChild(width)
size.appendChild(height)
size.appendChild(depth)
root.appendChild(size)
nodeframe = doc.createElement("frame")
nodeframe.appendChild(doc.createTextNode(imgname[:-4]))
objects = []
for obj in tree1.findall("object"):
obj_struct = {
}
obj_struct["name"] = obj.find("name").text
obj_struct["pose"] = obj.find("pose").text
obj_struct["truncated"] = obj.find("truncated").text
obj_struct["difficult"] = obj.find("difficult").text
bbox = obj.find("bndbox")
obj_struct["bbox"] = [int(bbox.find("xmin").text),
int(bbox.find("ymin").text),
int(bbox.find("xmax").text),
int(bbox.find("ymax").text)]
objects.append(obj_struct)
for obj in objects:
nodeobject = doc.createElement("object")
nodename = doc.createElement("name")
nodepose = doc.createElement("pose")
nodetruncated = doc.createElement("truncated")
nodedifficult = doc.createElement("difficult")
nodebndbox = doc.createElement("bndbox")
nodexmin = doc.createElement("xmin")
nodeymin = doc.createElement("ymin")
nodexmax = doc.createElement("xmax")
nodeymax = doc.createElement("ymax")
nodename.appendChild(doc.createTextNode(obj["name"]))
nodepose.appendChild(doc.createTextNode(obj["pose"]))
nodetruncated.appendChild(doc.createTextNode(obj["truncated"]))
nodedifficult.appendChild(doc.createTextNode(obj["difficult"]))
nodexmin.appendChild(doc.createTextNode(str(obj["bbox"][0])))
nodeymin.appendChild(doc.createTextNode(str(obj["bbox"][1])))
nodexmax.appendChild(doc.createTextNode(str(obj["bbox"][2])))
nodeymax.appendChild(doc.createTextNode(str(obj["bbox"][3])))
nodebndbox.appendChild(nodexmin)
nodebndbox.appendChild(nodeymin)
nodebndbox.appendChild(nodexmax)
nodebndbox.appendChild(nodeymax)
nodeobject.appendChild(nodename)
nodeobject.appendChild(nodepose)
nodeobject.appendChild(nodetruncated)
nodeobject.appendChild(nodedifficult)
nodeobject.appendChild(nodebndbox)
root.appendChild(nodeobject)
fp = open(save_xml + str(cnt) + ".xml", "w")
doc.writexml(fp, indent='\t', addindent='\t', newl='\n', encoding="utf-8")
fp.close()
边栏推荐
猜你喜欢

【南瓜书ML】(task3)决策树(更新ing)

转行软件测试薪资10K | 手中有粮心中有底,在任何时候都是真理

What is the experience of writing concurrent tool classes (semaphore, cyclicbarrier, countdownlatch) by yourself in line 30?

60 open-ended test questions, recite them and get a pay rise directly

【C语言基础】14 文件、声明和格式化输入输出

【C语言基础】16 可变数组(数组长度可扩展)

【循环语句】

Reverse pairs in an array

博世BOSCH EDI项目案例

在线问题反馈模块实战(十一):实现图片下载功能
随机推荐
广发期货是什么级别?开户安全可靠吗?
【C语言基础】14 文件、声明和格式化输入输出
Visual full link log tracking
JS div scroll to the bottom
A concise tutorial for soft exam system architecture designer | reverse engineering
在线问题反馈模块实战(十一):实现图片下载功能
卡特兰数---
L-半胱氨酸修饰的金纳米粒子(Cys-GNPs)和牛血清白蛋白/生物素化白蛋白纳米粒
The technical points of the new project can be guided if necessary
【汇总篇】
Jeecgboot import document
用现代化的开发方法和思维,打跑遗留系统“拦路虎”
Android开发学习日记--内容提供者(跨应用间的数据库修改)
华泰证券可以网上开户吗安全吗
Multi UA V cooperative exploring for the unknown interior environment based on dynamic target tracking
non-Boost Asio 笔记: UDP UART SocketCAN Multicast UDS
指针的底层机制
软考 系统架构设计师 简明教程 | 逆向工程
2.判断语句
Anaconda 换源以及安装opencv