From 42a9cd1c79c236b127d022130bbb7be8ae821e7a Mon Sep 17 00:00:00 2001 From: Robin Cole Date: Mon, 26 Aug 2019 07:50:30 +0100 Subject: [PATCH] Filter events by confidence Events are only fired for detections above the configured confidence threshold, to prevent many events for low confidence detections --- README.md | 2 +- .../deepstack_object/image_processing.py | 23 ++++++++++--------- 2 files changed, 13 insertions(+), 12 deletions(-) diff --git a/README.md b/README.md index 36357eb..b1ca748 100644 --- a/README.md +++ b/README.md @@ -67,7 +67,7 @@ Configuration variables:

#### Event `image_processing.object_detected` -An event `image_processing.object_detected` is fired for each object detected. An example use case for this is incrementing a [counter](https://www.home-assistant.io/components/counter/) when a person is detected. The `image_processing.object_detected` event payload includes: +An event `image_processing.object_detected` is fired for each object detected above the configured `confidence` threshold. An example use case for this is incrementing a [counter](https://www.home-assistant.io/components/counter/) when a person is detected. The `image_processing.object_detected` event payload includes: - `classifier` : the classifier (i.e. `deepstack_object`) - `entity_id` : the entity id responsible for the event diff --git a/custom_components/deepstack_object/image_processing.py b/custom_components/deepstack_object/image_processing.py index a7c5419..cacd887 100644 --- a/custom_components/deepstack_object/image_processing.py +++ b/custom_components/deepstack_object/image_processing.py @@ -161,7 +161,7 @@ def process_image(self, image): get_confidences_above_threshold( self._targets_confidences, self._confidence)) self._predictions = get_objects_summary(predictions_json) - self.fire_prediction_events(predictions_json) + self.fire_prediction_events(predictions_json, self._confidence) if hasattr(self, "_save_file_folder") and self._state > 0: self.save_image( image, predictions_json, self._target, self._save_file_folder) @@ -194,18 +194,19 @@ def save_image(self, image, predictions_json, target, directory): except Exception as exc: _LOGGER.error("Error saving bounding box image : %s", exc) - def fire_prediction_events(self, predictions_json): - """Fire events based on predictions""" + def fire_prediction_events(self, predictions_json, confidence): + """Fire events based on predictions if above confidence threshold.""" for prediction in predictions_json: - self.hass.bus.fire( - EVENT_OBJECT_DETECTED, { - 'classifier': CLASSIFIER, - ATTR_ENTITY_ID: self.entity_id, - OBJECT: prediction['label'], - ATTR_CONFIDENCE: format_confidence( - prediction['confidence']) - }) + if format_confidence(prediction['confidence']) > confidence: + self.hass.bus.fire( + EVENT_OBJECT_DETECTED, { + 'classifier': CLASSIFIER, + ATTR_ENTITY_ID: self.entity_id, + OBJECT: prediction['label'], + ATTR_CONFIDENCE: format_confidence( + prediction['confidence']) + }) def fire_saved_file_event(self, save_path): """Fire event when saving a file"""