• xml转换成txt (VOC转换为YOLO)


    #!/usr/bin/env python3
    # -*- coding: utf-8 -*-
    
    import copy
    from lxml.etree import Element, SubElement, tostring, ElementTree
    
    import xml.etree.ElementTree as ET
    import pickle
    import os
    from os import listdir, getcwd
    from os.path import join
    
    def convet(size,box):
        x_center = (box[0] + box[1]) / 2.0
        y_center = (box[2] + box[3]) / 2.0
        x = x_center / size[0]
        y = y_center / size[1]
        w = (box[1] - box[0]) / size[0]
        h = (box[3] - box[2]) / size[1]
        return (x,y,w,h)
    
    def convert_annotation(xml_path, save_txt_path, classes):
        xml_files = os.listdir(xml_path)
        print(xml_path)
        for xml_name in xml_files:
            print(xml_name)
            xml_file = os.path.join(xml_path,xml_name)
            out_txt_path = os.path.join(save_txt_path,xml_name.split('.')[0]+'.txt')
            out_txt_f = open(out_txt_path,'w')
            tree = ET.parse(xml_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))
                print(w,h,b)
    
                bb = convet((w,h),b)
                out_txt_f.write(str(cls_id) + " " + " ".join([str(a) for a in bb]) + '\n')
    
    if __name__ == '__main__':
        classes = ['rolled-in_scale', 'patches', 'crazing', 'pitted_surface', 'inclusion', 'scratches']
        #要抓换的voc文件路径
        xml_files = r'D:\Code\YOLO\datasets\NEU\NEU-DEF\Annotations'
    
        #保存的txt路径
        save_txt_files = r'D:\Code\YOLO\datasets\NEU\NEU-DEF\labels'
        convert_annotation(xml_files, save_txt_files, classes)
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
  • 相关阅读:
    Vue--v-for
    pycharm2023.3专业版安装后打开无反应
    Vue系列(三)之 基础语法下篇【事件处理,表单综合案例,组件通信】
    小程序InnerAudioContext设置问题记录
    【UniApp】-uni-app-数据缓存
    数据准备之日志采集发展历程
    ES分布式搜索-索引库操作
    LeetCode:261. 以图判树 - Python
    李宏毅2022《机器学习/深度学习》——学习笔记(5)
    C++容器①
  • 原文地址:https://blog.csdn.net/m0_50127633/article/details/133808147