-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathdeepdetector.py
124 lines (92 loc) · 4.22 KB
/
deepdetector.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
import sys
sys.path.append('/root/darkflow')
from net.build import TFNet
from basecomponent import BaseComponent
from annotator import annotate
class DeepDetector(BaseComponent):
'''
A DeepDetector uses a YOLOv2 convolutional neural network model for
object detection.
'''
def __init__(self, cfg):
BaseComponent.__init__(self, cfg)
params = self.cfg['params']
tfnet_cfg = {
"model": params['model'],
"load": params['weights'],
"config" : '/root/darkflow/cfg',
"verbalise" : True,
"threshold": 0.1
}
self.nn = TFNet(tfnet_cfg)
def execute(self, input_data, input_directory, output_directory):
# Check what configured inputs are - whether complete image or ROIs output by some
# other components.
all_detections = []
for source in self.cfg['inputs']:
if source == 'files':
detections = self.detect_in_image(input_data)
all_detections.extend(detections)
else:
triggerlabels = self.cfg['params'].get('triggerlabels')
if not triggerlabels:
print("Warning: pipeline file specifies {} in inputs but there are no triggerlabels in params".format(source))
continue
comp_outputs = input_data.get(source)
if comp_outputs:
comp_reports = comp_outputs['reports']
detections = self.detect_in_rois(input_data, comp_reports)
all_detections.extend(detections)
# Each detection is of the form
# {"label":"person", "confidence": 0.56, "topleft": {"x": 184, "y": 101}, "bottomright": {"x": 274, "y": 382}}
# These should be transformed to our preferred JSON output documented in basecomponent.py
reports = []
for d in all_detections:
r = {
'labels' : [
{
'label' : d['label'],
# The float() here is because that confidence value is actually a np.float32
# and that creates serialization typeerror problems while writing report to
# json.
'confidence' : float(d['confidence'])
}
],
'rect' : [
d['topleft']['x'],
d['topleft']['y'],
d['bottomright']['x'],
d['bottomright']['y'],
]
}
reports.append(r)
results = {
'reports' : reports
}
print(results)
return results
def detect_in_image(self, input_data):
print("Deep detector starting " + input_data['file'])
detections = self.nn.return_predict(input_data['img'])
print("Deep detector completed" + input_data['file'])
return detections
def detect_in_rois(self, input_data, comp_reports):
img = input_data['img']
roi_detections = []
for r in comp_reports:
if ('all' in self.cfg['params']['triggerlabels']) or \
any( [ l['label'] in self.cfg['params']['triggerlabels'] for l in r['labels'] ] ) :
rect = r['rect']
x_offset = rect[0]
y_offset = rect[1]
roi = img[ rect[1]:rect[3], rect[0]:rect[2], :]
detections = self.nn.return_predict(roi)
# These detections in ROI are relative to ROI. So we must add ROI origin to
# those coordinates to make them full image coordinates.
for d in detections:
d['topleft']['x'] += x_offset
d['bottomright']['x'] += x_offset
d['topleft']['y'] += y_offset
d['bottomright']['y'] += y_offset
roi_detections.extend(detections)
return roi_detections