YOLO-COCO数据集转换

 output_dir  保存地址

dataeest_path  yolo格式地址

import os
import json
from PIL import Image

# 设置数据集路径
output_dir = "D:\mmdetection-3.3.0\data"
dataset_path = "X:\peanut\peaunt"
images_path = os.path.join(dataset_path, "images")
labels_path = os.path.join(dataset_path, "labels")

# 类别映射
categories = [
    {"id": 1, "name": "spot"},

    # 添加更多类别
]


# YOLO格式转COCO格式的函数
def convert_yolo_to_coco(x_center, y_center, width, height, img_width, img_height):
    x_min = (x_center - width / 2) * img_width
    y_min = (y_center - height / 2) * img_height
    width = width * img_width
    height = height * img_height
    return [x_min, y_min, width, height]


# 初始化COCO数据结构
def init_coco_format():
    return {
        "images": [],
        "annotations": [],
        "categories": categories
    }


# 处理每个数据集分区
for split in ['train', 'test', 'val']:
    coco_format = init_coco_format()
    annotation_id = 1

    for img_name in os.listdir(os.path.join(images_path, split)):
        if img_name.lower().endswith(('.png', '.jpg', '.jpeg')):
            img_path = os.path.join(images_path, split, img_name)
            label_path = os.path.join(labels_path, split, img_name.replace("jpg", "txt"))

            img = Image.open(img_path)
            img_width, img_height = img.size
            image_info = {
                "file_name": img_name,
                "id": len(coco_format["images"]) + 1,
                "width": img_width,
                "height": img_height
            }
            coco_format["images"].append(image_info)

            if os.path.exists(label_path):
                with open(label_path, "r") as file:
                    for line in file:
                        category_id, x_center, y_center, width, height = map(float, line.split())
                        bbox = convert_yolo_to_coco(x_center, y_center, width, height, img_width, img_height)
                        annotation = {
                            "id": annotation_id,
                            "image_id": image_info["id"],
                            "category_id": int(category_id) + 1,
                            "bbox": bbox,
                            "area": bbox[2] * bbox[3],
                            "iscrowd": 0
                        }
                        coco_format["annotations"].append(annotation)
                        annotation_id += 1

    # 为每个分区保存JSON文件
    with open(os.path.join(output_dir, f"{split}_coco_format.json"), "w") as json_file:
        json.dump(coco_format, json_file, indent=4)

  • 7
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
COCO数据集转换YOLO格式需要进行以下步骤: 1. 下载COCO数据集:首先,你需要从COCO官方网站下载COCO数据集COCO数据集包含了大量的图像和对应的标注信息,用于目标检测任务。 2. 解析COCO标注文件:COCO数据集的标注信息保存在JSON格式的文件中。你需要使用相应的库(如Python中的json库)来解析这些标注文件,获取图像的路径、目标类别、边界框等信息。 3. 转换YOLO格式:YOLO格式要求每个图像的标注信息保存在一个单独的文本文件中,与对应的图像文件放在同一目录下。每个文本文件的命名应与对应的图像文件相同,只是扩展名不同(如.jpg对应.txt)。每个文本文件中的每一行表示一个目标,包含目标类别和边界框的位置信息。 - 目标类别:YOLO使用整数编码来表示不同的目标类别。你需要将COCO数据集中的目标类别映射为对应的整数编码。可以创建一个字典来存储类别与编码之间的映射关系。 - 边界框位置:YOLO使用归一化坐标来表示边界框的位置。边界框的位置信息包括左上角和右下角的坐标。你需要将COCO数据集中的边界框位置转换为归一化坐标。 4. 生成YOLO标注文件:根据上述转换规则,遍历COCO数据集中的每个图像及其对应的标注信息,将其转换YOLO格式的标注信息,并保存到对应的文本文件中。 完成上述步骤后,你就可以使用YOLO框架来训练和测试目标检测模型了。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

小徐爱打球

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

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

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

打赏作者

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

抵扣说明:

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

余额充值