当前位置:网站首页>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
xminandyminLess than 0,xmaxandymaxIt 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)
边栏推荐
- unity2019_ Input management
- Technical dry goods | thinking about the unification of dynamic and static diagrams of AI framework
- Iterm2设置
- JS to implement publish and subscribe
- An intern's journey to cnosdb
- Go language foundation ------17 ----- channel creation, read-write, security shutdown, multiplexing select
- 华为交换机:配置telnet和ssh、web访问
- Oracle queries grouped by time
- [at] abc 258G - Triangle 三元组可达-暴力
- oracle 插入单引号
猜你喜欢

oracle 插入单引号
![[MySQL 12] MySQL 8.0.18 reinitialization](/img/e1/9874df18bbc8d80c3c5c5fe39aefc9.png)
[MySQL 12] MySQL 8.0.18 reinitialization

Technical dry goods | some thoughts on the future of AI architecture

unity2019_ Input management

The difference between hdmi2.1 and hdmi2.0 and the conversion of PD signals.

创业团队如何落地敏捷测试,提升质量效能?丨声网开发者创业讲堂 Vol.03

什么是数据类型?数据类型有什么用?

【LeetCode】4. Best time to buy and sell stock

【踩坑系列】mysql 修改root密码失败

Unity performance optimization
随机推荐
register关键字
Project experience sharing: handwritten Chinese character recognition based on Shengsi mindspire
Differences between tp3.2 and tp5.0
Go language - loop statement
Pulitzer Prize in the field of information graphics - malofiej Award
Docker installs MySQL and successfully uses Navicat connection
[MySQL 12] MySQL 8.0.18 reinitialization
[global product discovery 2] the first pure cloud augmented reality (AR) platform - Israel
Static keyword
Technology dry goods | Roberta of the migration of mindspore NLP model - emotion analysis task
[cocos creator] get the resource UUID
一条通往服务器所有端口的隧道
Idea unreference Display Effect
EtherCAT state machine transition (ESM)
Oracle queries grouped by time
Idea dereference display effect
一个实习生的CnosDB之旅
Technical dry goods Shengsi mindspire dynamic transformer with variable sequence length has been released!
Unity performance optimization
Ventuz Foundation Series "one step at the door"