yolo数据集格式(txt)转coco格式,方便mmyolo转标签格式

近期使用mmyolo过程中发现工具自带的yolo2coco.py在转换完数据集格式后,可视化标签的时候会有标签错乱情况,具体原因也没找到,肯定是转换过程代码有问题,于是重新做一份代码直接从yolo数据集转化为coco的json格式。

代码如下:

只需要维护

category_map

data["categories"]

这两个标签id和标签名即可。

我的txt标签格式如下

标签类别为:

{"id": 0, "name": "person"},

{"id": 1, "name": "robotic-arm"}

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

# 定义类别映射
category_map = {
    "0": 0,
    "1": 1
}

def txt_to_json(input_dir, output_json):
    # 初始化输出数据结构
    data = {
        "images": [],
        "annotations": [],
        "categories": [
            {"id": 0, "name": "person"},
            {"id": 1, "name": "robotic-arm"}
        ]
    }

    # 用于生成唯一的 image_id 和 annotation_id
    image_id = 1
    annotation_id = 1

    # 动态读取图像尺寸
    def get_image_size(image_path):
        with Image.open(image_path) as img:
            return img.width, img.height

    # 遍历输入目录中的所有文件
    for filename in os.listdir(input_dir):
        if filename.endswith('.txt'):
            # 提取图像文件名
            image_name = filename.replace('.txt', '.jpg')
            image_path = os.path.join(input_dir, image_name)

            # 动态读取图像尺寸
            if os.path.exists(image_path):
                image_width, image_height = get_image_size(image_path)
            else:
                print(f"警告: 图像文件 {image_name} 不存在,跳过文件 {filename}")
                continue

            image_info = {
                "id": image_id,
                "width": image_width,
                "height": image_height,
                "file_name": image_name
            }
            data['images'].append(image_info)

            # 读取文本文件
            with open(os.path.join(input_dir, filename), 'r') as file:
                lines = file.readlines()

            # 遍历文件中的每一行
            for line in lines:
                parts = line.strip().split()
                if len(parts) != 5:
                    print(f"警告: 文件 {filename} 中的行格式错误: {line.strip()}")
                    continue  # 跳过格式错误的行

                category_id = int(parts[0])
                x_center = float(parts[1]) * image_width
                y_center = float(parts[2]) * image_height
                bbox_width = float(parts[3]) * image_width
                bbox_height = float(parts[4]) * image_height

                x_min = int(x_center - bbox_width / 2)
                y_min = int(y_center - bbox_height / 2)
                bbox = [x_min, y_min, bbox_width, bbox_height]
                area = bbox_width * bbox_height

                annotation_info = {
                    "id": annotation_id,
                    "image_id": image_id,
                    "category_id": category_id,
                    "bbox": bbox,
                    "area": area,
                    "iscrowd": 0
                }
                data['annotations'].append(annotation_info)
                annotation_id += 1

            image_id += 1

    # 保存到 JSON 文件
    with open(output_json, 'w') as json_file:
        json.dump(data, json_file, indent=4)

    print(f"转换完成,输出文件保存为 {output_json}")


# 调用函数
input_dir = './personDataset/train'
output_json = './trainval.json'
txt_to_json(input_dir, output_json)

input_dir = './personDataset/test'
output_json = './test.json'
txt_to_json(input_dir, output_json)

结果:

![](https://i-blog.csdnimg.cn/direct/7e18c88f355340d5aaae4d466bda827e.png![在这里插入图片描述](https://i-blog.csdnimg.cn/direct/f7095e1cd23b4431bf795f934efcd0eb.png)

相关推荐
程序员JerrySUN8 小时前
RK3588 项目实战总结:从 GStreamer、DP 显示、OpenCV 到 YOLO 部署的一次完整梳理
人工智能·opencv·yolo
Byron Loong9 小时前
【机器视觉】标注软件 labelMe和lableImg对比
yolo·计算机视觉
QQ6765800810 小时前
AI赋能识别之围栏破损识别 围栏缺陷检测数据集 栅栏破损识别数据集围栏孔洞识别植被入侵检测图像数据集 目标检测图像数据集第10125期
人工智能·yolo·目标检测·目标跟踪·围栏破损识别·围栏缺陷识别·栅栏破损识别
深度学习lover11 小时前
<数据集>yolo扑克牌识别<目标检测>
人工智能·python·yolo·目标检测·计算机视觉·扑克牌识别
QQ6765800812 小时前
AI人工智能图像识别 兔子动物分类研究 宠物行业物种鉴别及畜牧业兔种监测 兔种监测识别 YOLO图像数据集 兔类物种的计算机视觉识别模型训练 第10363期
人工智能·yolo·目标检测·目标跟踪·分类·宠物·宠物行业鉴别
melonbo2 天前
Jetson Nano 平台YOLO目标检测优化详细设计方案
yolo·目标检测·目标跟踪
Linux猿2 天前
植物病害图像数据集 YOLO 目标检测 | 可下载
人工智能·yolo·目标检测·yolo数据集·yolo目标检测·yolo目标检测数据集·植物病害图像数据集
毕胜客源码2 天前
改进yolov8的香蕉成熟度检测系统,改进前后的模型指标对比,有技术文档,支持图像、视频和摄像实时检测
人工智能·python·深度学习·yolo·django
河西石头2 天前
YOLO训练中 `PytorchStreamReader` 错误的真相
yolo·模型训练·模型文件·pt文件下载失败·reading zip
Linux猿2 天前
汽车牌照数据集 YOLO 目标检测 | 可下载
yolo·目标检测·目标检测数据集·yolo目标检测·yolo目标检测数据集·汽车牌照数据集