Allow improve_contrast to be toggled via mqtt (#3011)

* Toggle improve_contrast for cameras via MQTT

* Process parameter to mqtt toggle improve_contrast

* Update mqtt docs for improve_contrast topic

* Spacing

* Add class variable and update in process_frames

* Pass to constructor

* pass by reference mistake

* remove parameter

* remove parameter
This commit is contained in:
Josh Hawkins 2022-04-16 08:52:02 -05:00 committed by GitHub
parent a5016afdd4
commit 65e0ec7826
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 56 additions and 10 deletions

View File

@ -117,3 +117,11 @@ Topic to turn snapshots for a camera on and off. Expected values are `ON` and `O
### `frigate/<camera_name>/snapshots/state`
Topic with current state of snapshots for a camera. Published values are `ON` and `OFF`.
### `frigate/<camera_name>/improve_contrast/set`
Topic to turn improve_contrast for a camera on and off. Expected values are `ON` and `OFF`.
### `frigate/<camera_name>/improve_contrast/state`
Topic with current state of improve_contrast for a camera. Published values are `ON` and `OFF`.

View File

@ -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),

View File

@ -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,6 +39,7 @@ class MotionDetector:
)
# Improve contrast
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

View File

@ -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

View File

@ -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
)