VIA标注格式转Labelme标注格式

最近做计算机视觉相关工作,采用VIA工具进行标注,现在需要将其转为Labelme格式。

python 复制代码
import json
import cv2
import os
import numpy as np

'''
0 正常
1 异常
2 有
3 无
'''

# 读取json文件
with open('01725bff-0fc6-4017-ad33-830df526438a.json', 'r', encoding='utf-8') as f:
    data = json.load(f)
    
attribute = data['attribute']
file = data['file']
metadata = data['metadata']

label_data = {}

for key, value in metadata.items():
    file_id = key.split('_')[0]
    fname = file[file_id]['fname']
    if fname not in label_data.keys():
        label_data[fname] = []
    xy_class = value['xy'][0]
    xy_coords = value['xy'][1:]
    av = value['av']['1']
    if av == "0":
        av = 2
    if av == "1":
        av = 3
    if av == "2":
        av = 0
    if av == "3":
        av = 1  
    if xy_class == 7:
        label_data[fname].append((xy_coords, av))
        
# 以上是获取via标注的数据,下面是转换为labelme格式的json文件

with open('template.json', 'r', encoding='utf-8') as f:
    template = json.load(f)
template['shapes'] = []

for f_n, d in label_data.items():
    f_n = f_n.replace('http://192.168.122.111:8099/images/bo_pian', '.')
    # f_w = open(f_n.replace('.jpg', '.txt'), 'w', encoding='utf-8')
    json_w = f_n.replace('.jpg', '.json')
    f_name = os.path.basename(f_n)
    template['imagePath'] = f_name
    img = cv2.imread(f_n)
    # 获取图片的宽高
    height, width, _ = img.shape
    template['imageHeight'] = height
    template['imageWidth'] = width
    shapes = []
    for coords, av in d:
        shape = {
            "label": str(av),
            "text": "",
            "points": np.array(coords).reshape(-1, 2).tolist(),
            "group_id": None,
            "shape_type": "polygon",
            "flags": {}
        }
        shapes.append(shape)
    template['shapes'] = shapes
    # 将json数据写入文件
    with open(json_w, 'w', encoding='utf-8') as f:
        json.dump(template, f, ensure_ascii=False, indent=4)
        

    
相关推荐
wno7044 分钟前
Spring Security权限控制
java·python·spring
ShineWinsu37 分钟前
对于Coze—AI:SDK的解析
人工智能·python·ai·sdk·项目·coze·字节跳动
右耳朵猫AI1 小时前
Python周刊2026W38 | 标准流编码修复、PEP 845/846 草案、解析器提速 10%、集合字典二次复杂度
python·ai·数据科学
微软技术分享1 小时前
大模型网络结构:模型接口测试用例参考
python
Bruce_Liuxiaowei2 小时前
Python 实例赋值遮蔽类属性:一个从不报错的静默陷阱
开发语言·python·语法糖
做运维的阿瑞2 小时前
Python 标准库汇总:分类速览与常用模块清单
linux·运维·python
why-geo2 小时前
Hermes Agent 与 Python 的会产生什么样的碰撞
人工智能·python·ai编程
正经教主3 小时前
【FDE系列】阶段2:Day 29:FastAPI 进阶 — Pydantic 模型与完整 CRUD 实战
人工智能·python·fde
gCode Teacher 格码致知3 小时前
Pandas教学-6:coerce详解
python·pandas
梦在远山后3 小时前
Electron 与 FastAPI 如何完成流式 Agent 对话
python·langchain·agent