labelme的json数据转成labelImg的xml数据

更新时间:2023-06-23 14:42:51 阅读: 评论:0

labelme的json数据转成labelImg的xml数据
所有⽂件在json_to_xml⽂件夹中,共有三个python⽂件,分别包括main.py,create_xml_anno.py,read_json_anno.py。下⾯分别介绍这三个⽂件。
输⼊:由labelme的rectangle或这polygon⽅式标注⽣成的json⽂件;
输出:类似于labelImg的rectangle⽅式标注⽣成的xml⽂件,是左上⾓和右下⾓的坐标。
read_json_anno
# -*- coding: utf-8 -*-
import numpy as np
import json
class ReadAnno:
def __init__(lf, json_path, process_mode="rectangle"):
lf.json_data = json.load(open(json_path))
lf.filename = lf.json_data['imagePath']
lf.width = lf.json_data['imageWidth']
lf.height = lf.json_data['imageHeight']
asrt process_mode in ["rectangle", "polygon"]
if process_mode == "rectangle":
lf.process_polygon_shapes()
elif process_mode == "polygon":
lf.process_polygon_shapes()
狡猾怎么写def process_rectangle_shapes(lf):
for single_shape in lf.json_data['shapes']:
bbox_class = single_shape['label']
xmin = single_shape['points'][0][0]
ymin = single_shape['points'][0][1]
xmax = single_shape['points'][1][0]
ymax = single_shape['points'][1][1]
def process_polygon_shapes(lf):
for single_shape in lf.json_data['shapes']:
bbox_class = single_shape['label']
temp_points = []
for couple_point in single_shape['points']:
x = float(couple_point[0])
y = float(couple_point[1])
temp_points.append([x,y])
temp_points = np.array(temp_points)
xmin, ymin = temp_points.min(axis=0)
xmax, ymax = temp_points.max(axis=0)
def get_width_height(lf):
return lf.width, lf.height
def get_filename(lf):
return lf.filename
def get_coordis(lf):
dis
当中有2种模式,⼀种rectangle,表⽰json⽂件是通过labelme的rectangle⽅式标注,⼀种polygon,表⽰json⽂件是通过labelme的polygon⽅式标注。根据实际情况进⾏变换。
create_xml_anno
怎么清理手机内存
# -*- coding: utf-8 -*-
from xml.dom.minidom import Document
class CreateAnno:
def __init__(lf,):
lf.doc = Document()  # 创建DOM⽂档对象
lf.anno = ateElement('annotation')  # 创建根元素
lf.anno = ateElement('annotation')  # 创建根元素
lf.doc.appendChild(lf.anno)
lf.add_folder()
lf.add_path()
lf.add_source()
lf.add_gmented()
# lf.add_filename()
# lf.add_pic_size(width_text_str=str(width), height_text_str=str(height), depth_text_str=str(depth))
def add_folder(lf, floder_text_str='JPEGImages'):
floder = ateElement('floder')  ##建⽴⾃⼰的开头
floder_text = ateTextNode(floder_text_str)  ##建⽴⾃⼰的⽂本信息
floder.appendChild(floder_text)  ##⾃⼰的内容
lf.anno.appendChild(floder)
def add_filename(lf, filename_text_str='00000.jpg'):
filename = ateElement('filename')
filename_text = ateTextNode(filename_text_str)
filename.appendChild(filename_text)
lf.anno.appendChild(filename)
def add_path(lf, path_text_str="None"):
path = ateElement('path')
path_text = ateTextNode(path_text_str)
path.appendChild(path_text)
lf.anno.appendChild(path)
def add_source(lf, databa_text_str="Unknow"):
source = ateElement('source')
databa = ateElement('databa')
databa_text = ateTextNode(databa_text_str)  # 元素内容写⼊
databa.appendChild(databa_text)
source.appendChild(databa)
lf.anno.appendChild(source)
def add_pic_size(lf, width_text_str="0", height_text_str="0", depth_text_str="3"):
size = ateElement('size')
width = ateElement('width')
width_text = ateTextNode(width_text_str)  # 元素内容写⼊
width.appendChild(width_text)
size.appendChild(width)
height = ateElement('height')
height_text = ateTextNode(height_text_str)
height.appendChild(height_text)
size.appendChild(height)
depth = ateElement('depth')
depth_text = ateTextNode(depth_text_str)
depth.appendChild(depth_text)
无花果叶size.appendChild(depth)
lf.anno.appendChild(size)
def add_gmented(lf, gmented_text_str="0"):
小孩咬人
gmented = ateElement('gmented')
gmented_text = ateTextNode(gmented_text_str)
gmented.appendChild(gmented_text)
lf.anno.appendChild(gmented)
def add_object(lf,
name_text_str="None",
xmin_text_str="0",
ymin_text_str="0",
ymin_text_str="0",
xmax_text_str="0",
ymax_text_str="0",
po_text_str="Unspecified",
truncated_text_str="0",下颌骨突出
difficult_text_str="0"):
object = ateElement('object')
name = ateElement('name')
狼怎么画简笔画
name_text = ateTextNode(name_text_str)
name.appendChild(name_text)
object.appendChild(name)
po = ateElement('po')
po_text = ateTextNode(po_text_str)
po.appendChild(po_text)
object.appendChild(po)
truncated = ateElement('truncated')
truncated_text = ateTextNode(truncated_text_str)
truncated.appendChild(truncated_text)
object.appendChild(truncated)
difficult = ateElement('difficult')
difficult_text = ateTextNode(difficult_text_str)
博山饭店difficult.appendChild(difficult_text)
object.appendChild(difficult)
银耳桂圆汤
bndbox = ateElement('bndbox')
xmin = ateElement('xmin')
xmin_text = ateTextNode(xmin_text_str)
xmin.appendChild(xmin_text)
bndbox.appendChild(xmin)
ymin = ateElement('ymin')
ymin_text = ateTextNode(ymin_text_str)
ymin.appendChild(ymin_text)
bndbox.appendChild(ymin)
xmax = ateElement('xmax')
xmax_text = ateTextNode(xmax_text_str)
xmax.appendChild(xmax_text)
bndbox.appendChild(xmax)
ymax = ateElement('ymax')
ymax_text = ateTextNode(ymax_text_str)
ymax.appendChild(ymax_text)
bndbox.appendChild(ymax)
object.appendChild(bndbox)
lf.anno.appendChild(object)
def get_anno(lf):
return lf.anno
def get_doc(lf):
return lf.doc
def save_doc(lf, save_path):
with open(save_path, "w") as f:
lf.doc.writexml(f, indent='\t', newl='\n', addindent='\t', encoding='utf-8') main
三个重要参数:json所在的⽂件夹,将要保存的xml⽂件夹,原数据的标注⽅式。
import os
from tqdm import tqdm
from json_ad_json_anno import ReadAnno
from json_ate_xml_anno import CreateAnno
def json_transform_xml(json_path, xml_path, process_mode="rectangle"):
json_path = json_path
json_anno = ReadAnno(json_path, process_mode=process_mode)
width, height = _width_height()
filename = _filename()
coordis = _coordis()
xml_anno = CreateAnno()
xml_anno.add_filename(filename)
xml_anno.add_pic_size(width_text_str=str(width), height_text_str=str(height), depth_text_str=str(3))    for xmin,ymin,xmax,ymax,label in coordis:
xml_anno.add_object(name_text_str=str(label),
xmin_text_str=str(int(xmin)),
ymin_text_str=str(int(ymin)),
xmax_text_str=str(int(xmax)),
ymax_text_str=str(int(ymax)))
xml_anno.save_doc(xml_path)
if __name__ == "__main__":
root_json_dir = r"E:\json"
root_save_xml_dir = r"E:\xml"
for json_filename in tqdm(os.listdir(root_json_dir)):
json_path = os.path.join(root_json_dir, json_filename)
save_xml_path = os.path.join(root_save_xml_dir, place(".json", ".xml"))
json_transform_xml(json_path, save_xml_path, process_mode="polygon")

本文发布于:2023-06-23 14:42:51,感谢您对本站的认可!

本文链接:https://www.wtabcd.cn/fanwen/fan/82/1021783.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

标签:件夹   元素   数据   创建   情况
相关文章
留言与评论(共有 0 条评论)
   
验证码:
推荐文章
排行榜
Copyright ©2019-2022 Comsenz Inc.Powered by © 专利检索| 网站地图