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

多个coco数据标注文件合并

一、coco数据集是什么?

COCO(Common Objects in Context)是一个用于目标检测和图像分割任务的标注格式。如果你有多个COCO格式的JSON文件,你可能需要将它们合并成一个文件,以便更方便地处理和管理数据。在这篇博客中,我们将介绍一个用Python编写的脚本,可以实现这一合并操作。

二、完整代码

import json
import osdef merge_coco_files(folder_path):merged_data = {"info": {"year": 2023,"version": "1","date_created": "no need record"},"images": [],"annotations": [],"licenses": [{"id": 1,"name": "Unknown","url": ""}],"categories": [{"id": 1,"name": "hd","supercategory": ""}]}image_id_counter = 1annotation_id_counter = 1for root, dirs, files in os.walk(folder_path):for file in files:if file.endswith(".json"):file_path = os.path.join(root, file)with open(file_path, 'r') as f:data = json.load(f)# Update image IDs and filenamesfor image in data["images"]:image["id"] = image_id_counterimage_id_counter += 1# Use the original file name from the COCO fileimage["file_name"] = image["file_name"]# Append the updated image to the merged_data only if it's not already presentif image not in merged_data["images"]:merged_data["images"].append(image)# Update annotation IDs and image IDsfor annotation in data["annotations"]:annotation["id"] = annotation_id_counterannotation_id_counter += 1annotation["image_id"] = image_id_counter - 1  # Use the last assigned image ID# Append the updated annotation to the merged_datamerged_data["annotations"].append(annotation)# Save the merged data to a new JSON fileoutput_path = os.path.join(folder_path, "merged_coco.json")with open(output_path, 'w') as output_file:json.dump(merged_data, output_file, indent=4)print(f'Merged data saved to: {output_path}')# Provide the path to the folder containing the COCO JSON files
folder_path = r''
merge_coco_files(folder_path)

脚本的主要步骤包括:

初始化合并后的数据结构。
遍历指定文件夹中的所有JSON文件。
对每个JSON文件中的图像和注释进行ID的更新。
将更新后的数据保存为新的JSON文件。

使用方法

为了使用这个脚本,你只需提供包含COCO JSON文件的文件夹路径,并运行脚本。合并后的数据将保存在原始文件夹中,并命名为"merged_coco.json"。

这个脚本可以帮助你更好地组织和管理COCO格式的数据,使其更适用于你的目标检测或图像分割项目。

http://www.lryc.cn/news/286351.html

相关文章:

  • Kubernetes(K8S)拉取本地镜像部署Pod 实现类似函数/微服务功能(可设置参数并实时调用)
  • k8s使用ingress实现应用的灰度发布升级
  • 最新热门商用GPT4.0带MJ绘画去授权版本自定义三方接口(开心版)
  • Halcon基于形状的模板匹配inspect_shape_model
  • html中根元素以及根元素字体的含义
  • 51单片机1-6
  • vue2(Vuex)、vue3(Pinia)、react(Redux)状态管理
  • 用户画像项目背景
  • Go使用记忆化搜索的套路【以20240121力扣每日一题为例】
  • 【LeetCode】每日一题 2024_1_21 分割数组的最大值(二分)
  • bevy the book 20140118翻译(全)
  • MySQL数据库面试知识点
  • 超优秀的三维模型轻量化、格式转换、可视化部署平台!
  • 云原生全栈监控解决方案(全面详解)
  • 代码随想录二刷 | 回溯 |复原IP地址
  • windows资源管理器占用过高CPU的问题
  • redis的常见数据类型和应用场景(非八股)------大总结(学了要会用-------教你如何使用)
  • UE 可靠UDP实现原理
  • 智慧博物馆信息化系统建设(1)
  • 【数据结构和算法】--- 二叉树(3)--二叉树链式结构的实现(1)
  • Cesium for Unity包无法加载
  • Leetcode—40.组合总和II【中等】
  • vscode连不上虚拟机,一直密码错误
  • 力扣每日一题 --- 972. 相等的有理数
  • EXECL 单元格字符串链接 CONCAT :应用:将一行数据转为json
  • 基于Python实现人脸识别相似度对比
  • CSS 蜡烛效果
  • 渗透测试之Kali如何利用CVE-2019-0708漏洞渗透Win7
  • Docker(二)安装指南:主要介绍在 Linux 、Windows 10 和 macOS 上的安装
  • LeetCode 410. 分割数组的最大值