2024-02-21 00:26:09 +01:00
|
|
|
"""Maintain review segments in db."""
|
|
|
|
|
2024-02-27 14:37:39 +01:00
|
|
|
import json
|
2024-02-21 00:26:09 +01:00
|
|
|
import logging
|
|
|
|
import os
|
|
|
|
import random
|
|
|
|
import string
|
2024-04-02 15:07:50 +02:00
|
|
|
import sys
|
2024-02-21 00:26:09 +01:00
|
|
|
import threading
|
|
|
|
from enum import Enum
|
|
|
|
from multiprocessing.synchronize import Event as MpEvent
|
2024-04-20 00:11:41 +02:00
|
|
|
from pathlib import Path
|
2024-02-21 00:26:09 +01:00
|
|
|
from typing import Optional
|
|
|
|
|
|
|
|
import cv2
|
|
|
|
import numpy as np
|
|
|
|
|
|
|
|
from frigate.comms.config_updater import ConfigSubscriber
|
|
|
|
from frigate.comms.detections_updater import DetectionSubscriber, DetectionTypeEnum
|
|
|
|
from frigate.comms.inter_process import InterProcessRequestor
|
|
|
|
from frigate.config import CameraConfig, FrigateConfig
|
2024-04-24 15:44:28 +02:00
|
|
|
from frigate.const import (
|
|
|
|
CLEAR_ONGOING_REVIEW_SEGMENTS,
|
|
|
|
CLIPS_DIR,
|
|
|
|
UPSERT_REVIEW_SEGMENT,
|
|
|
|
)
|
2024-04-02 15:07:50 +02:00
|
|
|
from frigate.events.external import ManualEventState
|
2024-02-21 00:26:09 +01:00
|
|
|
from frigate.models import ReviewSegment
|
|
|
|
from frigate.object_processing import TrackedObject
|
|
|
|
from frigate.util.image import SharedMemoryFrameManager, calculate_16_9_crop
|
|
|
|
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
|
|
|
|
THUMB_HEIGHT = 180
|
|
|
|
THUMB_WIDTH = 320
|
|
|
|
|
2024-03-09 15:08:06 +01:00
|
|
|
THRESHOLD_ALERT_ACTIVITY = 120
|
|
|
|
THRESHOLD_DETECTION_ACTIVITY = 30
|
|
|
|
|
2024-02-21 00:26:09 +01:00
|
|
|
|
|
|
|
class SeverityEnum(str, Enum):
|
|
|
|
alert = "alert"
|
|
|
|
detection = "detection"
|
|
|
|
|
|
|
|
|
|
|
|
class PendingReviewSegment:
|
|
|
|
def __init__(
|
|
|
|
self,
|
|
|
|
camera: str,
|
|
|
|
frame_time: float,
|
|
|
|
severity: SeverityEnum,
|
2024-03-30 19:45:42 +01:00
|
|
|
detections: dict[str, str],
|
2024-04-16 22:56:28 +02:00
|
|
|
sub_labels: set[str],
|
2024-06-25 14:38:37 +02:00
|
|
|
zones: list[str],
|
2024-04-16 22:56:28 +02:00
|
|
|
audio: set[str],
|
2024-02-21 00:26:09 +01:00
|
|
|
):
|
|
|
|
rand_id = "".join(random.choices(string.ascii_lowercase + string.digits, k=6))
|
|
|
|
self.id = f"{frame_time}-{rand_id}"
|
|
|
|
self.camera = camera
|
|
|
|
self.start_time = frame_time
|
|
|
|
self.severity = severity
|
|
|
|
self.detections = detections
|
2024-04-16 22:56:28 +02:00
|
|
|
self.sub_labels = sub_labels
|
2024-02-21 00:26:09 +01:00
|
|
|
self.zones = zones
|
|
|
|
self.audio = audio
|
|
|
|
self.last_update = frame_time
|
|
|
|
|
|
|
|
# thumbnail
|
2024-05-14 15:38:03 +02:00
|
|
|
self._frame = np.zeros((THUMB_HEIGHT * 3 // 2, THUMB_WIDTH), np.uint8)
|
|
|
|
self.has_frame = False
|
2024-02-21 00:26:09 +01:00
|
|
|
self.frame_active_count = 0
|
2024-04-20 00:11:41 +02:00
|
|
|
self.frame_path = os.path.join(
|
|
|
|
CLIPS_DIR, f"review/thumb-{self.camera}-{self.id}.webp"
|
|
|
|
)
|
2024-02-21 00:26:09 +01:00
|
|
|
|
|
|
|
def update_frame(
|
|
|
|
self, camera_config: CameraConfig, frame, objects: list[TrackedObject]
|
|
|
|
):
|
|
|
|
min_x = camera_config.frame_shape[1]
|
|
|
|
min_y = camera_config.frame_shape[0]
|
|
|
|
max_x = 0
|
|
|
|
max_y = 0
|
|
|
|
|
|
|
|
# find bounds for all boxes
|
|
|
|
for o in objects:
|
|
|
|
min_x = min(o["box"][0], min_x)
|
|
|
|
min_y = min(o["box"][1], min_y)
|
|
|
|
max_x = max(o["box"][2], max_x)
|
|
|
|
max_y = max(o["box"][3], max_y)
|
|
|
|
|
|
|
|
region = calculate_16_9_crop(
|
|
|
|
camera_config.frame_shape, min_x, min_y, max_x, max_y
|
|
|
|
)
|
|
|
|
|
|
|
|
# could not find suitable 16:9 region
|
|
|
|
if not region:
|
|
|
|
return
|
|
|
|
|
|
|
|
self.frame_active_count = len(objects)
|
|
|
|
color_frame = cv2.cvtColor(frame, cv2.COLOR_YUV2BGR_I420)
|
|
|
|
color_frame = color_frame[region[1] : region[3], region[0] : region[2]]
|
|
|
|
width = int(THUMB_HEIGHT * color_frame.shape[1] / color_frame.shape[0])
|
2024-05-14 15:38:03 +02:00
|
|
|
self._frame = cv2.resize(
|
2024-02-21 00:26:09 +01:00
|
|
|
color_frame, dsize=(width, THUMB_HEIGHT), interpolation=cv2.INTER_AREA
|
|
|
|
)
|
|
|
|
|
2024-05-14 15:38:03 +02:00
|
|
|
if self._frame is not None:
|
|
|
|
self.has_frame = True
|
2024-04-11 14:42:16 +02:00
|
|
|
cv2.imwrite(
|
2024-05-14 15:38:03 +02:00
|
|
|
self.frame_path, self._frame, [int(cv2.IMWRITE_WEBP_QUALITY), 60]
|
2024-04-11 14:42:16 +02:00
|
|
|
)
|
2024-02-21 00:26:09 +01:00
|
|
|
|
2024-05-09 15:20:33 +02:00
|
|
|
def save_full_frame(self, camera_config: CameraConfig, frame):
|
|
|
|
color_frame = cv2.cvtColor(frame, cv2.COLOR_YUV2BGR_I420)
|
|
|
|
width = int(THUMB_HEIGHT * color_frame.shape[1] / color_frame.shape[0])
|
2024-05-14 15:38:03 +02:00
|
|
|
self._frame = cv2.resize(
|
2024-05-09 15:20:33 +02:00
|
|
|
color_frame, dsize=(width, THUMB_HEIGHT), interpolation=cv2.INTER_AREA
|
|
|
|
)
|
|
|
|
|
2024-05-14 15:38:03 +02:00
|
|
|
if self._frame is not None:
|
|
|
|
self.has_frame = True
|
2024-05-09 15:20:33 +02:00
|
|
|
cv2.imwrite(
|
2024-05-14 15:38:03 +02:00
|
|
|
self.frame_path, self._frame, [int(cv2.IMWRITE_WEBP_QUALITY), 60]
|
2024-05-09 15:20:33 +02:00
|
|
|
)
|
|
|
|
|
2024-04-11 14:42:16 +02:00
|
|
|
def get_data(self, ended: bool) -> dict:
|
2024-02-21 00:26:09 +01:00
|
|
|
return {
|
2024-07-24 16:58:23 +02:00
|
|
|
ReviewSegment.id.name: self.id,
|
|
|
|
ReviewSegment.camera.name: self.camera,
|
|
|
|
ReviewSegment.start_time.name: self.start_time,
|
|
|
|
ReviewSegment.end_time.name: self.last_update if ended else None,
|
|
|
|
ReviewSegment.severity.name: self.severity.value,
|
|
|
|
ReviewSegment.thumb_path.name: self.frame_path,
|
|
|
|
ReviewSegment.data.name: {
|
2024-03-30 19:45:42 +01:00
|
|
|
"detections": list(set(self.detections.keys())),
|
|
|
|
"objects": list(set(self.detections.values())),
|
2024-04-16 22:56:28 +02:00
|
|
|
"sub_labels": list(self.sub_labels),
|
2024-06-25 14:38:37 +02:00
|
|
|
"zones": self.zones,
|
2024-02-21 00:26:09 +01:00
|
|
|
"audio": list(self.audio),
|
|
|
|
},
|
2024-06-12 01:05:20 +02:00
|
|
|
}.copy()
|
2024-02-21 00:26:09 +01:00
|
|
|
|
|
|
|
|
|
|
|
class ReviewSegmentMaintainer(threading.Thread):
|
|
|
|
"""Maintain review segments."""
|
|
|
|
|
|
|
|
def __init__(self, config: FrigateConfig, stop_event: MpEvent):
|
|
|
|
threading.Thread.__init__(self)
|
|
|
|
self.name = "review_segment_maintainer"
|
|
|
|
self.config = config
|
|
|
|
self.active_review_segments: dict[str, Optional[PendingReviewSegment]] = {}
|
|
|
|
self.frame_manager = SharedMemoryFrameManager()
|
|
|
|
|
2024-09-28 15:49:04 +02:00
|
|
|
logger.error(f"All attributes are {config.model.all_attributes}")
|
|
|
|
|
2024-02-21 00:26:09 +01:00
|
|
|
# create communication for review segments
|
|
|
|
self.requestor = InterProcessRequestor()
|
|
|
|
self.config_subscriber = ConfigSubscriber("config/record/")
|
|
|
|
self.detection_subscriber = DetectionSubscriber(DetectionTypeEnum.all)
|
|
|
|
|
2024-04-02 15:07:50 +02:00
|
|
|
# manual events
|
|
|
|
self.indefinite_events: dict[str, dict[str, any]] = {}
|
|
|
|
|
2024-04-20 00:11:41 +02:00
|
|
|
# ensure dirs
|
|
|
|
Path(os.path.join(CLIPS_DIR, "review")).mkdir(exist_ok=True)
|
|
|
|
|
2024-02-21 00:26:09 +01:00
|
|
|
self.stop_event = stop_event
|
|
|
|
|
2024-04-24 15:44:28 +02:00
|
|
|
# clear ongoing review segments from last instance
|
|
|
|
self.requestor.send_data(CLEAR_ONGOING_REVIEW_SEGMENTS, "")
|
|
|
|
|
2024-04-23 04:20:30 +02:00
|
|
|
def new_segment(
|
|
|
|
self,
|
|
|
|
segment: PendingReviewSegment,
|
|
|
|
) -> None:
|
|
|
|
"""New segment."""
|
|
|
|
new_data = segment.get_data(ended=False)
|
|
|
|
self.requestor.send_data(UPSERT_REVIEW_SEGMENT, new_data)
|
2024-07-24 16:58:23 +02:00
|
|
|
start_data = {k: v for k, v in new_data.items()}
|
2024-04-23 04:20:30 +02:00
|
|
|
self.requestor.send_data(
|
|
|
|
"reviews",
|
|
|
|
json.dumps(
|
|
|
|
{
|
|
|
|
"type": "new",
|
|
|
|
"before": start_data,
|
|
|
|
"after": start_data,
|
|
|
|
}
|
|
|
|
),
|
|
|
|
)
|
|
|
|
|
|
|
|
def update_segment(
|
|
|
|
self,
|
|
|
|
segment: PendingReviewSegment,
|
|
|
|
camera_config: CameraConfig,
|
|
|
|
frame,
|
|
|
|
objects: list[TrackedObject],
|
2024-06-12 23:48:54 +02:00
|
|
|
prev_data: dict[str, any],
|
2024-04-23 04:20:30 +02:00
|
|
|
) -> None:
|
2024-04-11 14:42:16 +02:00
|
|
|
"""Update segment."""
|
2024-05-14 15:38:03 +02:00
|
|
|
if frame is not None:
|
|
|
|
segment.update_frame(camera_config, frame, objects)
|
|
|
|
|
2024-04-23 04:20:30 +02:00
|
|
|
new_data = segment.get_data(ended=False)
|
|
|
|
self.requestor.send_data(UPSERT_REVIEW_SEGMENT, new_data)
|
2024-04-11 14:42:16 +02:00
|
|
|
self.requestor.send_data(
|
|
|
|
"reviews",
|
|
|
|
json.dumps(
|
2024-04-23 04:20:30 +02:00
|
|
|
{
|
|
|
|
"type": "update",
|
2024-07-24 16:58:23 +02:00
|
|
|
"before": {k: v for k, v in prev_data.items()},
|
|
|
|
"after": {k: v for k, v in new_data.items()},
|
2024-04-23 04:20:30 +02:00
|
|
|
}
|
2024-04-11 14:42:16 +02:00
|
|
|
),
|
|
|
|
)
|
|
|
|
|
2024-06-23 15:45:10 +02:00
|
|
|
def end_segment(
|
|
|
|
self,
|
|
|
|
segment: PendingReviewSegment,
|
|
|
|
prev_data: dict[str, any],
|
|
|
|
) -> None:
|
2024-02-21 00:26:09 +01:00
|
|
|
"""End segment."""
|
2024-04-23 04:20:30 +02:00
|
|
|
final_data = segment.get_data(ended=True)
|
|
|
|
self.requestor.send_data(UPSERT_REVIEW_SEGMENT, final_data)
|
2024-02-27 14:37:39 +01:00
|
|
|
self.requestor.send_data(
|
|
|
|
"reviews",
|
|
|
|
json.dumps(
|
2024-04-23 04:20:30 +02:00
|
|
|
{
|
|
|
|
"type": "end",
|
2024-07-24 16:58:23 +02:00
|
|
|
"before": {k: v for k, v in prev_data.items()},
|
|
|
|
"after": {k: v for k, v in final_data.items()},
|
2024-04-23 04:20:30 +02:00
|
|
|
}
|
2024-02-27 14:37:39 +01:00
|
|
|
),
|
|
|
|
)
|
2024-02-21 00:26:09 +01:00
|
|
|
self.active_review_segments[segment.camera] = None
|
|
|
|
|
|
|
|
def update_existing_segment(
|
|
|
|
self,
|
|
|
|
segment: PendingReviewSegment,
|
|
|
|
frame_time: float,
|
|
|
|
objects: list[TrackedObject],
|
|
|
|
) -> None:
|
|
|
|
"""Validate if existing review segment should continue."""
|
|
|
|
camera_config = self.config.cameras[segment.camera]
|
|
|
|
active_objects = get_active_objects(frame_time, camera_config, objects)
|
2024-06-12 23:48:54 +02:00
|
|
|
prev_data = segment.get_data(False)
|
2024-02-21 00:26:09 +01:00
|
|
|
|
|
|
|
if len(active_objects) > 0:
|
2024-06-11 16:19:17 +02:00
|
|
|
should_update = False
|
|
|
|
|
2024-04-02 15:07:50 +02:00
|
|
|
if frame_time > segment.last_update:
|
|
|
|
segment.last_update = frame_time
|
2024-02-21 00:26:09 +01:00
|
|
|
|
|
|
|
for object in active_objects:
|
2024-03-30 19:45:42 +01:00
|
|
|
if not object["sub_label"]:
|
|
|
|
segment.detections[object["id"]] = object["label"]
|
2024-09-28 15:49:04 +02:00
|
|
|
elif object["sub_label"][0] in self.config.model.all_attributes:
|
2024-03-30 19:45:42 +01:00
|
|
|
segment.detections[object["id"]] = object["sub_label"][0]
|
|
|
|
else:
|
|
|
|
segment.detections[object["id"]] = f'{object["label"]}-verified'
|
2024-04-16 22:56:28 +02:00
|
|
|
segment.sub_labels.add(object["sub_label"][0])
|
2024-02-21 00:26:09 +01:00
|
|
|
|
2024-04-13 14:08:20 +02:00
|
|
|
# if object is alert label
|
|
|
|
# and has entered required zones or required zones is not set
|
2024-02-21 00:26:09 +01:00
|
|
|
# mark this review as alert
|
|
|
|
if (
|
2024-04-13 14:08:20 +02:00
|
|
|
segment.severity != SeverityEnum.alert
|
|
|
|
and object["label"] in camera_config.review.alerts.labels
|
|
|
|
and (
|
|
|
|
not camera_config.review.alerts.required_zones
|
|
|
|
or (
|
|
|
|
len(object["current_zones"]) > 0
|
|
|
|
and set(object["current_zones"])
|
|
|
|
& set(camera_config.review.alerts.required_zones)
|
|
|
|
)
|
|
|
|
)
|
2024-02-21 00:26:09 +01:00
|
|
|
):
|
|
|
|
segment.severity = SeverityEnum.alert
|
2024-06-11 16:19:17 +02:00
|
|
|
should_update = True
|
2024-02-21 00:26:09 +01:00
|
|
|
|
|
|
|
# keep zones up to date
|
|
|
|
if len(object["current_zones"]) > 0:
|
2024-06-25 14:38:37 +02:00
|
|
|
for zone in object["current_zones"]:
|
|
|
|
if zone not in segment.zones:
|
|
|
|
segment.zones.append(zone)
|
2024-04-13 14:08:20 +02:00
|
|
|
|
|
|
|
if len(active_objects) > segment.frame_active_count:
|
2024-06-11 16:19:17 +02:00
|
|
|
should_update = True
|
|
|
|
|
|
|
|
if should_update:
|
2024-04-13 14:08:20 +02:00
|
|
|
try:
|
|
|
|
frame_id = f"{camera_config.name}{frame_time}"
|
|
|
|
yuv_frame = self.frame_manager.get(
|
|
|
|
frame_id, camera_config.frame_shape_yuv
|
|
|
|
)
|
2024-09-03 18:22:30 +02:00
|
|
|
|
|
|
|
if yuv_frame is None:
|
|
|
|
logger.debug(f"Failed to get frame {frame_id} from SHM")
|
|
|
|
return
|
|
|
|
|
2024-04-23 04:20:30 +02:00
|
|
|
self.update_segment(
|
2024-06-12 23:48:54 +02:00
|
|
|
segment, camera_config, yuv_frame, active_objects, prev_data
|
2024-04-23 04:20:30 +02:00
|
|
|
)
|
2024-04-13 14:08:20 +02:00
|
|
|
self.frame_manager.close(frame_id)
|
|
|
|
except FileNotFoundError:
|
|
|
|
return
|
2024-02-21 00:26:09 +01:00
|
|
|
else:
|
2024-05-14 15:38:03 +02:00
|
|
|
if not segment.has_frame:
|
|
|
|
try:
|
|
|
|
frame_id = f"{camera_config.name}{frame_time}"
|
|
|
|
yuv_frame = self.frame_manager.get(
|
|
|
|
frame_id, camera_config.frame_shape_yuv
|
|
|
|
)
|
2024-09-03 18:22:30 +02:00
|
|
|
|
|
|
|
if yuv_frame is None:
|
|
|
|
logger.debug(f"Failed to get frame {frame_id} from SHM")
|
|
|
|
return
|
|
|
|
|
2024-05-14 15:38:03 +02:00
|
|
|
segment.save_full_frame(camera_config, yuv_frame)
|
|
|
|
self.frame_manager.close(frame_id)
|
2024-06-12 23:48:54 +02:00
|
|
|
self.update_segment(segment, camera_config, None, [], prev_data)
|
2024-05-14 15:38:03 +02:00
|
|
|
except FileNotFoundError:
|
|
|
|
return
|
|
|
|
|
2024-02-21 00:26:09 +01:00
|
|
|
if segment.severity == SeverityEnum.alert and frame_time > (
|
2024-03-09 15:08:06 +01:00
|
|
|
segment.last_update + THRESHOLD_ALERT_ACTIVITY
|
2024-02-21 00:26:09 +01:00
|
|
|
):
|
2024-06-23 15:45:10 +02:00
|
|
|
self.end_segment(segment, prev_data)
|
2024-03-09 15:08:06 +01:00
|
|
|
elif frame_time > (segment.last_update + THRESHOLD_DETECTION_ACTIVITY):
|
2024-06-23 15:45:10 +02:00
|
|
|
self.end_segment(segment, prev_data)
|
2024-02-21 00:26:09 +01:00
|
|
|
|
|
|
|
def check_if_new_segment(
|
|
|
|
self,
|
|
|
|
camera: str,
|
|
|
|
frame_time: float,
|
|
|
|
objects: list[TrackedObject],
|
|
|
|
) -> None:
|
|
|
|
"""Check if a new review segment should be created."""
|
|
|
|
camera_config = self.config.cameras[camera]
|
|
|
|
active_objects = get_active_objects(frame_time, camera_config, objects)
|
|
|
|
|
|
|
|
if len(active_objects) > 0:
|
2024-03-30 19:45:42 +01:00
|
|
|
detections: dict[str, str] = {}
|
2024-04-16 22:56:28 +02:00
|
|
|
sub_labels = set()
|
2024-06-25 14:38:37 +02:00
|
|
|
zones: list[str] = []
|
2024-04-13 14:08:20 +02:00
|
|
|
severity = None
|
2024-02-21 00:26:09 +01:00
|
|
|
|
|
|
|
for object in active_objects:
|
2024-03-30 19:45:42 +01:00
|
|
|
if not object["sub_label"]:
|
|
|
|
detections[object["id"]] = object["label"]
|
2024-09-28 15:49:04 +02:00
|
|
|
elif object["sub_label"][0] in self.config.model.all_attributes:
|
2024-03-30 19:45:42 +01:00
|
|
|
detections[object["id"]] = object["sub_label"][0]
|
|
|
|
else:
|
|
|
|
detections[object["id"]] = f'{object["label"]}-verified'
|
2024-04-16 22:56:28 +02:00
|
|
|
sub_labels.add(object["sub_label"][0])
|
2024-02-21 00:26:09 +01:00
|
|
|
|
2024-04-13 14:08:20 +02:00
|
|
|
# if object is alert label
|
|
|
|
# and has entered required zones or required zones is not set
|
|
|
|
# mark this review as alert
|
|
|
|
if (
|
|
|
|
severity != SeverityEnum.alert
|
|
|
|
and object["label"] in camera_config.review.alerts.labels
|
|
|
|
and (
|
|
|
|
not camera_config.review.alerts.required_zones
|
|
|
|
or (
|
|
|
|
len(object["current_zones"]) > 0
|
|
|
|
and set(object["current_zones"])
|
|
|
|
& set(camera_config.review.alerts.required_zones)
|
|
|
|
)
|
|
|
|
)
|
|
|
|
):
|
|
|
|
severity = SeverityEnum.alert
|
|
|
|
|
|
|
|
# if object is detection label
|
|
|
|
# and review is not already a detection or alert
|
|
|
|
# and has entered required zones or required zones is not set
|
|
|
|
# mark this review as alert
|
|
|
|
if (
|
|
|
|
not severity
|
|
|
|
and (
|
2024-05-30 14:45:34 +02:00
|
|
|
camera_config.review.detections.labels is None
|
2024-04-13 14:08:20 +02:00
|
|
|
or object["label"] in (camera_config.review.detections.labels)
|
|
|
|
)
|
|
|
|
and (
|
|
|
|
not camera_config.review.detections.required_zones
|
|
|
|
or (
|
|
|
|
len(object["current_zones"]) > 0
|
|
|
|
and set(object["current_zones"])
|
|
|
|
& set(camera_config.review.detections.required_zones)
|
|
|
|
)
|
|
|
|
)
|
|
|
|
):
|
|
|
|
severity = SeverityEnum.detection
|
|
|
|
|
2024-06-25 14:38:37 +02:00
|
|
|
for zone in object["current_zones"]:
|
|
|
|
if zone not in zones:
|
|
|
|
zones.append(zone)
|
2024-02-21 00:26:09 +01:00
|
|
|
|
2024-04-13 14:08:20 +02:00
|
|
|
if severity:
|
|
|
|
self.active_review_segments[camera] = PendingReviewSegment(
|
|
|
|
camera,
|
|
|
|
frame_time,
|
2024-04-16 22:56:28 +02:00
|
|
|
severity,
|
2024-04-13 14:08:20 +02:00
|
|
|
detections,
|
2024-04-16 22:56:28 +02:00
|
|
|
sub_labels=sub_labels,
|
2024-04-13 14:08:20 +02:00
|
|
|
audio=set(),
|
|
|
|
zones=zones,
|
|
|
|
)
|
2024-02-23 01:03:13 +01:00
|
|
|
|
2024-04-13 14:08:20 +02:00
|
|
|
try:
|
|
|
|
frame_id = f"{camera_config.name}{frame_time}"
|
|
|
|
yuv_frame = self.frame_manager.get(
|
|
|
|
frame_id, camera_config.frame_shape_yuv
|
|
|
|
)
|
2024-09-03 18:22:30 +02:00
|
|
|
|
|
|
|
if yuv_frame is None:
|
|
|
|
logger.debug(f"Failed to get frame {frame_id} from SHM")
|
|
|
|
return
|
|
|
|
|
2024-04-13 14:08:20 +02:00
|
|
|
self.active_review_segments[camera].update_frame(
|
|
|
|
camera_config, yuv_frame, active_objects
|
|
|
|
)
|
|
|
|
self.frame_manager.close(frame_id)
|
2024-04-23 04:20:30 +02:00
|
|
|
self.new_segment(self.active_review_segments[camera])
|
2024-04-13 14:08:20 +02:00
|
|
|
except FileNotFoundError:
|
|
|
|
return
|
2024-02-21 00:26:09 +01:00
|
|
|
|
|
|
|
def run(self) -> None:
|
|
|
|
while not self.stop_event.is_set():
|
|
|
|
# check if there is an updated config
|
|
|
|
while True:
|
|
|
|
(
|
|
|
|
updated_topic,
|
|
|
|
updated_record_config,
|
|
|
|
) = self.config_subscriber.check_for_update()
|
|
|
|
|
|
|
|
if not updated_topic:
|
|
|
|
break
|
|
|
|
|
|
|
|
camera_name = updated_topic.rpartition("/")[-1]
|
|
|
|
self.config.cameras[camera_name].record = updated_record_config
|
|
|
|
|
2024-06-21 23:30:19 +02:00
|
|
|
(topic, data) = self.detection_subscriber.check_for_update(timeout=1)
|
2024-02-21 00:26:09 +01:00
|
|
|
|
|
|
|
if not topic:
|
|
|
|
continue
|
|
|
|
|
|
|
|
if topic == DetectionTypeEnum.video:
|
|
|
|
(
|
|
|
|
camera,
|
|
|
|
frame_time,
|
|
|
|
current_tracked_objects,
|
|
|
|
motion_boxes,
|
|
|
|
regions,
|
|
|
|
) = data
|
|
|
|
elif topic == DetectionTypeEnum.audio:
|
|
|
|
(
|
|
|
|
camera,
|
|
|
|
frame_time,
|
|
|
|
dBFS,
|
|
|
|
audio_detections,
|
|
|
|
) = data
|
2024-04-02 15:07:50 +02:00
|
|
|
elif topic == DetectionTypeEnum.api:
|
|
|
|
(
|
|
|
|
camera,
|
|
|
|
frame_time,
|
|
|
|
manual_info,
|
|
|
|
) = data
|
|
|
|
|
|
|
|
if camera not in self.indefinite_events:
|
|
|
|
self.indefinite_events[camera] = {}
|
2024-02-21 00:26:09 +01:00
|
|
|
|
2024-05-17 15:26:42 +02:00
|
|
|
current_segment = self.active_review_segments.get(camera)
|
|
|
|
|
2024-02-21 00:26:09 +01:00
|
|
|
if not self.config.cameras[camera].record.enabled:
|
2024-05-17 15:26:42 +02:00
|
|
|
if current_segment:
|
|
|
|
self.update_existing_segment(current_segment, frame_time, [])
|
2024-02-21 00:26:09 +01:00
|
|
|
|
2024-05-17 15:26:42 +02:00
|
|
|
continue
|
2024-02-21 00:26:09 +01:00
|
|
|
|
|
|
|
if current_segment is not None:
|
|
|
|
if topic == DetectionTypeEnum.video:
|
|
|
|
self.update_existing_segment(
|
|
|
|
current_segment,
|
|
|
|
frame_time,
|
|
|
|
current_tracked_objects,
|
|
|
|
)
|
|
|
|
elif topic == DetectionTypeEnum.audio and len(audio_detections) > 0:
|
2024-04-13 14:08:20 +02:00
|
|
|
camera_config = self.config.cameras[camera]
|
|
|
|
|
2024-04-02 15:07:50 +02:00
|
|
|
if frame_time > current_segment.last_update:
|
|
|
|
current_segment.last_update = frame_time
|
|
|
|
|
2024-04-13 14:08:20 +02:00
|
|
|
for audio in audio_detections:
|
|
|
|
if audio in camera_config.review.alerts.labels:
|
|
|
|
current_segment.audio.add(audio)
|
|
|
|
current_segment.severity = SeverityEnum.alert
|
|
|
|
elif (
|
2024-05-30 14:45:34 +02:00
|
|
|
camera_config.review.detections.labels is None
|
2024-04-13 14:08:20 +02:00
|
|
|
or audio in camera_config.review.detections.labels
|
|
|
|
):
|
|
|
|
current_segment.audio.add(audio)
|
2024-04-02 15:07:50 +02:00
|
|
|
elif topic == DetectionTypeEnum.api:
|
|
|
|
if manual_info["state"] == ManualEventState.complete:
|
|
|
|
current_segment.detections[manual_info["event_id"]] = (
|
|
|
|
manual_info["label"]
|
|
|
|
)
|
|
|
|
current_segment.severity = SeverityEnum.alert
|
|
|
|
current_segment.last_update = manual_info["end_time"]
|
|
|
|
elif manual_info["state"] == ManualEventState.start:
|
|
|
|
self.indefinite_events[camera][manual_info["event_id"]] = (
|
|
|
|
manual_info["label"]
|
|
|
|
)
|
|
|
|
current_segment.detections[manual_info["event_id"]] = (
|
|
|
|
manual_info["label"]
|
|
|
|
)
|
|
|
|
current_segment.severity = SeverityEnum.alert
|
|
|
|
|
|
|
|
# temporarily make it so this event can not end
|
|
|
|
current_segment.last_update = sys.maxsize
|
|
|
|
elif manual_info["state"] == ManualEventState.end:
|
2024-08-11 14:32:39 +02:00
|
|
|
event_id = manual_info["event_id"]
|
|
|
|
|
|
|
|
if event_id in self.indefinite_events[camera]:
|
|
|
|
self.indefinite_events[camera].pop(event_id)
|
|
|
|
current_segment.last_update = manual_info["end_time"]
|
|
|
|
else:
|
|
|
|
logger.error(
|
|
|
|
f"Event with ID {event_id} has a set duration and can not be ended manually."
|
|
|
|
)
|
2024-02-21 00:26:09 +01:00
|
|
|
else:
|
|
|
|
if topic == DetectionTypeEnum.video:
|
|
|
|
self.check_if_new_segment(
|
|
|
|
camera,
|
|
|
|
frame_time,
|
|
|
|
current_tracked_objects,
|
|
|
|
)
|
|
|
|
elif topic == DetectionTypeEnum.audio and len(audio_detections) > 0:
|
2024-04-13 14:08:20 +02:00
|
|
|
severity = None
|
|
|
|
|
|
|
|
camera_config = self.config.cameras[camera]
|
|
|
|
detections = set()
|
|
|
|
|
|
|
|
for audio in audio_detections:
|
|
|
|
if audio in camera_config.review.alerts.labels:
|
|
|
|
detections.add(audio)
|
|
|
|
severity = SeverityEnum.alert
|
|
|
|
elif (
|
2024-05-30 14:45:34 +02:00
|
|
|
camera_config.review.detections.labels is None
|
2024-04-13 14:08:20 +02:00
|
|
|
or audio in camera_config.review.detections.labels
|
|
|
|
):
|
|
|
|
detections.add(audio)
|
|
|
|
|
|
|
|
if not severity:
|
|
|
|
severity = SeverityEnum.detection
|
|
|
|
|
|
|
|
if severity:
|
|
|
|
self.active_review_segments[camera] = PendingReviewSegment(
|
|
|
|
camera,
|
|
|
|
frame_time,
|
|
|
|
severity,
|
|
|
|
{},
|
|
|
|
set(),
|
2024-06-25 14:38:37 +02:00
|
|
|
[],
|
2024-04-13 14:08:20 +02:00
|
|
|
detections,
|
|
|
|
)
|
2024-04-02 15:07:50 +02:00
|
|
|
elif topic == DetectionTypeEnum.api:
|
|
|
|
self.active_review_segments[camera] = PendingReviewSegment(
|
|
|
|
camera,
|
|
|
|
frame_time,
|
|
|
|
SeverityEnum.alert,
|
|
|
|
{manual_info["event_id"]: manual_info["label"]},
|
|
|
|
set(),
|
2024-06-25 14:38:37 +02:00
|
|
|
[],
|
2024-04-16 22:56:28 +02:00
|
|
|
set(),
|
2024-04-02 15:07:50 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
if manual_info["state"] == ManualEventState.start:
|
|
|
|
self.indefinite_events[camera][manual_info["event_id"]] = (
|
|
|
|
manual_info["label"]
|
|
|
|
)
|
|
|
|
# temporarily make it so this event can not end
|
2024-05-08 14:34:22 +02:00
|
|
|
self.active_review_segments[camera].last_update = sys.maxsize
|
2024-04-02 15:07:50 +02:00
|
|
|
elif manual_info["state"] == ManualEventState.complete:
|
|
|
|
self.active_review_segments[camera].last_update = manual_info[
|
|
|
|
"end_time"
|
|
|
|
]
|
2024-02-21 00:26:09 +01:00
|
|
|
|
2024-04-11 14:42:16 +02:00
|
|
|
self.config_subscriber.stop()
|
|
|
|
self.requestor.stop()
|
|
|
|
self.detection_subscriber.stop()
|
|
|
|
logger.info("Exiting review maintainer...")
|
|
|
|
|
2024-02-21 00:26:09 +01:00
|
|
|
|
|
|
|
def get_active_objects(
|
|
|
|
frame_time: float, camera_config: CameraConfig, all_objects: list[TrackedObject]
|
|
|
|
) -> list[TrackedObject]:
|
|
|
|
"""get active objects for detection."""
|
|
|
|
return [
|
|
|
|
o
|
|
|
|
for o in all_objects
|
2024-04-13 14:08:20 +02:00
|
|
|
if o["motionless_count"]
|
|
|
|
< camera_config.detect.stationary.threshold # no stationary objects
|
|
|
|
and o["position_changes"] > 0 # object must have moved at least once
|
|
|
|
and o["frame_time"] == frame_time # object must be detected in this frame
|
|
|
|
and not o["false_positive"] # object must not be a false positive
|
|
|
|
and (
|
|
|
|
o["label"] in camera_config.review.alerts.labels
|
|
|
|
or (
|
2024-05-30 14:45:34 +02:00
|
|
|
camera_config.review.detections.labels is None
|
2024-04-13 14:08:20 +02:00
|
|
|
or o["label"] in camera_config.review.detections.labels
|
|
|
|
)
|
|
|
|
) # object must be in the alerts or detections label list
|
2024-02-21 00:26:09 +01:00
|
|
|
]
|