全网最全的yolo系列转换工具,从txt转xml,再从xml转txt,亲自测试好用

2024-08-25 04:36

本文主要是介绍全网最全的yolo系列转换工具,从txt转xml,再从xml转txt,亲自测试好用,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

        在训练yolo的过程中,难免涉及标注的数据格式转化,经过了几次修改和迭代,最终把转化代码跟大家一起分享。

        先把xml转txt部分的代码分享一下,py_convert_xml2txt.py:

# -*- coding:utf-8 -*-import os
import shutil
import cv2
import numpy as np
import xml.etree.ElementTree as ETwordname_coco = ['person','bicycle','car','motorbike','aeroplane','bus','train','truck','boat','traffic light','fire hydrant','stop sign','parking meter','bench','bird','cat','dog','horse','sheep','cow','elephant','bear', 'zebra','giraffe','backpack','umbrella','handbag','tie','suitcase','frisbee','skis','snowboard','sports ball','kite','baseball bat','baseball glove','skateboard','surfboard','tennis racket','bottle','wine glass','cup','fork','knife','spoon','bowl','banana','apple','sandwich','orange','broccoli','carrot','hot dog','pizza','donut','cake','chair','sofa','potted plant','bed','dining table','toilet','tv','laptop','mouse','remote','keyboard','cell phone','microwave','oven','toaster','sink','refrigerator','book','clock','vase','scissors','teddy bear','hair drier','toothbrush']wordname_voc = ['aeroplane','bicycle','bird','boat','bottle','bus','car','cat','chair','cow','diningtable','dog','horse','motorbike','person','pottedplant','sheep','sofa','sheep','train','tvmonitor']wordname_zkrc = ['person','car','cat','dog']#person car truck cat dogdef convert(size, box):dw = 1. / (size[0])dh = 1. / (size[1])x = (box[0] + box[1]) / 2.0 - 1y = (box[2] + box[3]) / 2.0 - 1w = box[1] - box[0]h = box[3] - box[2]x = x * dww = w * dwy = y * dhh = h * dhreturn (x, y, w, h)def convert_xml2txt_voc():#text_dir = './data/JPEGImages/'xml_dir = "./data/LR_person_car_dataset/train/"imagedir = "./data/LR_person_car_dataset/train/"out_txt_dir = './InfraredData/txt/'out_temp_txt_dir = './InfraredData/temp_Txt/'out_images_dir = './InfraredData/Images/'title = "20240823_4_"count = 0list = os.listdir(xml_dir)  # 列出文件夹下所有的目录与文件for i in range(0, len(list)):name = os.path.splitext(list[i])if name[1] == '.xml':print(list[i])#changeTitle = title + str(i + 1) + "_"xml_path = xml_dir + list[i]jpg_path = imagedir + name[0] + '.jpg'if not os.path.isfile(jpg_path):jpg_path = imagedir + name[0] + '.jpeg'if not os.path.isfile(jpg_path):jpg_path = imagedir + name[0] + '.bmp'if not os.path.isfile(jpg_path):jpg_path = imagedir + name[0] + '.png'image = cv2.imread(jpg_path)if image is None:continue# if not os.path.isfile(jpg_path):#     continuecount = count + 1changeTitle = title + str(count) + "_"out_file_name = out_temp_txt_dir + changeTitle + '.txt'out_jpg_name = out_images_dir + changeTitle + '.jpg'out_file = open(out_file_name, 'w')tree = ET.parse(xml_path)root = tree.getroot()size = root.find('size')w = int(size.find('width').text)h = int(size.find('height').text)num = 0for obj in root.iter('object'):cls = obj.find('name').text# print(cls)# num += 1# cls_id = 0#classes.index(cls)#cls_id = wordname_voc.index(cls)cls_id = -1if cls == 'person':cls_id = 0elif cls == 'car':cls_id = 1elif cls == 'cat':cls_id = 2elif cls == 'dog':cls_id = 3else:continuenum += 1xmlbox = obj.find('bndbox')b = (float(xmlbox.find('xmin').text), float(xmlbox.find('xmax').text), float(xmlbox.find('ymin').text),float(xmlbox.find('ymax').text))bb = convert((w, h), b)out_file.write(str(cls_id) + " " + " ".join([str(a) for a in bb]) + '\n')#     left = int(xmlbox.find('xmin').text)#     top = int(xmlbox.find('ymin').text)#     right = int(xmlbox.find('xmax').text)#     bottom = int(xmlbox.find('ymax').text)#     font = cv2.FONT_HERSHEY_SIMPLEX#     cv2.putText(image, cls, (left, top - 10), font, 1.2, (255, 255, 255), 2)#     cv2.rectangle(image, (left, top), (right, bottom), (0, 0, 255), 2)# cv2.imshow("image", image)# cv2.waitKey(0)out_file.close()if num > 0:cv2.imwrite(out_jpg_name, image)shutil.copy(out_file_name, out_txt_dir)# if num > 1:#     shutil.copy(jpg_path, out_images_dir)def check_txt_ok():test_dir = './InfraredData/test/'image_dir = "./InfraredData/Images/"txt_dir = './InfraredData/txt/'list = os.listdir(image_dir)  # 列出文件夹下所有的目录与文件for i in range(0, len(list)):name = os.path.splitext(list[i])#print(name)#if name[1] == '.jpg':#print(name[1])count = 0if name[1] == '.jpg':jpg_path = image_dir + list[i]image = cv2.imread(jpg_path)shape = image.shapeprint(shape)# cv2.imshow("image",imaga)# cv2.waitKey(0)#print(name[1])filename = txt_dir + name[0] + '.txt'if os.path.isfile(filename):with open(filename, 'r') as file_to_read:while True:lines = file_to_read.readline()  # 整行读取数据if not lines:breakpasscurLine = lines.split(" ")#floatLine = map(float, lines)# for i in len(lines):#     print(lines[i])#print(curLine[0],curLine[1],curLine[2],curLine[3],curLine[4])numbers = [float(x) for x in curLine]numbers[0] = numbers[0]numbers[1] = numbers[1]*shape[1]numbers[2] = numbers[2] * shape[0]numbers[3] = numbers[3]*shape[1]/2numbers[4]= numbers[4] * shape[0]/2rect_info = [int(x) for x in numbers]#print(rect_info[0],rect_info[1], rect_info[2], rect_info[3], rect_info[4])font = cv2.FONT_HERSHEY_SIMPLEXcv2.putText(image, wordname_zkrc[rect_info[0]], (rect_info[1] - rect_info[3],rect_info[2] - rect_info[4] - 10), font, 1.2, (255, 255, 255), 2)cv2.rectangle(image,(rect_info[1] - rect_info[3],rect_info[2] - rect_info[4]),(rect_info[1] + rect_info[3],rect_info[2] + rect_info[4]), (0, 0, 255), 2)# saveFile = out_path + '/' +  name[0] + '.jpg'#cv2.imwrite(saveFile,image)# count = count + 1# if count == 1:#     returnsaveFile = test_dir + name[0] + '.jpg'cv2.imwrite(saveFile,image)# cv2.imshow("image", image)# cv2.waitKey(0)if __name__ == "__main__":#convert_xml2txt_coco()#convert_xml2txt_voc()check_txt_ok()

         再把txt转xml部分的代码分享一下,py_convert_txt2xml.py:

 

import os
import shutil
import xml.etree.ElementTree as ET
from xml.etree.ElementTree import Element, SubElement
from PIL import Image
import cv2
wordname_zkrc = ['person','car','cat','dog']
#wordname_zkrc = ['person','vehicle','animal','object']class Xml_make(object):def __init__(self):super().__init__()def __indent(self, elem, level=0):i = "\n" + level * "\t"if len(elem):if not elem.text or not elem.text.strip():elem.text = i + "\t"if not elem.tail or not elem.tail.strip():elem.tail = ifor elem in elem:self.__indent(elem, level + 1)if not elem.tail or not elem.tail.strip():elem.tail = ielse:if level and (not elem.tail or not elem.tail.strip()):elem.tail = idef _imageinfo(self, list_top):annotation_root = ET.Element('annotation')annotation_root.set('verified', 'no')tree = ET.ElementTree(annotation_root)'''0:xml_savepath 1:folder,2:filename,3:path4:checked,5:width,6:height,7:depth'''folder_element = ET.Element('folder')folder_element.text = list_top[1]annotation_root.append(folder_element)filename_element = ET.Element('filename')filename_element.text = list_top[2]annotation_root.append(filename_element)path_element = ET.Element('path')path_element.text = list_top[3]annotation_root.append(path_element)# checked_element = ET.Element('checked')# checked_element.text = list_top[4]# annotation_root.append(checked_element)source_element = ET.Element('source')database_element = SubElement(source_element, 'database')database_element.text = 'Unknown'annotation_root.append(source_element)size_element = ET.Element('size')width_element = SubElement(size_element, 'width')width_element.text = str(list_top[5])height_element = SubElement(size_element, 'height')height_element.text = str(list_top[6])depth_element = SubElement(size_element, 'depth')depth_element.text = str(list_top[7])annotation_root.append(size_element)segmented_person_element = ET.Element('segmented')segmented_person_element.text = '0'annotation_root.append(segmented_person_element)return tree, annotation_rootdef _bndbox(self, annotation_root, list_bndbox):for i in range(0, len(list_bndbox), 9):object_element = ET.Element('object')name_element = SubElement(object_element, 'name')name_element.text = list_bndbox[i]# flag_element = SubElement(object_element, 'flag')# flag_element.text = list_bndbox[i + 1]pose_element = SubElement(object_element, 'pose')pose_element.text = list_bndbox[i + 2]truncated_element = SubElement(object_element, 'truncated')truncated_element.text = list_bndbox[i + 3]difficult_element = SubElement(object_element, 'difficult')difficult_element.text = list_bndbox[i + 4]bndbox_element = SubElement(object_element, 'bndbox')xmin_element = SubElement(bndbox_element, 'xmin')xmin_element.text = str(list_bndbox[i + 5])ymin_element = SubElement(bndbox_element, 'ymin')ymin_element.text = str(list_bndbox[i + 6])xmax_element = SubElement(bndbox_element, 'xmax')xmax_element.text = str(list_bndbox[i + 7])ymax_element = SubElement(bndbox_element, 'ymax')ymax_element.text = str(list_bndbox[i + 8])annotation_root.append(object_element)return annotation_rootdef txt_to_xml(self, list_top, list_bndbox):tree, annotation_root = self._imageinfo(list_top)annotation_root = self._bndbox(annotation_root, list_bndbox)self.__indent(annotation_root)tree.write(list_top[0], encoding='utf-8', xml_declaration=True)def txt_2_xml(source_path, xml_save_dir, jpg_save_dir,txt_dir):COUNT = 0for folder_path_tuple, folder_name_list, file_name_list in os.walk(source_path):for file_name in file_name_list:file_suffix = os.path.splitext(file_name)[-1]if file_suffix != '.jpg':continuelist_top = []list_bndbox = []path = os.path.join(folder_path_tuple, file_name)xml_save_path = os.path.join(xml_save_dir, file_name.replace(file_suffix, '.xml'))txt_path = os.path.join(txt_dir, file_name.replace(file_suffix, '.txt'))filename = file_name#os.path.splitext(file_name)[0]checked = 'NO'#print(file_name)im = Image.open(path)im_w = im.size[0]im_h = im.size[1]#shutil.copy(path, jpg_save_dir)width = str(im_w)height = str(im_h)depth = '3'flag = 'rectangle'pose = 'Unspecified'truncated = '0'difficult = '0'list_top.extend([xml_save_path, folder_path_tuple, filename, path, checked, width, height, depth])for line in open(txt_path, 'r'):line = line.strip()info = line.split(' ')if 0 == int(info[0]):name = wordname_zkrc[0]elif 1 == int(info[0]):name = wordname_zkrc[1]elif 2 == int(info[0]):name = wordname_zkrc[2]elif 3 == int(info[0]):name = wordname_zkrc[3]else:name = wordname_zkrc[4]x_cen = float(info[1]) * im_wy_cen = float(info[2]) * im_hw = float(info[3]) * im_wh = float(info[4]) * im_hxmin = int(x_cen - w / 2)ymin = int(y_cen - h / 2)xmax = int(x_cen + w / 2)ymax = int(y_cen + h / 2)if xmin < 0:xmin = 0if ymin < 0:ymin = 0if xmax > im_w - 1:xmax = im_w - 1if ymax > im_h - 1:ymax = im_h - 1if w > 4 and h > 4:list_bndbox.extend([name, flag, pose, truncated, difficult,str(xmin), str(ymin), str(xmax), str(ymax)])if xmin < 0 or xmax > im_w - 1 or ymin < 0 or ymax > im_h - 1:print(xml_save_path)Xml_make().txt_to_xml(list_top, list_bndbox)COUNT += 1#print(COUNT, xml_save_path)if __name__ == "__main__":out_xml_path = "./4_classData/"  # .xml输出文件存放地址out_jpg_path = "./4_classData/"  # .jpg输出文件存放地址txt_path = "./images_all/"  # yolov3标注.txt和图片文件夹images_path = "./images_all/"  # image文件存放地址txt_2_xml(images_path, out_xml_path, out_jpg_path, txt_path)

这篇关于全网最全的yolo系列转换工具,从txt转xml,再从xml转txt,亲自测试好用的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1104557

相关文章

Linux使用dd命令来复制和转换数据的操作方法

《Linux使用dd命令来复制和转换数据的操作方法》Linux中的dd命令是一个功能强大的数据复制和转换实用程序,它以较低级别运行,通常用于创建可启动的USB驱动器、克隆磁盘和生成随机数据等任务,本文... 目录简介功能和能力语法常用选项示例用法基础用法创建可启动www.chinasem.cn的 USB 驱动

基于Python开发电脑定时关机工具

《基于Python开发电脑定时关机工具》这篇文章主要为大家详细介绍了如何基于Python开发一个电脑定时关机工具,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... 目录1. 简介2. 运行效果3. 相关源码1. 简介这个程序就像一个“忠实的管家”,帮你按时关掉电脑,而且全程不需要你多做

在MyBatis的XML映射文件中<trim>元素所有场景下的完整使用示例代码

《在MyBatis的XML映射文件中<trim>元素所有场景下的完整使用示例代码》在MyBatis的XML映射文件中,trim元素用于动态添加SQL语句的一部分,处理前缀、后缀及多余的逗号或连接符,示... 在MyBATis的XML映射文件中,<trim>元素用于动态地添加SQL语句的一部分,例如SET或W

Python xmltodict实现简化XML数据处理

《Pythonxmltodict实现简化XML数据处理》Python社区为提供了xmltodict库,它专为简化XML与Python数据结构的转换而设计,本文主要来为大家介绍一下如何使用xmltod... 目录一、引言二、XMLtodict介绍设计理念适用场景三、功能参数与属性1、parse函数2、unpa

关于Maven中pom.xml文件配置详解

《关于Maven中pom.xml文件配置详解》pom.xml是Maven项目的核心配置文件,它描述了项目的结构、依赖关系、构建配置等信息,通过合理配置pom.xml,可以提高项目的可维护性和构建效率... 目录1. POM文件的基本结构1.1 项目基本信息2. 项目属性2.1 引用属性3. 项目依赖4. 构

基于C#实现PDF文件合并工具

《基于C#实现PDF文件合并工具》这篇文章主要为大家详细介绍了如何基于C#实现一个简单的PDF文件合并工具,文中的示例代码简洁易懂,有需要的小伙伴可以跟随小编一起学习一下... 界面主要用于发票PDF文件的合并。经常出差要报销的很有用。代码using System;using System.Col

redis-cli命令行工具的使用小结

《redis-cli命令行工具的使用小结》redis-cli是Redis的命令行客户端,支持多种参数用于连接、操作和管理Redis数据库,本文给大家介绍redis-cli命令行工具的使用小结,感兴趣的... 目录基本连接参数基本连接方式连接远程服务器带密码连接操作与格式参数-r参数重复执行命令-i参数指定命

Python 标准库time时间的访问和转换问题小结

《Python标准库time时间的访问和转换问题小结》time模块为Python提供了处理时间和日期的多种功能,适用于多种与时间相关的场景,包括获取当前时间、格式化时间、暂停程序执行、计算程序运行时... 目录模块介绍使用场景主要类主要函数 - time()- sleep()- localtime()- g

使用Python实现批量访问URL并解析XML响应功能

《使用Python实现批量访问URL并解析XML响应功能》在现代Web开发和数据抓取中,批量访问URL并解析响应内容是一个常见的需求,本文将详细介绍如何使用Python实现批量访问URL并解析XML响... 目录引言1. 背景与需求2. 工具方法实现2.1 单URL访问与解析代码实现代码说明2.2 示例调用

JAVA中整型数组、字符串数组、整型数和字符串 的创建与转换的方法

《JAVA中整型数组、字符串数组、整型数和字符串的创建与转换的方法》本文介绍了Java中字符串、字符数组和整型数组的创建方法,以及它们之间的转换方法,还详细讲解了字符串中的一些常用方法,如index... 目录一、字符串、字符数组和整型数组的创建1、字符串的创建方法1.1 通过引用字符数组来创建字符串1.2