You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

66 lines
2.8KB

  1. # VisDrone2019-DET dataset https://github.com/VisDrone/VisDrone-Dataset
  2. # Train command: python train.py --data visdrone.yaml
  3. # Default dataset location is next to YOLOv5:
  4. # /parent_folder
  5. # /VisDrone
  6. # /yolov5
  7. # train and val data as 1) directory: path/images/, 2) file: path/images.txt, or 3) list: [path1/images/, path2/images/]
  8. train: ../VisDrone/VisDrone2019-DET-train/images # 6471 images
  9. val: ../VisDrone/VisDrone2019-DET-val/images # 548 images
  10. test: ../VisDrone/VisDrone2019-DET-test-dev/images # 1610 images
  11. # number of classes
  12. nc: 10
  13. # class names
  14. names: [ 'pedestrian', 'people', 'bicycle', 'car', 'van', 'truck', 'tricycle', 'awning-tricycle', 'bus', 'motor' ]
  15. # download command/URL (optional) --------------------------------------------------------------------------------------
  16. download: |
  17. import os
  18. from pathlib import Path
  19. from utils.general import download
  20. def visdrone2yolo(dir):
  21. from PIL import Image
  22. from tqdm import tqdm
  23. def convert_box(size, box):
  24. # Convert VisDrone box to YOLO xywh box
  25. dw = 1. / size[0]
  26. dh = 1. / size[1]
  27. return (box[0] + box[2] / 2) * dw, (box[1] + box[3] / 2) * dh, box[2] * dw, box[3] * dh
  28. (dir / 'labels').mkdir(parents=True, exist_ok=True) # make labels directory
  29. pbar = tqdm((dir / 'annotations').glob('*.txt'), desc=f'Converting {dir}')
  30. for f in pbar:
  31. img_size = Image.open((dir / 'images' / f.name).with_suffix('.jpg')).size
  32. lines = []
  33. with open(f, 'r') as file: # read annotation.txt
  34. for row in [x.split(',') for x in file.read().strip().splitlines()]:
  35. if row[4] == '0': # VisDrone 'ignored regions' class 0
  36. continue
  37. cls = int(row[5]) - 1
  38. box = convert_box(img_size, tuple(map(int, row[:4])))
  39. lines.append(f"{cls} {' '.join(f'{x:.6f}' for x in box)}\n")
  40. with open(str(f).replace(os.sep + 'annotations' + os.sep, os.sep + 'labels' + os.sep), 'w') as fl:
  41. fl.writelines(lines) # write label.txt
  42. # Download
  43. dir = Path('../VisDrone') # dataset directory
  44. urls = ['https://github.com/ultralytics/yolov5/releases/download/v1.0/VisDrone2019-DET-train.zip',
  45. 'https://github.com/ultralytics/yolov5/releases/download/v1.0/VisDrone2019-DET-val.zip',
  46. 'https://github.com/ultralytics/yolov5/releases/download/v1.0/VisDrone2019-DET-test-dev.zip',
  47. 'https://github.com/ultralytics/yolov5/releases/download/v1.0/VisDrone2019-DET-test-challenge.zip']
  48. download(urls, dir=dir)
  49. # Convert
  50. for d in 'VisDrone2019-DET-train', 'VisDrone2019-DET-val', 'VisDrone2019-DET-test-dev':
  51. visdrone2yolo(dir / d) # convert VisDrone annotations to YOLO labels