-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathfilterVisDroneLabels.py
58 lines (48 loc) · 2.13 KB
/
filterVisDroneLabels.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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# Code to read annotations in the YOLO format and remove
# annotations from classes that are filtered out
#
# Follow this directory structure to use this code as-is
#
# └── working directory
# └── visDrone2YOLO.py
# └── viewConvertedLabels.py
# └── filterVisDroneLabels.py
# └── VisDrone2019-DET-train
# └── images
# └── labels
# └── labels-filtered (will be created)
# └── VisDrone2019-DET-val
# └── images
# └── labels
# └── labels-filtered (will be created)
# └── VisDrone2019-DET-test-dev
# └── images
# └── labels
# └── labels-filtered (will be created)
import os
from os import listdir
from os.path import isfile, join
label_dir = "VisDrone2019-DET-val/labels/"
output_dir = "VisDrone2019-DET-val/labels-filtered/"
if not os.path.exists(output_dir):
os.makedirs(output_dir)
# Read all filenames in the labels directory and add the names to a list
fileNames = [file_name for file_name in listdir(label_dir) if isfile(join(label_dir, file_name))]
# Add the class labels that will be retained after filtering, eg: 0, 1, 2
# Range: 0-9. All labels not added will be filtered out.
retain_labels = [2, 5, 7]
# Setting replace_label_names to true will convert the original labels to
# the new labels starting from 0. eg: [2, 5, 7] --> [0, 1, 2]
replace_label_names = True
for file in fileNames:
basename = os.path.basename(file)
filename = os.path.splitext(basename)[0]
with open(os.path.join(label_dir, f"{filename}.txt"), 'r', encoding='utf8') as f:
for line in f:
label = int(line[0]) # Label is the first character on the line
if(label in retain_labels):
with open(os.path.join(output_dir, f"{filename}.txt"), "a+", encoding="utf-8") as file:
if replace_label_names == True:
line = str(retain_labels.index(label)) + line[1:]
else:
file.write(line)