已有xml数据下新增单个类别(多条数据)

导包

from xml.etree.ElementTree import parse, Element
import xml.etree.ElementTree as ET
import os
import json

解析xml

save_Xml_Path = 'D:/test/xml_write/xml'


def parse_xml(name, tmp_list):
    fileName = 'D:/test/xml_write/xml_person/Annotations1/' + name[0:-4] + '.xml'
    doc = parse(fileName)
    root = doc.getroot()
    for item in tmp_list:
        element = Element('object')

        print(type(item))
        for i, v in item.items():
            e = Element(i)
            if isinstance(v, str):
                e.text = v
            if isinstance(v, dict):
                for i1, v1 in v.items():
                    e2 = Element(i1)
                    e2.text = v1
                    e.append(e2)
            element.append(e)
            print(type(e))
    print(type(element))
    root.append(element)
    name = fileName.split('/')[-1]
    xml_name = save_Xml_Path + '/' + name
    # print(xml_name)
    doc.write(xml_name, xml_declaration=True)

读取save.txt 将相应的数据取出并新增进xml中

def read_txt():
    for line in open('data/save.txt', 'r', encoding='utf-8'):
        jsonArr = json.loads(line.replace("'", "\""))
        if len(jsonArr) > 0:
            name = jsonArr[0]['name']
            print(name)
            tmp_list = []
            for item in jsonArr:
                if item['predicted_class'] == 'person':
                    xmin = str(item['position'][0])
                    ymin = str(item['position'][1])
                    xmax = str(item['position'][2])
                    ymax = str(item['position'][3])
                    tmp_1 = {
                            'name': 'person',
                            'pose': 'Unspecified',
                            'truncated': '0',
                            'difficult': '0',
                            'bndbox': {
                                'xmin': xmin,
                                'ymin': ymin,
                                'xmax': xmax,
                                'ymax': ymax,
                            }}
                    tmp_list.append(tmp_1)
        print('{} ---- {}'.format(name, tmp_list))
        if tmp_list:
            parse_xml(name, tmp_list)


if __name__ == '__main__':
    read_txt()

save.txt的格式如下

[{'name': '31011529001320014341-2021-05-12-12-49-43.450.jpeg', 'predicted_class': 'car', 'score': 1.0, 'position': [6, 138, 327, 427]}, {'name': '31011529001320014341-2021-05-12-12-49-43.450.jpeg', 'predicted_class': 'car', 'score': 0.999, 'position': [366, 6, 530, 127]}, {'name': '31011529001320014341-2021-05-12-12-49-43.450.jpeg', 'predicted_class': 'car', 'score': 0.804, 'position': [509, 6, 579, 61]}, {'name': '31011529001320014341-2021-05-12-12-49-43.450.jpeg', 'predicted_class': 'person', 'score': 0.733, 'position': [572, 38, 662, 180]}, {'name': '31011529001320014341-2021-05-12-12-49-43.450.jpeg', 'predicted_class': 'person', 'score': 0.96, 'position': [569, 17, 668, 163]}]
[{'name': '31011529001320014341-2021-05-12-11-46-14.931.jpeg', 'predicted_class': 'car', 'score': 1.0, 'position': [6, 135, 310, 436]}, {'name': '31011529001320014341-2021-05-12-11-46-14.931.jpeg', 'predicted_class': 'car', 'score': 0.968, 'position': [364, 6, 546, 131]}, {'name': '31011529001320014341-2021-05-12-11-46-14.931.jpeg', 'predicted_class': 'motorbike', 'score': 0.789, 'position': [563, 88, 654, 185]}, {'name': '31011529001320014341-2021-05-12-11-46-14.931.jpeg', 'predicted_class': 'person', 'score': 0.623, 'position': [570, 35, 649, 175]}]

你可能感兴趣的:(已有xml数据下新增单个类别(多条数据))