Python合并同名Labelme标注文件内容
- 前言
- 前提条件
- 相关介绍
- 实验环境
- Python合并同名Labelme标注文件内容
- Json文件
- 代码实现
- 输出结果
- json文件
前言
- 本文是个人使用Python处理文件的电子笔记,由于水平有限,难免出现错漏,敬请批评改正。
(https://blog.csdn.net/friendshiptang/category_11653584.html)专栏或我的个人主页查看- YOLOv5:添加SE、CBAM、CoordAtt、ECA注意力机制
- YOLOv5:yolov5s.yaml配置文件解读、增加小目标检测层
- YOLOv5:IoU、GIoU、DIoU、CIoU、EIoU
- YOLOv7训练自己的数据集(口罩检测)
- YOLOv8训练自己的数据集(足球检测)
前提条件
- 熟悉Python
相关介绍
- 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
}
- 更多精彩内容,可点击进入Python日常小操作专栏或我的个人主页查看
- YOLOv5:添加SE、CBAM、CoordAtt、ECA注意力机制
- YOLOv5:yolov5s.yaml配置文件解读、增加小目标检测层
- YOLOv5:IoU、GIoU、DIoU、CIoU、EIoU
- YOLOv7训练自己的数据集(口罩检测)
- YOLOv8训练自己的数据集(足球检测)