1.准备数据集
(1)下载数据集
这里以CUB-200-2011数据集为例。CUB-200-2011(Caltech-UCSD Birds 200-2011)是一个包含200种鸟类的数据集,每种鸟类大约有60张图片,总共有11788张图片。数据集中包含了图片和标注信息(包括边界框)。下载链接:CUB-200-2011
(2)解压
(3)转换成yolov8所需格式
生成标签文件(labels):打开你克隆下来的ultralytics项目,在项目中新建一个python文件convert_to_yolov8_format.py,粘贴下面的python代码。并打开终端输入以下bash命令运行它。
python convert_to_yolov8_format.py
#1.处理标签文件
import os
import pandas as pd
from PIL import Image
# 数据集路径
dataset_dir = '/path/to/CUB_200_201' #这里切换为自己的路径,解压后的数据集位置
images_dir = os.path.join(dataset_dir, 'images')
annotations_dir = dataset_dir
# 加载数据
bounding_boxes = pd.read_csv(os.path.join(annotations_dir, 'bounding_boxes.txt'), sep='\s+', header=None, names=['image_id', 'x', 'y', 'width', 'height'])
class_labels = pd.read_csv(os.path.join(annotations_dir, 'image_class_labels.txt'), sep='\s+', header=None, names=['image_id', 'class_id'])
image_paths = pd.read_csv(os.path.join(annotations_dir, 'images.txt'), sep='\s+', header=None, names=['image_id', 'image_path'])
# 合并数据帧
data = pd.merge(bounding_boxes, class_labels, on='image_id')
data = pd.merge(data, image_paths, on='image_id')
# 创建YOLO格式的标注文件
for _, row in data.iterrows():
image_path = os.path.join(images_dir, row['image_path'])
image = Image.open(image_path)
dw = 1. / image.width
dh = 1. / image.height
# 计算归一化坐标
x = (row['x'] + row['width'] / 2.0) * dw
y = (row['y'] + row['height'] / 2.0) * dh
w = row['width'] * dw
h = row['height'] * dh
# 类别ID调整为从0开始(如果需要)
class_id = row['class_id'] - 1
# 生成标注文件
txt_path = image_path.replace


1427

被折叠的 条评论
为什么被折叠?



