【问题标题】:How do I convert a label file of a particular format of json to txt label files based on yolo?如何基于yolo将特定格式的json标签文件转换为txt标签文件?
【发布时间】:2021-09-11 07:32:51
【问题描述】:

我收到了 jpg 格式的图像数据集和 JSON 格式的标签,但我在尝试使用暗网 yolov4 进行训练时遇到了问题。

JSON格式的标签如下。

"annotations": [
    {
        "image_id": 0,
        "file_name": "image_47010552850673.jpg",
        "objects": [
            {
                "object_id": 0,
                "class": "person",
                "position": [1480, 151, 1508, 169]
            },
            {
                "object_id": 1,
                "class": "car",
                "position": [792, 123, 843, 246]
            },
            {
                "object_id": 2,
                "class": "person",
                "position": [245, 667, 286, 695]
            }
        ]
    },
    {
        "image_id": 1,
        "file_name": "image_68475401035381.jpg",
        "objects": [
            {
                "object_id": 3,
                "class": "person",
                "position": [1090, 374, 1096, 389]
            },
            {
                "object_id": 4,
                "class": "car",
                "position": [1279, 620, 1346, 655]
            }
        ]
    }, ...

位置的格式如下。

position = [xmin, ymin, xmax, ymax] <- pixel values

总共有六个类,每个标签类对应的编号如下。

汽车:0,卡车:1,公共汽车:2,等等车辆:3,自行车:4,人:5

图片尺寸为 1920x1080

它有大约 100,000 张图像和 36GB 的容量。

所有图像都标记在一个 JSON 文件中,JSON 文件的容量约为 124 MB。

我想把上面的JSON文件转换成标准化yolo格式的文本文件。

例如)

文件名:image_name.txt

内容:

class_number normalized_center_x normalized_center_y normalized_width normalized_height

另外,

normalized_centered_x = (xmin+xmax)÷2÷x_sizeof_image

normalized_centered_y = (ymin+ymax)÷2÷y_sizeof_image

normalized_width = (xmax-xmin)÷x_sizeof_image

normalized_height = (ymax-ymin)÷y_sizeof_image

就我而言,

normalized_centered_x = (位置[0]+位置[2])÷2÷1920

normalized_centered_y = (位置[1]+位置[3])÷2÷1080

normalized_width = (位置[2]-位置[0])÷1920

normalized_height = (位置[3]-位置[1])÷1080

上述json的darknet yolov4 txt文件列表实际示例如下。

文件名:image_47010552850673.txt

内容:

5 0.778125 0.148148148 0.014583333 0.016666667

0 0.42578125 0.170833333 0.0265625 0.113888889

5 0.13828125 0.630555556 0.021354167 0.025925926

文件名:image_68475401035381.txt

内容:

5 0.569270833 0.353240741 0.003125 0.013888889

0 0.68359375 0.590277778 0.034895833 0.032407407

如何在 Python 中做到这一点?

【问题讨论】:

    标签: python json object-detection yolo darknet


    【解决方案1】:

    我发现自己回答了我提出的问题。

    import json
    
    
    classes = ["car", "truck", "bus", "etc vehicle", "bike", "person"]
    
    
    # box form[x,y,w,h]
    def convert(size, box):
        dw = 1. / size[0]
        dh = 1. / size[1]
        x = (box[0] + box[2]) * dw / 2
        y = (box[1] + box[3]) * dh / 2
        w = (box[2] - box[0]) * dw
        h = (box[3] - box[1]) * dh
        return (x, y, w, h)
    
    
    def convert_annotation():
        with open('labels.json', 'r') as f:
            datas = json.load(f)
            data = datas["annotations"]
            width = 1920
            height = 1080
        for item1 in data:
            file_name = item1["file_name"]
            objects = item1["objects"]
            outfile = open('./darknet2/%s.txt' % (file_name[:-4]), 'a+')
            for item2 in objects:
                cls = item2["class"]
                cls_id = classes.index(cls)
                box = item2["position"]
                bb = convert((width, height), box)
                outfile.write(str(cls_id) + " " + " ".join([str(a) for a in bb]) + '\n')
            outfile.close()
    
    
    if __name__ == '__main__':
        convert_annotation()
    

    【讨论】:

      猜你喜欢
      • 2022-06-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-12-11
      • 2021-02-04
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多