From 00fc465b3a9084d5e2ca9744331f6bbd921dddf8 Mon Sep 17 00:00:00 2001 From: Robin Cole Date: Fri, 22 Jan 2021 05:06:51 +0000 Subject: [PATCH] Adds always_save_latest_jpg --- README.md | 2 ++ .../deepstack_object/image_processing.py | 24 +++++++++---------- 2 files changed, 14 insertions(+), 12 deletions(-) diff --git a/README.md b/README.md index 3b60913..36ed26e 100644 --- a/README.md +++ b/README.md @@ -30,6 +30,7 @@ image_processing: # confidence: 80 save_file_folder: /config/snapshots/ save_timestamped_file: True + always_save_latest_jpg: True scale: 0.75 # roi_x_min: 0.35 roi_x_max: 0.8 @@ -54,6 +55,7 @@ Configuration variables: - **confidence**: (Optional) The confidence (in %) above which detected targets are counted in the sensor state. Default value: 80 - **save_file_folder**: (Optional) The folder to save processed images to. Note that folder path should be added to [whitelist_external_dirs](https://www.home-assistant.io/docs/configuration/basic/) - **save_timestamped_file**: (Optional, default `False`, requires `save_file_folder` to be configured) Save the processed image with the time of detection in the filename. +- **always_save_latest_jpg**: (Optional, default `False`, requires `save_file_folder` to be configured) Always save the last processed image, even if there were no detections. - **scale**: (optional, default 1.0), range 0.1-1.0, applies a scaling factor to the images that are saved. This reduces the disk space used by saved images, and is especially beneficial when using high resolution cameras. - **show_boxes**: (optional, default `True`), if `False` bounding boxes are not shown on saved images - **roi_x_min**: (optional, default 0), range 0-1, must be less than roi_x_max diff --git a/custom_components/deepstack_object/image_processing.py b/custom_components/deepstack_object/image_processing.py index b233150..4da55d2 100644 --- a/custom_components/deepstack_object/image_processing.py +++ b/custom_components/deepstack_object/image_processing.py @@ -71,6 +71,7 @@ CONF_TIMEOUT = "timeout" CONF_SAVE_FILE_FOLDER = "save_file_folder" CONF_SAVE_TIMESTAMPTED_FILE = "save_timestamped_file" +CONF_ALWAYS_SAVE_LATEST_JPG = "always_save_latest_jpg" CONF_SHOW_BOXES = "show_boxes" CONF_ROI_Y_MIN = "roi_y_min" CONF_ROI_X_MIN = "roi_x_min" @@ -134,6 +135,7 @@ ), vol.Optional(CONF_SAVE_FILE_FOLDER): cv.isdir, vol.Optional(CONF_SAVE_TIMESTAMPTED_FILE, default=False): cv.boolean, + vol.Optional(CONF_ALWAYS_SAVE_LATEST_JPG, default=False): cv.boolean, vol.Optional(CONF_SHOW_BOXES, default=True): cv.boolean, } ) @@ -232,6 +234,7 @@ def setup_platform(hass, config, add_devices, discovery_info=None): show_boxes=config[CONF_SHOW_BOXES], save_file_folder=save_file_folder, save_timestamped_file=config.get(CONF_SAVE_TIMESTAMPTED_FILE), + always_save_latest_jpg=config.get(CONF_ALWAYS_SAVE_LATEST_JPG), camera_entity=camera.get(CONF_ENTITY_ID), name=camera.get(CONF_NAME), ) @@ -259,6 +262,7 @@ def __init__( show_boxes, save_file_folder, save_timestamped_file, + always_save_latest_jpg, camera_entity, name=None, ): @@ -305,6 +309,7 @@ def __init__( self._image_height = None self._save_file_folder = save_file_folder self._save_timestamped_file = save_timestamped_file + self._always_save_latest_jpg = always_save_latest_jpg self._image = None def process_image(self, image): @@ -364,11 +369,11 @@ def process_image(self, image): if self._state > 0: self._last_detection = dt_util.now().strftime(DATETIME_FORMAT) - if self._save_file_folder and self._state > 0: - saved_image_path = self.save_image( - self._targets_found, - self._save_file_folder, - ) + if self._save_file_folder: + if self._state > 0 or self._always_save_latest_jpg: + saved_image_path = self.save_image( + self._targets_found, self._save_file_folder, + ) # Fire events for target in self._targets_found: @@ -415,8 +420,8 @@ def device_state_attributes(self) -> Dict: ] if self._save_file_folder: attr[CONF_SAVE_FILE_FOLDER] = str(self._save_file_folder) - if self._save_timestamped_file: attr[CONF_SAVE_TIMESTAMPTED_FILE] = self._save_timestamped_file + attr[CONF_ALWAYS_SAVE_LATEST_JPG] = self._always_save_latest_jpg return attr def save_image(self, targets, directory) -> str: @@ -434,12 +439,7 @@ def save_image(self, targets, directory) -> str: roi_tuple = tuple(self._roi_dict.values()) if roi_tuple != DEFAULT_ROI and self._show_boxes: draw_box( - draw, - roi_tuple, - img.width, - img.height, - text="ROI", - color=GREEN, + draw, roi_tuple, img.width, img.height, text="ROI", color=GREEN, ) for obj in targets: