via标注工具输出的json格式数据,如何转化为yolo格式数据

via标注工具输出的json格式数据,如何转化为yolo格式数据?

基于GPT4和Treabhar的编写:

  • 在转换数据格式时,你需要知道YOLO训练格式的标准。对于YOLO,标注数据通常由一个.txt文件对应一个图像文件,.txt文件中包含了该图像的所有标注信息。每一行代表一个对象,每一行有5个值,分别是:

  • 类别索引(从0开始计数)

  • 对象中心x坐标(相对于图像宽度的比例)

  • 对象中心y坐标(相对于图像高度的比例)

  • 对象的宽度(相对于图像宽度的比例)

  • 对象的高度(相对于图像高度的比例)

所有的值都是归一化的,范围在0到1之间。

Python脚本,它读取由VIA标注工具生成的JSON文件,并将其转换为YOLO格式:

import json
import os
from PIL import Image

def convert(size, box):
    dw = 1. / size[0]
    dh = 1. / size[1]
    x = (box[0] + box[1]) / 2.0
    y = (box[2] + box[3]) / 2.0
    w = box[1] - box[0]
    h = box[3] - box[2]
    x = x * dw
    w = w * dw
    y = y * dh
    h = h * dh
    return [x, y, w, h]

def convert_to_yolo_label(coco_format_json, yolo_format_path):
    with open(coco_format_json, "r") as json_file:
        data = json.load(json_file)
        images = data["_via_img_metadata"]
        for key, value in images.items():
            filename = value["filename"]
            regions = value["regions"]
            for region in regions:
                x = region["shape_attributes"]["x"]
                y = region["shape_attributes"]["y"]
                width = region["shape_attributes"]["width"]
                height = region["shape_attributes"]["height"]
                class_id = region["region_attributes"]["class_id"]
                
                img = Image.open(os.path.join(yolo_format_path, filename))
                w = int(img.size[0])
                h = int(img.size[1])

                bb = convert((w,h), (x,x+width,y,y+height))

                with open(os.path.join(yolo_format_path, filename.split(".")[0] + ".txt"), "a") as yolo_file:
                    yolo_file.write(str(class_id) + " " + " ".join([str(a) for a in bb]) + '\n')

convert_to_yolo_label("via_export_json.json", "/path/to/yolo/data")
  • 这个脚本假设你已经将VIA工具的输出标记为 "class_id" 的类别属性。如果你使用的是不同的属性名,那么需要修改脚本中的相应部分。此外,确保你的图像文件和VIA的JSON文件都位于正确的路径下。

参考GPT和自己的思路:需要先了解一下via标注工具输出的json格式是怎么样的,然后才能确定如何转化为yolo格式数据。一般来说,要将via标注工具的输出转化为yolo格式,需要对json文件中的数据进行解析和重新格式化。其中,json文件中会包含图像的基本信息、每个标注框的坐标及类别等信息,可以通过编写脚本来将这些信息转化为yolo格式所需的格式,如每个标注框的中心坐标、宽度和高度等。具体实现方法,建议参考相关的转换脚本或工具。

引用ChatGPT部分内容作参考:
将via标注工具输出的json格式数据转换为yolo格式数据需要进行以下步骤:

1.读取json文件并解析其中的标注信息。
2.将标注信息转换为yolo格式的标注信息。
3.将转换后的标注信息写入到yolo格式的标注文件中。
下面是一个Python代码示例,可以将via标注工具输出的json格式数据转换为yolo格式数据:

import json

# 读取json文件
with open('annotations.json', 'r') as f:
    data = json.load(f)

# 定义类别列表
classes = ['class1', 'class2', 'class3']

# 定义yolo格式的标注文件名
output_file = 'annotations.txt'

# 遍历每个图像的标注信息
for image in data.values():
    # 获取图像的宽度和高度
    width = image['width']
    height = image['height']

    # 遍历每个标注框的信息
    for annotation in image['annotations']:
        # 获取标注框的类别和坐标
        class_id = annotation['class']
        x, y, w, h = annotation['x'], annotation['y'], annotation['width'], annotation['height']

        # 将坐标转换为yolo格式
        x_center = x + w / 2
        y_center = y + h / 2
        x_center /= width
        y_center /= height
        w /= width
        h /= height

        # 将标注信息写入到yolo格式的标注文件中
        with open(output_file, 'a') as f:
            f.write(f"{classes[class_id]} {x_center} {y_center} {w} {h}\n")

在上面的代码中,我们首先读取了via标注工具输出的json文件,并定义了类别列表和yolo格式的标注文件名。然后,我们遍历了每个图像的标注信息,并将标注框的坐标转换为yolo格式。最后,我们将转换后的标注信息写入到yolo格式的标注文件中。

PHP

<?php
// 读取json文件
$json = file_get_contents('annotations.json');
$data = json_decode($json, true);

// 定义类别列表
$classes = array('class1', 'class2', 'class3');

// 定义yolo格式的标注文件名
$output_file = 'annotations.txt';

// 遍历每个图像的标注信息
foreach ($data as $image) {
    // 获取图像的宽度和高度
    $width = $image['width'];
    $height = $image['height'];

    // 遍历每个标注框的信息
    foreach ($image['annotations'] as $annotation) {
        // 获取标注框的类别和坐标
        $class_id = $annotation['class'];
        $x = $annotation['x'];
        $y = $annotation['y'];
        $w = $annotation['width'];
        $h = $annotation['height'];

        // 将坐标转换为yolo格式
        $x_center = $x + $w / 2;
        $y_center = $y + $h / 2;
        $x_center /= $width;
        $y_center /= $height;
        $w /= $width;
        $h /= $height;

        // 将标注信息写入到yolo格式的标注文件中
        $line = $classes[$class_id] . ' ' . $x_center . ' ' . $y_center . ' ' . $w . ' ' . $h . "\n";
        file_put_contents($output_file, $line, FILE_APPEND);
    }
}

在上面的代码中,我们首先读取了via标注工具输出的json文件,并定义了类别列表和yolo格式的标注文件名。然后,我们遍历了每个图像的标注信息,并将标注框的坐标转换为yolo格式。最后,我们将转换后的标注信息写入到yolo格式的标注文件中。