diff --git a/docs/docs/integrations/mqtt.md b/docs/docs/integrations/mqtt.md index fbf6079da..57bf9a9fb 100644 --- a/docs/docs/integrations/mqtt.md +++ b/docs/docs/integrations/mqtt.md @@ -117,3 +117,11 @@ Topic to turn snapshots for a camera on and off. Expected values are `ON` and `O ### `frigate//snapshots/state` Topic with current state of snapshots for a camera. Published values are `ON` and `OFF`. + +### `frigate//improve_contrast/set` + +Topic to turn improve_contrast for a camera on and off. Expected values are `ON` and `OFF`. + +### `frigate//improve_contrast/state` + +Topic with current state of improve_contrast for a camera. Published values are `ON` and `OFF`. diff --git a/frigate/app.py b/frigate/app.py index 8f0aa36da..efea7d92c 100644 --- a/frigate/app.py +++ b/frigate/app.py @@ -92,6 +92,9 @@ class FrigateApp: "detection_enabled": mp.Value( "i", self.config.cameras[camera_name].detect.enabled ), + "improve_contrast_enabled": mp.Value( + "i", self.config.cameras[camera_name].motion.improve_contrast + ), "detection_fps": mp.Value("d", 0.0), "detection_frame": mp.Value("d", 0.0), "read_start": mp.Value("d", 0.0), diff --git a/frigate/motion.py b/frigate/motion.py index a3142b69b..093359ab0 100644 --- a/frigate/motion.py +++ b/frigate/motion.py @@ -5,7 +5,7 @@ from frigate.config import MotionConfig class MotionDetector: - def __init__(self, frame_shape, config: MotionConfig): + def __init__(self, frame_shape, config: MotionConfig, improve_contrast_enabled): self.config = config self.frame_shape = frame_shape self.resize_factor = frame_shape[0] / config.frame_height @@ -24,6 +24,7 @@ class MotionDetector: ) self.mask = np.where(resized_mask == [0]) self.save_images = False + self.improve_contrast = improve_contrast_enabled def detect(self, frame): motion_boxes = [] @@ -38,14 +39,15 @@ class MotionDetector: ) # Improve contrast - minval = np.percentile(resized_frame, 4) - maxval = np.percentile(resized_frame, 96) - # don't adjust if the image is a single color - if minval < maxval: - resized_frame = np.clip(resized_frame, minval, maxval) - resized_frame = ( - ((resized_frame - minval) / (maxval - minval)) * 255 - ).astype(np.uint8) + if self.improve_contrast.value: + minval = np.percentile(resized_frame, 4) + maxval = np.percentile(resized_frame, 96) + # don't adjust if the image is a single color + if minval < maxval: + resized_frame = np.clip(resized_frame, minval, maxval) + resized_frame = ( + ((resized_frame - minval) / (maxval - minval)) * 255 + ).astype(np.uint8) # mask frame resized_frame[self.mask] = [255] diff --git a/frigate/mqtt.py b/frigate/mqtt.py index 8f01f8604..f73536801 100644 --- a/frigate/mqtt.py +++ b/frigate/mqtt.py @@ -89,6 +89,30 @@ def create_mqtt_client(config: FrigateConfig, camera_metrics): state_topic = f"{message.topic[:-4]}/state" client.publish(state_topic, payload, retain=True) + def on_improve_contrast_command(client, userdata, message): + payload = message.payload.decode() + logger.debug(f"on_improve_contrast_toggle: {message.topic} {payload}") + + camera_name = message.topic.split("/")[-3] + + motion_settings = config.cameras[camera_name].motion + + if payload == "ON": + if not camera_metrics[camera_name]["improve_contrast_enabled"].value: + logger.info(f"Turning on improve contrast for {camera_name} via mqtt") + camera_metrics[camera_name]["improve_contrast_enabled"].value = True + motion_settings.improve_contrast = True + elif payload == "OFF": + if camera_metrics[camera_name]["improve_contrast_enabled"].value: + logger.info(f"Turning off improve contrast for {camera_name} via mqtt") + camera_metrics[camera_name]["improve_contrast_enabled"].value = False + motion_settings.improve_contrast = False + else: + logger.warning(f"Received unsupported value at {message.topic}: {payload}") + + state_topic = f"{message.topic[:-4]}/state" + client.publish(state_topic, payload, retain=True) + def on_restart_command(client, userdata, message): restart_frigate() @@ -128,6 +152,9 @@ def create_mqtt_client(config: FrigateConfig, camera_metrics): client.message_callback_add( f"{mqtt_config.topic_prefix}/{name}/detect/set", on_detect_command ) + client.message_callback_add( + f"{mqtt_config.topic_prefix}/{name}/improve_contrast/set", on_improve_contrast_command + ) client.message_callback_add( f"{mqtt_config.topic_prefix}/restart", on_restart_command @@ -173,6 +200,11 @@ def create_mqtt_client(config: FrigateConfig, camera_metrics): "ON" if config.cameras[name].detect.enabled else "OFF", retain=True, ) + client.publish( + f"{mqtt_config.topic_prefix}/{name}/improve_contrast/state", + "ON" if config.cameras[name].motion.improve_contrast else "OFF", + retain=True, + ) return client diff --git a/frigate/video.py b/frigate/video.py index 93639c72f..2ea1ae1a0 100755 --- a/frigate/video.py +++ b/frigate/video.py @@ -362,12 +362,13 @@ def track_camera( frame_queue = process_info["frame_queue"] detection_enabled = process_info["detection_enabled"] + improve_contrast_enabled = process_info["improve_contrast_enabled"] frame_shape = config.frame_shape objects_to_track = config.objects.track object_filters = config.objects.filters - motion_detector = MotionDetector(frame_shape, config.motion) + motion_detector = MotionDetector(frame_shape, config.motion, improve_contrast_enabled) object_detector = RemoteObjectDetector( name, labelmap, detection_queue, result_connection, model_shape )