当前位置:网站首页>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)
边栏推荐
- 微软安全响应中心
- 方正锐利重磅升级到12.0版本,包装印前处理更加便捷、高效!
- Retail philosophy retail psychological warfare after reading -- 7-11 is a good product!
- haproxy+keepalived集群搭建02
- Precautions for opensips and TLS SIP trunk docking
- Getting started with minicom
- 华为S5700交换机初始化和配置telnet,ssh用户方法
- EtherCAT state machine transition (ESM)
- Screenshot tool snipaste
- [cocos creator] Click the button to switch the interface
猜你喜欢
方正锐利重磅升级到12.0版本,包装印前处理更加便捷、高效!
Screenshot tool snipaste
Viz artist advanced script video tutorial -- stringmap use and vertex operation
Open the influence list of "National Meteorological Short Videos (Kwai, Tiktok) in November" in an interactive way“
HDMI2.1与HDMI2.0的区别以及转换PD信号。
Go language foundation ------17 ----- channel creation, read-write, security shutdown, multiplexing select
什么是数据类型?数据类型有什么用?
Pycharm remote ssh pyenv error: pydev debugger: warning: trying to add breakpoint to file that does
【踩坑系列】mysql 修改root密码失败
多旅行商问题——公式和求解过程概述
随机推荐
Generate video using clipout in viz engine
Worldview satellite remote sensing image data / meter resolution remote sensing image
PHP微信抢红包的算法
Technology dry goods | Roberta of the migration of mindspore NLP model - emotion analysis task
regular expression
Zohocrm deluge function application time verification
Quality blog——
An intern's journey to cnosdb
Unity XR realizes interaction (grasping, moving, rotating, transmitting, shooting) -pico
tslib库的移植
P2622 关灯问题II(状态压缩 搜索)
PHP wechat red packet grabbing algorithm
[USACO12MAR]Cows in a Skyscraper G(状态压缩dp)
idea取消引用顯示效果
在浏览器输入url后执行什么
华为S5700交换机初始化和配置SSH和TELNET远程登录方法
Technical dry goods | Bert model for the migration of mindspore NLP model - text matching task (2): training and evaluation
jsutlis
Pat class a 1028 list sorting
Technical dry goods | some thoughts on the future of AI architecture