xml格式文件转txt格式以及解决数据集划分问题

        很多小伙伴可能在找到数据集后发现他的label文件是xml格式的,对于xml格式的文件是不能在YOLO中进行识别的,需要转化为txt格式文件。笔者在尝试了一些办法后,找到了一个比较好用的,分享如下:

一.xml格式转txt

        注意需要提前建立好存放txt标注的label文件。

import xml.etree.ElementTree as ET
from os import listdir, getcwd
import glob
import cv2
 
# xml格式文件注释
#
#  <object>
#    <name>person</name>
#    <pose>Unspecified</pose>
#    <truncated>0</truncated>
#    <difficult>0</difficult>
#    <bndbox>
#      <xmin>476</xmin>
#      <ymin>137</ymin>
#      <xmax>498</xmax>
#      <ymax>156</ymax>
#    </bndbox>
#  </object>

classes = ["person"] #<name>person</name>
 
def convert(size, box):#新增对框框范围的判断,防止0作为被除数
    if size[0]==0:
        dw=size[0]
    else:
        dw = 1.0 / size[0]

    if size[1] == 0:
        dw = size[1]
    else:
        dh = 1.0 / 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)
    return ('%.6f'%x,'%.6f'%y,'%.6f'%w,'%.6f'%h)
 
def convert_annotation(image_name,image_path):
    print(f"Processing {image_name}")
    f = open('./Annotations/' + image_name[:-3] + 'xml', encoding="utf8") # xml文件存放文件夹路径(Annoatations文件夹下)
    out_file = open('./label/' + image_name[:-3] + 'txt', 'w')  # 这里表示的是存放转换后的txt文件路径,所以需要我们自己先建立好label文件
    xml_text = f.read()
    root = ET.fromstring(xml_text)
    f.close()
    size = root.find('size')
    #填补图片高宽缺失
    img = cv2.imread(image_path)
    sz = img.shape

    w=int(sz[1])
    h=int(sz[0])
    # w = int(size.find('width').text)
    # h = int(size.find('height').text)
 
    for obj in root.iter('object'):
        cls = obj.find('name').text
        if cls not in classes:
            print(cls)
            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')
 
wd = getcwd()
 
if __name__ == '__main__':
 
    for image_path in glob.glob("./JPEGImages/*.jpg"):  # 放图片的文件夹,根据自己图片的类型替换JPG
        image_name = image_path.split('\\')[-1]
        convert_annotation(image_name,image_path)

二.数据集划分

        只需要修改最后面main的部分即可。

import os
import shutil
import random

random.seed(0)


def split_data(file_path, new_file_path, train_rate, val_rate, test_rate):
    eachclass_image = []
    for image in os.listdir(file_path):
        eachclass_image.append(image)
    total = len(eachclass_image)
    random.shuffle(eachclass_image)
    train_images = eachclass_image[0:int(train_rate * total)]
    val_images = eachclass_image[int(train_rate * total):int((train_rate + val_rate) * total)]
    test_images = eachclass_image[int((train_rate + val_rate) * total):]

    for image in train_images:
        print(image)
        old_path = file_path + '/' + image
        new_path1 = new_file_path + '/' + 'train' + '/' + 'images'
        if not os.path.exists(new_path1):
            os.makedirs(new_path1)
        new_path = new_path1 + '/' + image
        shutil.copy(old_path, new_path)
    new_name = os.listdir(new_file_path + '/' + 'train' + '/' + 'images')

    for im in new_name:
        old_xmlpath = txtpath + '/' + im[:-3] + 'txt'
        new_xmlpath1 = new_file_path + '/' + 'train' + '/' + 'labels'
        if not os.path.exists(new_xmlpath1):
            os.makedirs(new_xmlpath1)
        new_xmlpath = new_xmlpath1 + '/' + im[:-3] + 'txt'
        shutil.copy(old_xmlpath, new_xmlpath)

    for image in val_images:
        old_path = file_path + '/' + image
        new_path1 = new_file_path + '/' + 'val' + '/' + 'images'
        if not os.path.exists(new_path1):
            os.makedirs(new_path1)
        new_path = new_path1 + '/' + image
        shutil.copy(old_path, new_path)
    new_name = os.listdir(new_file_path + '/' + 'val' + '/' + 'images')

    for im in new_name:
        old_xmlpath = txtpath + '/' + im[:-3] + 'txt'
        new_xmlpath1 = new_file_path + '/' + 'val' + '/' + 'labels'
        if not os.path.exists(new_xmlpath1):
            os.makedirs(new_xmlpath1)
        new_xmlpath = new_xmlpath1 + '/' + im[:-3] + 'txt'
        shutil.copy(old_xmlpath, new_xmlpath)

    for image in test_images:
        old_path = file_path + '/' + image
        new_path1 = new_file_path + '/' + 'test' + '/' + 'images'
        if not os.path.exists(new_path1):
            os.makedirs(new_path1)
        new_path = new_path1 + '/' + image
        shutil.copy(old_path, new_path)
    new_name = os.listdir(new_file_path + '/' + 'test' + '/' + 'images')

    for im in new_name:
        old_xmlpath = txtpath + '/' + im[:-3] + 'txt'
        new_xmlpath1 = new_file_path + '/' + 'test' + '/' + 'labels'
        if not os.path.exists(new_xmlpath1):
            os.makedirs(new_xmlpath1)
        new_xmlpath = new_xmlpath1 + '/' + im[:-3] + 'txt'
        shutil.copy(old_xmlpath, new_xmlpath)


if __name__ == '__main__':
    file_path = "JPEGImages" #这里放你存JPG照片的地址
    txtpath = 'labels'       #这里放你存label的地址
    new_file_path = "datases"    #这里填你打算新建立的文件夹的名字,在下面的函数中可以更改你对数据集中train,val and test的划分。
    split_data(file_path, new_file_path, train_rate=0.7, val_rate=0.1, test_rate=0.2) 

        按照上述步骤,就可以得到自己训练的数据集了。

评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值