def fire_prediction_events(self, predictions_json, confidence):
        """Fire events based on predictions if above confidence threshold."""

        for prediction in predictions_json:
            if ds.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: ds.format_confidence(prediction["confidence"]),
                    },
                )
    def save_image(self, image, predictions_json, target, directory):
        """Save a timestamped image with bounding boxes around targets."""
        from PIL import Image, ImageDraw
        import io

        img = Image.open(io.BytesIO(bytearray(image))).convert("RGB")
        draw = ImageDraw.Draw(img)

        for prediction in predictions_json:
            prediction_confidence = ds.format_confidence(prediction["confidence"])
            if (
                prediction["label"] == target
                and prediction_confidence >= self._confidence
            ):
                draw_box(draw, prediction, str(prediction_confidence))

        now = datetime.datetime.now().strftime("%Y-%m-%d-%H-%M-%S")
        latest_save_path = directory + "deepstack_latest_{}.jpg".format(target)
        timestamp_save_path = directory + "deepstack_{}_{}.jpg".format(target, now)
        try:
            img.save(latest_save_path)
            img.save(timestamp_save_path)
            self.fire_saved_file_event(timestamp_save_path)
            _LOGGER.info("Saved bounding box image to %s", timestamp_save_path)
        except Exception as exc:
            _LOGGER.error("Error saving bounding box image : %s", exc)
    def process_image(self, image):
        """Process an image."""
        self._state = None
        self._targets_confidences = []
        self._predictions = {}
        try:
            self._dsobject.detect(image)
        except ds.DeepstackException as exc:
            _LOGGER.error("Depstack error : %s", exc)
            return

        predictions = self._dsobject.predictions.copy()

        if len(predictions) > 0:
            raw_confidences = ds.get_object_confidences(predictions, self._target)
            self._targets_confidences = [
                ds.format_confidence(confidence) for confidence in raw_confidences
            ]
            self._state = len(
                ds.get_confidences_above_threshold(
                    self._targets_confidences, self._confidence
                )
            )
            self._predictions = ds.get_objects_summary(predictions)
            self.fire_prediction_events(predictions, self._confidence)
            if hasattr(self, "_save_file_folder") and self._state > 0:
                self.save_image(
                    image, predictions, self._target, self._save_file_folder
                )
    def process_image(self, image):
        """Process an image."""
        self._image_width, self._image_height = Image.open(
            io.BytesIO(bytearray(image))).size
        self._state = None
        self._targets_confidences = []
        self._predictions = {}
        self._summary = {}
        try:
            self._dsobject.detect(image)
        except ds.DeepstackException as exc:
            _LOGGER.error("Depstack error : %s", exc)
            return

        self._predictions = self._dsobject.predictions.copy()

        if len(self._predictions) > 0:
            raw_confidences = ds.get_object_confidences(
                self._predictions, self._target)
            self._targets_confidences = [
                ds.format_confidence(confidence)
                for confidence in raw_confidences
            ]
            self._state = len(
                ds.get_confidences_above_threshold(self._targets_confidences,
                                                   self._confidence))
            if self._state > 0:
                self._last_detection = dt_util.now().strftime(DATETIME_FORMAT)
            self._summary = ds.get_objects_summary(self._predictions)
            self.fire_prediction_events(self._predictions, self._confidence)
            if hasattr(self, "_save_file_folder") and self._state > 0:
                self.save_image(image, self._predictions, self._target,
                                self._save_file_folder)
Example #5
0
    def save_image(self, image, predictions, target, directory):
        """Save a timestamped image with bounding boxes around targets."""

        img = Image.open(io.BytesIO(bytearray(image))).convert("RGB")
        draw = ImageDraw.Draw(img)

        for prediction in predictions:
            prediction_confidence = ds.format_confidence(
                prediction["confidence"])
            if (prediction["label"] == target
                    and prediction_confidence >= self._confidence):
                box = get_box(prediction, self._image_width,
                              self._image_height)
                draw_box(
                    draw,
                    box,
                    self._image_width,
                    self._image_height,
                    str(prediction_confidence),
                )

        latest_save_path = directory + "deepstack_latest_{}.jpg".format(target)
        timestamp_save_path = directory + "deepstack_{}_{}.jpg".format(
            target, self._last_detection.strftime("%Y-%m-%d-%H-%M-%S"))
        try:
            img.save(latest_save_path)
            img.save(timestamp_save_path)
            self.fire_saved_file_event(timestamp_save_path)
            _LOGGER.info("Saved bounding box image to %s", timestamp_save_path)
        except Exception as exc:
            _LOGGER.error("Error saving bounding box image : %s", exc)
Example #6
0
 def fire_prediction_events(self, predictions, confidence):
     """Fire events based on predictions if above confidence threshold."""
     for prediction in predictions:
         if ds.format_confidence(prediction["confidence"]) > confidence:
             box = get_box(prediction, self._image_width,
                           self._image_height)
             self.hass.bus.fire(
                 EVENT_OBJECT_DETECTED,
                 {
                     ATTR_ENTITY_ID:
                     self.entity_id,
                     OBJECT:
                     prediction["label"],
                     ATTR_CONFIDENCE:
                     ds.format_confidence(prediction["confidence"]),
                     BOX:
                     box,
                 },
             )
    def save_image(self, image, predictions, target, directory):
        """Save a timestamped image with bounding boxes around targets."""
        img = Image.open(io.BytesIO(bytearray(image))).convert("RGB")
        draw = ImageDraw.Draw(img)

        for prediction in predictions:
            prediction_confidence = ds.format_confidence(prediction["score"])
            if (prediction["name"] in target
                    and prediction_confidence >= self._confidence):
                draw_box(
                    draw,
                    prediction['box'],
                    self._image_width,
                    self._image_height,
                    text=str(prediction_confidence),
                    color=RED,
                )

        latest_save_path = directory + "{}_latest_{}.jpg".format(
            self._name, target[0])
        img.save(latest_save_path)
    def save_image(self, image, predictions, target, directory):
        """Save a timestamped image with bounding boxes around targets."""

        img = Image.open(io.BytesIO(bytearray(image))).convert("RGB")
        draw = ImageDraw.Draw(img)

        for prediction in predictions:
            prediction_confidence = ds.format_confidence(
                prediction["confidence"])
            if (prediction["label"] == target
                    and prediction_confidence >= self._confidence):
                box = get_box(prediction, self._image_width,
                              self._image_height)
                draw_box(
                    draw,
                    box,
                    self._image_width,
                    self._image_height,
                    text=str(prediction_confidence),
                    color=RED,
                )

        latest_save_path = directory + "{}_latest_{}.jpg".format(
            self._name, target)
        img.save(latest_save_path)

        if self._save_timestamped_file:
            timestamp_save_path = directory + "{}_{}_{}.jpg".format(
                self._name, target, self._last_detection)

            out_file = open(timestamp_save_path, "wb")
            img.save(out_file, format="JPEG")
            out_file.flush()
            os.fsync(out_file)
            out_file.close()
            self.fire_saved_file_event(timestamp_save_path)
            _LOGGER.info("Saved bounding box image to %s", timestamp_save_path)