VIA标注格式转coco格式代码

该代码片段展示了如何读取JSON文件中的图像标注数据,将其转换为COCO(CommonObjectDetectionandSegmentation)格式,用于计算机视觉任务。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

import json
import os
import cv2

json_dir = "/home/annotations" #VIA directory
img_dir = "/home/images" #source image directory
json_files = os.listdir(json_dir)
names = []
for file in json_files:
    name = file.split(".")[0]
    names.append(name)

category = {
    "carpetweed": 0,
    "morningglory": 1,
    "palmer_amaranth": 2
}

c = 0  # image id counter
a = 0  # annotation id counter
images = []
annotations = []

for js, name in zip(json_files, names):
    # print(f"{json_dir}/{js}")

    with open(f"{json_dir}/{js}", 'r') as f:
        data = json.load(f)
        json_obj = f"via_{name}"
        file_name = data[json_obj]["filename"]
        print(file_name)
        im = cv2.imread(f"{img_dir}/{file_name}")
        width = im.shape[1]
        height = im.shape[0]
        dictionary = {
            "id": c,
            "license": 1,
            "file_name": str(file_name),
            "width": width,
            "height": height,
            "date_captured": "null"
        }
        images.append(dictionary)
        for i in range(len(data[json_obj]['regions'])):
            category_id = category.get(data[json_obj]['regions'][i]['region_attributes']["class"])
            x = int(data[json_obj]['regions'][i]['shape_attributes']["x"])
            y = int(data[json_obj]['regions'][i]['shape_attributes']["y"])
            w = int(data[json_obj]['regions'][i]['shape_attributes']["width"])
            h = int(data[json_obj]['regions'][i]['shape_attributes']["height"])
            bbox = [x, y, w, h]
            area = int(w * h)
            an_dict = {
                "id": a,
                "image_id": c,
                "category_id": category_id,
                "bbox": bbox,
                "area": area,
                "segmentation": [],
                "iscrowd": 0}
            annotations.append(an_dict)
            a = a + 1
        c = c + 1


info = {
    "year": "2021",
    "version": "1",
    "description": "Cotton Weed by Dr. Yuzhen Lu",
    "contributor": "",
    "url": "",
    "date_created": ""
}
licenses = [
    {
        "id": 1,
        "url": "",
        "name": ""
    }
]

categories = [
    {
        "id": 0,
        "name": "carpetweed",
        "supercategory": "cotton_weed"
    },
    {
        "id": 1,
        "name": "morningglory",
        "supercategory": "cotton_weed"
    },
    {
        "id": 2,
        "name": "palmer_amaranth",
        "supercategory": "cotton_weed"
    }
]

final_json = {"info": info, "licenses": licenses, "categories": categories, "images": images, "annotations": annotations}

save_dir = "/home/coco" #coco json save directory
with open(f"{save_dir}/_annotations.coco.json", "w") as write_file:
    json.dump(final_json, write_file)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

毕竟是shy哥

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

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

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

打赏作者

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

抵扣说明:

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

余额充值