训练YOLO v4模型时,xml格式转txt格式

YOLO训练时需要的标签数据是txt格式的

而以前做Faster-RCNN的时候,标签文件是xml格式的

特别当下载别人标记好的数据集时,很有可能提供的是xml格式的文件,那么如何将xml格式转为txt格式?

代码如下:

import xml.etree.ElementTree as ET
import pickle
import os
from os import listdir, getcwd
from os.path import join

sets = []
classes = ["face","face_mask"]  ##修改为自己的类别

#原样保留。size为图片大小
# 将ROI的坐标转换为yolo需要的坐标
# size是图片的w和h
# box里保存的是ROI的坐标(x,y的最大值和最小值)
# 返回值为ROI中心点相对于图片大小的比例坐标,和ROI的w、h相对于图片大小的比例
def convert(size, box):
    dw = 1./(size[0])
    dh = 1./(size[1])
    x = (box[0] + box[1])/2.0 - 1
    y = (box[2] + box[3])/2.0 - 1
    w = box[1] - box[0]
    h = box[3] - box[2]
    x = x*dw
    w = w*dw
    y = y*dh
    h = h*dh
    return (x,y,w,h)

def convert_annotation(image_add):
    #image_add进来的是带地址的.jpg
    image_add = os.path.split(image_add)[1]        #截取文件名带后缀
    image_add = image_add[0:image_add.find('.',1)] #删除后缀,现在只有文件名没有后缀
    #现在传进来的只有图片名没有后缀
   
    in_file = open('G:/1/xml/' + image_add + '.xml')        #修改为你自己的输入目录
    out_file = open('G:/1/labels/%s.txt'%(image_add), 'w')  #修改为你自己的输出目录

    tree=ET.parse(in_file)
    root = tree.getroot()
    
    if root.find('size'):

        size = root.find('size')
        w = int(size.find('width').text)    #偶尔xml标记出错,width或height设置为0了
        h = int(size.find('height').text)   #需要标记出来,便于单独处理
        if w==0:
            print("出错! width或height为0:  "+image_add)
            os.remove("G:/set/"+image_add+".jpg")
            os.remove("G:/set/"+image_add+".xml")
            return
        #在一个XML中每个Object的迭代
        for obj in root.iter('object'):
            #iter()方法可以递归遍历元素/树的所有子元素
            difficult = obj.find('difficult').text
            cls = obj.find('name').text
            #如果训练标签中的品种不在程序预定品种,或者difficult = 1,跳过此object
            if cls not in classes or int(difficult)==1:
                continue
            #cls_id 只等于1
            cls_id = classes.index(cls)
            xmlbox = obj.find('bndbox')
            #b是每个Object中,一个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')
    else:
        print("出错!xml缺少size:  "+image_add)      #偶尔xml缺少size,需要标记出来,便于单独处理
        os.remove("G:/set/"+image_add+".jpg")
        os.remove("G:/set/"+image_add+".xml")


image_adds = open("G:/1/train.txt")        #修改为你自己的训练数据集目录
for image_add in image_adds:
    #print(image_add)
    #image_add = image_add.strip()
    #print (image_add)
    convert_annotation(image_add)

 

  • 5
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 6
    评论
你可以参考以下代码来进行xml格式yolo txt格式换: ```python import xml.etree.ElementTree as ET import os def convert(size, box): dw = 1. / size[0] dh = 1. / size[1] x = (box[0] + box[1]) / 2.0 y = (box[2] + box[3]) / 2.0 w = box[1] - box[0] h = box[3] - box[2] x = x * dw w = w * dw y = y * dh h = h * dh return (x, y, w, h) def convert_annotation(xml_path, txt_path): in_file = open(xml_path) out_file = open(txt_path, 'w') tree = ET.parse(in_file) root = tree.getroot() size = root.find('size') w = int(size.find('width').text) h = int(size.find('height').text) for obj in root.iter('object'): difficult = obj.find('difficult').text cls = obj.find('name').text if cls not in classes or int(difficult) == 1: continue cls_id = classes.index(cls) xmlbox = 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') xml_folder = '/path/to/xml/folder' txt_folder = '/path/to/txt/folder' classes = ['class1', 'class2', 'class3'] for xml_file in os.listdir(xml_folder): xml_path = os.path.join(xml_folder, xml_file) txt_file = os.path.splitext(xml_file)[0] + '.txt' txt_path = os.path.join(txt_folder, txt_file) convert_annotation(xml_path, txt_path) ``` 这段代码可以将指定文件夹中的所有xml格式的标注文件换成yolo txt格式的标注文件,其中classes变量需要根据实际情况修改。
评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值