【目标检测】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)
相关推荐
hixiong1235 小时前
C# TensorRT部署RF-DETR目标检测&分割模型
人工智能·目标检测·计算机视觉·ai·c#
likerhood6 小时前
Fastjson中的JSON.parseObject()详细讲解
java·json
AI棒棒牛8 小时前
YOLOv13最新创新改进系列:YOLOv13特征可视化,特征提取图,科技感满满,丰富实验神器!!!
人工智能·科技·yolo·目标检测·计算机视觉
钓了猫的鱼儿9 小时前
【数据集】红外无人机目标检测数据集VOC+YOLO格式10000张1类别
yolo·目标检测·无人机
輕華9 小时前
YOLOv5 实战:从 GitHub 拉取到自定义数据集训练
yolo·github
guo_xiao_xiao_11 小时前
YOLOv11城市道路自行车目标检测数据集-999张-Bicycle-1
人工智能·yolo·目标检测
guo_xiao_xiao_11 小时前
YOLOv11机场跑道大型客机-飞机目标检测数据集-201张-Airplane-1_2_2
人工智能·yolo·目标检测
老神在在00112 小时前
JSON Schema
测试工具·json
羊羊小栈13 小时前
基于「YOLO目标检测 + 多模态AI分析」的宠物猫狗健康智能检测分析预警系统
人工智能·yolo·目标检测·计算机视觉·毕业设计·大作业
南斯拉夫的铁托14 小时前
YOLO学习笔记
笔记·学习·yolo