2020-11-04 13:31:25 +01:00
|
|
|
import datetime
|
|
|
|
import json
|
2020-11-04 04:26:39 +01:00
|
|
|
import logging
|
2020-11-08 23:05:15 +01:00
|
|
|
import os
|
2020-08-02 15:46:36 +02:00
|
|
|
import queue
|
2020-11-04 13:31:25 +01:00
|
|
|
import threading
|
2020-02-16 04:07:54 +01:00
|
|
|
from collections import Counter, defaultdict
|
2024-04-09 01:19:45 +02:00
|
|
|
from multiprocessing.synchronize import Event as MpEvent
|
2022-04-16 17:38:07 +02:00
|
|
|
from typing import Callable
|
2020-11-04 13:31:25 +01:00
|
|
|
|
|
|
|
import cv2
|
|
|
|
import numpy as np
|
|
|
|
|
2024-02-19 14:26:59 +01:00
|
|
|
from frigate.comms.detections_updater import DetectionPublisher, DetectionTypeEnum
|
2022-11-24 03:03:20 +01:00
|
|
|
from frigate.comms.dispatcher import Dispatcher
|
2024-04-09 01:19:45 +02:00
|
|
|
from frigate.comms.events_updater import EventEndSubscriber, EventUpdatePublisher
|
2024-04-30 15:09:50 +02:00
|
|
|
from frigate.comms.inter_process import InterProcessRequestor
|
2022-11-20 14:36:01 +01:00
|
|
|
from frigate.config import (
|
2023-05-29 12:31:17 +02:00
|
|
|
FrigateConfig,
|
2022-11-20 14:36:01 +01:00
|
|
|
MqttConfig,
|
|
|
|
RecordConfig,
|
2023-05-29 12:31:17 +02:00
|
|
|
SnapshotsConfig,
|
2023-12-28 14:48:44 +01:00
|
|
|
ZoomingModeEnum,
|
2022-11-20 14:36:01 +01:00
|
|
|
)
|
2024-09-28 15:49:04 +02:00
|
|
|
from frigate.const import CLIPS_DIR, UPDATE_CAMERA_ACTIVITY
|
2024-03-23 17:11:32 +01:00
|
|
|
from frigate.events.types import EventStateEnum, EventTypeEnum
|
2023-07-08 14:04:47 +02:00
|
|
|
from frigate.ptz.autotrack import PtzAutoTrackerThread
|
2024-10-17 18:02:27 +02:00
|
|
|
from frigate.track.tracked_object import TrackedObject
|
2023-07-06 16:28:50 +02:00
|
|
|
from frigate.util.image import (
|
2021-06-15 22:19:49 +02:00
|
|
|
SharedMemoryFrameManager,
|
|
|
|
draw_box_with_label,
|
|
|
|
draw_timestamp,
|
2024-10-17 18:02:27 +02:00
|
|
|
is_better_thumbnail,
|
2023-09-01 14:03:47 +02:00
|
|
|
is_label_printable,
|
2021-06-15 22:19:49 +02:00
|
|
|
)
|
2020-02-16 04:07:54 +01:00
|
|
|
|
2020-11-04 04:26:39 +01:00
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
2021-02-17 14:23:32 +01:00
|
|
|
|
2020-09-07 19:17:42 +02:00
|
|
|
# Maintains the state of a camera
|
2021-02-17 14:23:32 +01:00
|
|
|
class CameraState:
|
2021-07-13 15:51:15 +02:00
|
|
|
def __init__(
|
2023-07-08 14:04:47 +02:00
|
|
|
self,
|
|
|
|
name,
|
|
|
|
config: FrigateConfig,
|
|
|
|
frame_manager: SharedMemoryFrameManager,
|
|
|
|
ptz_autotracker_thread: PtzAutoTrackerThread,
|
2021-07-13 15:51:15 +02:00
|
|
|
):
|
2020-09-07 19:17:42 +02:00
|
|
|
self.name = name
|
|
|
|
self.config = config
|
2021-06-24 07:51:41 +02:00
|
|
|
self.camera_config = config.cameras[name]
|
2020-09-07 19:17:42 +02:00
|
|
|
self.frame_manager = frame_manager
|
2022-04-16 17:38:07 +02:00
|
|
|
self.best_objects: dict[str, TrackedObject] = {}
|
2021-05-23 16:29:39 +02:00
|
|
|
self.object_counts = defaultdict(int)
|
2024-09-02 15:24:15 +02:00
|
|
|
self.active_object_counts = defaultdict(int)
|
2022-04-16 17:38:07 +02:00
|
|
|
self.tracked_objects: dict[str, TrackedObject] = {}
|
2020-11-11 23:55:50 +01:00
|
|
|
self.frame_cache = {}
|
2021-05-23 16:29:39 +02:00
|
|
|
self.zone_objects = defaultdict(list)
|
2020-11-08 23:05:15 +01:00
|
|
|
self._current_frame = np.zeros(self.camera_config.frame_shape_yuv, np.uint8)
|
2020-10-07 23:44:21 +02:00
|
|
|
self.current_frame_lock = threading.Lock()
|
2020-09-07 19:17:42 +02:00
|
|
|
self.current_frame_time = 0.0
|
2021-01-12 14:00:08 +01:00
|
|
|
self.motion_boxes = []
|
|
|
|
self.regions = []
|
2020-09-07 19:17:42 +02:00
|
|
|
self.previous_frame_id = None
|
2021-05-23 16:29:39 +02:00
|
|
|
self.callbacks = defaultdict(list)
|
2023-07-08 14:04:47 +02:00
|
|
|
self.ptz_autotracker_thread = ptz_autotracker_thread
|
2020-09-07 19:17:42 +02:00
|
|
|
|
2020-12-19 15:22:31 +01:00
|
|
|
def get_current_frame(self, draw_options={}):
|
2020-10-07 23:44:21 +02:00
|
|
|
with self.current_frame_lock:
|
2020-10-11 19:16:05 +02:00
|
|
|
frame_copy = np.copy(self._current_frame)
|
|
|
|
frame_time = self.current_frame_time
|
2021-02-17 14:23:32 +01:00
|
|
|
tracked_objects = {k: v.to_dict() for k, v in self.tracked_objects.items()}
|
2020-12-19 15:22:31 +01:00
|
|
|
motion_boxes = self.motion_boxes.copy()
|
|
|
|
regions = self.regions.copy()
|
2021-01-11 18:09:43 +01:00
|
|
|
|
2020-10-11 19:16:05 +02:00
|
|
|
frame_copy = cv2.cvtColor(frame_copy, cv2.COLOR_YUV2BGR_I420)
|
|
|
|
# draw on the frame
|
2024-05-28 20:44:20 +02:00
|
|
|
if draw_options.get("mask"):
|
|
|
|
mask_overlay = np.where(self.camera_config.motion.mask == [0])
|
|
|
|
frame_copy[mask_overlay] = [0, 0, 0]
|
|
|
|
|
2021-02-17 14:23:32 +01:00
|
|
|
if draw_options.get("bounding_boxes"):
|
2020-10-11 19:16:05 +02:00
|
|
|
# draw the bounding boxes on the frame
|
|
|
|
for obj in tracked_objects.values():
|
2021-05-23 16:29:39 +02:00
|
|
|
if obj["frame_time"] == frame_time:
|
2024-02-26 14:37:56 +01:00
|
|
|
if obj["stationary"]:
|
|
|
|
color = (220, 220, 220)
|
|
|
|
thickness = 1
|
|
|
|
else:
|
|
|
|
thickness = 2
|
|
|
|
color = self.config.model.colormap[obj["label"]]
|
2021-05-23 16:29:39 +02:00
|
|
|
else:
|
2020-10-11 19:16:05 +02:00
|
|
|
thickness = 1
|
2021-02-17 14:23:32 +01:00
|
|
|
color = (255, 0, 0)
|
2020-10-11 19:16:05 +02:00
|
|
|
|
2023-07-08 14:04:47 +02:00
|
|
|
# draw thicker box around ptz autotracked object
|
|
|
|
if (
|
|
|
|
self.camera_config.onvif.autotracking.enabled
|
2023-10-22 18:59:13 +02:00
|
|
|
and self.ptz_autotracker_thread.ptz_autotracker.autotracker_init[
|
|
|
|
self.name
|
|
|
|
]
|
2023-07-08 14:04:47 +02:00
|
|
|
and self.ptz_autotracker_thread.ptz_autotracker.tracked_object[
|
|
|
|
self.name
|
|
|
|
]
|
|
|
|
is not None
|
|
|
|
and obj["id"]
|
|
|
|
== self.ptz_autotracker_thread.ptz_autotracker.tracked_object[
|
|
|
|
self.name
|
|
|
|
].obj_data["id"]
|
2023-10-22 18:59:13 +02:00
|
|
|
and obj["frame_time"] == frame_time
|
2023-07-08 14:04:47 +02:00
|
|
|
):
|
|
|
|
thickness = 5
|
|
|
|
color = self.config.model.colormap[obj["label"]]
|
|
|
|
|
2023-12-28 14:48:44 +01:00
|
|
|
# debug autotracking zooming - show the zoom factor box
|
|
|
|
if (
|
|
|
|
self.camera_config.onvif.autotracking.zooming
|
|
|
|
!= ZoomingModeEnum.disabled
|
|
|
|
):
|
|
|
|
max_target_box = self.ptz_autotracker_thread.ptz_autotracker.tracked_object_metrics[
|
|
|
|
self.name
|
|
|
|
]["max_target_box"]
|
|
|
|
side_length = max_target_box * (
|
|
|
|
max(
|
|
|
|
self.camera_config.detect.width,
|
|
|
|
self.camera_config.detect.height,
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
centroid_x = (obj["box"][0] + obj["box"][2]) // 2
|
|
|
|
centroid_y = (obj["box"][1] + obj["box"][3]) // 2
|
|
|
|
top_left = (
|
|
|
|
int(centroid_x - side_length // 2),
|
|
|
|
int(centroid_y - side_length // 2),
|
|
|
|
)
|
|
|
|
bottom_right = (
|
|
|
|
int(centroid_x + side_length // 2),
|
|
|
|
int(centroid_y + side_length // 2),
|
|
|
|
)
|
|
|
|
cv2.rectangle(
|
|
|
|
frame_copy,
|
|
|
|
top_left,
|
|
|
|
bottom_right,
|
|
|
|
(255, 255, 0),
|
|
|
|
2,
|
|
|
|
)
|
|
|
|
|
2020-10-11 19:16:05 +02:00
|
|
|
# draw the bounding boxes on the frame
|
2021-02-17 14:23:32 +01:00
|
|
|
box = obj["box"]
|
2023-09-01 14:03:47 +02:00
|
|
|
text = (
|
|
|
|
obj["label"]
|
|
|
|
if (
|
|
|
|
not obj.get("sub_label")
|
|
|
|
or not is_label_printable(obj["sub_label"][0])
|
|
|
|
)
|
|
|
|
else obj["sub_label"][0]
|
|
|
|
)
|
2021-02-17 14:23:32 +01:00
|
|
|
draw_box_with_label(
|
|
|
|
frame_copy,
|
|
|
|
box[0],
|
|
|
|
box[1],
|
|
|
|
box[2],
|
|
|
|
box[3],
|
2023-09-01 14:03:47 +02:00
|
|
|
text,
|
2021-05-23 16:29:39 +02:00
|
|
|
f"{obj['score']:.0%} {int(obj['area'])}",
|
2021-02-17 14:23:32 +01:00
|
|
|
thickness=thickness,
|
|
|
|
color=color,
|
|
|
|
)
|
2021-01-11 18:09:43 +01:00
|
|
|
|
2023-06-17 16:56:22 +02:00
|
|
|
# draw any attributes
|
|
|
|
for attribute in obj["current_attributes"]:
|
|
|
|
box = attribute["box"]
|
|
|
|
draw_box_with_label(
|
|
|
|
frame_copy,
|
|
|
|
box[0],
|
|
|
|
box[1],
|
|
|
|
box[2],
|
|
|
|
box[3],
|
|
|
|
attribute["label"],
|
|
|
|
f"{attribute['score']:.0%}",
|
|
|
|
thickness=thickness,
|
|
|
|
color=color,
|
|
|
|
)
|
|
|
|
|
2021-02-17 14:23:32 +01:00
|
|
|
if draw_options.get("regions"):
|
2020-12-19 15:22:31 +01:00
|
|
|
for region in regions:
|
2021-02-17 14:23:32 +01:00
|
|
|
cv2.rectangle(
|
|
|
|
frame_copy,
|
|
|
|
(region[0], region[1]),
|
|
|
|
(region[2], region[3]),
|
|
|
|
(0, 255, 0),
|
|
|
|
2,
|
|
|
|
)
|
2020-12-19 15:22:31 +01:00
|
|
|
|
2021-02-17 14:23:32 +01:00
|
|
|
if draw_options.get("zones"):
|
2020-12-19 15:22:31 +01:00
|
|
|
for name, zone in self.camera_config.zones.items():
|
2021-02-17 14:23:32 +01:00
|
|
|
thickness = (
|
|
|
|
8
|
|
|
|
if any(
|
2021-05-23 16:29:39 +02:00
|
|
|
name in obj["current_zones"] for obj in tracked_objects.values()
|
2021-02-17 14:23:32 +01:00
|
|
|
)
|
|
|
|
else 2
|
|
|
|
)
|
2020-12-19 15:22:31 +01:00
|
|
|
cv2.drawContours(frame_copy, [zone.contour], -1, zone.color, thickness)
|
2021-01-11 18:09:43 +01:00
|
|
|
|
2021-02-17 14:23:32 +01:00
|
|
|
if draw_options.get("motion_boxes"):
|
2020-12-19 15:22:31 +01:00
|
|
|
for m_box in motion_boxes:
|
2021-02-17 14:23:32 +01:00
|
|
|
cv2.rectangle(
|
|
|
|
frame_copy,
|
|
|
|
(m_box[0], m_box[1]),
|
|
|
|
(m_box[2], m_box[3]),
|
|
|
|
(0, 0, 255),
|
|
|
|
2,
|
|
|
|
)
|
2020-12-19 15:22:31 +01:00
|
|
|
|
2021-02-17 14:23:32 +01:00
|
|
|
if draw_options.get("timestamp"):
|
2021-06-24 23:02:46 +02:00
|
|
|
color = self.camera_config.timestamp_style.color
|
2021-04-28 19:54:25 +02:00
|
|
|
draw_timestamp(
|
2021-02-17 14:23:32 +01:00
|
|
|
frame_copy,
|
2021-04-28 19:54:25 +02:00
|
|
|
frame_time,
|
2021-06-15 22:19:49 +02:00
|
|
|
self.camera_config.timestamp_style.format,
|
|
|
|
font_effect=self.camera_config.timestamp_style.effect,
|
|
|
|
font_thickness=self.camera_config.timestamp_style.thickness,
|
2021-09-04 23:39:56 +02:00
|
|
|
font_color=(color.blue, color.green, color.red),
|
2021-06-15 22:19:49 +02:00
|
|
|
position=self.camera_config.timestamp_style.position,
|
2021-02-17 14:23:32 +01:00
|
|
|
)
|
2021-01-11 18:09:43 +01:00
|
|
|
|
2020-10-11 19:16:05 +02:00
|
|
|
return frame_copy
|
2020-10-07 23:44:21 +02:00
|
|
|
|
2020-11-25 17:37:41 +01:00
|
|
|
def finished(self, obj_id):
|
|
|
|
del self.tracked_objects[obj_id]
|
|
|
|
|
2022-04-18 13:52:13 +02:00
|
|
|
def on(self, event_type: str, callback: Callable[[dict], None]):
|
2020-09-07 19:17:42 +02:00
|
|
|
self.callbacks[event_type].append(callback)
|
|
|
|
|
2020-12-19 15:22:31 +01:00
|
|
|
def update(self, frame_time, current_detections, motion_boxes, regions):
|
2020-11-08 23:05:15 +01:00
|
|
|
# get the new frame
|
2020-09-07 19:17:42 +02:00
|
|
|
frame_id = f"{self.name}{frame_time}"
|
2024-09-03 18:22:30 +02:00
|
|
|
|
2021-02-17 14:23:32 +01:00
|
|
|
current_frame = self.frame_manager.get(
|
|
|
|
frame_id, self.camera_config.frame_shape_yuv
|
|
|
|
)
|
2020-09-07 19:17:42 +02:00
|
|
|
|
2024-09-03 18:22:30 +02:00
|
|
|
if current_frame is None:
|
|
|
|
logger.debug(f"Failed to get frame {frame_id} from SHM")
|
|
|
|
|
2021-05-23 16:29:39 +02:00
|
|
|
tracked_objects = self.tracked_objects.copy()
|
|
|
|
current_ids = set(current_detections.keys())
|
|
|
|
previous_ids = set(tracked_objects.keys())
|
|
|
|
removed_ids = previous_ids.difference(current_ids)
|
|
|
|
new_ids = current_ids.difference(previous_ids)
|
|
|
|
updated_ids = current_ids.intersection(previous_ids)
|
2020-09-07 19:17:42 +02:00
|
|
|
|
|
|
|
for id in new_ids:
|
2021-05-23 16:29:39 +02:00
|
|
|
new_obj = tracked_objects[id] = TrackedObject(
|
2024-10-16 23:22:34 +02:00
|
|
|
self.config.model,
|
2021-08-16 15:02:04 +02:00
|
|
|
self.camera_config,
|
|
|
|
self.frame_cache,
|
|
|
|
current_detections[id],
|
2021-02-17 14:23:32 +01:00
|
|
|
)
|
2020-09-07 19:17:42 +02:00
|
|
|
|
|
|
|
# call event handlers
|
2021-02-17 14:23:32 +01:00
|
|
|
for c in self.callbacks["start"]:
|
2020-11-25 19:06:01 +01:00
|
|
|
c(self.name, new_obj, frame_time)
|
2021-01-11 18:09:43 +01:00
|
|
|
|
2020-09-07 19:17:42 +02:00
|
|
|
for id in updated_ids:
|
2021-05-23 16:29:39 +02:00
|
|
|
updated_obj = tracked_objects[id]
|
2023-07-08 14:04:47 +02:00
|
|
|
thumb_update, significant_update, autotracker_update = updated_obj.update(
|
2024-09-03 18:22:30 +02:00
|
|
|
frame_time, current_detections[id], current_frame is not None
|
2021-07-07 14:02:36 +02:00
|
|
|
)
|
2020-11-05 15:39:21 +01:00
|
|
|
|
2023-07-08 14:04:47 +02:00
|
|
|
if autotracker_update or significant_update:
|
|
|
|
for c in self.callbacks["autotrack"]:
|
|
|
|
c(self.name, updated_obj, frame_time)
|
|
|
|
|
2024-09-03 18:22:30 +02:00
|
|
|
if thumb_update and current_frame is not None:
|
2020-12-20 14:19:18 +01:00
|
|
|
# ensure this frame is stored in the cache
|
2021-02-17 14:23:32 +01:00
|
|
|
if (
|
|
|
|
updated_obj.thumbnail_data["frame_time"] == frame_time
|
|
|
|
and frame_time not in self.frame_cache
|
|
|
|
):
|
2020-12-20 14:19:18 +01:00
|
|
|
self.frame_cache[frame_time] = np.copy(current_frame)
|
2021-02-17 14:23:32 +01:00
|
|
|
|
2021-01-22 13:40:01 +01:00
|
|
|
updated_obj.last_updated = frame_time
|
2021-02-17 14:23:32 +01:00
|
|
|
|
2022-02-08 14:31:07 +01:00
|
|
|
# if it has been more than 5 seconds since the last thumb update
|
2021-07-07 14:02:36 +02:00
|
|
|
# and the last update is greater than the last publish or
|
2022-02-08 14:31:07 +01:00
|
|
|
# the object has changed significantly
|
2021-02-17 14:23:32 +01:00
|
|
|
if (
|
|
|
|
frame_time - updated_obj.last_published > 5
|
|
|
|
and updated_obj.last_updated > updated_obj.last_published
|
2022-02-08 14:31:07 +01:00
|
|
|
) or significant_update:
|
2020-12-20 14:19:18 +01:00
|
|
|
# call event handlers
|
2021-02-17 14:23:32 +01:00
|
|
|
for c in self.callbacks["update"]:
|
2020-12-20 14:19:18 +01:00
|
|
|
c(self.name, updated_obj, frame_time)
|
2021-01-22 13:40:01 +01:00
|
|
|
updated_obj.last_published = frame_time
|
2021-01-11 18:09:43 +01:00
|
|
|
|
2020-09-07 19:17:42 +02:00
|
|
|
for id in removed_ids:
|
|
|
|
# publish events to mqtt
|
2021-05-23 16:29:39 +02:00
|
|
|
removed_obj = tracked_objects[id]
|
2023-05-29 12:31:17 +02:00
|
|
|
if "end_time" not in removed_obj.obj_data:
|
2021-02-17 14:23:32 +01:00
|
|
|
removed_obj.obj_data["end_time"] = frame_time
|
|
|
|
for c in self.callbacks["end"]:
|
2020-11-25 19:06:01 +01:00
|
|
|
c(self.name, removed_obj, frame_time)
|
2020-09-07 19:17:42 +02:00
|
|
|
|
2020-11-08 23:05:15 +01:00
|
|
|
# TODO: can i switch to looking this up and only changing when an event ends?
|
2020-09-07 19:17:42 +02:00
|
|
|
# maintain best objects
|
2024-04-30 15:09:50 +02:00
|
|
|
camera_activity: dict[str, list[any]] = {
|
|
|
|
"motion": len(motion_boxes) > 0,
|
|
|
|
"objects": [],
|
|
|
|
}
|
|
|
|
|
2021-05-23 16:29:39 +02:00
|
|
|
for obj in tracked_objects.values():
|
2021-02-17 14:23:32 +01:00
|
|
|
object_type = obj.obj_data["label"]
|
2024-09-02 15:24:15 +02:00
|
|
|
active = obj.is_active()
|
2024-04-30 15:09:50 +02:00
|
|
|
|
|
|
|
if not obj.false_positive:
|
|
|
|
label = object_type
|
2024-05-01 16:07:56 +02:00
|
|
|
sub_label = None
|
2024-04-30 15:09:50 +02:00
|
|
|
|
2024-05-01 02:35:23 +02:00
|
|
|
if obj.obj_data.get("sub_label"):
|
2024-09-28 15:49:04 +02:00
|
|
|
if (
|
|
|
|
obj.obj_data.get("sub_label")[0]
|
|
|
|
in self.config.model.all_attributes
|
|
|
|
):
|
2024-05-01 02:35:23 +02:00
|
|
|
label = obj.obj_data["sub_label"][0]
|
|
|
|
else:
|
|
|
|
label = f"{object_type}-verified"
|
2024-05-01 16:07:56 +02:00
|
|
|
sub_label = obj.obj_data["sub_label"][0]
|
2024-04-30 15:09:50 +02:00
|
|
|
|
|
|
|
camera_activity["objects"].append(
|
2024-05-01 16:07:56 +02:00
|
|
|
{
|
|
|
|
"id": obj.obj_data["id"],
|
|
|
|
"label": label,
|
|
|
|
"stationary": not active,
|
|
|
|
"area": obj.obj_data["area"],
|
|
|
|
"ratio": obj.obj_data["ratio"],
|
|
|
|
"score": obj.obj_data["score"],
|
|
|
|
"sub_label": sub_label,
|
|
|
|
}
|
2024-04-30 15:09:50 +02:00
|
|
|
)
|
|
|
|
|
2024-09-09 16:29:05 +02:00
|
|
|
# if we don't have access to the current frame or
|
|
|
|
# if the object's thumbnail is not from the current frame, skip
|
|
|
|
if (
|
|
|
|
current_frame is None
|
2024-10-21 16:38:48 +02:00
|
|
|
or obj.thumbnail_data is None
|
2024-09-09 16:29:05 +02:00
|
|
|
or obj.false_positive
|
|
|
|
or obj.thumbnail_data["frame_time"] != frame_time
|
|
|
|
):
|
2020-09-07 19:17:42 +02:00
|
|
|
continue
|
2024-09-09 16:29:05 +02:00
|
|
|
|
2020-09-07 19:17:42 +02:00
|
|
|
if object_type in self.best_objects:
|
|
|
|
current_best = self.best_objects[object_type]
|
|
|
|
now = datetime.datetime.now().timestamp()
|
2021-01-11 18:09:43 +01:00
|
|
|
# if the object is a higher score than the current best score
|
2020-09-18 14:07:46 +02:00
|
|
|
# or the current object is older than desired, use the new object
|
2021-02-17 14:23:32 +01:00
|
|
|
if (
|
|
|
|
is_better_thumbnail(
|
2023-06-17 16:56:22 +02:00
|
|
|
object_type,
|
2021-02-17 14:23:32 +01:00
|
|
|
current_best.thumbnail_data,
|
|
|
|
obj.thumbnail_data,
|
|
|
|
self.camera_config.frame_shape,
|
|
|
|
)
|
|
|
|
or (now - current_best.thumbnail_data["frame_time"])
|
|
|
|
> self.camera_config.best_image_timeout
|
|
|
|
):
|
2020-11-10 04:31:45 +01:00
|
|
|
self.best_objects[object_type] = obj
|
2021-02-17 14:23:32 +01:00
|
|
|
for c in self.callbacks["snapshot"]:
|
2020-11-25 19:06:01 +01:00
|
|
|
c(self.name, self.best_objects[object_type], frame_time)
|
2020-09-07 19:17:42 +02:00
|
|
|
else:
|
2020-11-10 04:31:45 +01:00
|
|
|
self.best_objects[object_type] = obj
|
2021-02-17 14:23:32 +01:00
|
|
|
for c in self.callbacks["snapshot"]:
|
2020-11-25 19:06:01 +01:00
|
|
|
c(self.name, self.best_objects[object_type], frame_time)
|
2021-01-11 18:09:43 +01:00
|
|
|
|
2024-04-30 15:09:50 +02:00
|
|
|
for c in self.callbacks["camera_activity"]:
|
|
|
|
c(self.name, camera_activity)
|
|
|
|
|
2020-09-07 19:17:42 +02:00
|
|
|
# update overall camera state for each object type
|
2021-05-23 16:29:39 +02:00
|
|
|
obj_counter = Counter(
|
|
|
|
obj.obj_data["label"]
|
|
|
|
for obj in tracked_objects.values()
|
|
|
|
if not obj.false_positive
|
|
|
|
)
|
2021-01-11 18:09:43 +01:00
|
|
|
|
2024-09-02 15:24:15 +02:00
|
|
|
active_obj_counter = Counter(
|
|
|
|
obj.obj_data["label"]
|
|
|
|
for obj in tracked_objects.values()
|
|
|
|
if not obj.false_positive and obj.active
|
|
|
|
)
|
|
|
|
|
2022-03-10 13:03:00 +01:00
|
|
|
# keep track of all labels detected for this camera
|
|
|
|
total_label_count = 0
|
2024-09-02 15:24:15 +02:00
|
|
|
total_active_label_count = 0
|
2022-03-10 13:03:00 +01:00
|
|
|
|
2024-09-02 15:24:15 +02:00
|
|
|
# report on all detected objects
|
2020-09-07 19:17:42 +02:00
|
|
|
for obj_name, count in obj_counter.items():
|
2022-03-10 13:03:00 +01:00
|
|
|
total_label_count += count
|
|
|
|
|
2020-12-01 15:07:17 +01:00
|
|
|
if count != self.object_counts[obj_name]:
|
|
|
|
self.object_counts[obj_name] = count
|
2021-02-17 14:23:32 +01:00
|
|
|
for c in self.callbacks["object_status"]:
|
2020-12-01 15:07:17 +01:00
|
|
|
c(self.name, obj_name, count)
|
2020-09-07 19:17:42 +02:00
|
|
|
|
2024-09-02 15:24:15 +02:00
|
|
|
# update the active count on all detected objects
|
|
|
|
# To ensure we emit 0's if all objects are stationary, we need to loop
|
|
|
|
# over the set of all objects, not just active ones.
|
|
|
|
for obj_name in set(obj_counter):
|
|
|
|
count = active_obj_counter[obj_name]
|
|
|
|
total_active_label_count += count
|
|
|
|
|
|
|
|
if count != self.active_object_counts[obj_name]:
|
|
|
|
self.active_object_counts[obj_name] = count
|
|
|
|
for c in self.callbacks["active_object_status"]:
|
|
|
|
c(self.name, obj_name, count)
|
|
|
|
|
2022-03-10 13:03:00 +01:00
|
|
|
# publish for all labels detected for this camera
|
|
|
|
if total_label_count != self.object_counts.get("all"):
|
|
|
|
self.object_counts["all"] = total_label_count
|
|
|
|
for c in self.callbacks["object_status"]:
|
|
|
|
c(self.name, "all", total_label_count)
|
|
|
|
|
2024-09-02 15:24:15 +02:00
|
|
|
# publish active label counts for this camera
|
|
|
|
if total_active_label_count != self.active_object_counts.get("all"):
|
|
|
|
self.active_object_counts["all"] = total_active_label_count
|
|
|
|
for c in self.callbacks["active_object_status"]:
|
|
|
|
c(self.name, "all", total_active_label_count)
|
|
|
|
|
2020-12-01 15:07:17 +01:00
|
|
|
# expire any objects that are >0 and no longer detected
|
2021-02-17 14:23:32 +01:00
|
|
|
expired_objects = [
|
|
|
|
obj_name
|
|
|
|
for obj_name, count in self.object_counts.items()
|
2021-05-23 16:29:39 +02:00
|
|
|
if count > 0 and obj_name not in obj_counter
|
2021-02-17 14:23:32 +01:00
|
|
|
]
|
2020-09-07 19:17:42 +02:00
|
|
|
for obj_name in expired_objects:
|
2022-03-10 13:03:00 +01:00
|
|
|
# Ignore the artificial all label
|
|
|
|
if obj_name == "all":
|
|
|
|
continue
|
|
|
|
|
2020-12-01 15:07:17 +01:00
|
|
|
self.object_counts[obj_name] = 0
|
2021-02-17 14:23:32 +01:00
|
|
|
for c in self.callbacks["object_status"]:
|
2020-12-01 15:07:17 +01:00
|
|
|
c(self.name, obj_name, 0)
|
2024-09-02 15:24:15 +02:00
|
|
|
# Only publish if the object was previously active.
|
|
|
|
if self.active_object_counts[obj_name] > 0:
|
|
|
|
for c in self.callbacks["active_object_status"]:
|
|
|
|
c(self.name, obj_name, 0)
|
|
|
|
self.active_object_counts[obj_name] = 0
|
2021-02-17 14:23:32 +01:00
|
|
|
for c in self.callbacks["snapshot"]:
|
2020-11-25 19:06:01 +01:00
|
|
|
c(self.name, self.best_objects[obj_name], frame_time)
|
2021-01-11 18:09:43 +01:00
|
|
|
|
2020-11-08 23:05:15 +01:00
|
|
|
# cleanup thumbnail frame cache
|
2021-05-23 16:29:39 +02:00
|
|
|
current_thumb_frames = {
|
|
|
|
obj.thumbnail_data["frame_time"]
|
|
|
|
for obj in tracked_objects.values()
|
2024-09-10 15:39:37 +02:00
|
|
|
if not obj.false_positive and obj.thumbnail_data is not None
|
2021-05-23 16:29:39 +02:00
|
|
|
}
|
|
|
|
current_best_frames = {
|
|
|
|
obj.thumbnail_data["frame_time"] for obj in self.best_objects.values()
|
|
|
|
}
|
2021-02-17 14:23:32 +01:00
|
|
|
thumb_frames_to_delete = [
|
|
|
|
t
|
|
|
|
for t in self.frame_cache.keys()
|
2021-05-23 16:29:39 +02:00
|
|
|
if t not in current_thumb_frames and t not in current_best_frames
|
2021-02-17 14:23:32 +01:00
|
|
|
]
|
2020-11-08 23:05:15 +01:00
|
|
|
for t in thumb_frames_to_delete:
|
2020-11-11 23:55:50 +01:00
|
|
|
del self.frame_cache[t]
|
2021-01-11 18:09:43 +01:00
|
|
|
|
2020-10-11 19:16:05 +02:00
|
|
|
with self.current_frame_lock:
|
2021-05-23 16:29:39 +02:00
|
|
|
self.tracked_objects = tracked_objects
|
|
|
|
self.motion_boxes = motion_boxes
|
|
|
|
self.regions = regions
|
2024-09-03 18:22:30 +02:00
|
|
|
|
|
|
|
if current_frame is not None:
|
|
|
|
self.current_frame_time = frame_time
|
|
|
|
self._current_frame = current_frame
|
|
|
|
|
|
|
|
if self.previous_frame_id is not None:
|
|
|
|
self.frame_manager.close(self.previous_frame_id)
|
|
|
|
|
2020-10-11 19:16:05 +02:00
|
|
|
self.previous_frame_id = frame_id
|
2020-09-07 19:17:42 +02:00
|
|
|
|
2021-02-17 14:23:32 +01:00
|
|
|
|
2020-02-16 04:07:54 +01:00
|
|
|
class TrackedObjectProcessor(threading.Thread):
|
2021-02-17 14:23:32 +01:00
|
|
|
def __init__(
|
|
|
|
self,
|
|
|
|
config: FrigateConfig,
|
2022-11-24 03:03:20 +01:00
|
|
|
dispatcher: Dispatcher,
|
2021-02-17 14:23:32 +01:00
|
|
|
tracked_objects_queue,
|
2023-07-08 14:04:47 +02:00
|
|
|
ptz_autotracker_thread,
|
2021-02-17 14:23:32 +01:00
|
|
|
stop_event,
|
|
|
|
):
|
2024-10-03 03:35:46 +02:00
|
|
|
super().__init__(name="detected_frames_processor")
|
2020-11-08 23:05:15 +01:00
|
|
|
self.config = config
|
2022-11-24 03:03:20 +01:00
|
|
|
self.dispatcher = dispatcher
|
2020-02-16 04:07:54 +01:00
|
|
|
self.tracked_objects_queue = tracked_objects_queue
|
2024-04-09 01:19:45 +02:00
|
|
|
self.stop_event: MpEvent = stop_event
|
2022-04-16 17:38:07 +02:00
|
|
|
self.camera_states: dict[str, CameraState] = {}
|
2020-09-22 04:02:00 +02:00
|
|
|
self.frame_manager = SharedMemoryFrameManager()
|
2022-05-15 14:45:04 +02:00
|
|
|
self.last_motion_detected: dict[str, float] = {}
|
2023-07-08 14:04:47 +02:00
|
|
|
self.ptz_autotracker_thread = ptz_autotracker_thread
|
2024-04-30 15:09:50 +02:00
|
|
|
|
|
|
|
self.requestor = InterProcessRequestor()
|
2024-02-19 14:26:59 +01:00
|
|
|
self.detection_publisher = DetectionPublisher(DetectionTypeEnum.video)
|
2024-03-23 17:11:32 +01:00
|
|
|
self.event_sender = EventUpdatePublisher()
|
2024-04-09 01:19:45 +02:00
|
|
|
self.event_end_subscriber = EventEndSubscriber()
|
2020-09-07 19:17:42 +02:00
|
|
|
|
2024-04-30 15:09:50 +02:00
|
|
|
self.camera_activity: dict[str, dict[str, any]] = {}
|
|
|
|
|
2024-09-02 15:24:15 +02:00
|
|
|
# {
|
|
|
|
# 'zone_name': {
|
|
|
|
# 'person': {
|
|
|
|
# 'camera_1': 2,
|
|
|
|
# 'camera_2': 1
|
|
|
|
# }
|
|
|
|
# }
|
|
|
|
# }
|
|
|
|
self.zone_data = defaultdict(lambda: defaultdict(dict))
|
|
|
|
self.active_zone_data = defaultdict(lambda: defaultdict(dict))
|
|
|
|
|
2020-11-25 19:06:01 +01:00
|
|
|
def start(camera, obj: TrackedObject, current_frame_time):
|
2024-03-23 17:11:32 +01:00
|
|
|
self.event_sender.publish(
|
|
|
|
(
|
|
|
|
EventTypeEnum.tracked_object,
|
|
|
|
EventStateEnum.start,
|
|
|
|
camera,
|
|
|
|
obj.to_dict(),
|
|
|
|
)
|
2023-04-30 19:07:14 +02:00
|
|
|
)
|
2020-09-07 19:17:42 +02:00
|
|
|
|
2020-11-25 19:06:01 +01:00
|
|
|
def update(camera, obj: TrackedObject, current_frame_time):
|
2021-10-23 23:18:13 +02:00
|
|
|
obj.has_snapshot = self.should_save_snapshot(camera, obj)
|
|
|
|
obj.has_clip = self.should_retain_recording(camera, obj)
|
2020-12-20 14:19:18 +01:00
|
|
|
after = obj.to_dict()
|
2021-02-17 14:23:32 +01:00
|
|
|
message = {
|
|
|
|
"before": obj.previous,
|
|
|
|
"after": after,
|
|
|
|
"type": "new" if obj.previous["false_positive"] else "update",
|
|
|
|
}
|
2022-11-26 03:10:09 +01:00
|
|
|
self.dispatcher.publish("events", json.dumps(message), retain=False)
|
2020-12-20 14:19:18 +01:00
|
|
|
obj.previous = after
|
2024-03-23 17:11:32 +01:00
|
|
|
self.event_sender.publish(
|
2023-04-30 19:07:14 +02:00
|
|
|
(
|
|
|
|
EventTypeEnum.tracked_object,
|
2024-03-23 17:11:32 +01:00
|
|
|
EventStateEnum.update,
|
2023-04-30 19:07:14 +02:00
|
|
|
camera,
|
|
|
|
obj.to_dict(include_thumbnail=True),
|
|
|
|
)
|
2021-10-23 23:18:13 +02:00
|
|
|
)
|
2020-09-07 19:17:42 +02:00
|
|
|
|
2023-07-08 14:04:47 +02:00
|
|
|
def autotrack(camera, obj: TrackedObject, current_frame_time):
|
|
|
|
self.ptz_autotracker_thread.ptz_autotracker.autotrack_object(camera, obj)
|
|
|
|
|
2020-11-25 19:06:01 +01:00
|
|
|
def end(camera, obj: TrackedObject, current_frame_time):
|
2021-09-15 14:16:52 +02:00
|
|
|
# populate has_snapshot
|
|
|
|
obj.has_snapshot = self.should_save_snapshot(camera, obj)
|
|
|
|
obj.has_clip = self.should_retain_recording(camera, obj)
|
|
|
|
|
|
|
|
# write the snapshot to disk
|
|
|
|
if obj.has_snapshot:
|
|
|
|
snapshot_config: SnapshotsConfig = self.config.cameras[camera].snapshots
|
|
|
|
jpg_bytes = obj.get_jpg_bytes(
|
|
|
|
timestamp=snapshot_config.timestamp,
|
|
|
|
bounding_box=snapshot_config.bounding_box,
|
|
|
|
crop=snapshot_config.crop,
|
|
|
|
height=snapshot_config.height,
|
|
|
|
quality=snapshot_config.quality,
|
|
|
|
)
|
|
|
|
if jpg_bytes is None:
|
|
|
|
logger.warning(f"Unable to save snapshot for {obj.obj_data['id']}.")
|
|
|
|
else:
|
|
|
|
with open(
|
|
|
|
os.path.join(CLIPS_DIR, f"{camera}-{obj.obj_data['id']}.jpg"),
|
|
|
|
"wb",
|
|
|
|
) as j:
|
|
|
|
j.write(jpg_bytes)
|
|
|
|
|
|
|
|
# write clean snapshot if enabled
|
|
|
|
if snapshot_config.clean_copy:
|
|
|
|
png_bytes = obj.get_clean_png()
|
|
|
|
if png_bytes is None:
|
|
|
|
logger.warning(
|
|
|
|
f"Unable to save clean snapshot for {obj.obj_data['id']}."
|
|
|
|
)
|
|
|
|
else:
|
|
|
|
with open(
|
|
|
|
os.path.join(
|
|
|
|
CLIPS_DIR,
|
|
|
|
f"{camera}-{obj.obj_data['id']}-clean.png",
|
|
|
|
),
|
|
|
|
"wb",
|
|
|
|
) as p:
|
|
|
|
p.write(png_bytes)
|
|
|
|
|
2020-11-26 03:22:54 +01:00
|
|
|
if not obj.false_positive:
|
2021-02-17 14:23:32 +01:00
|
|
|
message = {
|
|
|
|
"before": obj.previous,
|
|
|
|
"after": obj.to_dict(),
|
|
|
|
"type": "end",
|
|
|
|
}
|
2022-12-09 02:00:44 +01:00
|
|
|
self.dispatcher.publish("events", json.dumps(message), retain=False)
|
2023-07-08 14:04:47 +02:00
|
|
|
self.ptz_autotracker_thread.ptz_autotracker.end_object(camera, obj)
|
2021-09-15 14:16:52 +02:00
|
|
|
|
2024-03-23 17:11:32 +01:00
|
|
|
self.event_sender.publish(
|
2023-04-30 19:07:14 +02:00
|
|
|
(
|
|
|
|
EventTypeEnum.tracked_object,
|
2024-03-23 17:11:32 +01:00
|
|
|
EventStateEnum.end,
|
2023-04-30 19:07:14 +02:00
|
|
|
camera,
|
|
|
|
obj.to_dict(include_thumbnail=True),
|
|
|
|
)
|
|
|
|
)
|
2021-02-17 14:23:32 +01:00
|
|
|
|
2020-11-25 19:06:01 +01:00
|
|
|
def snapshot(camera, obj: TrackedObject, current_frame_time):
|
2022-11-20 14:36:01 +01:00
|
|
|
mqtt_config: MqttConfig = self.config.cameras[camera].mqtt
|
2021-02-05 04:44:44 +01:00
|
|
|
if mqtt_config.enabled and self.should_mqtt_snapshot(camera, obj):
|
2020-12-22 22:18:34 +01:00
|
|
|
jpg_bytes = obj.get_jpg_bytes(
|
|
|
|
timestamp=mqtt_config.timestamp,
|
|
|
|
bounding_box=mqtt_config.bounding_box,
|
|
|
|
crop=mqtt_config.crop,
|
2021-02-17 14:23:32 +01:00
|
|
|
height=mqtt_config.height,
|
2021-07-02 14:47:03 +02:00
|
|
|
quality=mqtt_config.quality,
|
2020-12-22 22:18:34 +01:00
|
|
|
)
|
2021-02-09 14:35:41 +01:00
|
|
|
|
|
|
|
if jpg_bytes is None:
|
2021-02-17 14:23:32 +01:00
|
|
|
logger.warning(
|
|
|
|
f"Unable to send mqtt snapshot for {obj.obj_data['id']}."
|
|
|
|
)
|
2021-02-09 14:35:41 +01:00
|
|
|
else:
|
2022-11-24 03:03:20 +01:00
|
|
|
self.dispatcher.publish(
|
2022-11-26 03:10:09 +01:00
|
|
|
f"{camera}/{obj.obj_data['label']}/snapshot",
|
2021-02-17 14:23:32 +01:00
|
|
|
jpg_bytes,
|
|
|
|
retain=True,
|
|
|
|
)
|
|
|
|
|
2020-09-07 19:17:42 +02:00
|
|
|
def object_status(camera, object_name, status):
|
2022-11-26 03:10:09 +01:00
|
|
|
self.dispatcher.publish(f"{camera}/{object_name}", status, retain=False)
|
2020-09-07 19:17:42 +02:00
|
|
|
|
2024-09-02 15:24:15 +02:00
|
|
|
def active_object_status(camera, object_name, status):
|
|
|
|
self.dispatcher.publish(
|
|
|
|
f"{camera}/{object_name}/active", status, retain=False
|
|
|
|
)
|
|
|
|
|
2024-04-30 15:09:50 +02:00
|
|
|
def camera_activity(camera, activity):
|
|
|
|
last_activity = self.camera_activity.get(camera)
|
|
|
|
|
|
|
|
if not last_activity or activity != last_activity:
|
|
|
|
self.camera_activity[camera] = activity
|
|
|
|
self.requestor.send_data(UPDATE_CAMERA_ACTIVITY, self.camera_activity)
|
|
|
|
|
2020-11-08 23:05:15 +01:00
|
|
|
for camera in self.config.cameras.keys():
|
2023-07-08 14:04:47 +02:00
|
|
|
camera_state = CameraState(
|
|
|
|
camera, self.config, self.frame_manager, self.ptz_autotracker_thread
|
|
|
|
)
|
2021-02-17 14:23:32 +01:00
|
|
|
camera_state.on("start", start)
|
2023-07-08 14:04:47 +02:00
|
|
|
camera_state.on("autotrack", autotrack)
|
2021-02-17 14:23:32 +01:00
|
|
|
camera_state.on("update", update)
|
|
|
|
camera_state.on("end", end)
|
|
|
|
camera_state.on("snapshot", snapshot)
|
|
|
|
camera_state.on("object_status", object_status)
|
2024-09-02 15:24:15 +02:00
|
|
|
camera_state.on("active_object_status", active_object_status)
|
2024-04-30 15:09:50 +02:00
|
|
|
camera_state.on("camera_activity", camera_activity)
|
2020-09-07 19:17:42 +02:00
|
|
|
self.camera_states[camera] = camera_state
|
|
|
|
|
2021-02-05 04:44:44 +01:00
|
|
|
def should_save_snapshot(self, camera, obj: TrackedObject):
|
2021-09-15 14:16:52 +02:00
|
|
|
if obj.false_positive:
|
|
|
|
return False
|
|
|
|
|
|
|
|
snapshot_config: SnapshotsConfig = self.config.cameras[camera].snapshots
|
|
|
|
|
|
|
|
if not snapshot_config.enabled:
|
|
|
|
return False
|
|
|
|
|
2022-02-05 14:09:31 +01:00
|
|
|
# object never changed position
|
|
|
|
if obj.obj_data["position_changes"] == 0:
|
|
|
|
return False
|
|
|
|
|
2021-02-05 04:44:44 +01:00
|
|
|
# if there are required zones and there is no overlap
|
2021-09-15 14:16:52 +02:00
|
|
|
required_zones = snapshot_config.required_zones
|
2021-12-12 16:29:57 +01:00
|
|
|
if len(required_zones) > 0 and not set(obj.entered_zones) & set(required_zones):
|
2021-02-17 14:23:32 +01:00
|
|
|
logger.debug(
|
|
|
|
f"Not creating snapshot for {obj.obj_data['id']} because it did not enter required zones"
|
|
|
|
)
|
2021-02-05 04:44:44 +01:00
|
|
|
return False
|
|
|
|
|
|
|
|
return True
|
|
|
|
|
2024-04-13 14:08:20 +02:00
|
|
|
def should_retain_recording(self, camera: str, obj: TrackedObject):
|
2021-09-15 14:16:52 +02:00
|
|
|
if obj.false_positive:
|
|
|
|
return False
|
|
|
|
|
|
|
|
record_config: RecordConfig = self.config.cameras[camera].record
|
|
|
|
|
|
|
|
# Recording is disabled
|
|
|
|
if not record_config.enabled:
|
|
|
|
return False
|
|
|
|
|
2022-02-05 14:09:31 +01:00
|
|
|
# object never changed position
|
|
|
|
if obj.obj_data["position_changes"] == 0:
|
|
|
|
return False
|
|
|
|
|
2024-09-02 15:22:53 +02:00
|
|
|
# If the object is not considered an alert or detection
|
2024-04-13 14:08:20 +02:00
|
|
|
review_config = self.config.cameras[camera].review
|
2024-09-02 15:22:53 +02:00
|
|
|
if not (
|
|
|
|
(
|
|
|
|
obj.obj_data["label"] in review_config.alerts.labels
|
|
|
|
and (
|
|
|
|
not review_config.alerts.required_zones
|
|
|
|
or set(obj.entered_zones) & set(review_config.alerts.required_zones)
|
|
|
|
)
|
|
|
|
)
|
|
|
|
or (
|
2024-09-17 01:23:10 +02:00
|
|
|
(
|
|
|
|
not review_config.detections.labels
|
|
|
|
or obj.obj_data["label"] in review_config.detections.labels
|
|
|
|
)
|
|
|
|
and (
|
|
|
|
not review_config.detections.required_zones
|
|
|
|
or set(obj.entered_zones) & set(review_config.alerts.required_zones)
|
|
|
|
)
|
2021-09-15 14:16:52 +02:00
|
|
|
)
|
|
|
|
):
|
|
|
|
logger.debug(
|
2024-09-02 15:22:53 +02:00
|
|
|
f"Not creating clip for {obj.obj_data['id']} because it did not qualify as an alert or detection"
|
2021-09-15 14:16:52 +02:00
|
|
|
)
|
|
|
|
return False
|
|
|
|
|
|
|
|
return True
|
|
|
|
|
2021-02-05 04:44:44 +01:00
|
|
|
def should_mqtt_snapshot(self, camera, obj: TrackedObject):
|
2022-02-05 14:09:31 +01:00
|
|
|
# object never changed position
|
|
|
|
if obj.obj_data["position_changes"] == 0:
|
|
|
|
return False
|
|
|
|
|
2021-02-05 04:44:44 +01:00
|
|
|
# if there are required zones and there is no overlap
|
|
|
|
required_zones = self.config.cameras[camera].mqtt.required_zones
|
2021-12-12 16:29:57 +01:00
|
|
|
if len(required_zones) > 0 and not set(obj.entered_zones) & set(required_zones):
|
2021-02-17 14:23:32 +01:00
|
|
|
logger.debug(
|
|
|
|
f"Not sending mqtt for {obj.obj_data['id']} because it did not enter required zones"
|
|
|
|
)
|
2021-02-05 04:44:44 +01:00
|
|
|
return False
|
|
|
|
|
|
|
|
return True
|
|
|
|
|
2022-05-15 14:45:04 +02:00
|
|
|
def update_mqtt_motion(self, camera, frame_time, motion_boxes):
|
2022-05-15 14:03:33 +02:00
|
|
|
# publish if motion is currently being detected
|
|
|
|
if motion_boxes:
|
2022-05-15 14:45:04 +02:00
|
|
|
# only send ON if motion isn't already active
|
|
|
|
if self.last_motion_detected.get(camera, 0) == 0:
|
2022-11-24 03:03:20 +01:00
|
|
|
self.dispatcher.publish(
|
2022-11-26 03:10:09 +01:00
|
|
|
f"{camera}/motion",
|
2022-05-15 14:03:33 +02:00
|
|
|
"ON",
|
|
|
|
retain=False,
|
|
|
|
)
|
|
|
|
|
|
|
|
# always updated latest motion
|
2022-05-15 14:45:04 +02:00
|
|
|
self.last_motion_detected[camera] = frame_time
|
|
|
|
elif self.last_motion_detected.get(camera, 0) > 0:
|
2022-05-15 14:03:33 +02:00
|
|
|
mqtt_delay = self.config.cameras[camera].motion.mqtt_off_delay
|
|
|
|
|
|
|
|
# If no motion, make sure the off_delay has passed
|
2022-05-15 14:45:04 +02:00
|
|
|
if frame_time - self.last_motion_detected.get(camera, 0) >= mqtt_delay:
|
2022-11-24 03:03:20 +01:00
|
|
|
self.dispatcher.publish(
|
2022-11-26 03:10:09 +01:00
|
|
|
f"{camera}/motion",
|
2022-05-15 14:03:33 +02:00
|
|
|
"OFF",
|
|
|
|
retain=False,
|
|
|
|
)
|
|
|
|
# reset the last_motion so redundant `off` commands aren't sent
|
2022-05-15 14:45:04 +02:00
|
|
|
self.last_motion_detected[camera] = 0
|
2022-05-15 14:03:33 +02:00
|
|
|
|
2020-02-16 04:07:54 +01:00
|
|
|
def get_best(self, camera, label):
|
2020-11-12 00:44:51 +01:00
|
|
|
# TODO: need a lock here
|
|
|
|
camera_state = self.camera_states[camera]
|
|
|
|
if label in camera_state.best_objects:
|
|
|
|
best_obj = camera_state.best_objects[label]
|
2020-12-19 15:48:34 +01:00
|
|
|
best = best_obj.thumbnail_data.copy()
|
2021-02-17 14:23:32 +01:00
|
|
|
best["frame"] = camera_state.frame_cache.get(
|
|
|
|
best_obj.thumbnail_data["frame_time"]
|
|
|
|
)
|
2020-11-12 00:44:51 +01:00
|
|
|
return best
|
2020-02-16 04:07:54 +01:00
|
|
|
else:
|
2020-09-13 15:57:47 +02:00
|
|
|
return {}
|
2021-01-11 18:09:43 +01:00
|
|
|
|
2020-12-19 15:22:31 +01:00
|
|
|
def get_current_frame(self, camera, draw_options={}):
|
2022-12-31 15:54:10 +01:00
|
|
|
if camera == "birdseye":
|
|
|
|
return self.frame_manager.get(
|
|
|
|
"birdseye",
|
|
|
|
(self.config.birdseye.height * 3 // 2, self.config.birdseye.width),
|
|
|
|
)
|
|
|
|
|
2020-12-19 15:22:31 +01:00
|
|
|
return self.camera_states[camera].get_current_frame(draw_options)
|
2020-02-16 04:07:54 +01:00
|
|
|
|
2022-11-29 04:47:20 +01:00
|
|
|
def get_current_frame_time(self, camera) -> int:
|
|
|
|
"""Returns the latest frame time for a given camera."""
|
|
|
|
return self.camera_states[camera].current_frame_time
|
|
|
|
|
2020-03-13 22:13:01 +01:00
|
|
|
def run(self):
|
2021-05-21 17:39:14 +02:00
|
|
|
while not self.stop_event.is_set():
|
2020-08-02 15:46:36 +02:00
|
|
|
try:
|
2021-02-17 14:23:32 +01:00
|
|
|
(
|
|
|
|
camera,
|
|
|
|
frame_time,
|
|
|
|
current_tracked_objects,
|
|
|
|
motion_boxes,
|
|
|
|
regions,
|
2023-02-04 03:15:47 +01:00
|
|
|
) = self.tracked_objects_queue.get(True, 1)
|
2020-08-02 15:46:36 +02:00
|
|
|
except queue.Empty:
|
|
|
|
continue
|
2020-02-16 15:00:41 +01:00
|
|
|
|
2020-09-07 19:17:42 +02:00
|
|
|
camera_state = self.camera_states[camera]
|
|
|
|
|
2021-02-17 14:23:32 +01:00
|
|
|
camera_state.update(
|
|
|
|
frame_time, current_tracked_objects, motion_boxes, regions
|
|
|
|
)
|
2020-09-07 19:17:42 +02:00
|
|
|
|
2022-05-15 14:45:04 +02:00
|
|
|
self.update_mqtt_motion(camera, frame_time, motion_boxes)
|
2022-05-15 14:03:33 +02:00
|
|
|
|
2021-12-11 15:59:54 +01:00
|
|
|
tracked_objects = [
|
|
|
|
o.to_dict() for o in camera_state.tracked_objects.values()
|
|
|
|
]
|
|
|
|
|
2024-02-19 14:26:59 +01:00
|
|
|
# publish info on this frame
|
2024-06-21 23:30:19 +02:00
|
|
|
self.detection_publisher.publish(
|
2021-12-11 05:56:29 +01:00
|
|
|
(
|
|
|
|
camera,
|
|
|
|
frame_time,
|
2021-12-11 15:59:54 +01:00
|
|
|
tracked_objects,
|
2021-12-11 05:56:29 +01:00
|
|
|
motion_boxes,
|
|
|
|
regions,
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
2020-12-01 15:07:17 +01:00
|
|
|
# update zone counts for each label
|
|
|
|
# for each zone in the current camera
|
2020-11-08 23:05:15 +01:00
|
|
|
for zone in self.config.cameras[camera].zones.keys():
|
2020-12-01 15:07:17 +01:00
|
|
|
# count labels for the camera in the zone
|
2021-05-23 16:29:39 +02:00
|
|
|
obj_counter = Counter(
|
|
|
|
obj.obj_data["label"]
|
|
|
|
for obj in camera_state.tracked_objects.values()
|
|
|
|
if zone in obj.current_zones and not obj.false_positive
|
|
|
|
)
|
2024-09-02 15:24:15 +02:00
|
|
|
active_obj_counter = Counter(
|
|
|
|
obj.obj_data["label"]
|
|
|
|
for obj in camera_state.tracked_objects.values()
|
|
|
|
if (
|
|
|
|
zone in obj.current_zones
|
|
|
|
and not obj.false_positive
|
|
|
|
and obj.active
|
|
|
|
)
|
|
|
|
)
|
2022-03-10 13:03:00 +01:00
|
|
|
total_label_count = 0
|
2024-09-02 15:24:15 +02:00
|
|
|
total_active_label_count = 0
|
2021-01-11 18:09:43 +01:00
|
|
|
|
2020-12-01 15:07:17 +01:00
|
|
|
# update counts and publish status
|
2021-05-23 16:29:39 +02:00
|
|
|
for label in set(self.zone_data[zone].keys()) | set(obj_counter.keys()):
|
2022-03-10 13:03:00 +01:00
|
|
|
# Ignore the artificial all label
|
|
|
|
if label == "all":
|
|
|
|
continue
|
|
|
|
|
2020-12-01 15:07:17 +01:00
|
|
|
# if we have previously published a count for this zone/label
|
|
|
|
zone_label = self.zone_data[zone][label]
|
2024-09-02 15:24:15 +02:00
|
|
|
active_zone_label = self.active_zone_data[zone][label]
|
2020-12-01 15:07:17 +01:00
|
|
|
if camera in zone_label:
|
|
|
|
current_count = sum(zone_label.values())
|
2024-09-02 15:24:15 +02:00
|
|
|
current_active_count = sum(active_zone_label.values())
|
2021-02-17 14:23:32 +01:00
|
|
|
zone_label[camera] = (
|
|
|
|
obj_counter[label] if label in obj_counter else 0
|
|
|
|
)
|
2024-09-02 15:24:15 +02:00
|
|
|
active_zone_label[camera] = (
|
|
|
|
active_obj_counter[label]
|
|
|
|
if label in active_obj_counter
|
|
|
|
else 0
|
|
|
|
)
|
2020-12-01 15:07:17 +01:00
|
|
|
new_count = sum(zone_label.values())
|
2024-09-02 15:24:15 +02:00
|
|
|
new_active_count = sum(active_zone_label.values())
|
2020-12-01 15:07:17 +01:00
|
|
|
if new_count != current_count:
|
2022-11-24 03:03:20 +01:00
|
|
|
self.dispatcher.publish(
|
2022-11-26 03:10:09 +01:00
|
|
|
f"{zone}/{label}",
|
2021-02-17 14:23:32 +01:00
|
|
|
new_count,
|
|
|
|
retain=False,
|
|
|
|
)
|
2024-09-02 15:24:15 +02:00
|
|
|
if new_active_count != current_active_count:
|
|
|
|
self.dispatcher.publish(
|
|
|
|
f"{zone}/{label}/active",
|
|
|
|
new_active_count,
|
|
|
|
retain=False,
|
|
|
|
)
|
2022-03-10 13:03:00 +01:00
|
|
|
|
|
|
|
# Set the count for the /zone/all topic.
|
|
|
|
total_label_count += new_count
|
2024-09-02 15:24:15 +02:00
|
|
|
total_active_label_count += new_active_count
|
2022-03-10 13:03:00 +01:00
|
|
|
|
2020-12-01 15:07:17 +01:00
|
|
|
# if this is a new zone/label combo for this camera
|
|
|
|
else:
|
|
|
|
if label in obj_counter:
|
|
|
|
zone_label[camera] = obj_counter[label]
|
2024-09-02 15:24:15 +02:00
|
|
|
active_zone_label[camera] = active_obj_counter[label]
|
2022-11-24 03:03:20 +01:00
|
|
|
self.dispatcher.publish(
|
2022-11-26 03:10:09 +01:00
|
|
|
f"{zone}/{label}",
|
2021-02-17 14:23:32 +01:00
|
|
|
obj_counter[label],
|
|
|
|
retain=False,
|
|
|
|
)
|
2024-09-02 15:24:15 +02:00
|
|
|
self.dispatcher.publish(
|
|
|
|
f"{zone}/{label}/active",
|
|
|
|
active_obj_counter[label],
|
|
|
|
retain=False,
|
|
|
|
)
|
2020-11-25 17:37:41 +01:00
|
|
|
|
2022-03-10 13:03:00 +01:00
|
|
|
# Set the count for the /zone/all topic.
|
|
|
|
total_label_count += obj_counter[label]
|
2024-09-02 15:24:15 +02:00
|
|
|
total_active_label_count += active_obj_counter[label]
|
2022-03-10 13:03:00 +01:00
|
|
|
|
|
|
|
# if we have previously published a count for this zone all labels
|
|
|
|
zone_label = self.zone_data[zone]["all"]
|
2024-09-02 15:24:15 +02:00
|
|
|
active_zone_label = self.active_zone_data[zone]["all"]
|
2022-03-10 13:03:00 +01:00
|
|
|
if camera in zone_label:
|
|
|
|
current_count = sum(zone_label.values())
|
2024-09-02 15:24:15 +02:00
|
|
|
current_active_count = sum(active_zone_label.values())
|
2022-03-10 13:03:00 +01:00
|
|
|
zone_label[camera] = total_label_count
|
2024-09-02 15:24:15 +02:00
|
|
|
active_zone_label[camera] = total_active_label_count
|
2022-03-10 13:03:00 +01:00
|
|
|
new_count = sum(zone_label.values())
|
2024-09-02 15:24:15 +02:00
|
|
|
new_active_count = sum(active_zone_label.values())
|
2022-03-10 13:03:00 +01:00
|
|
|
|
|
|
|
if new_count != current_count:
|
2022-11-24 03:03:20 +01:00
|
|
|
self.dispatcher.publish(
|
2022-11-26 03:10:09 +01:00
|
|
|
f"{zone}/all",
|
2022-03-10 13:03:00 +01:00
|
|
|
new_count,
|
|
|
|
retain=False,
|
|
|
|
)
|
2024-09-02 15:24:15 +02:00
|
|
|
if new_active_count != current_active_count:
|
|
|
|
self.dispatcher.publish(
|
|
|
|
f"{zone}/all/active",
|
|
|
|
new_active_count,
|
|
|
|
retain=False,
|
|
|
|
)
|
2022-03-10 13:03:00 +01:00
|
|
|
# if this is a new zone all label for this camera
|
|
|
|
else:
|
|
|
|
zone_label[camera] = total_label_count
|
2024-09-02 15:24:15 +02:00
|
|
|
active_zone_label[camera] = total_active_label_count
|
2022-11-24 03:03:20 +01:00
|
|
|
self.dispatcher.publish(
|
2022-11-26 03:10:09 +01:00
|
|
|
f"{zone}/all",
|
2022-03-10 13:03:00 +01:00
|
|
|
total_label_count,
|
|
|
|
retain=False,
|
|
|
|
)
|
2024-09-02 15:24:15 +02:00
|
|
|
self.dispatcher.publish(
|
|
|
|
f"{zone}/all/active",
|
|
|
|
total_active_label_count,
|
|
|
|
retain=False,
|
|
|
|
)
|
2022-03-10 13:03:00 +01:00
|
|
|
|
2020-11-25 17:37:41 +01:00
|
|
|
# cleanup event finished queue
|
2024-04-09 01:19:45 +02:00
|
|
|
while not self.stop_event.is_set():
|
|
|
|
update = self.event_end_subscriber.check_for_update(timeout=0.01)
|
|
|
|
|
|
|
|
if not update:
|
|
|
|
break
|
|
|
|
|
2024-06-21 23:30:19 +02:00
|
|
|
event_id, camera, _ = update
|
2020-11-25 17:37:41 +01:00
|
|
|
self.camera_states[camera].finished(event_id)
|
2021-05-21 17:39:14 +02:00
|
|
|
|
2024-04-30 15:09:50 +02:00
|
|
|
self.requestor.stop()
|
2024-02-19 14:26:59 +01:00
|
|
|
self.detection_publisher.stop()
|
2024-03-23 17:11:32 +01:00
|
|
|
self.event_sender.stop()
|
2024-04-09 01:19:45 +02:00
|
|
|
self.event_end_subscriber.stop()
|
2023-05-29 12:31:17 +02:00
|
|
|
logger.info("Exiting object processor...")
|