YOLOV5——将 json 转化为 YOLO 需要的数据格式

YOLO需要的标注数据是每个图片一个 txt 文件

json 标注数据文件内容包含:

name:图片文件名

category:类别id

bbox:目标框信息xyrb格式,分别指[左上角x坐标,左上角y坐标,右下角x坐标,右下角y坐标]

score:预测的分数

如下格式

[
    {
        "name": "235_2_t20201127123021723_CAM2.jpg",
        "image_height": 6000,
        "image_width": 8192,
        "category": 5,
        "bbox": [
            1876.06,
            998.04,
            1883.06,
            1004.04
        ]
    },
    {
        "name": "235_2_t20201127123021723_CAM2.jpg",
        "image_height": 6000,
        "image_width": 8192,
        "category": 5,
        "bbox": [
            1655.06,
            1094.04,
            1663.06,
            1102.04
        ]
    },
    {
        "name": "235_2_t20201127123021723_CAM2.jpg",
        "image_height": 6000,
        "image_width": 8192,
        "category": 5,
        "bbox": [
            1909.06,
            1379.04,
            1920.06,
            1388.04
        ]
    }
]

 

我们需要将 json 中的数据转化为每个文件一个 txt 文件

其中 yolo 需要的标注数据格式:

235_2_t20201127123021723_CAM2.txt 文件 

数据格式:类别id  中心点x坐标   中心点y坐标  w    h(相对于图片宽高)

5 0.229438 0.16684 0.000854 0.001
5 0.202522 0.183007 0.000977 0.001333

json2txt 代码

import os
import json


json_dir = train_annos.json  # json文件路径
out_dir = output/  # 输出的 txt 文件路径


def main():
    # 读取 json 文件数据
    with open(json_dir, r) as load_f:
        content = json.load(load_f)
    # 循环处理
    for t in content:
        tmp = t[name].split(.)
        filename = out_dir + tmp[0] + .txt

        if os.path.exists(filename):
            # 计算 yolo 数据格式所需要的中心点的 相对 x, y 坐标, w,h 的值
            x = (t[bbox][0] + t[bbox][2]) / 2 / t[image_width]
            y = (t[bbox][1] + t[bbox][3]) / 2 / t[image_height]
            w = (t[bbox][2] - t[bbox][0]) / t[image_width]
            h = (t[bbox][3] - t[bbox][1]) / t[image_height]
            fp = open(filename, mode="r+", encoding="utf-8")
            file_str = str(t[category]) +   + str(round(x, 6)) +   + str(round(y, 6)) +   + str(round(w, 6)) +                          + str(round(h, 6))
            line_data = fp.readlines()

            if len(line_data) != 0:
                fp.write(\n + file_str)
            else:
                fp.write(file_str)
            fp.close()

        # 不存在则创建文件
        else:
            fp = open(filename, mode="w", encoding="utf-8")
            fp.close()


if __name__ == __main__:
    main()

 

YOLOV5——将 json 转化为 YOLO 需要的数据格式

上一篇:Driver.js的使用


下一篇:数据挖掘实践(9):基础理论(九)数学基础(九)概率(五)⻉叶斯公式