Yolo训练数据集生成(XML到txt),YOLO,制作,xml,转

发表时间:2020-12-25

YOLO训练数据集制作(xml转txt)

如题,只需要修改下面的class、xmlpath 和savepath两处即可。若运行不成功,可能试试修改obj.find(‘Difficult’).text为obj.find(‘difficult’).text

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

classes = ["liner", "container ship","bulk carrier","island reef","sailboat","other ship"] 


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)

wd = getcwd()

if __name__ == "__main__":
    xmlpath = "D:/2020_12_23增补/newxml"
    savepath = "D:/2020_12_23增补/txt"
    xmlfiles = os.listdir(xmlpath)
    for xml in xmlfiles:
        in_file = open(os.path.join(xmlpath,xml))
        out_file = open(os.path.join(savepath,xml.replace("xml","txt")), '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')

#```

文章来源互联网,如有侵权,请联系管理员删除。邮箱:417803890@qq.com / QQ:417803890

微配音

Python Free

邮箱:417803890@qq.com
QQ:417803890

皖ICP备19001818号
© 2019 copyright www.pythonf.cn - All rights reserved

微信扫一扫关注公众号:

联系方式

Python Free