python读取标注文件中的数据信息 json文件

标注后的数据信息文档一般有两种格式:xml和json
读取相关json的数据的代码整理了一下发出来,方便大家使用

#json文件
{
  "version": "4.5.7",
  "flags": {},
  "shapes": [
    {
      "label": "fastener",
      "points": [
        [
          444.44444444444446,
          173.91304347826087
        ],
        [
          509.1787439613527,
          224.6376811594203
        ],
        [
          439.13043478260875,
          313.5265700483092
        ],
        [
          372.46376811594206,
          262.3188405797102
        ]
      ],
      "group_id": null,
      "shape_type": "polygon",
      "flags": {}
    }
  ],
  "imagePath": "..\\1.jpg",
  "imageData": null,
  "imageHeight": 1080,
  "imageWidth": 1920
}

读取上述json文件的代码

 #json文件的函数 label_path:文件名称
def get_objects_string_from_label_file(label_path, quad_format):
    with open(label_path, 'r') as fp:
        data = json.load(fp)
        objects = []
        for shape in data['shapes']:
            pts = np.round(np.array(shape['points'])).astype(int)
            pts = np.reshape(pts, -1)
            cls_name = shape['label']
            objects.append([*pts,
                            cfg.Customer_DATA['CLASSES'].index(cls_name)])

    line = ''
    if quad_format:
        for obj in objects:
            info_str = ','.join(list(map(str, obj)))
            line += ' ' + info_str
    else:
        for obj in objects:
            coors = np.reshape(obj[:-1], (-1, 2))
            left, top = np.min(coors, 0)
            right, bottom = np.max(coors, 0)
            info_str = ','.join(list(map(str, [left, top, right, bottom, obj[-1]])))
            line += ' ' + info_str

    return line

返回的line是字符串格式,要保存在一个txt文档中,后面依次读取N张图像,所有的读取到的相关数据都存在这一个txt中

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值