Labelme标注json文件批量转xml格式的python代码

labelme标注json转xml

import os
from typing import List, Any
import numpy as np
import codecs
import json
from glob import glob
import cv2
import shutil
from sklearn.model_selection import train_test_split
 
labelme_path = "G:/12json/"      #json文件夹路径
saved_path = "G:/12json/"        # xml保存路径

if not os.path.exists(saved_path + "Annotations"):
    os.makedirs(saved_path + "Annotations")

files = glob(labelme_path + "*.json")
files = [i.replace("\\","/").split("/")[-1].split(".json")[0] for i in files]
print(files)

for json_file_ in files:
    json_filename = labelme_path + json_file_ + ".json"
    json_file = json.load(open(json_filename, "r", encoding="utf-8"))
    height, width, channels = 720,1280,3<
  • 3
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 5
    评论
好的,您可以使用以下代码实现: ```python import os import json import xml.etree.ElementTree as ET # 定义标签和颜色 LABELS = {'circle': 0} COLORS = {'circle': '0,255,0'} # 定义json文件路径和xml文件路径 json_dir = '/path/to/json/dir' xml_dir = '/path/to/xml/dir' # 遍历json文件夹中的所有文件 for json_file in os.listdir(json_dir): # 如果不是json文件,则跳过 if not json_file.endswith('.json'): continue # 读取json文件内容 with open(os.path.join(json_dir, json_file), 'r') as f: data = json.load(f) # 获取文件名(去掉扩展名) filename = os.path.splitext(json_file)[0] # 创建xml文件 root = ET.Element('annotation') # 添加filename元素 ET.SubElement(root, 'filename').text = filename + '.jpg' # 添加size元素 size = ET.SubElement(root, 'size') ET.SubElement(size, 'width').text = str(data['imageWidth']) ET.SubElement(size, 'height').text = str(data['imageHeight']) ET.SubElement(size, 'depth').text = '3' # 遍历shapes for shape in data['shapes']: label = shape['label'] if label not in LABELS: continue # 添加object元素 obj = ET.SubElement(root, 'object') ET.SubElement(obj, 'name').text = label ET.SubElement(obj, 'pose').text = 'Unspecified' ET.SubElement(obj, 'truncated').text = '0' ET.SubElement(obj, 'difficult').text = '0' # 添加bndbox元素 bndbox = ET.SubElement(obj, 'bndbox') if shape['shape_type'] == 'circle': # 如果是圆形,获取外切矩形 x, y = shape['points'][0] r = shape['points'][1][0] - x ET.SubElement(bndbox, 'xmin').text = str(round(x - r)) ET.SubElement(bndbox, 'ymin').text = str(round(y - r)) ET.SubElement(bndbox, 'xmax').text = str(round(x + r)) ET.SubElement(bndbox, 'ymax').text = str(round(y + r)) else: # 否则,获取矩形 x1, y1 = shape['points'][0] x2, y2 = shape['points'][1] ET.SubElement(bndbox, 'xmin').text = str(round(min(x1, x2))) ET.SubElement(bndbox, 'ymin').text = str(round(min(y1, y2))) ET.SubElement(bndbox, 'xmax').text = str(round(max(x1, x2))) ET.SubElement(bndbox, 'ymax').text = str(round(max(y1, y2))) # 添加difficult元素 ET.SubElement(obj, 'difficult').text = '0' # 添加bndbox颜色 ET.SubElement(obj, 'color').text = COLORS[label] # 保存xml文件 xml_file = os.path.join(xml_dir, filename + '.xml') tree = ET.ElementTree(root) tree.write(xml_file) ``` 这段代码会将指定文件夹中的所有json文件换为xml文件,并将xml文件保存到指定文件夹中。处理圆形标注时,会将圆形换为外切矩形。同时,会为每个标注添加颜色信息,以便在标注时更加直观。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Ben Dan

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值