全网最全的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

相关文章

Spring Security 从入门到进阶系列教程

Spring Security 入门系列 《保护 Web 应用的安全》 《Spring-Security-入门(一):登录与退出》 《Spring-Security-入门(二):基于数据库验证》 《Spring-Security-入门(三):密码加密》 《Spring-Security-入门(四):自定义-Filter》 《Spring-Security-入门(五):在 Sprin

性能测试介绍

性能测试是一种测试方法,旨在评估系统、应用程序或组件在现实场景中的性能表现和可靠性。它通常用于衡量系统在不同负载条件下的响应时间、吞吐量、资源利用率、稳定性和可扩展性等关键指标。 为什么要进行性能测试 通过性能测试,可以确定系统是否能够满足预期的性能要求,找出性能瓶颈和潜在的问题,并进行优化和调整。 发现性能瓶颈:性能测试可以帮助发现系统的性能瓶颈,即系统在高负载或高并发情况下可能出现的问题

字节面试 | 如何测试RocketMQ、RocketMQ?

字节面试:RocketMQ是怎么测试的呢? 答: 首先保证消息的消费正确、设计逆向用例,在验证消息内容为空等情况时的消费正确性; 推送大批量MQ,通过Admin控制台查看MQ消费的情况,是否出现消费假死、TPS是否正常等等问题。(上述都是临场发挥,但是RocketMQ真正的测试点,还真的需要探讨) 01 先了解RocketMQ 作为测试也是要简单了解RocketMQ。简单来说,就是一个分

高效录音转文字:2024年四大工具精选!

在快节奏的工作生活中,能够快速将录音转换成文字是一项非常实用的能力。特别是在需要记录会议纪要、讲座内容或者是采访素材的时候,一款优秀的在线录音转文字工具能派上大用场。以下推荐几个好用的录音转文字工具! 365在线转文字 直达链接:https://www.pdf365.cn/ 365在线转文字是一款提供在线录音转文字服务的工具,它以其高效、便捷的特点受到用户的青睐。用户无需下载安装任何软件,只

【测试】输入正确用户名和密码,点击登录没有响应的可能性原因

目录 一、前端问题 1. 界面交互问题 2. 输入数据校验问题 二、网络问题 1. 网络连接中断 2. 代理设置问题 三、后端问题 1. 服务器故障 2. 数据库问题 3. 权限问题: 四、其他问题 1. 缓存问题 2. 第三方服务问题 3. 配置问题 一、前端问题 1. 界面交互问题 登录按钮的点击事件未正确绑定,导致点击后无法触发登录操作。 页面可能存在

科研绘图系列:R语言扩展物种堆积图(Extended Stacked Barplot)

介绍 R语言的扩展物种堆积图是一种数据可视化工具,它不仅展示了物种的堆积结果,还整合了不同样本分组之间的差异性分析结果。这种图形表示方法能够直观地比较不同物种在各个分组中的显著性差异,为研究者提供了一种有效的数据解读方式。 加载R包 knitr::opts_chunk$set(warning = F, message = F)library(tidyverse)library(phyl

业务中14个需要进行A/B测试的时刻[信息图]

在本指南中,我们将全面了解有关 A/B测试 的所有内容。 我们将介绍不同类型的A/B测试,如何有效地规划和启动测试,如何评估测试是否成功,您应该关注哪些指标,多年来我们发现的常见错误等等。 什么是A/B测试? A/B测试(有时称为“分割测试”)是一种实验类型,其中您创建两种或多种内容变体——如登录页面、电子邮件或广告——并将它们显示给不同的受众群体,以查看哪一种效果最好。 本质上,A/B测

烟火目标检测数据集 7800张 烟火检测 带标注 voc yolo

一个包含7800张带标注图像的数据集,专门用于烟火目标检测,是一个非常有价值的资源,尤其对于那些致力于公共安全、事件管理和烟花表演监控等领域的人士而言。下面是对此数据集的一个详细介绍: 数据集名称:烟火目标检测数据集 数据集规模: 图片数量:7800张类别:主要包含烟火类目标,可能还包括其他相关类别,如烟火发射装置、背景等。格式:图像文件通常为JPEG或PNG格式;标注文件可能为X

【生成模型系列(初级)】嵌入(Embedding)方程——自然语言处理的数学灵魂【通俗理解】

【通俗理解】嵌入(Embedding)方程——自然语言处理的数学灵魂 关键词提炼 #嵌入方程 #自然语言处理 #词向量 #机器学习 #神经网络 #向量空间模型 #Siri #Google翻译 #AlexNet 第一节:嵌入方程的类比与核心概念【尽可能通俗】 嵌入方程可以被看作是自然语言处理中的“翻译机”,它将文本中的单词或短语转换成计算机能够理解的数学形式,即向量。 正如翻译机将一种语言

无线领夹麦克风什么牌子好用?揭秘领夹麦克风哪个牌子音质好!

随着短视频行业的星期,围绕着直播和视频拍摄的电子数码类产品也迎来了热销不减的高增长,其中除了数码相机外,最为重要的麦克风也得到了日益增长的高需求,尤其是无线领夹麦克风,近几年可谓是异常火爆。别看小小的一对无线麦克风,它对于视频拍摄的音质起到了极为关键的作用。 不过目前市面上的麦克风品牌种类多到让人眼花缭乱,盲目挑选的话容易踩雷,那么无线领夹麦克风什么牌子好用?今天就给大家推荐几款音质好的