当前位置:网站首页>Yolo series --- xml2txt script
Yolo series --- xml2txt script
2022-07-03 07:58:00 【Go into my collection and eat ashes~~】
YOLO series — xml2txt Script
YOLO The data format of the algorithm is basically VOC Format , I read a lot on the Internet xml2txt Script for , I found many problems , It can't run directly to get the right txt file . So I specially write an article blog~
One 、LabelImg There will be problems
Generally speaking ,VOC Format pictures are generally used LabelImg As a marking tool for manual marking . But I don't know whether it's the software or the labeling process , There will be the following small bug:
- Annotation generated xml file , The length and width of the picture is 0. I have never found the problem , However, it is more inclined to have exceptions when reading pictures .
- The size of the callout box is out of bounds . This problem seems to be based on LabelImg Caused by version , Some versions of LabelImg The callout box will appear
xmin
andymin
Less than 0,xmax
andymax
It will be larger than the length and width of the picture . It will eventually lead to the converted txt Negative or abnormal values appear .
Two 、xml2txt
Not many words , Go straight to the code :
import glob
import os
import cv2
import shutil
import numpy as np
import xml.etree.ElementTree as ET
# Change one xml File for txt
def single_xml_to_txt(xml_file, corr_img_path, save_txt_path):
if os.path.exists(corr_img_path):
try:
img = cv2.imread(corr_img_path)
true_width, true_height = img.shape[1], img.shape[0]
except:
# print(img_path)
pass
tree = ET.parse(xml_file)
root = tree.getroot()
# The saved txt File path
txt_file = save_txt_path + os.path.basename(xml_file.replace("xml", "txt"))
with open(txt_file, 'w') as txt_file:
try:
picture_width = int(root.find('size')[0].text)
picture_height = int(root.find('size')[1].text)
# Abnormal reading for image size , Make dimensional correction
if picture_width == 0 or picture_height == 0:
picture_width = true_width
picture_height = true_height
if picture_width != true_width or picture_height != true_height:
picture_width = true_width
picture_height = true_height
for member in root.findall('object'):
class_name = member.find("name").text
box_x_min = int(member.find("bndbox").find("xmin").text)
box_y_min = int(member.find("bndbox").find("ymin").text)
box_x_max = int(member.find("bndbox").find("xmax").text)
box_y_max = int(member.find("bndbox").find("ymax").text)
# For abnormal dimension box size , Perform truncation , Avoid conversion txt Value anomaly
if box_x_min < 0:
box_x_min = 0
elif box_y_min < 0:
box_y_min = 0
elif box_x_max > picture_width:
box_x_max = picture_width
elif box_y_max > picture_height:
box_y_max = picture_height
# The class name corresponds to index
class_num = class_names.index(class_name)
# Turn to relative position and width and height
x_center = (box_x_min + box_x_max) / (2 * picture_width)
y_center = (box_y_min + box_y_max) / (2 * picture_height)
width = (box_x_max - box_x_min) / picture_width
height = (box_y_max - box_y_min) / picture_height
txt_file.write(str(class_num) + ' ' + str(x_center) + ' ' + str(y_center) + ' ' + str(width) + ' ' + str(height) + '\n')
except:
pass
# Convert all files in the folder xml File for txt
def dir_xml_to_txt(xml_path, img_path, save_txt_path):
for index, xml_file in enumerate(glob.glob(xml_path+ '*.xml')):
corr_img_path = img_path + os.path.basename(xml_file).split(".")[0] + ".jpg"
try:
single_xml_to_txt(xml_file, corr_img_path, save_txt_path)
except:
print(xml_file)
else:
print("all:{}---now:{}".format(len(glob.glob(path + '*.xml')), index+1))
if __name__ == '__main__':
class_names = [] # Class name
xml_path = r"" #xml File path
img_path = r"" # Picture path
save_txt_path = r"" # Generate txt File save path
dir_xml_to_txt(xml_path, img_path, save_txt_path)
边栏推荐
- IP production stream is so close to me
- Register keyword
- The general trend of data news releases the power of visual reporting ----- essays after reading
- 微软安全响应中心
- 【cocos creator】点击按钮切换界面
- the installer has encountered an unexpected error installing this package
- LwIP learning socket (API)
- [at] abc 258G - Triangle 三元組可達-暴力
- Redis查看客户端连接
- [step on the pit series] MySQL failed to modify the root password
猜你喜欢
How can entrepreneurial teams implement agile testing to improve quality and efficiency? Voice network developer entrepreneurship lecture Vol.03
【cocos creator】点击按钮切换界面
Go language foundation ----- 19 ----- context usage principle, interface, derived context (the multiplexing of select can be better understood here)
C language learning notes (mind map)
[step on the pit series] MySQL failed to modify the root password
The difference between hdmi2.1 and hdmi2.0 and the conversion of PD signals.
Wechat applet taro learning record
HDMI2.1与HDMI2.0的区别以及转换PD信号。
Pat class a 1032 sharing
Technical dry goods | Bert model for the migration of mindspore NLP model - text matching task (2): training and evaluation
随机推荐
Technology dry goods | Roberta of the migration of mindspore NLP model - emotion analysis task
Open the influence list of "National Meteorological Short Videos (Kwai, Tiktok) in November" in an interactive way“
Go language - loop statement
haproxy+keepalived集群搭建02
【LeetCode】4. Best time to buy and sell stock
华为S5700交换机初始化和配置telnet,ssh用户方法
What to do after the browser enters the URL
Redis batch startup and shutdown script
Retail philosophy retail psychological warfare after reading -- 7-11 is a good product!
JS common basic case sorting (continuous update)
[USACO12MAR]Cows in a Skyscraper G(状态压缩dp)
PHP微信抢红包的算法
register关键字
Demonstration of plug-in use of ventuz basic series
regular expression
MAE
PDO and SDO concepts
tp3.2和tp5.0的区别
WorldView卫星遥感影像数据/米级分辨率遥感影像
华为S5700交换机初始化和配置SSH和TELNET远程登录方法