栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

Python将Json文件内容转为VOC中的XML文件

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Python将Json文件内容转为VOC中的XML文件

Python将Json文件内容转为VOC中的XML文件

前提条件相关介绍实验环境Json文件内容转为VOC中的XML文件

Json文件内容代码实现输出结果

前提条件

熟悉Python基本语法熟悉Python OS模块熟悉Python3 JSON 数据解析

相关介绍

Python是一种跨平台的计算机程序设计语言。是一个高层次的结合了解释性、编译性、互动性和面向对象的脚本语言。最初被设计用于编写自动化脚本(shell),随着版本的不断更新和语言新功能的添加,越多被用于独立的、大型项目的开发。Python OS模块提供了非常丰富的方法用来处理文件和目录。Python JSON (Javascript Object Notation) 是一种轻量级的数据交换格式。实验目标:Python将Json文件内容转为VOC中的XML文件

实验环境

Python 3.x (面向对象的高级语言)

Json文件内容转为VOC中的XML文件 Json文件内容

代码实现
# -*- coding: utf-8 -*-
"""
Created on 2022/02/25 10:00
@author: TFX
"""
import os
import json

path = r"citypersons_all_train.json"
file = open(path, "rb")
data = json.load(file)
img_list = data["images"]
annotations_list = data["annotations"]

new_xml=r"Annotations"
if not os.path.isdir(new_xml):
    os.makedirs(new_xml) 

for i in img_list:
	img_name = i["file_name"].split("/")[1]
	width, height = i["width"],i["height"]

	xml_name=img_name.split('.')[0]
	xml_file = open((new_xml + '\' + xml_name + '.xml'), 'w')

	xml_file.write('n')
	xml_file.write('    cityspersonn')
	xml_file.write('    ' + str(img_name)+ 'n')
	xml_file.write('    n')
	xml_file.write('        ' + str(width) + 'n')
	xml_file.write('        ' + str(height) + 'n')
	xml_file.write('        3n')
	xml_file.write('    n')
	
	for j in annotations_list:
		if i['id']==j['image_id']:
			x,y,w,h=j['bbox']

			xml_file.write('    n')
			xml_file.write('        ' + 'person' + 'n')
			xml_file.write('        Unspecifiedn')
			xml_file.write('        0n')
			xml_file.write('        0n')
			xml_file.write('        n')
			xml_file.write('            ' + str(x) + 'n')
			xml_file.write('            ' + str(y) + 'n')
			xml_file.write('            ' + str(x+w) + 'n')
			xml_file.write('            ' + str(y+h) + 'n')
			xml_file.write('        n')
			xml_file.write('    n')
	xml_file.write('n')
输出结果


    citysperson
    aachen_000000_000019_leftImg8bit.png
    
        497
        249
        3
    
    
        person
        Unspecified
        0
        0
        
            216.5646266686439
            108.0395278784154
            221.66312124268148
            120.90715704146263
        
    
    
        person
        Unspecified
        0
        0
        
            218.74969577180286
            107.5539569666023
            227.0044012726256
            120.90715704146263
        
    
    
        person
        Unspecified
        0
        0
        
            447.6963806916809
            105.85445877525643
            458.37894075156913
            131.58971710135089
        
    
    
        person
        Unspecified
        0
        0
        
            248.85509230421525
            52.19887301990856
            255.6530850695987
            60.45357852073131
        
    

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/744205.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号