|
| 1 | +from net.build import TFNet |
| 2 | +from basecomponent import BaseComponent |
| 3 | +from annotator import annotate |
| 4 | + |
| 5 | +class DeepDetector(BaseComponent): |
| 6 | + ''' |
| 7 | + A DeepDetector uses a YOLOv2 convolutional neural network model for |
| 8 | + object detection. |
| 9 | + ''' |
| 10 | + |
| 11 | + def __init__(self, cfg): |
| 12 | + BaseComponent.__init__(self, cfg) |
| 13 | + |
| 14 | + params = self.cfg['params'] |
| 15 | + |
| 16 | + tfnet_cfg = { |
| 17 | + "model": params['model'], |
| 18 | + "load": params['weights'], |
| 19 | + "config" : '/root/darkflow/cfg', |
| 20 | + "verbalise" : True, |
| 21 | + "threshold": 0.1 |
| 22 | + } |
| 23 | + |
| 24 | + self.nn = TFNet(tfnet_cfg) |
| 25 | + |
| 26 | + |
| 27 | + def execute(self, input_data, input_directory, output_directory): |
| 28 | + |
| 29 | + # Check what configured inputs are - whether complete image or ROIs output by some |
| 30 | + # other components. |
| 31 | + all_detections = [] |
| 32 | + for source in self.cfg['inputs']: |
| 33 | + if source == 'files': |
| 34 | + detections = self.detect_in_image(input_data) |
| 35 | + all_detections.extend(detections) |
| 36 | + |
| 37 | + else: |
| 38 | + triggerlabels = self.cfg['params'].get('triggerlabels') |
| 39 | + if not triggerlabels: |
| 40 | + print("Warning: pipeline file specifies {} in inputs but there are no triggerlabels in params".format(source)) |
| 41 | + continue |
| 42 | + |
| 43 | + comp_outputs = input_data.get(source) |
| 44 | + if comp_outputs: |
| 45 | + comp_reports = comp_outputs['reports'] |
| 46 | + detections = self.detect_in_rois(self, input_data, comp_reports) |
| 47 | + all_detections.extend(detections) |
| 48 | + |
| 49 | + # Each detection is of the form |
| 50 | + # {"label":"person", "confidence": 0.56, "topleft": {"x": 184, "y": 101}, "bottomright": {"x": 274, "y": 382}} |
| 51 | + # These should be transformed to our preferred JSON output documented in basecomponent.py |
| 52 | + |
| 53 | + reports = [] |
| 54 | + for d in all_detections: |
| 55 | + r = { |
| 56 | + 'labels' : [ |
| 57 | + { |
| 58 | + 'label' : d['label'], |
| 59 | + # The float() here is because that confidence value is actually a np.float32 |
| 60 | + # and that creates serialization typeerror problems while writing report to |
| 61 | + # json. |
| 62 | + 'confidence' : float(d['confidence']) |
| 63 | + } |
| 64 | + ], |
| 65 | + 'rect' : [ |
| 66 | + d['topleft']['x'], |
| 67 | + d['topleft']['y'], |
| 68 | + d['bottomright']['x'], |
| 69 | + d['bottomright']['y'], |
| 70 | + ] |
| 71 | + } |
| 72 | + |
| 73 | + reports.append(r) |
| 74 | + |
| 75 | + results = { |
| 76 | + 'reports' : reports |
| 77 | + } |
| 78 | + |
| 79 | + |
| 80 | + print(results) |
| 81 | + return results |
| 82 | + |
| 83 | + |
| 84 | + |
| 85 | + |
| 86 | + def detect_in_image(self, input_data): |
| 87 | + detections = self.nn.return_predict(input_data['img']) |
| 88 | + return detections |
| 89 | + |
| 90 | + |
| 91 | + |
| 92 | + |
| 93 | + def detect_in_rois(self, input_data, comp_reports): |
| 94 | + img = input_data['img'] |
| 95 | + roi_detections = [] |
| 96 | + |
| 97 | + for r in comp_reports: |
| 98 | + |
| 99 | + if ('all' in self.cfg['params']['triggerlabels']) or \ |
| 100 | + any( [ l['label'] in self.cfg['params']['triggerlabels'] for l in r['labels'] ] ) : |
| 101 | + |
| 102 | + rect = r['rect'] |
| 103 | + x_offset = rect[0] |
| 104 | + y_offset = rect[1] |
| 105 | + roi = img[ rect[1]:rect[3], rect[0]:rect[2], :] |
| 106 | + |
| 107 | + detections = self.nn.return_predict(roi) |
| 108 | + # These detections in ROI are relative to ROI. So we must add ROI origin to |
| 109 | + # those coordinates to make them full image coordinates. |
| 110 | + for d in detections: |
| 111 | + d['topleft']['x'] += x_offset |
| 112 | + d['bottomright']['x'] += x_offset |
| 113 | + |
| 114 | + d['topleft']['y'] += y_offset |
| 115 | + d['bottomright']['y'] += y_offset |
| 116 | + |
| 117 | + roi_detections.extend(detections) |
| 118 | + |
| 119 | + return roi_detections |
0 commit comments