json文件批量转换xml
生活随笔
收集整理的這篇文章主要介紹了
json文件批量转换xml
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
在參加比賽時,官方給出的標記文件是json格式,自己編寫代碼使其轉換為xml格式。使用的是Python 標準庫之 xml.etree.ElementTree。
官方給出的格式:
轉換代碼如下:
import os, json import copyimport cv2 import numpy as np try:import xml.etree.cElementTree as ElementTree except ImportError:import xml.etree.ElementTree as ElementTree# <Element 'data' at 0x0000021AE8F79540> type:<class 'xml.etree.ElementTree.Element'>template_file = '../intelligent/1.xml' # xml模板 target_dir = r'./xmll/' # 保存路徑 image_dir = r'./picture/' # 圖片文件夾train_file = '../intelligent/labels/' # 存儲了圖片信息的json文件 file_nameee = '' json_path = './labels' # 提取json def parse_json(d):arr = np.array([d['image_name'], d['ignore'],d['category_name'],d['id'],d['occlusion'],d['truncation'],d['bbox']])return arrdef chinese_english(str):if str == 'suv':str = 'suv'elif str == '專業作業車':str = 'Professional_work_car'elif str == '兒童':str = 'children'elif str == '面包車':str = 'Van_minibus'elif str == '大客車':str = 'bus'elif str == '大貨車':str = 'Big_truck'elif str == '小貨車':str = 'buggy'elif str == '成年人':str = 'adult'elif str == '電動/摩托三輪車':str = 'Electric_tricycle'elif str == '電動/摩托車':str = 'Electric_motorcycle'elif str == '自行車':str = 'bcycle'elif str == '轎車':str = 'car'else:str = 'other'return strdef read_xml(in_path):'''''讀取并解析xml文件in_path: xml路徑return: ElementTree'''print(in_path)#prese()解析xml文件tree = ElementTree.parse(in_path)return tree def tiqu(strrr):new_str = "" # 創建一個空字符串for ch in strrr:if ch.isdigit(): # 字符串中的方法,可以直接判斷ch是否是數字new_str += chelse:new_str += " "sub_list = new_str.split() # 對新的字符串切片num_list = list(map(int, sub_list)) # map方法,使列表中的元素按照指定方式轉變return num_listdir = os.listdir(json_path) for file in dir:trainfiles = json.load(open(train_file+file,encoding='UTF-8'))len_nn = len(trainfiles)#tree = ElementTree()for k, line in enumerate(trainfiles):arr = parse_json(line)file_name = arr[0] # 文件名print(file_name)file_nameee = file_nameif k == 0:print(11)label = arr[2] # 標簽名# 坐標label = chinese_english(label)xmin = str(int(arr[6][0] - arr[6][2] / 2))xmax = str(int(arr[6][0] + arr[6][2] / 2)) #ymin = str(int(arr[6][1] - arr[6][3] / 2))ymax = str(int(arr[6][1] + arr[6][3] / 2))tree = read_xml(template_file) # 解析樹root = tree.getroot() # 根節點root.find('filename').text = file_name# sizesz = root.find('size')imggg = image_dir + file_nameim = cv2.imread(imggg) # 讀取圖片信息str1 = arr[4]str2 = arr[5]tiqu_str1 = tiqu(str1)tiqu_str2 = tiqu(str2)print(tiqu_str1)print(tiqu_str2)sz.find('height').text = str(im.shape[0])sz.find('width').text = str(im.shape[1])sz.find('depth').text = str(im.shape[2])# objectobj = root.find('object')obj.find('name').text = labelif tiqu_str1[0] == 0 and tiqu_str2[0] == 0:obj.find('truncated').text = str(0)else:obj.find('truncated').text = str(1)bb = obj.find('bndbox')bb.find('xmin').text = xminbb.find('ymin').text = yminbb.find('xmax').text = xmaxbb.find('ymax').text = ymax# 有多個標簽需要添加objectelse:label = arr[2]label = chinese_english(label)xmin = str(int(arr[6][0] - arr[6][2] / 2))xmax = str(int(arr[6][0] + arr[6][2] / 2)) #ymin = str(int(arr[6][1] - arr[6][3] / 2))ymax = str(int(arr[6][1] + arr[6][3] / 2))obj_ori = root.find('object')obj = copy.deepcopy(obj_ori) # 注意這里深拷貝str1 = arr[4]str2 = arr[5]tiqu_str1 = tiqu(str1)tiqu_str2 = tiqu(str2)if tiqu_str1[0] == 0 and tiqu_str2[0] == 0:obj.find('truncated').text = str(0)else:obj.find('truncated').text = str(1)obj.find('name').text = labelbb = obj.find('bndbox')bb.find('xmin').text = xminbb.find('ymin').text = yminbb.find('xmax').text = xmaxbb.find('ymax').text = ymaxroot.append(obj)xml_file = file_nameee.replace('jpg','xml')print("deakpf"+xml_file)tree.write(target_dir + xml_file, encoding='utf-8')總結
以上是生活随笔為你收集整理的json文件批量转换xml的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 微服务的设计模式,你用了几个
- 下一篇: Codeforces - 272C D