当前位置:网站首页>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)
边栏推荐
- How does yarn link help developers debug NPM packages?
- Pycharm remote ssh pyenv error: pydev debugger: warning: trying to add breakpoint to file that does
- What is a data type? What is the use of data types?
- 【cocos creator】获取资源uuid
- Wechat applet taro learning record
- 什麼是定義?什麼是聲明?它們有何區別?
- [end of 2021] National Meteorological Short Video (Kwai, Tiktok) influence list in December
- Microsoft Security Response Center
- Harmonyos third training notes
- 什么是数据类型?数据类型有什么用?
猜你喜欢

一个实习生的CnosDB之旅

【LeetCode】2. Valid parentheses · valid parentheses

What to do after the browser enters the URL
![[MySQL 12] MySQL 8.0.18 reinitialization](/img/e1/9874df18bbc8d80c3c5c5fe39aefc9.png)
[MySQL 12] MySQL 8.0.18 reinitialization

What is a data type? What is the use of data types?

Worldview satellite remote sensing image data / meter resolution remote sensing image

oracle 插入单引号
![[end of 2021] National Meteorological Short Video (Kwai, Tiktok) influence list in December](/img/51/81ceaf8746ec7455ea8abf9f038e81.jpg)
[end of 2021] National Meteorological Short Video (Kwai, Tiktok) influence list in December

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

Iterm2设置
随机推荐
Redis batch startup and shutdown script
[at] abc 258G - Triangle 三元組可達-暴力
My touch screen production "brief history" 1
An intern's journey to cnosdb
Iterm2 setting
freetype库的移植
Open the influence list of "National Meteorological Short Videos (Kwai, Tiktok) in November" in an interactive way“
Zohocrm deluge function application time verification
How to configure GDAL under idea
LwIP learning socket (application)
Retail philosophy retail psychological warfare after reading -- 7-11 is a good product!
[cocos creator] get the resource UUID
华为交换机Console密码重置、设备初始化、默认密码
PHP常用排序算法
The difference between hdmi2.1 and hdmi2.0 and the conversion of PD signals.
the installer has encountered an unexpected error installing this package
I want to do large screen data visualization application feature analysis
yarn link 是如何帮助开发者对 NPM 包进行 debug 的?
Project experience sharing: handwritten Chinese character recognition based on Shengsi mindspire
My touch screen production "brief history" 2