VisDrone2019数据集转YOLO格式
今天跑VisDrone2019,发现数据集标注格式不是YOLO的, 在CSDN里找的Python源码做了格式转换(源作者没有保存下来),记录如下:
1、转YOLO
import os from pathlib import Path from PIL import Image from tqdm import tqdm def visdrone2yolo(dir): def convert_box(size, box): # Convert VisDrone box to YOLO CxCywh box,坐标进行了归一化 dw = 1. / size[0] dh = 1. / size[1] return (box[0] + box[2] / 2) * dw, (box[1] + box[3] / 2) * dh, box[2] * dw, box[3] * dh # (dir / 'labels').mkdir(parents=True, exist_ok=True) # make labels directory (dir / 'Annotations_YOLO').mkdir(parents=True, exist_ok=True) # make labels directory pbar = tqdm((dir / 'annotations').glob('*.txt'), desc=f'Converting {dir}') for f in pbar: try: img_size = Image.open((dir / 'images' / f.name).with_suffix('.jpg')).size lines = [] with open(f, 'r') as file: # read annotation.txt for row in [x.split(',') for x in file.read().strip().splitlines()]: if row[4] == '0': # VisDrone 'ignored regions' class 0 continue cls = int(row[5]) - 1 box = convert_box(img_size, tuple(map(int, row[:4]))) lines.append(f"{cls} {' '.join(f'{x:.6f}' for x in box)}\n") # 构建输出文件路径 output_path = str(f).replace(os.sep + 'annotations' + os.sep, os.sep + 'Annotations_YOLO' + os.sep) with open(output_path, 'w') as fl: fl.writelines(lines) # write label.txt except Exception as e: print(f"Error processing {f}: {e}") dir = Path(r'D:\VisDrone2019') # dataset文件夹下Visdrone2019文件夹路径 # Convert # for d in ['VisDrone2019-DET-train', 'VisDrone2019-DET-val', 'VisDrone2019-DET-test-dev']: # visdrone2yolo(dir / d) # convert VisDrone annotations to YOLO labels #visdrone2yolo(dir / 'VisDrone2019-DET-test-dev') # convert VisDrone annotations to YOLO labels visdrone2yolo(dir / 'VisDrone2019-DET-train')2、增加classes.txt文件,方便LabelImage中查看:
0: pedestrian 1: people 2: bicycle 3: car 4: van 5: truck 6: tricycle 7: awning-tricycle 8: bus 9: motor3、启动LabelImage,查看标注结果
4、配置YOLO项目源码,开始训练
