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)
        

    
相关推荐
Ivanqhz17 分钟前
MoE + LLM 应用
数据库·人工智能·pytorch·python·深度学习·算法·1024程序员节
Mr. zhihao19 分钟前
Deep Agent 架构深度解析:主Agent与子Agent完整交互机制与核心参数作用
python·ai·langgraph·deepagent
狗凯之家源码网26 分钟前
iOS C2 V2 与 Python FastAPI 全链路远控系统深度评测
python·ios·fastapi
超级架构师1 小时前
让 Fortran 模型自己调用 Agent:原生 Fortran 2008 实现深度剖析
人工智能·python·架构·机器人·开源·fortran
小蒜学长1 小时前
基于Django+Vue喵汪联盟宠物领养系统的设计与实现(代码+数据库+LW)
后端·python·django·宠物领养系统·流程规范化
云边有个稻草人1 小时前
Prometheus Pushgateway教程:离线任务指标上报、Shell与Python示例、远程访问
开发语言·python·prometheus
fanruitian1 小时前
pycharm 使用anaconda
ide·python·pycharm
Dwzun1 小时前
基于SpringBoot3+Vue3的油莎豆种植管理系统
java·前端·vue.js·spring boot·后端·python·计算机网络
代码山河10 小时前
Python就业方向全解析:后端、爬虫、数据分析、AI你选哪个?
python·学习·架构·教程·面向对象·项目
在世修行10 小时前
智学文枢:题目流水线
python·多线程·状态机·pyside