Python将Labelme的Json标注文件进行增、删、改、查

在这里插入图片描述

前言

前提条件

相关介绍

  • Python是一种跨平台的计算机程序设计语言。是一个高层次的结合了解释性、编译性、互动性和面向对象的脚本语言。最初被设计用于编写自动化脚本(shell),随着版本的不断更新和语言新功能的添加,越多被用于独立的、大型项目的开发。
  • PyTorch 是一个深度学习框架,封装好了很多网络和深度学习相关的工具方便我们调用,而不用我们一个个去单独写了。它分为 CPU 和 GPU 版本,其他框架还有 TensorFlow、Caffe 等。PyTorch 是由 Facebook 人工智能研究院(FAIR)基于 Torch 推出的,它是一个基于 Python 的可续计算包,提供两个高级功能:1、具有强大的 GPU 加速的张量计算(如 NumPy);2、构建深度神经网络时的自动微分机制。
  • YOLOv5是一种单阶段目标检测算法,该算法在YOLOv4的基础上添加了一些新的改进思路,使其速度与精度都得到了极大的性能提升。它是一个在COCO数据集上预训练的物体检测架构和模型系列,代表了Ultralytics对未来视觉AI方法的开源研究,其中包含了经过数千小时的研究和开发而形成的经验教训和最佳实践。
  • Labelme是一款图像标注工具,由麻省理工(MIT)的计算机科学和人工智能实验室(CSAIL)研发。它是用Python和PyQT编写的,开源且免费。Labelme支持Windows、Linux和Mac等操作系统。
  • 这款工具提供了直观的图形界面,允许用户在图像上标注多种类型的目标,例如矩形框、多边形、线条等,甚至包括更复杂的形状。标注结果以JSON格式保存,便于后续处理和分析。这些标注信息可以用于目标检测、图像分割、图像分类等任务。
  • 总的来说,Labelme是一款强大且易用的图像标注工具,可以满足不同的图像处理需求。
  • Labelme标注json文件是一种用于存储标注信息的文件格式,它包含了以下几个主要的字段:
    • version: Labelme的版本号,例如"4.5.6"。
    • flags: 一些全局的标志,例如是否是分割任务,是否有多边形,等等。
    • shapes: 一个列表,每个元素是一个字典,表示一个标注对象。每个字典包含了以下几个字段:
      • label: 标注对象的类别名称,例如"dog"。
      • points: 一个列表,每个元素是一个坐标对,表示标注对象的边界点,例如[[10, 20], [30, 40]]。
      • group_id: 标注对象的分组编号,用于表示属于同一组的对象,例如1。
      • shape_type: 标注对象的形状类型,例如"polygon",“rectangle”,“circle”,等等。
      • flags: 一些针对该标注对象的标志,例如是否是难例,是否被遮挡,等等。
    • lineColor: 标注对象的边界线颜色,例如[0, 255, 0, 128]。
    • fillColor: 标注对象的填充颜色,例如[255, 0, 0, 128]。
    • imagePath: 图像文件的相对路径,例如"img_001.jpg"。
    • imageData: 图像文件的二进制数据,经过base64编码后的字符串,例如"iVBORw0KGgoAAAANSUhEUgAA…"。
    • imageHeight: 图像的高度,例如600。
    • imageWidth: 图像的宽度,例如800。

以下是一个Labelme标注json文件的示例:

{
  "version": "4.5.6",
  "flags": {},
  "shapes": [
    {
      "label": "dog",
      "points": [
        [
          121.0,
          233.0
        ],
        [
          223.0,
          232.0
        ],
        [
          246.0,
          334.0
        ],
        [
          121.0,
          337.0
        ]
      ],
      "group_id": null,
      "shape_type": "polygon",
      "flags": {}
    }
  ],
  "lineColor": [
    0,
    255,
    0,
    128
  ],
  "fillColor": [
    255,
    0,
    0,
    128
  ],
  "imagePath": "img_001.jpg",
  "imageData": "iVBORw0KGgoAAAANSUhEUgAA...",
  "imageHeight": 600,
  "imageWidth": 800
}

实验环境

  • Python 3.x (面向对象的高级语言)

Json标注文件的增、删、改、查

  • 背景:将标注好的Labelme的Json文件进行增、删、改、查。
  • 目录结构示例
    在这里插入图片描述
  • jsons:原始Labelme标注文件所在的文件夹。
  • output_jsons:修改后的Labelme标注文件所在的文件夹。

{
    "version": "4.5.7",
    "flags": {},
    "shapes": [
        {
            "label": "22",
            "score": 0.99,
            "group_id": null,
            "shape_type": "rectangle",
            "flags": {},
            "points": [
                [
                    0.9600000000000009,
                    20.06000000000001
                ],
                [
                    442.19008,
                    399.21015
                ]
            ]
        }
    ],
    "imagePath": "000000000034.jpg",
    "imageData": null,
    "imageHeight": 425,
    "imageWidth": 640
}

代码实现

def add_info_in_json(in_json_path,out_json_path):
    '''
    添加信息到json文件
    '''
    shapes_dict = {
        'label': '0', 
        'points': [[20,50],[30,70]], # [[x1,y1],[x2,y2]]
        'group_id': None, 
        'shape_type': 'rectangle', 
        'flags': {}
        }

    with open(in_json_path,'r') as f:
        json_data = json.load(f)
    # print(json_data)

    json_data['shapes'].append(shapes_dict)

    # 创建一个写文件
    with open(out_json_path, "w", encoding='utf-8') as f:
        # 将修改后的数据写入文件
        f.write(json.dumps(json_data))

输出结果

{
    "version": "4.5.7",
    "flags": {},
    "shapes": [
        {
            "label": "22",
            "score": 0.99,
            "group_id": null,
            "shape_type": "rectangle",
            "flags": {},
            "points": [
                [
                    0.9600000000000009,
                    20.06000000000001
                ],
                [
                    442.19008,
                    399.21015
                ]
            ]
        },
        {
            "label": "0",
            "points": [
                [
                    20,
                    50
                ],
                [
                    30,
                    70
                ]
            ],
            "group_id": null,
            "shape_type": "rectangle",
            "flags": {}
        }
    ],
    "imagePath": "000000000034.jpg",
    "imageData": null,
    "imageHeight": 425,
    "imageWidth": 640
}

{
    "version": "4.5.7",
    "flags": {},
    "shapes": [
        {
            "label": "45",
            "score": 0.99,
            "group_id": null,
            "shape_type": "rectangle",
            "flags": {},
            "points": [
                [
                    1.0799999999999699,
                    187.69008
                ],
                [
                    612.66976,
                    473.53008
                ]
            ]
        },
        {
            "label": "45",
            "score": 0.99,
            "group_id": null,
            "shape_type": "rectangle",
            "flags": {},
            "points": [
                [
                    311.73024,
                    4.310160000000001
                ],
                [
                    631.01024,
                    232.99032
                ]
            ]
        },
        {
            "label": "50",
            "score": 0.99,
            "group_id": null,
            "shape_type": "rectangle",
            "flags": {},
            "points": [
                [
                    249.60032000000007,
                    229.27032
                ],
                [
                    565.84032,
                    474.35016
                ]
            ]
        },
        {
            "label": "45",
            "score": 0.99,
            "group_id": null,
            "shape_type": "rectangle",
            "flags": {},
            "points": [
                [
                    0.0003200000000092018,
                    13.510080000000002
                ],
                [
                    434.48032,
                    388.63007999999996
                ]
            ]
        },
        {
            "label": "49",
            "score": 0.99,
            "group_id": null,
            "shape_type": "rectangle",
            "flags": {},
            "points": [
                [
                    376.2,
                    40.35996
                ],
                [
                    451.75007999999997,
                    86.88996
                ]
            ]
        },
        {
            "label": "49",
            "score": 0.99,
            "group_id": null,
            "shape_type": "rectangle",
            "flags": {},
            "points": [
                [
                    465.7797119999999,
                    38.969952
                ],
                [
                    523.849728,
                    85.63996800000001
                ]
            ]
        },
        {
            "label": "49",
            "score": 0.99,
            "group_id": null,
            "shape_type": "rectangle",
            "flags": {},
            "points": [
                [
                    385.70016000000004,
                    73.65983999999999
                ],
                [
                    469.72,
                    144.16992
                ]
            ]
        },
        {
            "label": "49",
            "score": 0.99,
            "group_id": null,
            "shape_type": "rectangle",
            "flags": {},
            "points": [
                [
                    364.04959999999994,
                    2.4900960000000016
                ],
                [
                    458.80992,
                    73.559856
                ]
            ]
        }
    ],
    "imagePath": "000000000009.jpg",
    "imageData": null,
    "imageHeight": 480,
    "imageWidth": 640
}

代码实现

def del_info_in_json(in_json_path,out_json_path):
    '''
    删除json文件的信息
    '''
    with open(in_json_path,'r') as f:
        json_data = json.load(f)
    # print(json_data)

    # 以删除指定label为例,比如删除掉'label' == "49"的标注信息
    json_data_shape =  copy.deepcopy(json_data['shapes'])
    for i in json_data_shape:
        if i['label'] == "49":
            json_data['shapes'].remove(i)
            
    # 创建一个写文件
    with open(out_json_path, "w", encoding='utf-8') as f:
        # 将修改后的数据写入文件
        f.write(json.dumps(json_data))

输出结果

{
    "version": "4.5.7",
    "flags": {},
    "shapes": [
        {
            "label": "45",
            "score": 0.99,
            "group_id": null,
            "shape_type": "rectangle",
            "flags": {},
            "points": [
                [
                    1.0799999999999699,
                    187.69008
                ],
                [
                    612.66976,
                    473.53008
                ]
            ]
        },
        {
            "label": "45",
            "score": 0.99,
            "group_id": null,
            "shape_type": "rectangle",
            "flags": {},
            "points": [
                [
                    311.73024,
                    4.310160000000001
                ],
                [
                    631.01024,
                    232.99032
                ]
            ]
        },
        {
            "label": "50",
            "score": 0.99,
            "group_id": null,
            "shape_type": "rectangle",
            "flags": {},
            "points": [
                [
                    249.60032000000007,
                    229.27032
                ],
                [
                    565.84032,
                    474.35016
                ]
            ]
        },
        {
            "label": "45",
            "score": 0.99,
            "group_id": null,
            "shape_type": "rectangle",
            "flags": {},
            "points": [
                [
                    0.0003200000000092018,
                    13.510080000000002
                ],
                [
                    434.48032,
                    388.63007999999996
                ]
            ]
        }
    ],
    "imagePath": "000000000009.jpg",
    "imageData": null,
    "imageHeight": 480,
    "imageWidth": 640
}

{
    "version": "4.5.7",
    "flags": {},
    "shapes": [
        {
            "label": "45",
            "score": 0.99,
            "group_id": null,
            "shape_type": "rectangle",
            "flags": {},
            "points": [
                [
                    1.0799999999999699,
                    187.69008
                ],
                [
                    612.66976,
                    473.53008
                ]
            ]
        },
        {
            "label": "45",
            "score": 0.99,
            "group_id": null,
            "shape_type": "rectangle",
            "flags": {},
            "points": [
                [
                    311.73024,
                    4.310160000000001
                ],
                [
                    631.01024,
                    232.99032
                ]
            ]
        },
        {
            "label": "50",
            "score": 0.99,
            "group_id": null,
            "shape_type": "rectangle",
            "flags": {},
            "points": [
                [
                    249.60032000000007,
                    229.27032
                ],
                [
                    565.84032,
                    474.35016
                ]
            ]
        },
        {
            "label": "45",
            "score": 0.99,
            "group_id": null,
            "shape_type": "rectangle",
            "flags": {},
            "points": [
                [
                    0.0003200000000092018,
                    13.510080000000002
                ],
                [
                    434.48032,
                    388.63007999999996
                ]
            ]
        },
        {
            "label": "49",
            "score": 0.99,
            "group_id": null,
            "shape_type": "rectangle",
            "flags": {},
            "points": [
                [
                    376.2,
                    40.35996
                ],
                [
                    451.75007999999997,
                    86.88996
                ]
            ]
        },
        {
            "label": "49",
            "score": 0.99,
            "group_id": null,
            "shape_type": "rectangle",
            "flags": {},
            "points": [
                [
                    465.7797119999999,
                    38.969952
                ],
                [
                    523.849728,
                    85.63996800000001
                ]
            ]
        },
        {
            "label": "49",
            "score": 0.99,
            "group_id": null,
            "shape_type": "rectangle",
            "flags": {},
            "points": [
                [
                    385.70016000000004,
                    73.65983999999999
                ],
                [
                    469.72,
                    144.16992
                ]
            ]
        },
        {
            "label": "49",
            "score": 0.99,
            "group_id": null,
            "shape_type": "rectangle",
            "flags": {},
            "points": [
                [
                    364.04959999999994,
                    2.4900960000000016
                ],
                [
                    458.80992,
                    73.559856
                ]
            ]
        }
    ],
    "imagePath": "000000000009.jpg",
    "imageData": null,
    "imageHeight": 480,
    "imageWidth": 640
}

代码实现

def alter_info_in_json(in_json_path,out_json_path):
    '''
    修改json文件的信息
    '''
    with open(in_json_path,'r') as f:
        json_data = json.load(f)
    # print(json_data)

    # 以修改label为例,比如将'label' == "49"改为 'label' == "orange"
    json_data_shape =  copy.deepcopy(json_data['shapes'])
    for i in json_data_shape:
        if i['label'] == "49":
            i['label'] = "orange"
    json_data['shapes'] = json_data_shape
            
    # 创建一个写文件
    with open(out_json_path, "w", encoding='utf-8') as f:
        # 将修改后的数据写入文件
        f.write(json.dumps(json_data))

输出结果

{
    "version": "4.5.7",
    "flags": {},
    "shapes": [
        {
            "label": "45",
            "score": 0.99,
            "group_id": null,
            "shape_type": "rectangle",
            "flags": {},
            "points": [
                [
                    1.0799999999999699,
                    187.69008
                ],
                [
                    612.66976,
                    473.53008
                ]
            ]
        },
        {
            "label": "45",
            "score": 0.99,
            "group_id": null,
            "shape_type": "rectangle",
            "flags": {},
            "points": [
                [
                    311.73024,
                    4.310160000000001
                ],
                [
                    631.01024,
                    232.99032
                ]
            ]
        },
        {
            "label": "50",
            "score": 0.99,
            "group_id": null,
            "shape_type": "rectangle",
            "flags": {},
            "points": [
                [
                    249.60032000000007,
                    229.27032
                ],
                [
                    565.84032,
                    474.35016
                ]
            ]
        },
        {
            "label": "45",
            "score": 0.99,
            "group_id": null,
            "shape_type": "rectangle",
            "flags": {},
            "points": [
                [
                    0.0003200000000092018,
                    13.510080000000002
                ],
                [
                    434.48032,
                    388.63007999999996
                ]
            ]
        },
        {
            "label": "orange",
            "score": 0.99,
            "group_id": null,
            "shape_type": "rectangle",
            "flags": {},
            "points": [
                [
                    376.2,
                    40.35996
                ],
                [
                    451.75007999999997,
                    86.88996
                ]
            ]
        },
        {
            "label": "orange",
            "score": 0.99,
            "group_id": null,
            "shape_type": "rectangle",
            "flags": {},
            "points": [
                [
                    465.7797119999999,
                    38.969952
                ],
                [
                    523.849728,
                    85.63996800000001
                ]
            ]
        },
        {
            "label": "orange",
            "score": 0.99,
            "group_id": null,
            "shape_type": "rectangle",
            "flags": {},
            "points": [
                [
                    385.70016000000004,
                    73.65983999999999
                ],
                [
                    469.72,
                    144.16992
                ]
            ]
        },
        {
            "label": "orange",
            "score": 0.99,
            "group_id": null,
            "shape_type": "rectangle",
            "flags": {},
            "points": [
                [
                    364.04959999999994,
                    2.4900960000000016
                ],
                [
                    458.80992,
                    73.559856
                ]
            ]
        }
    ],
    "imagePath": "000000000009.jpg",
    "imageData": null,
    "imageHeight": 480,
    "imageWidth": 640
}

{
    "version": "4.5.7",
    "flags": {},
    "shapes": [
        {
            "label": "45",
            "score": 0.99,
            "group_id": null,
            "shape_type": "rectangle",
            "flags": {},
            "points": [
                [
                    1.0799999999999699,
                    187.69008
                ],
                [
                    612.66976,
                    473.53008
                ]
            ]
        },
        {
            "label": "45",
            "score": 0.99,
            "group_id": null,
            "shape_type": "rectangle",
            "flags": {},
            "points": [
                [
                    311.73024,
                    4.310160000000001
                ],
                [
                    631.01024,
                    232.99032
                ]
            ]
        },
        {
            "label": "50",
            "score": 0.99,
            "group_id": null,
            "shape_type": "rectangle",
            "flags": {},
            "points": [
                [
                    249.60032000000007,
                    229.27032
                ],
                [
                    565.84032,
                    474.35016
                ]
            ]
        },
        {
            "label": "45",
            "score": 0.99,
            "group_id": null,
            "shape_type": "rectangle",
            "flags": {},
            "points": [
                [
                    0.0003200000000092018,
                    13.510080000000002
                ],
                [
                    434.48032,
                    388.63007999999996
                ]
            ]
        },
        {
            "label": "49",
            "score": 0.99,
            "group_id": null,
            "shape_type": "rectangle",
            "flags": {},
            "points": [
                [
                    376.2,
                    40.35996
                ],
                [
                    451.75007999999997,
                    86.88996
                ]
            ]
        },
        {
            "label": "49",
            "score": 0.99,
            "group_id": null,
            "shape_type": "rectangle",
            "flags": {},
            "points": [
                [
                    465.7797119999999,
                    38.969952
                ],
                [
                    523.849728,
                    85.63996800000001
                ]
            ]
        },
        {
            "label": "49",
            "score": 0.99,
            "group_id": null,
            "shape_type": "rectangle",
            "flags": {},
            "points": [
                [
                    385.70016000000004,
                    73.65983999999999
                ],
                [
                    469.72,
                    144.16992
                ]
            ]
        },
        {
            "label": "49",
            "score": 0.99,
            "group_id": null,
            "shape_type": "rectangle",
            "flags": {},
            "points": [
                [
                    364.04959999999994,
                    2.4900960000000016
                ],
                [
                    458.80992,
                    73.559856
                ]
            ]
        }
    ],
    "imagePath": "000000000009.jpg",
    "imageData": null,
    "imageHeight": 480,
    "imageWidth": 640
}

代码实现

def query_info_in_json(in_json_path,out_json_path):
    '''
    查询json文件的信息
    '''
    with open(in_json_path,'r') as f:
        json_data = json.load(f)
    # print(json_data)

    # 以查询label信息为例,比如输出'label' == "49"的标注信息
    json_data_shape =  copy.deepcopy(json_data['shapes'])
    for i in json_data_shape:
        if i['label'] == "49":
            print(i)

输出结果

在这里插入图片描述

  • 17
    点赞
  • 33
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

FriendshipT

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值