Allow changing object detection via MQTT.

This commit is contained in:
Manuel Klimek 2025-08-15 10:57:39 +02:00
parent c2f8de94e8
commit 6a2a4bd79d
3 changed files with 41 additions and 12 deletions

View File

@ -68,6 +68,8 @@ class Dispatcher:
"birdseye_mode": self._on_birdseye_mode_command, "birdseye_mode": self._on_birdseye_mode_command,
"review_alerts": self._on_alerts_command, "review_alerts": self._on_alerts_command,
"review_detections": self._on_detections_command, "review_detections": self._on_detections_command,
"object_detection_enable": self._on_object_detection_enable_command,
"object_detection_disable": self._on_object_detection_disable_command,
} }
self._global_settings_handlers: dict[str, Callable] = { self._global_settings_handlers: dict[str, Callable] = {
"notifications": self._on_global_notification_command, "notifications": self._on_global_notification_command,
@ -641,3 +643,27 @@ class Dispatcher:
self.config_updater.publish(f"config/review/{camera_name}", review_settings) self.config_updater.publish(f"config/review/{camera_name}", review_settings)
self.publish(f"{camera_name}/review_detections/state", payload, retain=True) self.publish(f"{camera_name}/review_detections/state", payload, retain=True)
def _on_object_detection_enable_command(
self, camera_name: str, object_name: str
) -> None:
"""Callback for object detect topic."""
objects_settings = self.config.cameras[camera_name].objects
if object_name not in objects_settings.track:
logger.info(f"Turning on detection for {object_name} on {camera_name}")
objects_settings.track.append(object_name)
self.config_updater.publish(f"config/objects/{camera_name}", objects_settings)
def _on_object_detection_disable_command(
self, camera_name: str, object_name: str
) -> None:
"""Callback for object detect topic."""
objects_settings = self.config.cameras[camera_name].objects
if object_name in objects_settings.track:
logger.info(f"Turning off detection for {object_name} on {camera_name}")
objects_settings.track.remove(object_name)
self.config_updater.publish(f"config/objects/{camera_name}", objects_settings)

View File

@ -197,9 +197,7 @@ class MqttClient(Communicator): # type: ignore[misc]
payload="offline", payload="offline",
qos=1, qos=1,
retain=True, retain=True,
) ) # register callbacks
# register callbacks
callback_types = [ callback_types = [
"enabled", "enabled",
"recordings", "recordings",
@ -215,6 +213,8 @@ class MqttClient(Communicator): # type: ignore[misc]
"birdseye_mode", "birdseye_mode",
"review_alerts", "review_alerts",
"review_detections", "review_detections",
"object_detection_enable",
"object_detection_disable",
] ]
for name in self.config.cameras.keys(): for name in self.config.cameras.keys():

View File

@ -17,7 +17,7 @@ from setproctitle import setproctitle
from frigate.camera import CameraMetrics, PTZMetrics from frigate.camera import CameraMetrics, PTZMetrics
from frigate.comms.config_updater import ConfigSubscriber from frigate.comms.config_updater import ConfigSubscriber
from frigate.comms.inter_process import InterProcessRequestor from frigate.comms.inter_process import InterProcessRequestor
from frigate.config import CameraConfig, DetectConfig, ModelConfig from frigate.config import CameraConfig, DetectConfig, ModelConfig, ObjectConfig
from frigate.config.camera.camera import CameraTypeEnum from frigate.config.camera.camera import CameraTypeEnum
from frigate.const import ( from frigate.const import (
CACHE_DIR, CACHE_DIR,
@ -508,8 +508,6 @@ def track_camera(
frame_queue = camera_metrics.frame_queue frame_queue = camera_metrics.frame_queue
frame_shape = config.frame_shape frame_shape = config.frame_shape
objects_to_track = config.objects.track
object_filters = config.objects.filters
motion_detector = ImprovedMotionDetector( motion_detector = ImprovedMotionDetector(
frame_shape, frame_shape,
@ -543,8 +541,7 @@ def track_camera(
object_tracker, object_tracker,
detected_objects_queue, detected_objects_queue,
camera_metrics, camera_metrics,
objects_to_track, config.objects,
object_filters,
stop_event, stop_event,
ptz_metrics, ptz_metrics,
region_grid, region_grid,
@ -610,8 +607,7 @@ def process_frames(
object_tracker: ObjectTracker, object_tracker: ObjectTracker,
detected_objects_queue: Queue, detected_objects_queue: Queue,
camera_metrics: CameraMetrics, camera_metrics: CameraMetrics,
objects_to_track: list[str], objects_config: ObjectConfig,
object_filters,
stop_event: MpEvent, stop_event: MpEvent,
ptz_metrics: PTZMetrics, ptz_metrics: PTZMetrics,
region_grid: list[list[dict[str, Any]]], region_grid: list[list[dict[str, Any]]],
@ -619,6 +615,7 @@ def process_frames(
): ):
next_region_update = get_tomorrow_at_time(2) next_region_update = get_tomorrow_at_time(2)
detect_config_subscriber = ConfigSubscriber(f"config/detect/{camera_name}", True) detect_config_subscriber = ConfigSubscriber(f"config/detect/{camera_name}", True)
objects_config_subscriber = ConfigSubscriber(f"config/objects/{camera_name}", True)
enabled_config_subscriber = ConfigSubscriber(f"config/enabled/{camera_name}", True) enabled_config_subscriber = ConfigSubscriber(f"config/enabled/{camera_name}", True)
fps_tracker = EventsPerSecond() fps_tracker = EventsPerSecond()
@ -692,6 +689,12 @@ def process_frames(
if updated_detect_config: if updated_detect_config:
detect_config = updated_detect_config detect_config = updated_detect_config
# check for updated objects config
_, updated_objects_config = objects_config_subscriber.check_for_update()
if updated_objects_config:
objects_config = updated_objects_config
if ( if (
datetime.datetime.now().astimezone(datetime.timezone.utc) datetime.datetime.now().astimezone(datetime.timezone.utc)
> next_region_update > next_region_update
@ -836,8 +839,8 @@ def process_frames(
frame, frame,
model_config, model_config,
region, region,
objects_to_track, objects_config.track,
object_filters, objects_config.filters,
) )
) )