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')