COCO、VOC 和 YOLO三种主流目标检测数据格式的详细对比与示例说明

1,005 阅读3分钟

以下是 COCOVOCYOLO 三种主流目标检测数据格式的详细对比与示例说明,帮助你快速理解它们的结构和适用场景:


1. PASCAL VOC (XML格式)

特点

  • 通用性强:支持检测、分割、动作识别等多任务。
  • 元数据丰富:包含物体难度、截断状态等附加信息。
  • 文件结构
    VOC2012/
    ├── Annotations/       # 每个图片对应一个.xml文件
    ├── JPEGImages/        # 原始图片
    └── ImageSets/Main/    # 划分文件(train.txt, val.txt)
    

XML标注示例

<annotation>
  <filename>2007_000027.jpg</filename>
  <size>
    <width>500</width>
    <height>375</height>
    <depth>3</depth>
  </size>
  <object>
    <name>person</name>
    <bndbox>
      <xmin>174</xmin>
      <ymin>101</ymin>
      <xmax>349</xmax>
      <ymax>351</ymax>
    </bndbox>
    <difficult>0</difficult>  <!-- 0表示易检测,1表示困难 -->
    <truncated>1</truncated>  <!-- 物体是否被截断 -->
  </object>
</annotation>

适用场景

  • 需要细粒度分析(如困难样本统计)。
  • 多任务学习(检测+分割)。

2. COCO (JSON格式)

特点

  • 统一归档:所有标注存储在单个JSON文件中。
  • 支持密集任务:如实例分割(多边形标注)、关键点检测。
  • 文件结构
    coco/
    ├── train2017/       # 训练图片
    ├── val2017/         # 验证图片
    └── annotations/     # 标注文件
        ├── instances_train2017.json
        └── instances_val2017.json
    

JSON标注示例

{
  "images": [
    {
      "id": 1,
      "file_name": "0000001.jpg",
      "width": 640,
      "height": 480
    }
  ],
  "annotations": [
    {
      "id": 1,
      "image_id": 1,
      "category_id": 1,  # 类别ID(对应categories中的id)
      "bbox": [x, y, width, height],  # 左上角坐标+宽高
      "area": 50000,
      "segmentation": [[x1,y1,x2,y2,...]],  # 多边形坐标
      "iscrowd": 0  # 0表示单个物体,1表示密集群组
    }
  ],
  "categories": [
    {"id": 1, "name": "person"},
    {"id": 2, "name": "car"}
  ]
}

适用场景

  • 复杂任务(如实例分割)。
  • 需要高效管理大规模数据集。

3. YOLO (TXT格式)

特点

  • 轻量化:每个图片对应一个.txt文件,存储归一化坐标。
  • 算法友好:直接适配YOLO系列模型。
  • 文件结构
    yolov5_dataset/
    ├── images/
    │   ├── train/       # 图片
    │   └── val/
    ├── labels/
    │   ├── train/       # 标签(.txt)
    │   └── val/
    └── dataset.yaml     # 配置文件
    

TXT标注示例

# 000001.txt
0 0.53 0.65 0.12 0.24  # 类别ID 中心x 中心y 宽度 高度(均归一化到[0,1])
1 0.31 0.42 0.15 0.10
  • 归一化计算
    ( x_{\text{center}} = \frac{x_{\text{min}} + x_{\text{max}}}{2 \times \text{image_width}} )
    ( width = \frac{x_{\text{max}} - x_{\text{min}}}{\text{image_width}} )

适用场景

  • YOLO系列模型训练。
  • 需要快速读取的小型项目。

三者的核心区别

特性VOC (XML)COCO (JSON)YOLO (TXT)
标注存储方式单文件单图片单文件全数据集单文件单图片
坐标格式绝对像素值 (xmin,ymin...)绝对像素值 + 宽高归一化中心坐标 + 宽高
附加信息困难样本、截断标记分割多边形、密集标记仅基础检测信息
适用算法Faster R-CNN, SSDMask R-CNN, Detectron2YOLOv3/v5/v8
文件体积中等(多个XML)较大(单个JSON)最小(多个TXT)

格式转换工具推荐

  1. VOC → YOLO
    python yolov5/scripts/voc2yolo.py --voc_dir VOC2012 --output_dir VOC2012_YOLO
    
  2. COCO → YOLO
    python yolov5/scripts/coco2yolo.py --json annotations/instances_train2017.json --dir train2017
    
  3. 可视化验证
    from PIL import Image, ImageDraw
    # 以YOLO格式为例绘制标注框
    img = Image.open("image.jpg")
    draw = ImageDraw.Draw(img)
    with open("label.txt") as f:
        for line in f:
            cls_id, x, y, w, h = map(float, line.split())
            # 转换为像素坐标
            x1 = (x - w/2) * img.width
            y1 = (y - h/2) * img.height
            x2 = (x + w/2) * img.width
            y2 = (y + h/2) * img.height
            draw.rectangle([x1, y1, x2, y2], outline="red")
    img.show()