python 将xml汇总转换为CSV格式

注意:下面代码主要针对两种不同的xml写的,根据自身情况修改(分别是labelimg、标注精灵两款标注工具)

#解决了一个xml多个标签问题
import os
import glob
import pandas as pd
import xml.etree.ElementTree as ET
"""两种不同标准工具的xml转换为CSV(两种标注出来的xml格式不一样)"""
def xml_to_csv(path):
    xml_list = []
    a=0
    print(glob.glob(path + '/*.xml'))
    for xml_file in glob.glob(path + '/*.xml'):
        tree = ET.parse(xml_file)
        root = tree.getroot()
        a+=1
        print("地址:{}".format(xml_file),"第{}个".format(a))

        if root.findall('object'):##labelimg标注工具
            print("labelimg")
            for member in root.findall('object'):
                print(root.find('filename').text,
                         int(root.find('size')[0].text),
                         int(root.find('size')[1].text),
                         member[0].text,
                         int(member[4][0].text),
                         int(member[4][1].text),
                         int(member[4][2].text),
                         int(member[4][3].text))
                # ('133.jpg', 416, 416, 'phone', 82, 54, 339, 197)
                value = (root.find('filename').text,
                         int(root.find('size')[0].text),
                         int(root.find('size')[1].text),
                         member[0].text,
                         int(member[4][0].text),
                         int(member[4][1].text),
                         int(member[4][2].text),
                         int(member[4][3].text)
                         )
                xml_list.append(value)
            # column_name = ['filename', 'width', 'height', 'class', 'xmin', 'ymin', 'xmax', 'ymax']
            # xml_df = pd.DataFrame(xml_list, columns=column_name)
            # return xml_df
        else:##标注精灵
            print("标注精灵")
            for member in root.findall("outputs/object/item"):#利用xpath解析方式,百度自行了解
                if member[0].text=="1":#判断一个xml中有我不想要的隔离出来
                    continue
                print(root.find('path').text.split("\\")[-1],
                         int(root.find('size')[0].text),
                         int(root.find('size')[1].text),
                         member[0].text,
                         max(int(member[1][0].text),0),
                         max(int(member[1][1].text),0),
                         max(int(member[1][2].text),0),
                         max(int(member[1][3].text),0))
                # # ('133.jpg', 416, 416, 'phone', 82, 54, 339, 197)
                value = (root.find('path').text.split("\\")[-1],
                         int(root.find('size')[0].text),
                         int(root.find('size')[1].text),
                         member[0].text,
                         max(int(member[1][0].text), 0),
                         max(int(member[1][1].text), 0),
                         max(int(member[1][2].text), 0),
                         max(int(member[1][3].text), 0)
                         )
                xml_list.append(value)
            # column_name = ['filename', 'width', 'height', 'class', 'xmin', 'ymin', 'xmax', 'ymax']
    column_name = ['filename', 'width', 'height', 'class', 'xmin', 'ymin', 'xmax', 'ymax']
    xml_df = pd.DataFrame(xml_list, columns=column_name)
    return xml_df




def main():
    image_path = os.path.join('/home/hanqing/SSD-Tensorflow-master/VOC2019/', 'Annotations/test')#读取
    xml_df = xml_to_csv(image_path)
    xml_df.to_csv('/home/hanqing/SSD-Tensorflow-master/VOC2019/ImageSets/Main/csv/sj_test.csv', index=None)#保存
    print('Successfully converted xml to csv.')
main()

 

你可能感兴趣的:(图像标注)