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)
        

    
相关推荐
自可乐12 小时前
LangGraph从入门到精通:构建智能Agent的完整指南
人工智能·python·机器学习
m0_5613596712 小时前
使用Docker容器化你的Python应用
jvm·数据库·python
逻极12 小时前
Moltbot 快速入门指南(2026年1月最新版)
python·ai·aigc·智能助手·clawdbot·molbot
AAD5558889912 小时前
基于Deformable-DETR的植物叶片病害检测
python
Cemtery11612 小时前
Day40 早停策略和模型权重的保存
人工智能·python·深度学习·机器学习
Jackson@ML12 小时前
[Kimi重磅出击!]用Kimi Code智能高效开发Web应用程序指南
ide·python·kimi code
u01092727112 小时前
使用Scrapy框架构建分布式爬虫
jvm·数据库·python
MaoziShan12 小时前
[ICLR 2026] 一文读懂 AutoGEO:生成式搜索引擎优化(GEO)的自动化解决方案
人工智能·python·搜索引擎·语言模型·自然语言处理·内容运营·生成式搜索引擎
2401_8384725112 小时前
使用Python处理计算机图形学(PIL/Pillow)
jvm·数据库·python