Python合并同名Labelme标注文件内容

前言

前提条件

相关介绍

  • Python是一种跨平台的计算机程序设计语言。是一个高层次的结合了解释性、编译性、互动性和面向对象的脚本语言。最初被设计用于编写自动化脚本(shell),随着版本的不断更新和语言新功能的添加,越多被用于独立的、大型项目的开发。
  • Python OS模块是负责程序与操作系统的交互,提供了访问操作系统底层的接口和非常丰富的方法用来处理文件和目录。

实验环境

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

Python合并同名Labelme标注文件内容

在这里插入图片描述
在这里插入图片描述

Json文件

{
  "version": "5.2.0.post4",
  "flags": {},
  "shapes": [
    {
      "label": "cat",
      "points": [
        [
          10.799999999999997,
          112.30000000000001
        ],
        [
          261.2,
          258.7
        ]
      ],
      "group_id": null,
      "description": "",
      "shape_type": "rectangle",
      "flags": {}
    }
  ],
  "imagePath": "000019.jpg",
  "imageData": null,
  "imageHeight": 375,
  "imageWidth": 500
}
{
  "version": "5.2.0.post4",
  "flags": {},
  "shapes": [
    {
      "label": "cat",
      "points": [
        [
          231.2,
          87.10000000000001
        ],
        [
          484.8,
          253.9
        ]
      ],
      "group_id": null,
      "description": "",
      "shape_type": "rectangle",
      "flags": {}
    }
  ],
  "imagePath": "000019.jpg",
  "imageData": null,
  "imageHeight": 375,
  "imageWidth": 500
}

代码实现

import os
import json


def read_json(in_json_path,json_dict):
    with open(in_json_path,'r',encoding='utf-8') as f:
        json_data = json.load(f)
    print(json_data['shapes']) 


    json_dict["imagePath"] = json_data["imagePath"]
    # json_dict["imageData"] = str(base64.b64encode(open(img_path, "rb").read()))
    # # delete 'b and '
    # json_dict["imageData"] = json_dict["imageData"][2:-1]
    json_dict["imageData"] = None
    json_dict["imageHeight"] = json_data["imageHeight"]
    json_dict["imageWidth"] = json_data["imageWidth"]
    json_dict["shapes"].extend(json_data["shapes"])


if __name__ =="__main__":
    output_dir = 'new_jsons'

    if not os.path.exists(output_dir):
        os.mkdir(output_dir)
    
    json_dict = {
                    "version": "4.5.6",
                    "flags": {},
                    "shapes": [],
                }
    
    merge_dir_list = ['jsons1','jsons2']
    file_name_list = os.listdir(merge_dir_list[0])
    json_name_list = [i for i in file_name_list if i.endswith('.json')]
    print(json_name_list)
    
    # 遍历要合并的文件
    for json_name in json_name_list:
        for in_dir_name in merge_dir_list:
            in_json_path = os.path.join(in_dir_name,json_name)
            # print(in_json_path)
            read_json(in_json_path,json_dict)

    # 合并、写入新json文件
    output_json_path = os.path.join(output_dir,json_dict["imagePath"].split('.')[0]+'.json')
    # print(output_json_path)
    with open(output_json_path, 'w') as f:
        f.write(json.dumps(json_dict))

输出结果

在这里插入图片描述

json文件

{
    "version": "4.5.6",
    "flags": {},
    "shapes": [
        {
            "label": "cat",
            "points": [
                [
                    10.799999999999997,
                    112.30000000000001
                ],
                [
                    261.2,
                    258.7
                ]
            ],
            "group_id": null,
            "description": "",
            "shape_type": "rectangle",
            "flags": {}
        },
        {
            "label": "cat",
            "points": [
                [
                    231.2,
                    87.10000000000001
                ],
                [
                    484.8,
                    253.9
                ]
            ],
            "group_id": null,
            "description": "",
            "shape_type": "rectangle",
            "flags": {}
        }
    ],
    "imagePath": "000019.jpg",
    "imageData": null,
    "imageHeight": 375,
    "imageWidth": 500
}
  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

FriendshipT

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

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

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

打赏作者

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

抵扣说明:

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

余额充值