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

Commit 6f650f8

Browse files
authored
Merge pull request #204 from robmarkcole/fr-185
Adds always_save_latest_jpg
2 parents 463bde4 + 00fc465 commit 6f650f8

File tree

2 files changed

+14
-12
lines changed

2 files changed

+14
-12
lines changed

README.md

+2
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ image_processing:
3030
# confidence: 80
3131
save_file_folder: /config/snapshots/
3232
save_timestamped_file: True
33+
always_save_latest_jpg: True
3334
scale: 0.75
3435
# roi_x_min: 0.35
3536
roi_x_max: 0.8
@@ -54,6 +55,7 @@ Configuration variables:
5455
- **confidence**: (Optional) The confidence (in %) above which detected targets are counted in the sensor state. Default value: 80
5556
- **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/)
5657
- **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.
58+
- **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.
5759
- **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.
5860
- **show_boxes**: (optional, default `True`), if `False` bounding boxes are not shown on saved images
5961
- **roi_x_min**: (optional, default 0), range 0-1, must be less than roi_x_max

custom_components/deepstack_object/image_processing.py

+12-12
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@
7171
CONF_TIMEOUT = "timeout"
7272
CONF_SAVE_FILE_FOLDER = "save_file_folder"
7373
CONF_SAVE_TIMESTAMPTED_FILE = "save_timestamped_file"
74+
CONF_ALWAYS_SAVE_LATEST_JPG = "always_save_latest_jpg"
7475
CONF_SHOW_BOXES = "show_boxes"
7576
CONF_ROI_Y_MIN = "roi_y_min"
7677
CONF_ROI_X_MIN = "roi_x_min"
@@ -134,6 +135,7 @@
134135
),
135136
vol.Optional(CONF_SAVE_FILE_FOLDER): cv.isdir,
136137
vol.Optional(CONF_SAVE_TIMESTAMPTED_FILE, default=False): cv.boolean,
138+
vol.Optional(CONF_ALWAYS_SAVE_LATEST_JPG, default=False): cv.boolean,
137139
vol.Optional(CONF_SHOW_BOXES, default=True): cv.boolean,
138140
}
139141
)
@@ -232,6 +234,7 @@ def setup_platform(hass, config, add_devices, discovery_info=None):
232234
show_boxes=config[CONF_SHOW_BOXES],
233235
save_file_folder=save_file_folder,
234236
save_timestamped_file=config.get(CONF_SAVE_TIMESTAMPTED_FILE),
237+
always_save_latest_jpg=config.get(CONF_ALWAYS_SAVE_LATEST_JPG),
235238
camera_entity=camera.get(CONF_ENTITY_ID),
236239
name=camera.get(CONF_NAME),
237240
)
@@ -259,6 +262,7 @@ def __init__(
259262
show_boxes,
260263
save_file_folder,
261264
save_timestamped_file,
265+
always_save_latest_jpg,
262266
camera_entity,
263267
name=None,
264268
):
@@ -305,6 +309,7 @@ def __init__(
305309
self._image_height = None
306310
self._save_file_folder = save_file_folder
307311
self._save_timestamped_file = save_timestamped_file
312+
self._always_save_latest_jpg = always_save_latest_jpg
308313
self._image = None
309314

310315
def process_image(self, image):
@@ -364,11 +369,11 @@ def process_image(self, image):
364369
if self._state > 0:
365370
self._last_detection = dt_util.now().strftime(DATETIME_FORMAT)
366371

367-
if self._save_file_folder and self._state > 0:
368-
saved_image_path = self.save_image(
369-
self._targets_found,
370-
self._save_file_folder,
371-
)
372+
if self._save_file_folder:
373+
if self._state > 0 or self._always_save_latest_jpg:
374+
saved_image_path = self.save_image(
375+
self._targets_found, self._save_file_folder,
376+
)
372377

373378
# Fire events
374379
for target in self._targets_found:
@@ -415,8 +420,8 @@ def device_state_attributes(self) -> Dict:
415420
]
416421
if self._save_file_folder:
417422
attr[CONF_SAVE_FILE_FOLDER] = str(self._save_file_folder)
418-
if self._save_timestamped_file:
419423
attr[CONF_SAVE_TIMESTAMPTED_FILE] = self._save_timestamped_file
424+
attr[CONF_ALWAYS_SAVE_LATEST_JPG] = self._always_save_latest_jpg
420425
return attr
421426

422427
def save_image(self, targets, directory) -> str:
@@ -434,12 +439,7 @@ def save_image(self, targets, directory) -> str:
434439
roi_tuple = tuple(self._roi_dict.values())
435440
if roi_tuple != DEFAULT_ROI and self._show_boxes:
436441
draw_box(
437-
draw,
438-
roi_tuple,
439-
img.width,
440-
img.height,
441-
text="ROI",
442-
color=GREEN,
442+
draw, roi_tuple, img.width, img.height, text="ROI", color=GREEN,
443443
)
444444

445445
for obj in targets:

0 commit comments

Comments
 (0)