-
Notifications
You must be signed in to change notification settings - Fork 4
/
VisDrone2YOLO_lable_fl_drone.py
41 lines (37 loc) · 1.39 KB
/
VisDrone2YOLO_lable_fl_drone.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
import os
import pandas as pd
from PIL import Image
split = "val"
YOLO_LABELS_PATH = f"/home/tu666280/FL-Drones-Dataset/yolo-tph-data/{split}/labels"
VISANN_PATH = f"/home/tu666280/FL-Drones-Dataset/yolo-tph-data/{split}/annotationsvisdrone"
VISIMG_PATH = f"/home/tu666280/FL-Drones-Dataset/yolo-tph-data/{split}/frames"
def convert(bbox, img_size):
#将标注visDrone数据集标注转为yolov5
#bbox top_left_x top_left_y width height
dw = 1/(img_size[0])
dh = 1/(img_size[1])
x = bbox[0] + bbox[2]/2
y = bbox[1] + bbox[3]/2
x = x * dw
y = y * dh
w = bbox[2] * dw
h = bbox[3] * dh
return (x,y,w,h)
def ChangeToYolo5():
if not os.path.exists(YOLO_LABELS_PATH):
os.makedirs(YOLO_LABELS_PATH)
print(len(os.listdir(VISANN_PATH)))
for file in os.listdir(VISANN_PATH):
image_path = VISIMG_PATH + '/' + file.replace('txt', 'png')
ann_file = VISANN_PATH + '/' + file
out_file = open(YOLO_LABELS_PATH + '/' + file, 'w')
bbox = pd.read_csv(ann_file,header=None).values
img = Image.open(image_path)
img_size = img.size
for row in bbox:
if(row[4]==1 and 0<row[5]<11):
label = convert(row[:4], img_size)
out_file.write(str(row[5]-1) + " " + " ".join(str(f'{x:.6f}') for x in label) + '\n')
out_file.close()
if __name__ == '__main__':
ChangeToYolo5()