Skip to content
This repository has been archived by the owner on Mar 19, 2023. It is now read-only.

Commit

Permalink
Merge pull request #204 from robmarkcole/fr-185
Browse files Browse the repository at this point in the history
Adds always_save_latest_jpg
  • Loading branch information
robmarkcole committed Jan 22, 2021
2 parents 463bde4 + 00fc465 commit 6f650f8
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 12 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
24 changes: 12 additions & 12 deletions custom_components/deepstack_object/image_processing.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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,
}
)
Expand Down Expand Up @@ -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),
)
Expand Down Expand Up @@ -259,6 +262,7 @@ def __init__(
show_boxes,
save_file_folder,
save_timestamped_file,
always_save_latest_jpg,
camera_entity,
name=None,
):
Expand Down Expand Up @@ -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):
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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:
Expand All @@ -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:
Expand Down

0 comments on commit 6f650f8

Please sign in to comment.