mirror of
https://github.com/blakeblackshear/frigate.git
synced 2026-02-19 09:27:06 +03:00
backend
This commit is contained in:
parent
1b7fe9523d
commit
f31b8bf60a
@ -80,6 +80,8 @@ class Dispatcher:
|
|||||||
"snapshots": self._on_snapshots_command,
|
"snapshots": self._on_snapshots_command,
|
||||||
"birdseye": self._on_birdseye_command,
|
"birdseye": self._on_birdseye_command,
|
||||||
"birdseye_mode": self._on_birdseye_mode_command,
|
"birdseye_mode": self._on_birdseye_mode_command,
|
||||||
|
"alerts": self._on_alerts_command,
|
||||||
|
"detections": self._on_detections_command,
|
||||||
}
|
}
|
||||||
self._global_settings_handlers: dict[str, Callable] = {
|
self._global_settings_handlers: dict[str, Callable] = {
|
||||||
"notifications": self._on_notification_command,
|
"notifications": self._on_notification_command,
|
||||||
@ -182,6 +184,8 @@ class Dispatcher:
|
|||||||
"autotracking": self.config.cameras[
|
"autotracking": self.config.cameras[
|
||||||
camera
|
camera
|
||||||
].onvif.autotracking.enabled,
|
].onvif.autotracking.enabled,
|
||||||
|
"alerts": self.config.cameras[camera].review.alerts.enabled,
|
||||||
|
"detections": self.config.cameras[camera].review.detections.enabled,
|
||||||
}
|
}
|
||||||
|
|
||||||
self.publish("camera_activity", json.dumps(camera_status))
|
self.publish("camera_activity", json.dumps(camera_status))
|
||||||
@ -490,3 +494,47 @@ class Dispatcher:
|
|||||||
|
|
||||||
self.config_updater.publish(f"config/birdseye/{camera_name}", birdseye_settings)
|
self.config_updater.publish(f"config/birdseye/{camera_name}", birdseye_settings)
|
||||||
self.publish(f"{camera_name}/birdseye_mode/state", payload, retain=True)
|
self.publish(f"{camera_name}/birdseye_mode/state", payload, retain=True)
|
||||||
|
|
||||||
|
def _on_alerts_command(self, camera_name: str, payload: str) -> None:
|
||||||
|
"""Callback for alerts topic."""
|
||||||
|
review_settings = self.config.cameras[camera_name].review
|
||||||
|
|
||||||
|
if payload == "ON":
|
||||||
|
if not self.config.cameras[camera_name].review.alerts.enabled_in_config:
|
||||||
|
logger.error(
|
||||||
|
"Alerts must be enabled in the config to be turned on via MQTT."
|
||||||
|
)
|
||||||
|
return
|
||||||
|
|
||||||
|
if not review_settings.alerts.enabled:
|
||||||
|
logger.info(f"Turning on alerts for {camera_name}")
|
||||||
|
review_settings.alerts.enabled = True
|
||||||
|
elif payload == "OFF":
|
||||||
|
if review_settings.alerts.enabled:
|
||||||
|
logger.info(f"Turning off alerts for {camera_name}")
|
||||||
|
review_settings.alerts.enabled = False
|
||||||
|
|
||||||
|
self.config_updater.publish(f"config/review/{camera_name}", review_settings)
|
||||||
|
self.publish(f"{camera_name}/review/alerts/state", payload, retain=True)
|
||||||
|
|
||||||
|
def _on_detections_command(self, camera_name: str, payload: str) -> None:
|
||||||
|
"""Callback for detections topic."""
|
||||||
|
review_settings = self.config.cameras[camera_name].review
|
||||||
|
|
||||||
|
if payload == "ON":
|
||||||
|
if not self.config.cameras[camera_name].review.detections.enabled_in_config:
|
||||||
|
logger.error(
|
||||||
|
"Detections must be enabled in the config to be turned on via MQTT."
|
||||||
|
)
|
||||||
|
return
|
||||||
|
|
||||||
|
if not review_settings.detections.enabled:
|
||||||
|
logger.info(f"Turning on detections for {camera_name}")
|
||||||
|
review_settings.detections.enabled = True
|
||||||
|
elif payload == "OFF":
|
||||||
|
if review_settings.detections.enabled:
|
||||||
|
logger.info(f"Turning off detections for {camera_name}")
|
||||||
|
review_settings.detections.enabled = False
|
||||||
|
|
||||||
|
self.config_updater.publish(f"config/review/{camera_name}", review_settings)
|
||||||
|
self.publish(f"{camera_name}/review/detections/state", payload, retain=True)
|
||||||
|
|||||||
@ -104,6 +104,16 @@ class MqttClient(Communicator): # type: ignore[misc]
|
|||||||
),
|
),
|
||||||
retain=True,
|
retain=True,
|
||||||
)
|
)
|
||||||
|
self.publish(
|
||||||
|
f"{camera_name}/review/alerts/state",
|
||||||
|
"ON" if camera.review.alerts.enabled_in_config else "OFF",
|
||||||
|
retain=True,
|
||||||
|
)
|
||||||
|
self.publish(
|
||||||
|
f"{camera_name}/review/detections/state",
|
||||||
|
"ON" if camera.review.detections.enabled_in_config else "OFF",
|
||||||
|
retain=True,
|
||||||
|
)
|
||||||
|
|
||||||
if self.config.notifications.enabled_in_config:
|
if self.config.notifications.enabled_in_config:
|
||||||
self.publish(
|
self.publish(
|
||||||
|
|||||||
@ -13,6 +13,8 @@ DEFAULT_ALERT_OBJECTS = ["person", "car"]
|
|||||||
class AlertsConfig(FrigateBaseModel):
|
class AlertsConfig(FrigateBaseModel):
|
||||||
"""Configure alerts"""
|
"""Configure alerts"""
|
||||||
|
|
||||||
|
enabled: bool = Field(default=True, title="Enable alerts.")
|
||||||
|
|
||||||
labels: list[str] = Field(
|
labels: list[str] = Field(
|
||||||
default=DEFAULT_ALERT_OBJECTS, title="Labels to create alerts for."
|
default=DEFAULT_ALERT_OBJECTS, title="Labels to create alerts for."
|
||||||
)
|
)
|
||||||
@ -21,6 +23,10 @@ class AlertsConfig(FrigateBaseModel):
|
|||||||
title="List of required zones to be entered in order to save the event as an alert.",
|
title="List of required zones to be entered in order to save the event as an alert.",
|
||||||
)
|
)
|
||||||
|
|
||||||
|
enabled_in_config: Optional[bool] = Field(
|
||||||
|
default=None, title="Keep track of original state of alerts."
|
||||||
|
)
|
||||||
|
|
||||||
@field_validator("required_zones", mode="before")
|
@field_validator("required_zones", mode="before")
|
||||||
@classmethod
|
@classmethod
|
||||||
def validate_required_zones(cls, v):
|
def validate_required_zones(cls, v):
|
||||||
@ -33,6 +39,8 @@ class AlertsConfig(FrigateBaseModel):
|
|||||||
class DetectionsConfig(FrigateBaseModel):
|
class DetectionsConfig(FrigateBaseModel):
|
||||||
"""Configure detections"""
|
"""Configure detections"""
|
||||||
|
|
||||||
|
enabled: bool = Field(default=True, title="Enable detections.")
|
||||||
|
|
||||||
labels: Optional[list[str]] = Field(
|
labels: Optional[list[str]] = Field(
|
||||||
default=None, title="Labels to create detections for."
|
default=None, title="Labels to create detections for."
|
||||||
)
|
)
|
||||||
@ -41,6 +49,10 @@ class DetectionsConfig(FrigateBaseModel):
|
|||||||
title="List of required zones to be entered in order to save the event as a detection.",
|
title="List of required zones to be entered in order to save the event as a detection.",
|
||||||
)
|
)
|
||||||
|
|
||||||
|
enabled_in_config: Optional[bool] = Field(
|
||||||
|
default=None, title="Keep track of original state of detections."
|
||||||
|
)
|
||||||
|
|
||||||
@field_validator("required_zones", mode="before")
|
@field_validator("required_zones", mode="before")
|
||||||
@classmethod
|
@classmethod
|
||||||
def validate_required_zones(cls, v):
|
def validate_required_zones(cls, v):
|
||||||
|
|||||||
@ -148,7 +148,8 @@ class ReviewSegmentMaintainer(threading.Thread):
|
|||||||
|
|
||||||
# create communication for review segments
|
# create communication for review segments
|
||||||
self.requestor = InterProcessRequestor()
|
self.requestor = InterProcessRequestor()
|
||||||
self.config_subscriber = ConfigSubscriber("config/record/")
|
self.record_config_subscriber = ConfigSubscriber("config/record/")
|
||||||
|
self.review_config_subscriber = ConfigSubscriber("config/review/")
|
||||||
self.detection_subscriber = DetectionSubscriber(DetectionTypeEnum.all)
|
self.detection_subscriber = DetectionSubscriber(DetectionTypeEnum.all)
|
||||||
|
|
||||||
# manual events
|
# manual events
|
||||||
@ -226,6 +227,13 @@ class ReviewSegmentMaintainer(threading.Thread):
|
|||||||
)
|
)
|
||||||
self.active_review_segments[segment.camera] = None
|
self.active_review_segments[segment.camera] = None
|
||||||
|
|
||||||
|
def end_segment(self, camera: str) -> None:
|
||||||
|
"""End the pending segment for a camera."""
|
||||||
|
segment = self.active_review_segments.get(camera)
|
||||||
|
if segment:
|
||||||
|
prev_data = segment.get_data(False)
|
||||||
|
self._publish_segment_end(segment, prev_data)
|
||||||
|
|
||||||
def update_existing_segment(
|
def update_existing_segment(
|
||||||
self,
|
self,
|
||||||
segment: PendingReviewSegment,
|
segment: PendingReviewSegment,
|
||||||
@ -273,6 +281,7 @@ class ReviewSegmentMaintainer(threading.Thread):
|
|||||||
& set(camera_config.review.alerts.required_zones)
|
& set(camera_config.review.alerts.required_zones)
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
|
and camera_config.review.alerts.enabled
|
||||||
):
|
):
|
||||||
segment.severity = SeverityEnum.alert
|
segment.severity = SeverityEnum.alert
|
||||||
should_update = True
|
should_update = True
|
||||||
@ -369,13 +378,14 @@ class ReviewSegmentMaintainer(threading.Thread):
|
|||||||
& set(camera_config.review.alerts.required_zones)
|
& set(camera_config.review.alerts.required_zones)
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
|
and camera_config.review.alerts.enabled
|
||||||
):
|
):
|
||||||
severity = SeverityEnum.alert
|
severity = SeverityEnum.alert
|
||||||
|
|
||||||
# if object is detection label
|
# if object is detection label
|
||||||
# and review is not already a detection or alert
|
# and review is not already a detection or alert
|
||||||
# and has entered required zones or required zones is not set
|
# and has entered required zones or required zones is not set
|
||||||
# mark this review as alert
|
# mark this review as detection
|
||||||
if (
|
if (
|
||||||
not severity
|
not severity
|
||||||
and (
|
and (
|
||||||
@ -390,6 +400,7 @@ class ReviewSegmentMaintainer(threading.Thread):
|
|||||||
& set(camera_config.review.detections.required_zones)
|
& set(camera_config.review.detections.required_zones)
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
|
and camera_config.review.detections.enabled
|
||||||
):
|
):
|
||||||
severity = SeverityEnum.detection
|
severity = SeverityEnum.detection
|
||||||
|
|
||||||
@ -430,16 +441,56 @@ class ReviewSegmentMaintainer(threading.Thread):
|
|||||||
# check if there is an updated config
|
# check if there is an updated config
|
||||||
while True:
|
while True:
|
||||||
(
|
(
|
||||||
updated_topic,
|
updated_record_topic,
|
||||||
updated_record_config,
|
updated_record_config,
|
||||||
) = self.config_subscriber.check_for_update()
|
) = self.record_config_subscriber.check_for_update()
|
||||||
|
|
||||||
if not updated_topic:
|
(
|
||||||
|
updated_review_topic,
|
||||||
|
updated_review_config,
|
||||||
|
) = self.review_config_subscriber.check_for_update()
|
||||||
|
|
||||||
|
if not updated_record_topic and not updated_review_topic:
|
||||||
break
|
break
|
||||||
|
|
||||||
camera_name = updated_topic.rpartition("/")[-1]
|
if updated_record_topic:
|
||||||
|
camera_name = updated_record_topic.rpartition("/")[-1]
|
||||||
self.config.cameras[camera_name].record = updated_record_config
|
self.config.cameras[camera_name].record = updated_record_config
|
||||||
|
|
||||||
|
if updated_review_topic:
|
||||||
|
camera_name = updated_review_topic.rpartition("/")[-1]
|
||||||
|
old_alerts_enabled = self.config.cameras[
|
||||||
|
camera_name
|
||||||
|
].review.alerts.enabled
|
||||||
|
old_detections_enabled = self.config.cameras[
|
||||||
|
camera_name
|
||||||
|
].review.detections.enabled
|
||||||
|
self.config.cameras[camera_name].review = updated_review_config
|
||||||
|
|
||||||
|
# Check if alerts.enabled or detections.enabled has changed
|
||||||
|
if (
|
||||||
|
old_alerts_enabled
|
||||||
|
!= self.config.cameras[camera_name].review.alerts.enabled
|
||||||
|
or old_detections_enabled
|
||||||
|
!= self.config.cameras[camera_name].review.detections.enabled
|
||||||
|
):
|
||||||
|
segment = self.active_review_segments.get(camera_name)
|
||||||
|
if segment:
|
||||||
|
if (
|
||||||
|
not self.config.cameras[
|
||||||
|
camera_name
|
||||||
|
].review.alerts.enabled
|
||||||
|
and segment.severity == SeverityEnum.alert
|
||||||
|
):
|
||||||
|
self.end_segment(camera_name)
|
||||||
|
elif (
|
||||||
|
not self.config.cameras[
|
||||||
|
camera_name
|
||||||
|
].review.detections.enabled
|
||||||
|
and segment.severity == SeverityEnum.detection
|
||||||
|
):
|
||||||
|
self.end_segment(camera_name)
|
||||||
|
|
||||||
(topic, data) = self.detection_subscriber.check_for_update(timeout=1)
|
(topic, data) = self.detection_subscriber.check_for_update(timeout=1)
|
||||||
|
|
||||||
if not topic:
|
if not topic:
|
||||||
@ -475,12 +526,22 @@ class ReviewSegmentMaintainer(threading.Thread):
|
|||||||
|
|
||||||
if not self.config.cameras[camera].record.enabled:
|
if not self.config.cameras[camera].record.enabled:
|
||||||
if current_segment:
|
if current_segment:
|
||||||
self.update_existing_segment(
|
self.end_segment(camera)
|
||||||
current_segment, frame_name, frame_time, []
|
|
||||||
)
|
|
||||||
|
|
||||||
continue
|
continue
|
||||||
|
|
||||||
|
# Check if the current segment should be processed based on enabled settings
|
||||||
|
if current_segment:
|
||||||
|
if (
|
||||||
|
current_segment.severity == SeverityEnum.alert
|
||||||
|
and not self.config.cameras[camera].review.alerts.enabled
|
||||||
|
) or (
|
||||||
|
current_segment.severity == SeverityEnum.detection
|
||||||
|
and not self.config.cameras[camera].review.detections.enabled
|
||||||
|
):
|
||||||
|
self.end_segment(camera)
|
||||||
|
continue
|
||||||
|
|
||||||
|
# If we reach here, the segment can be processed (if it exists)
|
||||||
if current_segment is not None:
|
if current_segment is not None:
|
||||||
if topic == DetectionTypeEnum.video:
|
if topic == DetectionTypeEnum.video:
|
||||||
self.update_existing_segment(
|
self.update_existing_segment(
|
||||||
@ -496,19 +557,23 @@ class ReviewSegmentMaintainer(threading.Thread):
|
|||||||
current_segment.last_update = frame_time
|
current_segment.last_update = frame_time
|
||||||
|
|
||||||
for audio in audio_detections:
|
for audio in audio_detections:
|
||||||
if audio in camera_config.review.alerts.labels:
|
if (
|
||||||
|
audio in camera_config.review.alerts.labels
|
||||||
|
and camera_config.review.alerts.enabled
|
||||||
|
):
|
||||||
current_segment.audio.add(audio)
|
current_segment.audio.add(audio)
|
||||||
current_segment.severity = SeverityEnum.alert
|
current_segment.severity = SeverityEnum.alert
|
||||||
elif (
|
elif (
|
||||||
camera_config.review.detections.labels is None
|
camera_config.review.detections.labels is None
|
||||||
or audio in camera_config.review.detections.labels
|
or audio in camera_config.review.detections.labels
|
||||||
):
|
) and camera_config.review.detections.enabled:
|
||||||
current_segment.audio.add(audio)
|
current_segment.audio.add(audio)
|
||||||
elif topic == DetectionTypeEnum.api:
|
elif topic == DetectionTypeEnum.api:
|
||||||
if manual_info["state"] == ManualEventState.complete:
|
if manual_info["state"] == ManualEventState.complete:
|
||||||
current_segment.detections[manual_info["event_id"]] = (
|
current_segment.detections[manual_info["event_id"]] = (
|
||||||
manual_info["label"]
|
manual_info["label"]
|
||||||
)
|
)
|
||||||
|
if self.config.cameras[camera].review.alerts.enabled:
|
||||||
current_segment.severity = SeverityEnum.alert
|
current_segment.severity = SeverityEnum.alert
|
||||||
current_segment.last_update = manual_info["end_time"]
|
current_segment.last_update = manual_info["end_time"]
|
||||||
elif manual_info["state"] == ManualEventState.start:
|
elif manual_info["state"] == ManualEventState.start:
|
||||||
@ -518,6 +583,7 @@ class ReviewSegmentMaintainer(threading.Thread):
|
|||||||
current_segment.detections[manual_info["event_id"]] = (
|
current_segment.detections[manual_info["event_id"]] = (
|
||||||
manual_info["label"]
|
manual_info["label"]
|
||||||
)
|
)
|
||||||
|
if self.config.cameras[camera].review.alerts.enabled:
|
||||||
current_segment.severity = SeverityEnum.alert
|
current_segment.severity = SeverityEnum.alert
|
||||||
|
|
||||||
# temporarily make it so this event can not end
|
# temporarily make it so this event can not end
|
||||||
@ -534,6 +600,10 @@ class ReviewSegmentMaintainer(threading.Thread):
|
|||||||
)
|
)
|
||||||
else:
|
else:
|
||||||
if topic == DetectionTypeEnum.video:
|
if topic == DetectionTypeEnum.video:
|
||||||
|
if (
|
||||||
|
self.config.cameras[camera].review.alerts.enabled
|
||||||
|
or self.config.cameras[camera].review.detections.enabled
|
||||||
|
):
|
||||||
self.check_if_new_segment(
|
self.check_if_new_segment(
|
||||||
camera,
|
camera,
|
||||||
frame_name,
|
frame_name,
|
||||||
@ -547,13 +617,16 @@ class ReviewSegmentMaintainer(threading.Thread):
|
|||||||
detections = set()
|
detections = set()
|
||||||
|
|
||||||
for audio in audio_detections:
|
for audio in audio_detections:
|
||||||
if audio in camera_config.review.alerts.labels:
|
if (
|
||||||
|
audio in camera_config.review.alerts.labels
|
||||||
|
and camera_config.review.alerts.enabled
|
||||||
|
):
|
||||||
detections.add(audio)
|
detections.add(audio)
|
||||||
severity = SeverityEnum.alert
|
severity = SeverityEnum.alert
|
||||||
elif (
|
elif (
|
||||||
camera_config.review.detections.labels is None
|
camera_config.review.detections.labels is None
|
||||||
or audio in camera_config.review.detections.labels
|
or audio in camera_config.review.detections.labels
|
||||||
):
|
) and camera_config.review.detections.enabled:
|
||||||
detections.add(audio)
|
detections.add(audio)
|
||||||
|
|
||||||
if not severity:
|
if not severity:
|
||||||
@ -570,6 +643,7 @@ class ReviewSegmentMaintainer(threading.Thread):
|
|||||||
detections,
|
detections,
|
||||||
)
|
)
|
||||||
elif topic == DetectionTypeEnum.api:
|
elif topic == DetectionTypeEnum.api:
|
||||||
|
if self.config.cameras[camera].review.alerts.enabled:
|
||||||
self.active_review_segments[camera] = PendingReviewSegment(
|
self.active_review_segments[camera] = PendingReviewSegment(
|
||||||
camera,
|
camera,
|
||||||
frame_time,
|
frame_time,
|
||||||
@ -585,13 +659,20 @@ class ReviewSegmentMaintainer(threading.Thread):
|
|||||||
manual_info["label"]
|
manual_info["label"]
|
||||||
)
|
)
|
||||||
# temporarily make it so this event can not end
|
# temporarily make it so this event can not end
|
||||||
self.active_review_segments[camera].last_update = sys.maxsize
|
self.active_review_segments[
|
||||||
|
camera
|
||||||
|
].last_update = sys.maxsize
|
||||||
elif manual_info["state"] == ManualEventState.complete:
|
elif manual_info["state"] == ManualEventState.complete:
|
||||||
self.active_review_segments[camera].last_update = manual_info[
|
self.active_review_segments[
|
||||||
"end_time"
|
camera
|
||||||
]
|
].last_update = manual_info["end_time"]
|
||||||
|
else:
|
||||||
|
logger.warning(
|
||||||
|
f"Manual event API has been called for {camera}, but alerts are disabled. This manual event will not appear as an alert."
|
||||||
|
)
|
||||||
|
|
||||||
self.config_subscriber.stop()
|
self.record_config_subscriber.stop()
|
||||||
|
self.review_config_subscriber.stop()
|
||||||
self.requestor.stop()
|
self.requestor.stop()
|
||||||
self.detection_subscriber.stop()
|
self.detection_subscriber.stop()
|
||||||
logger.info("Exiting review maintainer...")
|
logger.info("Exiting review maintainer...")
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user