当前位置:网站首页>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)
边栏推荐
猜你喜欢

【cocos creator】点击按钮切换界面

oracle 插入单引号

OSPF experiment

Technical dry goods Shengsi mindspire dynamic transformer with variable sequence length has been released!

unity2019_ Input management

vcs import src < ros2. Repos failed
![[untitled]](/img/3d/27a7229e3f0ccf0ca5ae1f00a92513.jpg)
[untitled]

Unity performance optimization

Viz artist advanced script video tutorial -- stringmap use and vertex operation

HDMI2.1与HDMI2.0的区别以及转换PD信号。
随机推荐
Research shows that breast cancer cells are more likely to enter the blood when patients sleep
The difference between hdmi2.1 and hdmi2.0 and the conversion of PD signals.
[USACO12MAR]Cows in a Skyscraper G(状态压缩dp)
static关键字
Iterm2设置
Introduction of novel RNA based cancer therapies
一条通往服务器所有端口的隧道
优质博客——
使用 FileChannel 进行文件的复制拷贝
Harmonyos third training notes
Go language foundation ------17 ----- channel creation, read-write, security shutdown, multiplexing select
Go language - loop statement
[cocos creator] get the resource UUID
*p++、*++p、++*p、(*p)++
Zohocrm deluge function application time verification
STM32F103 SPI (pit Diary)
Wechat native applet cloud development learning record 01
Huawei switch: configure Telnet, SSH and web access
P2622 关灯问题II(状态压缩 搜索)
I want to do large screen data visualization application feature analysis