python提取xml/json文件中的标签信息到txt文件中

python提取xml文件中的标签信息到txt文件中

# 将xml中的类别属性写在txt文件中
# -*- coding:utf-8 -*-
import xml.etree.ElementTree as ET
import os


ann_filePath = r'xml/'
xml_list = os.listdir(ann_filePath)
xml_name_list = []
cls_list = []
# cls_name = ""
for xml_name in xml_list:
    cla_list = []
    cls_name = ""
    filename = os.path.join(ann_filePath, xml_name)
    xml_name = os.path.splitext(xml_name)[0]  # 去除后缀
    xml_name_list.append(xml_name)  # 图片名列表
    tree = ET.parse(filename)  # ElementTree对象
    objs = tree.findall('object')
    for obj in objs:
        cls = obj.find('name').text
        cls = 'new_' + cls
        if cls not in cla_list:
            cla_list.append(cls)
            cls_name = cls_name + cls + ' '

    print(xml_name,cls_name)
    with open(r"testxml1.txt", "a") as f:
        f.write(xml_name + '.jpg' + ' ' + str(cls_name) + '\n')

python提取json文件中的标签信息到txt文件中

# -*- coding:UTF-8 -*-
'''
将json文件转为类似voc中的xml格式
'''
import os
import numpy as np
import codecs
# from sklearn.model_selection import train_test_split

import json
from glob import glob
import cv2
import shutil
labelme_path = r"json_label/"
files = glob(labelme_path + "*.json")
files = [i.replace("\\","/").split("/")[-1].split(".json")[0] for i in files]
# print(files)
for json_file_ in files:
    cla_list = []
    cls_name = ""
    json_filename = labelme_path + json_file_ + ".json"
    json_file = json.load(open(json_filename, "r", encoding="utf-8"))
    for multi in json_file["shapes"]:
        label = multi["label"]
        cls = 'new_' + label
        if cls not in cla_list:
            cla_list.append(cls)
            cls_name = cls_name + cls + ' '
    with open(r"testxml1.txt", "a") as f:
        f.write(json_file_ + '.jpg' + ' ' + str(cls_name) + '\n')

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值