当前位置:网站首页>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')
边栏推荐
- 让你的正则表达式可读性提高一百倍
- Let you understand several common traffic exposure schemes in kubernetes cluster
- Traffic flow prediction pit climbing record (I): traffic flow data set, original data
- 搭建手机APP需要用到什么服务器
- JS daily question (11)
- Google gtest事件机制
- ios面试准备 - objective-c篇
- How to solve the problem of configuring the progress every time Office2010 is opened?
- Use jupyter (2) to establish shortcuts to open jupyter and common shortcut keys of jupyter
- What if excel is stuck and not saved? The solution of Excel not saved but stuck
猜你喜欢
![[untitled]](/img/6c/df2ebb3e39d1e47b8dd74cfdddbb06.gif)
[untitled]

2022杭电多校联赛第四场 题解

WPS插入超链接无法打开,提示“无法打开指定文件”怎么办!

Download addresses of various versions of MySQL and multi version coexistence installation
Let you understand several common traffic exposure schemes in kubernetes cluster

Use openmap and ArcGIS to draw maps and transportation networks of any region, and convert OMS data into SHP format

How does excel filter out the content you want? Excel table filtering content tutorial

Flink+Iceberg环境搭建及生产问题处理

新产品上市最全推广方案

office2010每次打开都要配置进度怎么解决?
随机推荐
AttributeError: ‘module‘ object has no attribute ‘create_connection‘
Common rules of makefile (make) (II)
DataSourceClosedException: dataSource already closed at Mon Oct 25 16:55:48 CST 2021
1 句代码,搞定 ASP.NET Core 绑定多个源到同一个类
GCC Basics
[untitled]
JDBC statement + resultset introduction
What if excel is stuck and not saved? The solution of Excel not saved but stuck
【无标题】
On prepayment of house purchase
[wechat applet] swiper slides the page, and the left and right sides of the slider show part of the front and back, showing part of the front and back
Recyclerview switches the focus up and down through the dpad key. When switching to the control outside the interface, the focus will jump left and right
Vscode configuration makefile compilation
Connection database time zone setting
学术 | [LaTex]超详细Texlive2022+Tex Studio下载安装配置
Quick start JDBC
Conv1d of torch
如何让照片中的人物笑起来?HMS Core视频编辑服务一键微笑功能,让人物笑容更自然
pulsar起client客户端时(client,producer,consumer)各个配置
使用近场探头和电流探头进行EMI干扰排查