当前位置: 首页 > news >正文

批量合并同名Labelme标注文件内容

假如一批数据,分两批分别标注了分割和关键点的json数据,或是分别标注了不同的类别,使用时如果要合并使用,就需要对两个同名的json文件进行合并。

json1:

json2:

合并后json:

脚本内容如下:

import os
import json
import shutildef 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"] = Nonejson_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": "5.1.1","flags": {},"shapes": [],}#  两个json文件存放路径merge_dir_list = ['json-1', 'json-2']   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)output = "G:/test/merge-json/out"# 遍历要合并的文件for json_name in json_name_list:# "shapes"属性初始化,合并一个文件后,shapes属性初始化json_dict["shapes"] = []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))# 文件移动,也可不移动shutil.move('G:/test/merge-json/new_jsons/' + json_name,output)


http://www.mrgr.cn/news/56252.html

相关文章:

  • 来个Oracle一键检查
  • LSL常见应用场景及示例<一>
  • Marin说PCB之GMSL2 通道上的ESD器件的设计注意事项--03
  • aws 把vpc残留删除干净
  • tracert和ping的区别
  • 爬虫实现验证码登录古诗文网【爬虫学习day.02】
  • freeRTOS中互斥锁与信号量使用?
  • vue3学习记录-v-model
  • Numpy基础02
  • 浏览器控制的无线开关
  • 【03】RabbitMQ核心功能扩展
  • LeetCode718:最长重复子数组
  • [DB] NSM
  • 在线教育(培训+考试)/企业培训-企业培训平台-企业培训平台系统-企业内部培训系统-在线教育-Java语言开发
  • 「AIGC」n8n AI Agent开源的工作流自动化工具
  • php基础:数据类型、常量、字符串
  • 【内信互联】私有化安全性企业远程运维办公解决方案
  • Redis-04 Redis管道
  • 《黑神话:悟空》:又是这只跨界的猴子,诠释了传承与创新的关系
  • 【1】从零开始学习目标检测:YOLO算法详解
  • 1024程序员节:永无bug
  • 《远程桌面方案全析:开启高效远程协作新时代》
  • 美容师流失率高怎么办?怎样可以降低美容师的流失率?博弈美业门店收银系统管理系统分享
  • [LeetCode] 50. Pow(x, n)
  • 基于STM32的多功能MP3播放器
  • 数字信号处理实验简介