当前位置:网站首页>Rolabelimg to data format data
Rolabelimg to data format data
2022-07-29 05:09:00 【AI Hao】
The first version
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)) # Draw any multilateral
elif type == 'robndbox':
obj_bnd = obj.find('robndbox')
obj_bnd.tag = 'bndbox' # Modify the node name
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)) # Draw arbitrary polygons
# 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()
# Convert to four point coordinates
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"# file location
filelist = os.listdir(dir)
for file in filelist:
edit_xml(os.path.join(dir,file))
come from :https://blog.csdn.net/qq_51882416/article/details/124247280
Second version
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)
# Convert to four point coordinates
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)
# Keep one decimal place
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')
边栏推荐
- Go memory model for concurrency
- How is the entered query SQL statement executed?
- Understand activity workflow
- The representation of time series analysis: is the era of learning coming?
- Climbing the pit of traffic flow prediction (II): the simplest LSTM predicts traffic flow using tensorflow2
- 荣耀2023内推,内推码ambubk
- Solve the warning prompt of MySQL mapping file
- What if the office prompts that the system configuration cannot run?
- Flink+Iceberg环境搭建及生产问题处理
- Northeast University Data Science Foundation (matlab) - Notes
猜你喜欢

How does word view document modification traces? How word views document modification traces

office2010每次打开都要配置进度怎么解决?

How to add traffic statistics codes to the legendary Development Zone website

Use jupyter (2) to establish shortcuts to open jupyter and common shortcut keys of jupyter

MySQL定时调用预置函数完成数据更新

Huawei ilearning AI mathematics foundation course notes

Torch.nn.crossentropyloss() details

让你的正则表达式可读性提高一百倍

Un7.28: common commands of redis client.
Let you understand several common traffic exposure schemes in kubernetes cluster
随机推荐
How to set row height and column width in excel? The method of setting row height and column width in Excel
新产品上市最全推广方案
How to install Office2010 installation package? How to install Office2010 installation package on computer
The person who goes to and from work on time and never wants to work overtime has been promoted in front of me
自贸经济中架起的“隐形桥梁”:国货精品与中国AI力量
Flink+iceberg environment construction and production problem handling
Let you understand several common traffic exposure schemes in kubernetes cluster
WPS如何进行快速截屏?WPS快速截屏的方法
JDBC statement + resultset introduction
Wps如何使用智能填充快速填充数据?Wps快速填充数据的方法
Double type nullpointexception in Flink flow calculation
2021-11-02
缓存穿透、缓存击穿、缓存雪崩以及解决方法
时间序列分析的表示学习时代来了?
roLabelImg转DATO格式数据
WPS insert hyperlink cannot be opened. What should I do if I prompt "unable to open the specified file"!
Reply from the Secretary of jindawei: the company is optimistic about the market prospect of NMN products and has launched a series of products
怎样监测微型的网站服务
Climbing the pit of traffic flow prediction (II): the simplest LSTM predicts traffic flow using tensorflow2
What servers are needed to build mobile app