当前位置:网站首页>roLabelImg转DATO格式数据
roLabelImg转DATO格式数据
2022-07-29 05:01:00 【AI浩】
第一个版本
import os
import xml.etree.ElementTree as ET
import math
import cv2
import numpy as np
def edit_xml(xml_file):
if ".xml" not in xml_file:
return
tree = ET.parse(xml_file)
objs = tree.findall('object')
txt=xml_file.replace(".xml",".txt")
png=xml_file.replace(".xml",".png")
src=cv2.imread(png,1)
with open(txt,'w') as wf:
wf.write("imagesource:GoogleEarth\n")
wf.write("gsd:0.115726939386\n")
for ix, obj in enumerate(objs):
x0text = ""
y0text =""
x1text = ""
y1text =""
x2text = ""
y2text = ""
x3text = ""
y3text = ""
difficulttext=""
className=""
obj_type = obj.find('type')
type = obj_type.text
obj_name = obj.find('name')
className = obj_name.text
obj_difficult= obj.find('difficult')
difficulttext = obj_difficult.text
if type == 'bndbox':
obj_bnd = obj.find('bndbox')
obj_xmin = obj_bnd.find('xmin')
obj_ymin = obj_bnd.find('ymin')
obj_xmax = obj_bnd.find('xmax')
obj_ymax = obj_bnd.find('ymax')
xmin = float(obj_xmin.text)
ymin = float(obj_ymin.text)
xmax = float(obj_xmax.text)
ymax = float(obj_ymax.text)
x0text = str(xmin)
y0text = str(ymin)
x1text = str(xmax)
y1text = str(ymin)
x2text = str(xmin)
y2text = str(ymax)
x3text = str(xmax)
y3text = str(ymax)
points=np.array([[int(x0text),int(y0text)],[int(x1text),int(y1text)],[int(x2text),int(y2text)],[int(x3text),int(y3text)]],np.int32)
cv2.polylines(src,[points],True,(255,0,0)) #画任意多边
elif type == 'robndbox':
obj_bnd = obj.find('robndbox')
obj_bnd.tag = 'bndbox' # 修改节点名
obj_cx = obj_bnd.find('cx')
obj_cy = obj_bnd.find('cy')
obj_w = obj_bnd.find('w')
obj_h = obj_bnd.find('h')
obj_angle = obj_bnd.find('angle')
cx = float(obj_cx.text)
cy = float(obj_cy.text)
w = float(obj_w.text)
h = float(obj_h.text)
angle = float(obj_angle.text)
x0text, y0text = rotatePoint(cx, cy, cx - w / 2, cy - h / 2, -angle)
x1text, y1text = rotatePoint(cx, cy, cx + w / 2, cy - h / 2, -angle)
x2text, y2text = rotatePoint(cx, cy, cx + w / 2, cy + h / 2, -angle)
x3text, y3text = rotatePoint(cx, cy, cx - w / 2, cy + h / 2, -angle)
points=np.array([[int(x0text),int(y0text)],[int(x1text),int(y1text)],[int(x2text),int(y2text)],[int(x3text),int(y3text)]],np.int32)
cv2.polylines(src,[points],True,(255,0,0)) #画任意多边形
# print(x0text,y0text,x1text,y1text,x2text,y2text,x3text,y3text,className,difficulttext)
wf.write("{} {} {} {} {} {} {} {} {} {}\n".format(x0text,y0text,x1text,y1text,x2text,y2text,x3text,y3text,className,difficulttext))
cv2.imshow("ddd",src)
cv2.waitKey()
# 转换成四点坐标
def rotatePoint(xc, yc, xp, yp, theta):
xoff = xp - xc;
yoff = yp - yc;
cosTheta = math.cos(theta)
sinTheta = math.sin(theta)
pResx = cosTheta * xoff + sinTheta * yoff
pResy = - sinTheta * xoff + cosTheta * yoff
return str(int(xc + pResx)), str(int(yc + pResy))
if __name__ == '__main__':
dir="/home/trust/PIC"#文件位置
filelist = os.listdir(dir)
for file in filelist:
edit_xml(os.path.join(dir,file))
来自:https://blog.csdn.net/qq_51882416/article/details/124247280
第二个版本
import os
import xml.etree.ElementTree as ET
import math
def voc_to_dota(xml_path, xml_name):
txt_name = xml_name[:-4] + '.txt'
txt_path = xml_path + '/txt_label'
if not os.path.exists(txt_path):
os.makedirs(txt_path)
txt_file = os.path.join(txt_path, txt_name)
file_path = os.path.join(xml_path, file_list[i])
tree = ET.parse(os.path.join(file_path))
root = tree.getroot()
# print(root[6][0].text)
with open(txt_file, "w+", encoding='UTF-8') as out_file:
# out_file.write('imagesource:null' + '\n' + 'gsd:null' + '\n')
for obj in root.findall('object'):
name = obj.find('name').text
difficult = obj.find('difficult').text
# print(name, difficult)
robndbox = obj.find('robndbox')
cx = float(robndbox.find('cx').text)
cy = float(robndbox.find('cy').text)
w = float(robndbox.find('w').text)
h = float(robndbox.find('h').text)
angle = float(robndbox.find('angle').text)
# print(cx, cy, w, h, angle)
p0x, p0y = rotatePoint(cx, cy, cx - w / 2, cy - h / 2, -angle)
p1x, p1y = rotatePoint(cx, cy, cx + w / 2, cy - h / 2, -angle)
p2x, p2y = rotatePoint(cx, cy, cx + w / 2, cy + h / 2, -angle)
p3x, p3y = rotatePoint(cx, cy, cx - w / 2, cy + h / 2, -angle)
data = str(p0x) + " " + str(p0y) + " " + str(p1x) + " " + str(p1y) + " " + \
str(p2x) + " " + str(p2y) + " " + str(p3x) + " " + str(p3y) + " "
data = data + name + " " + difficult + "\n"
out_file.write(data)
# 转换成四点坐标
def rotatePoint(xc, yc, xp, yp, theta):
xoff = xp - xc
yoff = yp - yc
cosTheta = math.cos(theta)
sinTheta = math.sin(theta)
pResx = cosTheta * xoff + sinTheta * yoff
pResy = - sinTheta * xoff + cosTheta * yoff
# pRes = (xc + pResx, yc + pResy)
# 保留一位小数点
return float(format(xc + pResx, '.1f')), float(format(yc + pResy, '.1f'))
# return xc + pResx, yc + pResy
if __name__ == '__main__':
root_path = '../annotation'
file_list = os.listdir(root_path)
for i in range(0, len(file_list)):
if ('.xml' in file_list[i]) or ('.XML' in file_list[i]):
voc_to_dota(root_path, file_list[i])
print('----------------------------------------{}{}----------------------------------------'
.format(file_list[i], ' has Done!'))
else:
print(file_list[i] + ' is not xml file')
边栏推荐
- ios面试准备 - 网络篇
- Academic | [latex] super detailed texlive2022+tex studio download installation configuration
- On prepayment of house purchase
- Conv2d of torch
- MySQL regularly calls preset functions to complete data update
- Wechat picture identification
- C language implementation of three chess
- stack和queue和优先级队列(大堆和小堆)模拟实现和仿函数讲解
- Use openmap and ArcGIS to draw maps and transportation networks of any region, and convert OMS data into SHP format
- P1009 [noip1998 popularization group] sum of factorials
猜你喜欢
Improve the readability of your regular expressions a hundred times
1 句代码,搞定 ASP.NET Core 绑定多个源到同一个类
2022杭电多校联赛第四场 题解
Recommendation system of online education
stack和queue和优先级队列(大堆和小堆)模拟实现和仿函数讲解
Reveal安装配置调试
Ethernet of network
The song of the virtual idol was originally generated in this way!
Solution | get the relevant information about the current employees' highest salary in each department |
让你的正则表达式可读性提高一百倍
随机推荐
五个关联分析,领略数据分析师一大重要必会处理技能
Ethernet of network
Excel怎么筛选出自己想要的内容?excel表格筛选内容教程
Use jupyter (2) to establish shortcuts to open jupyter and common shortcut keys of jupyter
网安学习-内网安全1
def fasterrcnn_ resnet50_ FPN () instance test
Using jupyter (I), install jupyter under windows, open the browser, and modify the default opening address
WPS插入超链接无法打开,提示“无法打开指定文件”怎么办!
Google gtest事件机制
怎样监测微型的网站服务
JS daily question (10)
IOS interview preparation - IOS
PHP determines whether the user has logged in. If logged in, the home page will be displayed. If not, enter the login page or registration page
Quick start JDBC
Academic | [latex] super detailed texlive2022+tex studio download installation configuration
Learn matlab to draw geographical map, line scatter bubble density map
Huawei ilearning AI mathematics foundation course notes
Conv1d of torch
GCC Basics
Solve the warning prompt of MySQL mapping file