-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdetect.py
225 lines (174 loc) · 7.61 KB
/
detect.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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
import argparse
import os
import sys
from glob import glob
import yaml
from Detector import Detector
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2'
class Range(object):
""" Enables argparse to check ranges
Source: https://stackoverflow.com/a/12117089/9943279
"""
def __init__(self, start, end):
self.start = start
self.end = end
def __eq__(self, other):
return self.start <= other <= self.end
def __repr__(self):
return '{0}-{1}'.format(self.start, self.end)
def assert_folder(paths):
""" Asserts every path in paths is a folder.
:param paths: str or list
"""
if isinstance(paths, list):
for path in paths:
assert os.path.isdir(path), 'Folder not found. \'{}\''.format(path)
else:
assert os.path.isdir(paths), 'Folder not found. \'{}\''.format(paths)
def assert_file(file_paths):
""" Asserts every path in file_paths is a file.
:param file_paths: str or list
"""
if isinstance(file_paths, list):
for file_path in file_paths:
assert os.path.isfile(file_path), 'File not found. \'{}\''.format(file_path)
else:
assert os.path.isfile(file_paths), 'File not found. \'{}\''.format(file_paths)
def check_config(config):
""" Check settings from config.yaml
"""
# check settings
assert isinstance(config['settings']['queue_size'], int), \
'Queue size must be integer.'
assert 0 < config['settings']['queue_size'], \
'Queue size must be larger than 0.'
assert isinstance(config['settings']['num_workers'], int), \
'FPS step size must be integer.'
assert 0 < config['settings']['num_workers'], \
'FPS step size must be larger than 0.'
assert isinstance(config['settings']['batch_size'], int), \
'Batch size must be integer.'
assert 0 < config['settings']['batch_size'], \
'Batch size must be larger than 0.'
# check data
assert isinstance(config['data']['fps_step'], int), \
'FPS step size must be integer.'
assert 0 < config['data']['fps_step'], \
'FPS step size must be larger than 0.'
assert_file(config['data']['label_map'])
assert isinstance(config['data']['target_sample_rate'], int), \
'Target sample rate must be integer.'
assert 0 < config['data']['target_sample_rate'], \
'Target sample rate must be larger than 0.'
assert isinstance(config['data']['relevant_future'], int), \
'Relevant future must be integer.'
assert 0 < config['data']['relevant_future'], \
'Relevant future must be larger than 0.'
# check road_condition
assert_file(config['road_condition']['graph_path'])
assert isinstance(config['road_condition']['thresh'], float), \
'Threshold must be float.'
assert 0.0 < config['road_condition']['thresh'] <= 1.0, \
'Threshold must be between 0.0 and 1.0.'
assert_file(config['road_condition']['label_map'])
assert isinstance(config['road_condition']['num_classes'], int), \
'Number of classes must be integer.'
assert 0 < config['road_condition']['num_classes'], \
'Number of classes must be larger than 0.'
assert isinstance(config['road_condition']['gpu_id'], int), \
'GPU ID must be integer.'
# check object_detection
# TODO check for multiple frameworks
assert isinstance(config['object_detection']['gpu_id'], int), \
'GPU ID must be integer.'
# check tensorflow
assert_file(config['object_detection']['tensorflow']['graph_path'])
assert config['object_detection']['tensorflow']['input_type'] in ['image_tensor', 'tf_example'], \
'Input type must be \'image_tensor\' or \'tf_example\'.'
assert_file(config['object_detection']['tensorflow']['label_map'])
assert isinstance(config['object_detection']['tensorflow']['thresh'], float), \
'Threshold must be float.'
assert 0.0 < config['object_detection']['tensorflow']['thresh'] <= 1.0, \
'Threshold must be between 0.0 and 1.0.'
assert isinstance(config['object_detection']['tensorflow']['max_class_id'], int), \
'Max number of classes must be integer.'
assert 0 < config['object_detection']['tensorflow']['max_class_id'], \
'Max number of classes must be larger than 0.'
def check_args(args):
"""
Checks if args are valid.
:return: true if args vaild, else false
"""
'''Argument groups'''
# Visualization group
if args.visualize:
assert args.thresh_vis <= 0 or args.thresh_vis >= 1, 'Threshold must be between 0 and 1.'
# Dataset group
assert_folder(args.data_path)
if args.videos == 'one':
assert_file(os.path.join(args.data_path, args.fname))
elif args.videos == 'list':
assert_file([os.path.join(args.data_path, fname) for fname in args.list])
elif args.videos == 'all':
video_files = glob(os.path.join(args.data_path, '*' + args.file_type))
assert_file([os.path.join(args.data_path, fname) for fname in video_files])
return args
def parse_args(args):
""" Parse the arguments.
"""
# Create parser, subparser and groups
parser = argparse.ArgumentParser(prog='Object Detection Evaluator',
formatter_class=argparse.ArgumentDefaultsHelpFormatter)
# Parser group for visualizations
vis_parser = parser.add_argument_group('Visualize settings')
vis_parser.add_argument('--visualize',
help='Show detections in video.',
action='store_const',
const=True,
default=False)
vis_parser.add_argument('--thresh_vis',
help='Minimum score threshold for a box to be visualized.',
type=float,
default=0.6,
choices=[Range(0.0, 1.0)])
# Parser group for dataset
ds_parser = parser.add_argument_group('Data settings')
ds_parser.add_argument('--data-path',
help='Path of the data folder.',
type=str,
required=True)
ds_parser.add_argument('--videos',
help='Define which video(s) should be evaluated.\n'
'\tFor \'one\' set \'--fname\'.\n'
'\tFor \'list\' set \'--list\'.\n'
'\tFor \'all\' uses all videos in given folder with \'--file-type\'.',
type=str,
choices=['one', 'list', 'all'],
default='one')
ds_parser.add_argument('--fname',
help='Filename of the video.',
type=str)
ds_parser.add_argument('--list',
help='List of filenames of videos.',
type=str,
nargs='*')
ds_parser.add_argument('--file-type',
help='File type of the videos.',
type=str,
choices=['avi', 'mp4'],
default='avi')
return check_args(parser.parse_args(args))
def main(args=None):
# Parse arguments
if args is None:
args = sys.argv[1:]
args = parse_args(args)
# Assert config file exists
assert os.path.isfile("./config.yaml"), 'Config file not found. Please do not rename or move!'
# Check config
with open("./config.yaml", 'r') as f:
check_config(yaml.safe_load(f))
detector = Detector(args)
detector.run()
if __name__ == '__main__':
main()