forked from positive666/yolo_research
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Visrdone_labesl.py
40 lines (36 loc) · 1.33 KB
/
Visrdone_labesl.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
import os
import pandas as pd
from PIL import Image
YOLO_LABELS_PATH = "../datasets/VisDrone/VisDrone2019-DET-train/labels"
VISANN_PATH = "../datasets/VisDrone/VisDrone2019-DET-train/annotations/"
VISIMG_PATH = "../datasets//VisDrone/VisDrone2019-DET-train/images/"
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', 'jpg')
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()