由mask制作coco8-seg标签用于YOLOV8自定义数据集训练

YOLOV8中coco8-seg数据集文件树

../datasets/coco8-seg
│   coco8-seg.yml
├───images
│   ├───test
│   │   ├───000000000001.jpg
│   │   └───000000000002.jpg
│   │       ...
│   ├───train
│   │   ├───000000000001.jpg
│   │   └───000000000002.jpg
│   │   	...
│   └───val
│       ├───000000000001.jpg
│       └───000000000002.jpg
│       	...
└───labels
    │   
    ├───test
    │   ├───000000000001.txt
    │   └───000000000002.txt
    │   	...
    │       
    ├───train
    │   ├───000000000001.txt
    │   └───000000000002.txt
    │   	...
    │       
    └───val
        ├───000000000001.txt
        └───000000000002.txt
			...
wAAACH5BAEKAAAALAAAAAABAAEAAAICRAEAOw==

标准的ultralytics/cfg/datasets/coco8-seg.yaml

# COCO8-seg dataset (first 8 images from COCO train2017) by Ultralytics
# Documentation: https://docs.ultralytics.com/datasets/segment/coco8-seg/
# Example usage: yolo train data=coco8-seg.yaml
# parent
# ├── ultralytics
# └── datasets
#     └── coco8-seg  ← downloads here (1 MB)

# Train/val/test sets as 1) dir: path/to/imgs, 2) file: path/to/imgs.txt, or 3) list: [path/to/imgs1, path/to/imgs2, ..]
path: ../datasets/coco8-seg # dataset root dir
train: images/train # train images (relative to 'path') 4 images
val: images/val # val images (relative to 'path') 4 images
test: # test images (optional)

# Classes
names:
  0: person
  1: bicycle
  2: car
  3: motorcycle
  4: airplane
  5: bus
  6: train
  7: truck
  8: boat
  9: traffic light
  10: fire hydrant
  11: stop sign
  12: parking meter
  13: bench
  14: bird
  15: cat
  16: dog
  17: horse
  18: sheep
  19: cow
  20: elephant
  21: bear
  22: zebra
  23: giraffe
  24: backpack
  25: umbrella
  26: handbag
  27: tie
  28: suitcase
  29: frisbee
  30: skis
  31: snowboard
  32: sports ball
  33: kite
  34: baseball bat
  35: baseball glove
  36: skateboard
  37: surfboard
  38: tennis racket
  39: bottle
  40: wine glass
  41: cup
  42: fork
  43: knife
  44: spoon
  45: bowl
  46: banana
  47: apple
  48: sandwich
  49: orange
  50: broccoli
  51: carrot
  52: hot dog
  53: pizza
  54: donut
  55: cake
  56: chair
  57: couch
  58: potted plant
  59: bed
  60: dining table
  61: toilet
  62: tv
  63: laptop
  64: mouse
  65: remote
  66: keyboard
  67: cell phone
  68: microwave
  69: oven
  70: toaster
  71: sink
  72: refrigerator
  73: book
  74: clock
  75: vase
  76: scissors
  77: teddy bear
  78: hair drier
  79: toothbrush

# Download script/URL (optional)
download: https://ultralytics.com/assets/coco8-seg.zip

共80个类别。其中.txt标签格式为:

<class-index> <x1> <y1> <x2> <y2> ... <xn> <yn>

如:

0 0.681 0.485 0.670 0.487 0.676 0.487
1 0.504 0.000 0.501 0.004 0.498 0.004 0.493 0.010 0.492 0.0104

标签文件(.txt)说明:

1)一张rgb图像对应一个.txt标签;

2)标签文件中由多行数据(类别号,<class-index> 和归一化的像素点<x1> <y1>),每行标记图像中一个类别的物体;

3)像素点数量不少于3个。


自定义yolov8分割数据集按照coco8-seg格式制作。类别数量自定义,需要增删coco8-seg.yaml中name字典。

这里的Class数量与模型输出对应,coco8-seg数据集对应的模型输出shape为:(bz, 4 + 80 + 32), 8400)、(bz, 32, 160, 160)。80对应类别数,如果数据集删除到2个类别,则输出的shape为(bz, 4 + 80 + 32), 8400)、(bz, 32, 160, 160)。其中,

4:为 box,(x, y, w, h);

80:为 class的confidence;

# yolo 标签制作by mask
import cv2
import numpy as np
import os

mask_path = r'green\mask'
mask_lists = os.listdir(mask_path)

for mask_name in tqdm(mask_lists):
    filename, _ = os.path.splitext(mask_name)
    label = str(0)  # 类别标签,YOLO格式中通常是1表示存在物体
    mask = cv2.imread(os.path.join(mask_path, mask_name), -1)
    contours, _ = cv2.findContours(mask, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)

    if contours:  # 确保至少有一个轮廓
        for coord in contours[0]:
            # 计算归一化坐标并格式化为8位长度的字符串
            x_normalized = coord[0][0] / 640
            y_normalized = coord[0][1] / 480
            x = str(x_normalized)[:8] # 使用zfill方法填充0直到长度为8
            y = str(y_normalized)[:8]

            # 构建标签字符串
            xy = f' {x} {y}'
            label += xy

    # 打开文件,准备写入标签
    with open(os.path.join('labels', filename + '.txt'), 'w') as f:
        # 写入标签
        f.write(label)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值