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)
        

    
相关推荐
SL-staff1 分钟前
技术实践:HR如何用JVS-Logic可视化编排实现考勤数据同步(含节点配置与异常处理)
开发语言·python·低代码·钉钉·可视化编排·jvs-logic·hr技术
zzzzzz3102 小时前
AI 助手最危险的不是报错,而是“看起来成功”:我给工作流加了四道保险
人工智能·python
用户8356290780512 小时前
如何使用 Python 将 PowerPoint 转换为 PDF 文档
后端·python
菜冻鱼2 小时前
Python-pytorch-高级技巧
开发语言·人工智能·pytorch·python·深度学习·神经网络·聚类
lpfasd1232 小时前
python webview打包版卡死、开发版正常
开发语言·python
用户8356290780512 小时前
如何使用 Python 为 PowerPoint 幻灯片添加切换效果
后端·python
菜冻鱼2 小时前
Python-pytorch-模型保存与加载
开发语言·人工智能·pytorch·python·深度学习·机器学习
亿牛云爬虫专家2 小时前
爬虫调度系统设计:用 Celery 实现按媒体权重动态调整的抓取频率控制
爬虫·python·隧道代理·舆情采集·媒体权重·频率控制·ip自动轮换
李可以量化3 小时前
Redis 从了解到精通(二)下:发布订阅进阶命令、量化场景落地与避坑指南
数据库·redis·python·qmt·ptrade
Code额3 小时前
Python 连接 DeepSeek API,OpenAI
开发语言·python·ai·ai编程