【目标检测】YOLO格式数据集txt标注转换为COCO格式JSON

YOLO格式数据集:

python 复制代码
images
|--train
|--test
|--val


labels
|--train
|--test
|--val

代码:

python 复制代码
import os
import json
from PIL import Image

# 设置数据集路径
dataset_path = "path/to/your/dataset"
images_path = os.path.join(dataset_path, "images")
labels_path = os.path.join(dataset_path, "labels")

# 类别映射
categories = [
    {"id": 1, "name": "category1"},
    {"id": 2, "name": "category2"},
    # 添加更多类别
]

# 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(f"path/to/output/{split}_coco_format.json", "w") as json_file:
        json.dump(coco_format, json_file, indent=4)
相关推荐
jianqiang.xue6 小时前
开了 -O2 延时函数全乱:volatile 救不了空循环
人工智能·单片机·嵌入式硬件·json·嵌入式·ai编程·代码审查
星云_byto7 小时前
EGM-Det:熵引导RGB-IR目标检测,VEDAI达60.3%
人工智能·目标检测·计算机视觉
qq_25294131689 小时前
道路自然灾害障碍检测数据集深度学习 目标检测 道路灾害 滑坡落石 智能巡检 YOLO格式 数据集发布9084期
深度学习·yolo·目标检测
阿enna9 小时前
把本地数据与权重迁移到服务器并启动训练(进阶)
运维·服务器·深度学习·yolo·目标检测·cnn·视觉检测
qq_252941316810 小时前
无人机视角路面裂纹坑洼检测数据集深度学习 目标检测 无人机航拍 路面病害 道路养护 YOLO格式 数据集发布9085期
深度学习·目标检测·无人机
如若12321 小时前
FIND:频域不变性解耦助力LiDAR 3D检测测试时自适应
人工智能·yolo·目标检测·计算机视觉·3d
微尘hjx1 天前
【BoostTrack 02】多轨迹算法BoostTrack-卡尔曼滤波预测过程
yolo·目标检测·目标跟踪·ultralytics·车跟踪·boosttrack·多轨迹算法
IMPYLH1 天前
JSON.stringify 悄悄丢掉的字段
json
qq_25294131682 天前
风机表面瑕疵检测数据集深度学习 目标检测 风机瑕疵 风电运维 工业缺陷 YOLO格式 数据集发布9080期
深度学习·yolo·目标检测
SEO_juper2 天前
Node.js Product 结构化数据生成器:给产品页一键生成 JSON-LD
开发语言·前端·javascript·人工智能·node.js·json·1024程序员节