forked from qqwweee/keras-yolo3
-
Notifications
You must be signed in to change notification settings - Fork 3
/
modify_annotation.py
58 lines (49 loc) · 2.22 KB
/
modify_annotation.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
import yaml
import argparse
import os
from yolo3.utils import get_classes, translate_classes
ap = argparse.ArgumentParser()
ap.add_argument("-g", "--config_path",
required=True,
default=None,
type=str,
help="The training configuration.")
ap.add_argument("-s", "--output_suffix",
required=True,
default=None,
type=str,
help="The suffix to be appended in the output annotation file.")
ap.add_argument("-t", "--run_class_translations",
required=False,
action="store_true",
help="Run class translations.")
ARGS = ap.parse_args()
train_config = None
with open(ARGS.config_path, 'r') as stream:
train_config = yaml.load(stream)
class_names = get_classes(train_config['classes_path'])
for annotation_path in [train_config['train_path'], train_config['test_path']]:
print('Checking annotation',annotation_path)
did_modify_something = False
output_annotation_path = annotation_path.replace('.txt', '_{}.txt'.format(ARGS.output_suffix))
if os.path.exists(output_annotation_path):
raise Exception('The output file already exists:',output_annotation_path)
with open(annotation_path) as f:
lines = f.readlines()
if ARGS.run_class_translations:
if 'class_translation_path' in train_config:
class_translation_config = None
with open(train_config['class_translation_path'], 'r') as stream:
class_translation_config = yaml.load(stream)
lines = translate_classes(lines,class_names,class_translation_config)
did_modify_something = True
else:
raise Exception('The configuration for class translation is not specified in the config file.')
if did_modify_something:
with open(output_annotation_path, 'w') as output_f:
print('Writting the modified annotation file to', output_annotation_path)
for annot_line in lines:
output_f.write(annot_line + '\n')
else:
print('You did not choose any modification to be applied')
print('Done! Now replace the train and test dataset paths with the new generated ones.')