2、Python 将Labelme工具生成的json数据格式转成LabelImg工具生成的xml 数据格式

这篇博客的主要目的是:(注意 只适用4个坐标点,多坐标需要修改一下)

tensorflow 的训练数据是图片+json;而caffe训练集是图片+xml;

因此想使用python 完成他人已经在的json标注好的数据集直接转xml数据;json--->xml (Labelme====>>>LabelImg)

首先提供的Labelme是生成的json串:如下图

 

{
  "version": "3.6.16",
  "flags": {},
  "shapes": [
    {
      "label": "nothing_hand",
      "line_color": null,
      "fill_color": null,
      "points": [
        [
          135,
          349
        ],
        [
          643,
          724
        ]
      ],
      "shape_type": "rectangle"
    },
    {
      "label": "nothing_hand",
      "line_color": null,
      "fill_color": null,
      "points": [
        [
          558,
          675
        ],
        [
          817,
          1079
        ]
      ],
      "shape_type": "rectangle"
    }
  ],
  "lineColor": [
    0,
    255,
    0,
    128
  ],
  "fillColor": [
    0,
    255,
    127,
    128
  ],
  "imagePath": "000000.jpg",
  "imageData": "",
  "imageHeight": 1080,
  "imageWidth": 1920
}

 

然后我写了一段python脚本提取了数据:

# -*- coding: utf-8 -*-
import xmltodict
import os
import sys
import json
import io
global null
null=''
def file_name(file_dir):
    L = []
    for root, dirs, files in os.walk(file_dir):
        for file in files:
            if os.path.splitext(file)[1] == '.json':
                L.append(os.path.join(root , file))
        return L
path='/home/ubuntu/JPEGImages'
m_folder=path.split("/")[-1]
print 'm_folder=',m_folder
m_database='Unknown'
print 'm_database=',m_database
m_depth=3
print 'm_depth=',m_depth
m_segmented=0
print 'm_segmented=',m_segmented

m_pose='Unspecified'
print 'm_pose=',m_pose
m_truncated=0
print 'm_truncated=',m_truncated
m_difficult=0
print 'm_difficult=',m_difficult
m_segmented=0
print 'm_segmented=',m_segmented

path_list =file_name(path)
for name in enumerate(path_list): 
	m_path =name[1]
	print 'm_path=',m_path
        file_json = io.open(m_path,'r',encoding='utf-8') 
	json_data = file_json.read()
	data = json.loads(json_data)
	m_filename=data['imagePath']
	print 'm_filename=',m_filename
	m_width=data['imageWidth']
	print 'm_width=',m_width
	m_height=data['imageHeight']
	print 'm_height=',m_height
	m_name_0=data['shapes'][0]['label']
	print 'm_name_0=',m_name_0
	m_xmin_0=data['shapes'][0]['points'][0][0]
	print 'm_xmin_0=',m_xmin_0
	m_xmax_0=data['shapes'][0]['points'][0][1]
	print 'm_xmax_0=',m_xmax_0
	m_ymin_0=data['shapes'][0]['points'][1][0]
	print 'm_ymin_0=',m_ymin_0
	m_ymax_0=data['shapes'][0]['points'][1][1]
	print 'm_ymax_0=',m_ymax_0
	m_name_1=data['shapes'][1]['label']
	print 'm_name_1=',m_name_1
	m_xmin_1=data['shapes'][1]['points'][0][0]
	print 'm_xmin_1=',m_xmin_1
	m_xmax_1=data['shapes'][1]['points'][0][1]
	print 'm_xmax_1=',m_xmax_1
	m_ymin_1=data['shapes'][1]['points'][1][0]
	print 'm_ymin_1=',m_ymin_1
	m_ymax_1=data['shapes'][1]['points'][1][1]
	print 'm_ymax_1=',m_ymax_1
	
	

下图是labelImg生成xml

<annotation>
	<folder>JPEGImages</folder>
	<filename>000000.jpg</filename>
	<path>/home/ubuntu/JPEGImages/000000.jpg</path>
	<source>
		<database>Unknown</database>
	</source>
	<size>
		<width>1920</width>
		<height>1080</height>
		<depth>3</depth>
	</size>
	<segmented>0</segmented>
	<object>
		<name>nothing</name>
		<pose>Unspecified</pose>
		<truncated>0</truncated>
		<difficult>0</difficult>
		<bndbox>
			<xmin>342</xmin>
			<ymin>330</ymin>
			<xmax>581</xmax>
			<ymax>753</ymax>
		</bndbox>
	</object>
	<object>
		<name>nothing</name>
		<pose>Unspecified</pose>
		<truncated>1</truncated>
		<difficult>0</difficult>
		<bndbox>
			<xmin>531</xmin>
			<ymin>680</ymin>
			<xmax>804</xmax>
			<ymax>1080</ymax>
		</bndbox>
	</object>
</annotation>

然后我仿照它使用python构建了其结构;

 

#encoding:utf-8

from xml.dom.minidom import Document
doc = Document()  #创建DOM文档对象
DOCUMENT = doc.createElement('annotation') #创建根元素

floder  = doc.createElement('floder')          ##建立自己的开头
floder_text  = doc.createTextNode('JPEGImages')##建立自己的文本信息 
floder.appendChild(floder_text)                ##自己的内容
DOCUMENT.appendChild(floder)                   
doc.appendChild(DOCUMENT)


filename  = doc.createElement('filename')           
filename_text  = doc.createTextNode('00000.jpg') 
filename.appendChild(filename_text)               
DOCUMENT.appendChild(filename)                   
doc.appendChild(DOCUMENT)

path  = doc.createElement('path')           
path_text  = doc.createTextNode('/home/ubuntu/JPEGImages/000000.jpg') 
path.appendChild(path_text)               
DOCUMENT.appendChild(path)                   
doc.appendChild(DOCUMENT)


source  = doc.createElement('source') 
database = doc.createElement('database')
database_text = doc.createTextNode('Unknow') #元素内容写入
database.appendChild(database_text)
source.appendChild(database)                  
DOCUMENT.appendChild(source)                   
doc.appendChild(DOCUMENT)

size  = doc.createElement('size') 
width = doc.createElement('width')
width_text = doc.createTextNode('1920') #元素内容写入
width.appendChild(width_text)
size.appendChild(width) 

height = doc.createElement('height')
height_text = doc.createTextNode('1080') 
height.appendChild(height_text)
size.appendChild(height)   

depth = doc.createElement('depth')
depth_text = doc.createTextNode('3') 
depth.appendChild(depth_text)
size.appendChild(depth) 
             
DOCUMENT.appendChild(size) 

segmented  = doc.createElement('segmented')           
segmented_text  = doc.createTextNode('0') 
segmented.appendChild(segmented_text)               
DOCUMENT.appendChild(segmented)                   
doc.appendChild(DOCUMENT)
                  

object  = doc.createElement('object') 
name = doc.createElement('name')
name_text = doc.createTextNode('nothing')
name.appendChild(name_text)
object.appendChild(name) 

pose = doc.createElement('pose')
pose_text = doc.createTextNode('Unspecified') 
pose.appendChild(pose_text)
object.appendChild(pose)   

truncated = doc.createElement('truncated')
truncated_text = doc.createTextNode('0') 
truncated.appendChild(truncated_text)
object.appendChild(truncated) 


bndbox  = doc.createElement('bndbox') 
xmin = doc.createElement('xmin')
xmin_text = doc.createTextNode('342')
xmin.appendChild(xmin_text)
bndbox.appendChild(xmin) 

ymin = doc.createElement('ymin')
ymin_text = doc.createTextNode('330') 
ymin.appendChild(ymin_text)
bndbox.appendChild(ymin)   

xmax = doc.createElement('xmax')
xmax_text = doc.createTextNode('581')
xmax.appendChild(xmax_text)
bndbox.appendChild(xmax) 

ymax = doc.createElement('ymax')
ymax_text = doc.createTextNode('753') 
ymax.appendChild(ymax_text)
bndbox.appendChild(ymax) 
object.appendChild(bndbox)

DOCUMENT.appendChild(object) 



object  = doc.createElement('object') 
name = doc.createElement('name')
name_text = doc.createTextNode('nothing')
name.appendChild(name_text)
object.appendChild(name) 

pose = doc.createElement('pose')
pose_text = doc.createTextNode('Unspecified') 
pose.appendChild(pose_text)
object.appendChild(pose)   

truncated = doc.createElement('truncated')
truncated_text = doc.createTextNode('0') 
truncated.appendChild(truncated_text)
object.appendChild(truncated) 



bndbox  = doc.createElement('bndbox') 
xmin = doc.createElement('xmin')
xmin_text = doc.createTextNode('342')
xmin.appendChild(xmin_text)
bndbox.appendChild(xmin) 

ymin = doc.createElement('ymin')
ymin_text = doc.createTextNode('330') 
ymin.appendChild(ymin_text)
bndbox.appendChild(ymin)   

xmax = doc.createElement('xmax')
xmax_text = doc.createTextNode('581')
xmax.appendChild(xmax_text)
bndbox.appendChild(xmax) 

ymax = doc.createElement('ymax')
ymax_text = doc.createTextNode('753') 
ymax.appendChild(ymax_text)
bndbox.appendChild(ymax) 
object.appendChild(bndbox)

DOCUMENT.appendChild(object) 
############item:Python处理XML之Minidom################

########### 将DOM对象doc写入文件
f = open('aha.xml','w')

doc.writexml(f,indent = '\t',newl = '\n', addindent = '\t',encoding='utf-8')
f.close()

然后我的想法是提取labelimg生成json的数据的关键字段进行填充我搭建的xml字段即可;

然后我进行两份代码整合;

# -*- coding: utf-8 -*-
import xmltodict
import os
import sys
import json
import io
import os
from xml.dom.minidom import Document
global null
null=''
def file_name(file_dir):
    L = []
    for root, dirs, files in os.walk(file_dir):
        for file in files:
            if os.path.splitext(file)[1] == '.json':
                L.append(os.path.join(root , file))
        return L
path='./JPEGImages'
m_folder=path.split("/")[-1]
print 'm_folder=',m_folder
m_database='Unknown'
print 'm_database=',m_database
m_depth=3
print 'm_depth=',m_depth
m_segmented=0
print 'm_segmented=',m_segmented

m_pose='Unspecified'
print 'm_pose=',m_pose
m_truncated=0
print 'm_truncated=',m_truncated
m_difficult=0
print 'm_difficult=',m_difficult
m_segmented=0
print 'm_segmented=',m_segmented

path_list =file_name(path)
for name in enumerate(path_list): 
	m_path =name[1]
        dir=os.path.dirname(m_path)
        print 'dir=',dir

        file_json = io.open(m_path,'r',encoding='utf-8') 
	json_data = file_json.read()
	data = json.loads(json_data)
	m_filename=data['imagePath']
	print 'm_filename=',m_filename
	m_path=dir+'/'+m_filename
	print 'm_path=',m_path
	m_width=data['imageWidth']
	print 'm_width=',m_width
	m_height=data['imageHeight']
	print 'm_height=',m_height
	object_name= os.path.splitext(m_filename)[0]
	new_object_name=object_name+'.xml'
	print new_object_name
	doc = Document()  #创建DOM文档对象
	DOCUMENT = doc.createElement('annotation') #创建根元素

	floder  = doc.createElement('floder')         
	floder_text  = doc.createTextNode(m_folder)
	floder.appendChild(floder_text)               
	DOCUMENT.appendChild(floder)                   
	doc.appendChild(DOCUMENT)


	filename  = doc.createElement('filename')           
	filename_text  = doc.createTextNode(m_filename) 
	filename.appendChild(filename_text)               
	DOCUMENT.appendChild(filename)                   
	doc.appendChild(DOCUMENT)

	path  = doc.createElement('path')           
	path_text  = doc.createTextNode(m_path) 
	path.appendChild(path_text)               
	DOCUMENT.appendChild(path)                   
	doc.appendChild(DOCUMENT)


	source  = doc.createElement('source') 
	database = doc.createElement('database')
	database_text = doc.createTextNode(m_database) #元素内容写入
	database.appendChild(database_text)
	source.appendChild(database)                  
	DOCUMENT.appendChild(source)                   
	doc.appendChild(DOCUMENT)

	size  = doc.createElement('size') 
	width = doc.createElement('width')
	width_text = doc.createTextNode(str(m_width)) #元素内容写入
	width.appendChild(width_text)
	size.appendChild(width) 

	height = doc.createElement('height')
	height_text = doc.createTextNode(str(m_height)) 
	height.appendChild(height_text)
	size.appendChild(height)   

	depth = doc.createElement('depth')
	depth_text = doc.createTextNode(str(m_depth)) 
	depth.appendChild(depth_text)
	size.appendChild(depth) 
             
	DOCUMENT.appendChild(size) 

	segmented  = doc.createElement('segmented')           
	segmented_text  = doc.createTextNode(str(m_segmented)) 
	segmented.appendChild(segmented_text)               
	DOCUMENT.appendChild(segmented)                   
	doc.appendChild(DOCUMENT)
        for i in  range(len(data['shapes'])):
                m_xmin_0=(data['shapes'][i]['points'][0][0]  if(data['shapes'][i]['points'][0][0]<data['shapes'][i]['points'][1][0]) else data['shapes'][i]['points'][1][0])
		print 'm_xmin_0=',m_xmin_0
                m_ymin_0=(data['shapes'][i]['points'][0][1]  if(data['shapes'][i]['points'][0][1]<data['shapes'][i]['points'][1][1]) else data['shapes'][i]['points'][1][1])
		print 'm_ymin_0=',m_ymin_0
                m_xmax_0=(data['shapes'][i]['points'][1][0]  if(data['shapes'][i]['points'][0][0]<data['shapes'][i]['points'][1][0]) else data['shapes'][i]['points'][0][0])
		print 'm_xmax_0=',m_xmax_0
                m_ymax_0=(data['shapes'][i]['points'][1][1]  if(data['shapes'][i]['points'][0][1]<data['shapes'][i]['points'][1][1]) else data['shapes'][i]['points'][0][1])
		print 'm_ymax_0=',m_ymax_0         
		m_name_0=data['shapes'][i]['label']
		print 'm_name_0=',m_name_0
		object  = doc.createElement('object') 
		name = doc.createElement('name')
		name_text = doc.createTextNode(m_name_0)
		name.appendChild(name_text)
		object.appendChild(name) 

		pose = doc.createElement('pose')
		pose_text = doc.createTextNode(m_pose) 
		pose.appendChild(pose_text)
		object.appendChild(pose)   

		truncated = doc.createElement('truncated')
		truncated_text = doc.createTextNode(str(m_truncated)) 
		truncated.appendChild(truncated_text)
		object.appendChild(truncated) 


		bndbox  = doc.createElement('bndbox') 
		xmin = doc.createElement('xmin')
		xmin_text = doc.createTextNode(str(m_xmin_0))
		xmin.appendChild(xmin_text)
		bndbox.appendChild(xmin) 

		ymin = doc.createElement('ymin')
		ymin_text = doc.createTextNode(str(m_ymin_0)) 
		ymin.appendChild(ymin_text)
		bndbox.appendChild(ymin)   
	
		xmax = doc.createElement('xmax')
		xmax_text = doc.createTextNode(str(m_xmax_0))
		xmax.appendChild(xmax_text)
		bndbox.appendChild(xmax) 

		ymax = doc.createElement('ymax')
		ymax_text = doc.createTextNode(str(m_ymax_0)) 
		ymax.appendChild(ymax_text)
		bndbox.appendChild(ymax) 
		object.appendChild(bndbox)

		DOCUMENT.appendChild(object) 
                new_path_filename='./Annotations'+'/'+new_object_name
                print 'new_path_filename=',new_path_filename
                f = open(new_path_filename,'w')

		doc.writexml(f,indent = '\t',newl = '\n', addindent = '\t',encoding='utf-8')
		f.close()

 

上述算法描述,只试用四个坐标点,多坐标点需要修改;参考片段代码

# -*- coding: utf-8 -*-
import os
import sys
import json
import io

path='./'

def file_name(file_dir):
    L = []
    for root, dirs, files in os.walk(file_dir):
        for file in files:
            if os.path.splitext(file)[1] == '.json':
                L.append(os.path.join(root , file))
        return L
s
for name in enumerate(file_name(path)): 
	m_path =name[1]
        dir=os.path.dirname(m_path)
        print 'dir=',dir
 
        file_json = io.open(m_path,'r',encoding='utf-8') 
	json_data = file_json.read()
	data = json.loads(json_data)
	m_filename=data['imagePath']
	m_path=dir+'/'+m_filename
	print 'm_path=',m_path
        for i in  range(0,len(data['shapes'])):
            for j in range(0,len(data['shapes'][i]['points'])):
                m_xmin_0=(data['shapes'][i]['points'][j][0]  if(data['shapes'][i]['points'][0][0]<data['shapes'][i]['points'][1][0]) else data['shapes'][i]['points'][1][0])
		print 'm_xmin_0=',m_xmin_0
                m_ymin_0=(data['shapes'][i]['points'][j][1]  if(data['shapes'][i]['points'][0][1]<data['shapes'][i]['points'][1][1]) else data['shapes'][i]['points'][1][1])
		print 'm_ymin_0=',m_ymin_0
                #m_xmax_0=(data['shapes'][i]['points'][1][0]  if(data['shapes'][i]['points'][0][0]<data['shapes'][i]['points'][1][0]) else data['shapes'][i]['points'][0][0])
		#print 'm_xmax_0=',m_xmax_0
                #m_ymax_0=(data['shapes'][i]['points'][1][1]  if(data['shapes'][i]['points'][0][1]<data['shapes'][i]['points'][1][1]) else data['shapes'][i]['points'][0][1])
		#print 'm_ymax_0=',m_ymax_0         

		

现学现用,python 果然很强大~~~

©️2020 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页