zoukankan      html  css  js  c++  java
  • YOLOV5——将 json 格式的标注数据转化为 YOLO 需要的 txt 格式

    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('
    ' + 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格式,原代码只输出标志文件

    https://www.cnblogs.com/yxyun/p/14250455.html

  • 相关阅读:
    Activity中Intent的知识
    详解 Android 的 Activity 组件
    Android xml资源文件中@的含义
    为App签名的其他方法
    如何将Android程序打包成apk文件
    创建Android虚拟设备(AVD)
    Android开发环境搭建
    论适应和麻木
    一些有趣的图片
    python 写一个scheme 解释器 (二)——简单求值器内核
  • 原文地址:https://www.cnblogs.com/zhaoyingjie/p/14519764.html
Copyright © 2011-2022 走看看