多个coco数据标注文件合并

一、coco数据集是什么?

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

二、完整代码

import json
import os

def 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 = 1
    annotation_id_counter = 1

    for 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 filenames
                    for image in data["images"]:
                        image["id"] = image_id_counter
                        image_id_counter += 1

                        # Use the original file name from the COCO file
                        image["file_name"] = image["file_name"]

                        # Append the updated image to the merged_data only if it's not already present
                        if image not in merged_data["images"]:
                            merged_data["images"].append(image)

                    # Update annotation IDs and image IDs
                    for annotation in data["annotations"]:
                        annotation["id"] = annotation_id_counter
                        annotation_id_counter += 1
                        annotation["image_id"] = image_id_counter - 1  # Use the last assigned image ID

                        # Append the updated annotation to the merged_data
                        merged_data["annotations"].append(annotation)

    # Save the merged data to a new JSON file
    output_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格式的数据,使其更适用于你的目标检测或图像分割项目。

相关推荐

  1. coco数据标注文件合并

    2024-01-24 04:20:02       53 阅读
  2. pyhton合并Excel文件

    2024-01-24 04:20:02       50 阅读
  3. 如何一键合并excel文件

    2024-01-24 04:20:02       54 阅读
  4. 合并大语言模型文件的方法

    2024-01-24 04:20:02       62 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-01-24 04:20:02       94 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-01-24 04:20:02       100 阅读
  3. 在Django里面运行非项目文件

    2024-01-24 04:20:02       82 阅读
  4. Python语言-面向对象

    2024-01-24 04:20:02       91 阅读

热门阅读

  1. 【Backbone】Vim(Vision Mamba)架构学习笔记

    2024-01-24 04:20:02       63 阅读
  2. 采用ERNIE计算 Perplexity (PPL)

    2024-01-24 04:20:02       49 阅读
  3. C#partial关键字(分布类)

    2024-01-24 04:20:02       49 阅读
  4. Structured Streaming基础--学习笔记

    2024-01-24 04:20:02       57 阅读
  5. NVIDIA 驱动和 CUDA 版本信息速查

    2024-01-24 04:20:02       57 阅读
  6. 代码随想录二刷 | 回溯 |复原IP地址

    2024-01-24 04:20:02       60 阅读
  7. 【C++PCL】点云处理K-Means点云分割

    2024-01-24 04:20:02       59 阅读