深度学习之目标检测从入门到精通——json转yolo格式

发布于:2024-04-27 ⋅ 阅读:(36) ⋅ 点赞:(0)

记录点:

import json
import os

name2id = {'person':0,'helmet':1,'Fire extinguisher':2,'Hook':3,'Gas cylinder':4}
               
def convert(img_size, box):
    dw = 1./(img_size[0])
    dh = 1./(img_size[1])
    x = (box[0] + box[2])/2.0 - 1
    y = (box[1] + box[3])/2.0 - 1
    w = box[2] - box[0]
    h = box[3] - box[1]
    x = x*dw
    w = w*dw
    y = y*dh
    h = h*dh
    return (x,y,w,h)
 
 
def decode_json(json_floder_path,json_name):
 
    txt_name = 'E:\\eclipse-workspace\\PyTorch\\PyTorch-YOLOv3\\data\\custom\\labels\\' + json_name[0:-5] + '.txt'
    txt_file = open(txt_name, 'w')
 
    json_path = os.path.join(json_floder_path, json_name)
    data = json.load(open(json_path, 'r', encoding='gb2312'))
 
    img_w = data['imageWidth']
    img_h = data['imageHeight']
 
    for i in data['shapes']:
        
        label_name = i['label']
        if (i['shape_type'] == 'rectangle'):
 
            x1 = int(i['points'][0][0])
            y1 = int(i['points'][0][1])
            x2 = int(i['points'][1][0])
            y2 = int(i['points'][1][1])
 
            bb = (x1,y1,x2,y2)
            bbox = convert((img_w,img_h),bb)
            txt_file.write(str(name2id[label_name]) + " " + " ".join([str(a) for a in bbox]) + '\n')
    
if __name__ == "__main__":
    
    json_floder_path = 'G:\\sinopec\\label-data-test\\json'
    json_names = os.listdir(json_floder_path)
    for json_name in json_names:
        decode_json(json_floder_path,json_name)
解读成博客,并附上关键代码

功能解析

JSON格式的标记文件中提取对象检测数据,并将其转换为适合训练YOLO模型的格式。主要功能包括读取标记信息,转换坐标系统,并生成对应的文本文件保存处理后的数据。以下是详细的解读和关键代码部分。

导入必要的模块
import json
import os
标签到ID的映射

定义字典name2id,用于将对象的名称映射到一个唯一的ID。

name2id = {'person':0, 'helmet':1, 'Fire extinguisher':2, 'Hook':3, 'Gas cylinder':4}
坐标转换函数

定义了convert函数,将标注的坐标转换为归一化坐标。这个步骤是因为YOLO模型需要归一化的边界框坐标作为输入。

def convert(img_size, box):
    dw = 1./(img_size[0])
    dh = 1./(img_size[1])
    x = (box[0] + box[2])/2.0 - 1
    y = (box[1] + box[3])/2.0 - 1
    w = box[2] - box[0]
    h = box[3] - box[1]
    x = x*dw
    w = w*dw
    y = y*dh
    h = h*dh
    return (x,y,w,h)
JSON解码和文件写入

decode_json函数负责读取JSON文件,解析其中的对象标注数据,并使用convert函数转换坐标。然后,它将转换后的坐标写入新的文本文件中。

def decode_json(json_floder_path, json_name):
    txt_name = 'E:\\...\\' + json_name[0:-5] + '.txt'
    txt_file = open(txt_name, 'w')
    json_path = os.path.join(json_floder_path, json_name)
    data = json.load(open(json_path, 'r', encoding='gb2312'))
    img_w = data['imageWidth']
    img_h = data['imageHeight']
    for i in data['shapes']:
        if i['shape_type'] == 'rectangle':
            x1 = int(i['points'][0][0])
            y1 = int(i['points'][0][1])
            x2 = int(i['points'][1][0])
            y2 = int(i['points'][1][1])
            bb = (x1,y1,x2,y2)
            bbox = convert((img_w, img_h), bb)
            txt_file.write(str(name2id[i['label']]) + " " + " ".join([str(a) for a in bbox]) + '\n')
主函数

给定文件夹中的所有JSON文件,并对每个文件调用decode_json函数处理。

if __name__ == "__main__":
    json_floder_path = 'G:\\sinopec\\label-data-test\\json'
    json_names = os.listdir(json_floder_path)
    for json_name in json_names:
        decode_json(json_floder_path, json_name)

总结

将JSON格式的标记数据中提取信息,转换为YOLO模型训练所需的格式。通过自动化这一过程,可以大大减少准备数据的时间和复杂性,提高机器学习项目的效率。