2019-02-26 03:27:02 +01:00
|
|
|
import datetime
|
2020-11-04 04:26:39 +01:00
|
|
|
import logging
|
2019-03-30 02:49:27 +01:00
|
|
|
import multiprocessing as mp
|
2022-12-09 04:03:54 +01:00
|
|
|
import os
|
2020-11-04 13:31:25 +01:00
|
|
|
import queue
|
2020-11-29 23:19:59 +01:00
|
|
|
import signal
|
2021-10-31 17:12:44 +01:00
|
|
|
import subprocess as sp
|
2020-11-04 13:31:25 +01:00
|
|
|
import threading
|
|
|
|
import time
|
|
|
|
|
2022-11-04 03:23:09 +01:00
|
|
|
import cv2
|
2021-10-31 17:12:44 +01:00
|
|
|
from setproctitle import setproctitle
|
2020-11-04 13:31:25 +01:00
|
|
|
|
2024-09-27 14:53:23 +02:00
|
|
|
from frigate.camera import CameraMetrics, PTZMetrics
|
2024-02-19 14:26:59 +01:00
|
|
|
from frigate.comms.config_updater import ConfigSubscriber
|
2024-02-15 01:24:36 +01:00
|
|
|
from frigate.comms.inter_process import InterProcessRequestor
|
2023-07-06 14:25:37 +02:00
|
|
|
from frigate.config import CameraConfig, DetectConfig, ModelConfig
|
2023-10-19 01:21:52 +02:00
|
|
|
from frigate.const import (
|
|
|
|
CACHE_DIR,
|
2023-11-05 21:30:29 +01:00
|
|
|
CACHE_SEGMENT_FORMAT,
|
2023-10-19 01:21:52 +02:00
|
|
|
REQUEST_REGION_GRID,
|
|
|
|
)
|
2020-12-04 13:59:03 +01:00
|
|
|
from frigate.log import LogPipe
|
2020-02-16 04:07:54 +01:00
|
|
|
from frigate.motion import MotionDetector
|
2023-06-11 15:45:11 +02:00
|
|
|
from frigate.motion.improved_motion import ImprovedMotionDetector
|
2023-05-29 12:31:17 +02:00
|
|
|
from frigate.object_detection import RemoteObjectDetector
|
2023-10-25 01:25:22 +02:00
|
|
|
from frigate.ptz.autotrack import ptz_moving_at_frame_time
|
2023-05-31 16:12:43 +02:00
|
|
|
from frigate.track import ObjectTracker
|
|
|
|
from frigate.track.norfair_tracker import NorfairTracker
|
2024-10-17 18:02:27 +02:00
|
|
|
from frigate.track.tracked_object import TrackedObjectAttribute
|
2023-11-04 03:21:29 +01:00
|
|
|
from frigate.util.builtin import EventsPerSecond, get_tomorrow_at_time
|
2023-07-06 16:28:50 +02:00
|
|
|
from frigate.util.image import (
|
2021-02-17 14:23:32 +01:00
|
|
|
FrameManager,
|
|
|
|
SharedMemoryFrameManager,
|
2023-06-11 15:45:11 +02:00
|
|
|
draw_box_with_label,
|
2023-10-19 01:21:52 +02:00
|
|
|
)
|
|
|
|
from frigate.util.object import (
|
|
|
|
create_tensor_input,
|
|
|
|
get_cluster_candidates,
|
|
|
|
get_cluster_region,
|
|
|
|
get_cluster_region_from_grid,
|
|
|
|
get_min_region_size,
|
|
|
|
get_startup_regions,
|
|
|
|
inside_any,
|
|
|
|
intersects_any,
|
|
|
|
is_object_filtered,
|
2023-10-24 02:20:21 +02:00
|
|
|
reduce_detections,
|
2021-02-17 14:23:32 +01:00
|
|
|
)
|
2023-07-06 16:28:50 +02:00
|
|
|
from frigate.util.services import listen
|
2019-02-26 03:27:02 +01:00
|
|
|
|
2020-11-04 04:26:39 +01:00
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
2021-02-17 14:23:32 +01:00
|
|
|
|
2020-12-04 13:59:03 +01:00
|
|
|
def stop_ffmpeg(ffmpeg_process, logger):
|
2020-11-29 23:19:59 +01:00
|
|
|
logger.info("Terminating the existing ffmpeg process...")
|
|
|
|
ffmpeg_process.terminate()
|
|
|
|
try:
|
|
|
|
logger.info("Waiting for ffmpeg to exit gracefully...")
|
|
|
|
ffmpeg_process.communicate(timeout=30)
|
|
|
|
except sp.TimeoutExpired:
|
2024-04-20 13:16:43 +02:00
|
|
|
logger.info("FFmpeg didn't exit. Force killing...")
|
2020-11-29 23:19:59 +01:00
|
|
|
ffmpeg_process.kill()
|
|
|
|
ffmpeg_process.communicate()
|
|
|
|
ffmpeg_process = None
|
|
|
|
|
2021-02-17 14:23:32 +01:00
|
|
|
|
|
|
|
def start_or_restart_ffmpeg(
|
|
|
|
ffmpeg_cmd, logger, logpipe: LogPipe, frame_size=None, ffmpeg_process=None
|
|
|
|
):
|
Use dataclasses for config handling
Use config data classes to eliminate some of the boilerplate associated
with setting up the configuration. In particular, using dataclasses
removes a lot of the boilerplate around assigning properties to the
object and allows these to be easily immutable by freezing them. In the
case of simple, non-nested dataclasses, this also provides more
convenient `asdict` helpers.
To set this up, where previously the objects would be parsed from the
config via the `__init__` method, create a `build` classmethod that does
this and calls the dataclass initializer.
Some of the objects are mutated at runtime, in particular some of the
zones are mutated to set the color (this might be able to be refactored
out) and some of the camera functionality can be enabled/disabled. Some
of the configs with `enabled` properties don't seem to have mqtt hooks
to be able to toggle this, in particular, the clips, snapshots, and
detect can be toggled but rtmp and record configs do not, but all of
these configs are still not frozen in case there is some other
functionality I am missing.
There are a couple other minor fixes here, one that was introduced
by me recently where `max_seconds` was not defined, the other to
properly `get()` the message payload when handling publishing mqtt
messages sent via websocket.
2021-05-23 00:28:15 +02:00
|
|
|
if ffmpeg_process is not None:
|
2020-12-04 13:59:03 +01:00
|
|
|
stop_ffmpeg(ffmpeg_process, logger)
|
2020-02-27 02:02:12 +01:00
|
|
|
|
2020-11-29 22:55:53 +01:00
|
|
|
if frame_size is None:
|
2021-02-17 14:23:32 +01:00
|
|
|
process = sp.Popen(
|
|
|
|
ffmpeg_cmd,
|
|
|
|
stdout=sp.DEVNULL,
|
|
|
|
stderr=logpipe,
|
|
|
|
stdin=sp.DEVNULL,
|
|
|
|
start_new_session=True,
|
|
|
|
)
|
2020-11-29 22:55:53 +01:00
|
|
|
else:
|
2021-02-17 14:23:32 +01:00
|
|
|
process = sp.Popen(
|
|
|
|
ffmpeg_cmd,
|
|
|
|
stdout=sp.PIPE,
|
|
|
|
stderr=logpipe,
|
|
|
|
stdin=sp.DEVNULL,
|
|
|
|
bufsize=frame_size * 10,
|
|
|
|
start_new_session=True,
|
|
|
|
)
|
2020-03-10 03:12:19 +01:00
|
|
|
return process
|
2020-02-27 02:02:12 +01:00
|
|
|
|
2021-02-17 14:23:32 +01:00
|
|
|
|
|
|
|
def capture_frames(
|
|
|
|
ffmpeg_process,
|
2024-09-03 18:22:30 +02:00
|
|
|
config: CameraConfig,
|
|
|
|
shm_frame_count: int,
|
2021-02-17 14:23:32 +01:00
|
|
|
frame_shape,
|
|
|
|
frame_manager: FrameManager,
|
|
|
|
frame_queue,
|
|
|
|
fps: mp.Value,
|
|
|
|
skipped_fps: mp.Value,
|
|
|
|
current_frame: mp.Value,
|
2023-02-04 15:58:45 +01:00
|
|
|
stop_event: mp.Event,
|
2021-02-17 14:23:32 +01:00
|
|
|
):
|
2020-11-03 15:15:58 +01:00
|
|
|
frame_size = frame_shape[0] * frame_shape[1]
|
2020-10-25 16:05:21 +01:00
|
|
|
frame_rate = EventsPerSecond()
|
2020-10-26 13:59:22 +01:00
|
|
|
frame_rate.start()
|
2020-10-25 16:05:21 +01:00
|
|
|
skipped_eps = EventsPerSecond()
|
|
|
|
skipped_eps.start()
|
2024-09-03 18:22:30 +02:00
|
|
|
|
2024-11-17 00:00:19 +01:00
|
|
|
# pre-create shms
|
|
|
|
for i in range(shm_frame_count):
|
|
|
|
frame_manager.create(f"{config.name}{i}", frame_size)
|
|
|
|
|
|
|
|
frame_index = 0
|
|
|
|
|
2020-08-22 14:05:20 +02:00
|
|
|
while True:
|
2020-10-25 16:05:21 +01:00
|
|
|
fps.value = frame_rate.eps()
|
2023-06-28 12:53:28 +02:00
|
|
|
skipped_fps.value = skipped_eps.eps()
|
2020-09-07 19:17:42 +02:00
|
|
|
current_frame.value = datetime.datetime.now().timestamp()
|
2024-11-17 00:00:19 +01:00
|
|
|
frame_name = f"{config.name}{frame_index}"
|
|
|
|
frame_buffer = frame_manager.write(frame_name)
|
2020-10-24 18:36:04 +02:00
|
|
|
try:
|
2020-12-12 16:12:15 +01:00
|
|
|
frame_buffer[:] = ffmpeg_process.stdout.read(frame_size)
|
2023-05-29 12:31:17 +02:00
|
|
|
except Exception:
|
2023-02-04 15:58:45 +01:00
|
|
|
# shutdown has been initiated
|
|
|
|
if stop_event.is_set():
|
|
|
|
break
|
2024-09-03 18:22:30 +02:00
|
|
|
|
|
|
|
logger.error(f"{config.name}: Unable to read frames from ffmpeg process.")
|
2020-12-12 16:12:15 +01:00
|
|
|
|
2023-05-29 12:31:17 +02:00
|
|
|
if ffmpeg_process.poll() is not None:
|
2022-02-06 15:46:41 +01:00
|
|
|
logger.error(
|
2024-09-03 18:22:30 +02:00
|
|
|
f"{config.name}: ffmpeg process is not running. exiting capture thread..."
|
2021-02-17 14:23:32 +01:00
|
|
|
)
|
2020-12-12 16:12:15 +01:00
|
|
|
break
|
2024-09-03 18:22:30 +02:00
|
|
|
|
2020-12-12 16:12:15 +01:00
|
|
|
continue
|
2020-08-22 14:05:20 +02:00
|
|
|
|
2020-10-25 16:05:21 +01:00
|
|
|
frame_rate.update()
|
2020-08-22 14:05:20 +02:00
|
|
|
|
2023-07-06 15:18:39 +02:00
|
|
|
# don't lock the queue to check, just try since it should rarely be full
|
|
|
|
try:
|
|
|
|
# add to the queue
|
2024-11-17 00:00:19 +01:00
|
|
|
frame_queue.put((frame_name, current_frame.value), False)
|
2024-07-09 18:49:08 +02:00
|
|
|
frame_manager.close(frame_name)
|
2023-07-06 15:18:39 +02:00
|
|
|
except queue.Full:
|
|
|
|
# if the queue is full, skip this frame
|
2020-10-25 16:05:21 +01:00
|
|
|
skipped_eps.update()
|
2024-09-03 18:22:30 +02:00
|
|
|
|
2024-11-17 00:00:19 +01:00
|
|
|
frame_index = 0 if frame_index == shm_frame_count - 1 else frame_index + 1
|
|
|
|
|
|
|
|
frame_manager.cleanup()
|
|
|
|
|
2021-02-17 14:23:32 +01:00
|
|
|
|
2020-10-25 16:05:21 +01:00
|
|
|
class CameraWatchdog(threading.Thread):
|
2021-02-17 14:23:32 +01:00
|
|
|
def __init__(
|
2022-12-09 04:03:54 +01:00
|
|
|
self,
|
|
|
|
camera_name,
|
|
|
|
config: CameraConfig,
|
2024-09-03 18:22:30 +02:00
|
|
|
shm_frame_count: int,
|
2022-12-09 04:03:54 +01:00
|
|
|
frame_queue,
|
|
|
|
camera_fps,
|
2023-06-28 12:53:28 +02:00
|
|
|
skipped_fps,
|
2022-12-09 04:03:54 +01:00
|
|
|
ffmpeg_pid,
|
|
|
|
stop_event,
|
2021-02-17 14:23:32 +01:00
|
|
|
):
|
2020-10-25 16:05:21 +01:00
|
|
|
threading.Thread.__init__(self)
|
2020-12-04 13:59:03 +01:00
|
|
|
self.logger = logging.getLogger(f"watchdog.{camera_name}")
|
2020-11-04 13:28:07 +01:00
|
|
|
self.camera_name = camera_name
|
2020-10-25 16:05:21 +01:00
|
|
|
self.config = config
|
2024-09-03 18:22:30 +02:00
|
|
|
self.shm_frame_count = shm_frame_count
|
2020-10-25 16:05:21 +01:00
|
|
|
self.capture_thread = None
|
2020-11-29 22:55:53 +01:00
|
|
|
self.ffmpeg_detect_process = None
|
2022-04-12 22:24:45 +02:00
|
|
|
self.logpipe = LogPipe(f"ffmpeg.{self.camera_name}.detect")
|
2022-12-09 04:03:54 +01:00
|
|
|
self.ffmpeg_other_processes: list[dict[str, any]] = []
|
2020-10-25 16:05:21 +01:00
|
|
|
self.camera_fps = camera_fps
|
2023-06-28 12:53:28 +02:00
|
|
|
self.skipped_fps = skipped_fps
|
2020-10-26 13:59:05 +01:00
|
|
|
self.ffmpeg_pid = ffmpeg_pid
|
2020-10-25 16:05:21 +01:00
|
|
|
self.frame_queue = frame_queue
|
2020-11-03 15:15:58 +01:00
|
|
|
self.frame_shape = self.config.frame_shape_yuv
|
|
|
|
self.frame_size = self.frame_shape[0] * self.frame_shape[1]
|
2024-09-10 15:39:37 +02:00
|
|
|
self.fps_overflow_count = 0
|
2020-11-29 23:19:59 +01:00
|
|
|
self.stop_event = stop_event
|
2023-06-30 14:14:39 +02:00
|
|
|
self.sleeptime = self.config.ffmpeg.retry_interval
|
2020-10-25 16:05:21 +01:00
|
|
|
|
|
|
|
def run(self):
|
2020-11-29 22:55:53 +01:00
|
|
|
self.start_ffmpeg_detect()
|
|
|
|
|
|
|
|
for c in self.config.ffmpeg_cmds:
|
2021-02-17 14:23:32 +01:00
|
|
|
if "detect" in c["roles"]:
|
2020-11-29 22:55:53 +01:00
|
|
|
continue
|
2021-02-17 14:23:32 +01:00
|
|
|
logpipe = LogPipe(
|
2022-04-12 22:24:45 +02:00
|
|
|
f"ffmpeg.{self.camera_name}.{'_'.join(sorted(c['roles']))}"
|
2021-02-17 14:23:32 +01:00
|
|
|
)
|
|
|
|
self.ffmpeg_other_processes.append(
|
|
|
|
{
|
|
|
|
"cmd": c["cmd"],
|
2022-12-09 04:03:54 +01:00
|
|
|
"roles": c["roles"],
|
2021-02-17 14:23:32 +01:00
|
|
|
"logpipe": logpipe,
|
|
|
|
"process": start_or_restart_ffmpeg(c["cmd"], self.logger, logpipe),
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
2023-06-30 14:14:39 +02:00
|
|
|
time.sleep(self.sleeptime)
|
|
|
|
while not self.stop_event.wait(self.sleeptime):
|
2020-10-25 16:05:21 +01:00
|
|
|
now = datetime.datetime.now().timestamp()
|
|
|
|
|
|
|
|
if not self.capture_thread.is_alive():
|
2022-11-29 04:47:20 +01:00
|
|
|
self.camera_fps.value = 0
|
2021-08-14 21:04:00 +02:00
|
|
|
self.logger.error(
|
2022-02-06 15:46:41 +01:00
|
|
|
f"Ffmpeg process crashed unexpectedly for {self.camera_name}."
|
2021-08-14 21:04:00 +02:00
|
|
|
)
|
|
|
|
self.logger.error(
|
2021-08-16 14:38:53 +02:00
|
|
|
"The following ffmpeg logs include the last 100 lines prior to exit."
|
2021-08-14 21:04:00 +02:00
|
|
|
)
|
2021-01-30 14:50:17 +01:00
|
|
|
self.logpipe.dump()
|
2020-11-29 22:55:53 +01:00
|
|
|
self.start_ffmpeg_detect()
|
|
|
|
elif now - self.capture_thread.current_frame.value > 20:
|
2022-11-29 04:47:20 +01:00
|
|
|
self.camera_fps.value = 0
|
2021-02-17 14:23:32 +01:00
|
|
|
self.logger.info(
|
|
|
|
f"No frames received from {self.camera_name} in 20 seconds. Exiting ffmpeg..."
|
|
|
|
)
|
2020-11-29 22:55:53 +01:00
|
|
|
self.ffmpeg_detect_process.terminate()
|
2020-10-25 16:05:21 +01:00
|
|
|
try:
|
2020-12-04 13:59:03 +01:00
|
|
|
self.logger.info("Waiting for ffmpeg to exit gracefully...")
|
2020-11-29 22:55:53 +01:00
|
|
|
self.ffmpeg_detect_process.communicate(timeout=30)
|
2020-10-25 16:05:21 +01:00
|
|
|
except sp.TimeoutExpired:
|
2023-01-30 00:20:42 +01:00
|
|
|
self.logger.info("FFmpeg did not exit. Force killing...")
|
|
|
|
self.ffmpeg_detect_process.kill()
|
|
|
|
self.ffmpeg_detect_process.communicate()
|
|
|
|
elif self.camera_fps.value >= (self.config.detect.fps + 10):
|
2024-09-10 15:39:37 +02:00
|
|
|
self.fps_overflow_count += 1
|
|
|
|
|
|
|
|
if self.fps_overflow_count == 3:
|
|
|
|
self.fps_overflow_count = 0
|
|
|
|
self.camera_fps.value = 0
|
|
|
|
self.logger.info(
|
|
|
|
f"{self.camera_name} exceeded fps limit. Exiting ffmpeg..."
|
|
|
|
)
|
|
|
|
self.ffmpeg_detect_process.terminate()
|
|
|
|
try:
|
|
|
|
self.logger.info("Waiting for ffmpeg to exit gracefully...")
|
|
|
|
self.ffmpeg_detect_process.communicate(timeout=30)
|
|
|
|
except sp.TimeoutExpired:
|
|
|
|
self.logger.info("FFmpeg did not exit. Force killing...")
|
|
|
|
self.ffmpeg_detect_process.kill()
|
|
|
|
self.ffmpeg_detect_process.communicate()
|
|
|
|
else:
|
|
|
|
# process is running normally
|
|
|
|
self.fps_overflow_count = 0
|
2021-02-17 14:23:32 +01:00
|
|
|
|
2020-11-29 22:55:53 +01:00
|
|
|
for p in self.ffmpeg_other_processes:
|
2021-02-17 14:23:32 +01:00
|
|
|
poll = p["process"].poll()
|
2022-12-09 04:03:54 +01:00
|
|
|
|
|
|
|
if self.config.record.enabled and "record" in p["roles"]:
|
2023-10-31 01:25:21 +01:00
|
|
|
latest_segment_time = self.get_latest_segment_datetime(
|
2022-12-09 04:03:54 +01:00
|
|
|
p.get(
|
2023-10-31 01:25:21 +01:00
|
|
|
"latest_segment_time",
|
|
|
|
datetime.datetime.now().astimezone(datetime.timezone.utc),
|
2022-12-09 04:03:54 +01:00
|
|
|
)
|
|
|
|
)
|
|
|
|
|
2023-10-31 01:25:21 +01:00
|
|
|
if datetime.datetime.now().astimezone(datetime.timezone.utc) > (
|
|
|
|
latest_segment_time + datetime.timedelta(seconds=120)
|
2022-12-09 04:03:54 +01:00
|
|
|
):
|
|
|
|
self.logger.error(
|
|
|
|
f"No new recording segments were created for {self.camera_name} in the last 120s. restarting the ffmpeg record process..."
|
|
|
|
)
|
|
|
|
p["process"] = start_or_restart_ffmpeg(
|
|
|
|
p["cmd"],
|
|
|
|
self.logger,
|
|
|
|
p["logpipe"],
|
|
|
|
ffmpeg_process=p["process"],
|
|
|
|
)
|
|
|
|
continue
|
|
|
|
else:
|
|
|
|
p["latest_segment_time"] = latest_segment_time
|
|
|
|
|
2021-06-25 18:37:21 +02:00
|
|
|
if poll is None:
|
2020-11-29 22:55:53 +01:00
|
|
|
continue
|
2022-12-09 04:03:54 +01:00
|
|
|
|
2021-02-17 14:23:32 +01:00
|
|
|
p["logpipe"].dump()
|
|
|
|
p["process"] = start_or_restart_ffmpeg(
|
|
|
|
p["cmd"], self.logger, p["logpipe"], ffmpeg_process=p["process"]
|
|
|
|
)
|
|
|
|
|
2021-05-21 17:39:14 +02:00
|
|
|
stop_ffmpeg(self.ffmpeg_detect_process, self.logger)
|
|
|
|
for p in self.ffmpeg_other_processes:
|
|
|
|
stop_ffmpeg(p["process"], self.logger)
|
|
|
|
p["logpipe"].close()
|
|
|
|
self.logpipe.close()
|
2021-02-17 14:23:32 +01:00
|
|
|
|
2020-11-29 22:55:53 +01:00
|
|
|
def start_ffmpeg_detect(self):
|
2021-02-17 14:23:32 +01:00
|
|
|
ffmpeg_cmd = [
|
|
|
|
c["cmd"] for c in self.config.ffmpeg_cmds if "detect" in c["roles"]
|
|
|
|
][0]
|
|
|
|
self.ffmpeg_detect_process = start_or_restart_ffmpeg(
|
|
|
|
ffmpeg_cmd, self.logger, self.logpipe, self.frame_size
|
|
|
|
)
|
2020-11-29 22:55:53 +01:00
|
|
|
self.ffmpeg_pid.value = self.ffmpeg_detect_process.pid
|
2021-02-17 14:23:32 +01:00
|
|
|
self.capture_thread = CameraCapture(
|
2024-09-03 18:22:30 +02:00
|
|
|
self.config,
|
|
|
|
self.shm_frame_count,
|
2021-02-17 14:23:32 +01:00
|
|
|
self.ffmpeg_detect_process,
|
|
|
|
self.frame_shape,
|
|
|
|
self.frame_queue,
|
|
|
|
self.camera_fps,
|
2023-06-28 12:53:28 +02:00
|
|
|
self.skipped_fps,
|
2023-02-04 15:58:45 +01:00
|
|
|
self.stop_event,
|
2021-02-17 14:23:32 +01:00
|
|
|
)
|
2020-11-01 17:55:11 +01:00
|
|
|
self.capture_thread.start()
|
2020-10-25 16:05:21 +01:00
|
|
|
|
2023-10-31 01:25:21 +01:00
|
|
|
def get_latest_segment_datetime(self, latest_segment: datetime.datetime) -> int:
|
2022-12-09 04:03:54 +01:00
|
|
|
"""Checks if ffmpeg is still writing recording segments to cache."""
|
|
|
|
cache_files = sorted(
|
|
|
|
[
|
|
|
|
d
|
|
|
|
for d in os.listdir(CACHE_DIR)
|
|
|
|
if os.path.isfile(os.path.join(CACHE_DIR, d))
|
|
|
|
and d.endswith(".mp4")
|
2024-06-09 20:45:26 +02:00
|
|
|
and not d.startswith("preview_")
|
2022-12-09 04:03:54 +01:00
|
|
|
]
|
|
|
|
)
|
2023-11-05 21:30:29 +01:00
|
|
|
newest_segment_time = latest_segment
|
2022-12-09 04:03:54 +01:00
|
|
|
|
|
|
|
for file in cache_files:
|
|
|
|
if self.camera_name in file:
|
|
|
|
basename = os.path.splitext(file)[0]
|
2023-11-05 21:30:29 +01:00
|
|
|
_, date = basename.rsplit("@", maxsplit=1)
|
|
|
|
segment_time = datetime.datetime.strptime(
|
|
|
|
date, CACHE_SEGMENT_FORMAT
|
|
|
|
).astimezone(datetime.timezone.utc)
|
|
|
|
if segment_time > newest_segment_time:
|
|
|
|
newest_segment_time = segment_time
|
|
|
|
|
|
|
|
return newest_segment_time
|
2022-12-09 04:03:54 +01:00
|
|
|
|
2021-02-17 14:23:32 +01:00
|
|
|
|
2020-03-14 21:32:51 +01:00
|
|
|
class CameraCapture(threading.Thread):
|
2023-02-04 15:58:45 +01:00
|
|
|
def __init__(
|
2023-06-28 12:53:28 +02:00
|
|
|
self,
|
2024-09-03 18:22:30 +02:00
|
|
|
config: CameraConfig,
|
|
|
|
shm_frame_count: int,
|
2023-06-28 12:53:28 +02:00
|
|
|
ffmpeg_process,
|
|
|
|
frame_shape,
|
|
|
|
frame_queue,
|
|
|
|
fps,
|
|
|
|
skipped_fps,
|
|
|
|
stop_event,
|
2023-02-04 15:58:45 +01:00
|
|
|
):
|
2020-03-14 21:32:51 +01:00
|
|
|
threading.Thread.__init__(self)
|
2024-09-03 18:22:30 +02:00
|
|
|
self.name = f"capture:{config.name}"
|
|
|
|
self.config = config
|
|
|
|
self.shm_frame_count = shm_frame_count
|
2020-03-14 21:32:51 +01:00
|
|
|
self.frame_shape = frame_shape
|
|
|
|
self.frame_queue = frame_queue
|
|
|
|
self.fps = fps
|
2023-02-04 15:58:45 +01:00
|
|
|
self.stop_event = stop_event
|
2023-06-28 12:53:28 +02:00
|
|
|
self.skipped_fps = skipped_fps
|
2020-09-22 04:02:00 +02:00
|
|
|
self.frame_manager = SharedMemoryFrameManager()
|
2020-03-14 21:32:51 +01:00
|
|
|
self.ffmpeg_process = ffmpeg_process
|
2021-02-17 14:23:32 +01:00
|
|
|
self.current_frame = mp.Value("d", 0.0)
|
2020-04-19 17:07:27 +02:00
|
|
|
self.last_frame = 0
|
2020-03-14 21:32:51 +01:00
|
|
|
|
|
|
|
def run(self):
|
2021-02-17 14:23:32 +01:00
|
|
|
capture_frames(
|
|
|
|
self.ffmpeg_process,
|
2024-09-03 18:22:30 +02:00
|
|
|
self.config,
|
|
|
|
self.shm_frame_count,
|
2021-02-17 14:23:32 +01:00
|
|
|
self.frame_shape,
|
|
|
|
self.frame_manager,
|
|
|
|
self.frame_queue,
|
|
|
|
self.fps,
|
|
|
|
self.skipped_fps,
|
|
|
|
self.current_frame,
|
2023-02-04 15:58:45 +01:00
|
|
|
self.stop_event,
|
2021-02-17 14:23:32 +01:00
|
|
|
)
|
|
|
|
|
2020-03-14 21:32:51 +01:00
|
|
|
|
2024-09-27 14:53:23 +02:00
|
|
|
def capture_camera(
|
|
|
|
name, config: CameraConfig, shm_frame_count: int, camera_metrics: CameraMetrics
|
|
|
|
):
|
2020-11-29 23:19:59 +01:00
|
|
|
stop_event = mp.Event()
|
2021-02-17 14:23:32 +01:00
|
|
|
|
2020-11-29 23:19:59 +01:00
|
|
|
def receiveSignal(signalNumber, frame):
|
|
|
|
stop_event.set()
|
2021-02-17 14:23:32 +01:00
|
|
|
|
2020-11-29 23:19:59 +01:00
|
|
|
signal.signal(signal.SIGTERM, receiveSignal)
|
|
|
|
signal.signal(signal.SIGINT, receiveSignal)
|
|
|
|
|
2023-02-02 00:49:18 +01:00
|
|
|
threading.current_thread().name = f"capture:{name}"
|
|
|
|
setproctitle(f"frigate.capture:{name}")
|
|
|
|
|
2021-02-17 14:23:32 +01:00
|
|
|
camera_watchdog = CameraWatchdog(
|
|
|
|
name,
|
|
|
|
config,
|
2024-09-03 18:22:30 +02:00
|
|
|
shm_frame_count,
|
2024-09-27 14:53:23 +02:00
|
|
|
camera_metrics.frame_queue,
|
|
|
|
camera_metrics.camera_fps,
|
|
|
|
camera_metrics.skipped_fps,
|
|
|
|
camera_metrics.ffmpeg_pid,
|
2021-02-17 14:23:32 +01:00
|
|
|
stop_event,
|
|
|
|
)
|
2020-10-25 16:05:21 +01:00
|
|
|
camera_watchdog.start()
|
|
|
|
camera_watchdog.join()
|
|
|
|
|
2021-02-17 14:23:32 +01:00
|
|
|
|
|
|
|
def track_camera(
|
|
|
|
name,
|
|
|
|
config: CameraConfig,
|
2022-11-04 03:23:09 +01:00
|
|
|
model_config,
|
2021-07-08 05:57:19 +02:00
|
|
|
labelmap,
|
2021-02-17 14:23:32 +01:00
|
|
|
detection_queue,
|
|
|
|
result_connection,
|
|
|
|
detected_objects_queue,
|
2024-09-27 14:53:23 +02:00
|
|
|
camera_metrics: CameraMetrics,
|
|
|
|
ptz_metrics: PTZMetrics,
|
2023-10-19 01:21:52 +02:00
|
|
|
region_grid,
|
2021-02-17 14:23:32 +01:00
|
|
|
):
|
2020-11-29 23:19:59 +01:00
|
|
|
stop_event = mp.Event()
|
2021-02-17 14:23:32 +01:00
|
|
|
|
2020-11-29 23:19:59 +01:00
|
|
|
def receiveSignal(signalNumber, frame):
|
|
|
|
stop_event.set()
|
2021-02-17 14:23:32 +01:00
|
|
|
|
2020-11-29 23:19:59 +01:00
|
|
|
signal.signal(signal.SIGTERM, receiveSignal)
|
|
|
|
signal.signal(signal.SIGINT, receiveSignal)
|
|
|
|
|
2020-11-04 13:28:07 +01:00
|
|
|
threading.current_thread().name = f"process:{name}"
|
2021-01-03 20:41:02 +01:00
|
|
|
setproctitle(f"frigate.process:{name}")
|
2020-03-10 03:12:19 +01:00
|
|
|
listen()
|
2020-02-16 04:07:54 +01:00
|
|
|
|
2024-09-27 14:53:23 +02:00
|
|
|
frame_queue = camera_metrics.frame_queue
|
2020-10-25 16:05:21 +01:00
|
|
|
|
2020-11-03 15:15:58 +01:00
|
|
|
frame_shape = config.frame_shape
|
|
|
|
objects_to_track = config.objects.track
|
|
|
|
object_filters = config.objects.filters
|
2020-02-16 04:07:54 +01:00
|
|
|
|
2023-06-11 15:45:11 +02:00
|
|
|
motion_detector = ImprovedMotionDetector(
|
2024-04-23 16:57:29 +02:00
|
|
|
frame_shape, config.motion, config.detect.fps, name=config.name
|
2022-04-16 15:42:44 +02:00
|
|
|
)
|
2021-02-17 14:23:32 +01:00
|
|
|
object_detector = RemoteObjectDetector(
|
2023-02-04 15:58:45 +01:00
|
|
|
name, labelmap, detection_queue, result_connection, model_config, stop_event
|
2021-02-17 14:23:32 +01:00
|
|
|
)
|
2020-02-16 04:07:54 +01:00
|
|
|
|
2023-07-11 13:23:20 +02:00
|
|
|
object_tracker = NorfairTracker(config, ptz_metrics)
|
2020-03-14 21:32:51 +01:00
|
|
|
|
2020-09-22 04:02:00 +02:00
|
|
|
frame_manager = SharedMemoryFrameManager()
|
2020-08-22 14:05:20 +02:00
|
|
|
|
2024-02-15 01:24:36 +01:00
|
|
|
# create communication for region grid updates
|
|
|
|
requestor = InterProcessRequestor()
|
|
|
|
|
2021-02-17 14:23:32 +01:00
|
|
|
process_frames(
|
|
|
|
name,
|
2024-02-15 01:24:36 +01:00
|
|
|
requestor,
|
2021-02-17 14:23:32 +01:00
|
|
|
frame_queue,
|
|
|
|
frame_shape,
|
2022-11-04 03:23:09 +01:00
|
|
|
model_config,
|
2021-10-31 17:48:49 +01:00
|
|
|
config.detect,
|
2021-02-17 14:23:32 +01:00
|
|
|
frame_manager,
|
|
|
|
motion_detector,
|
|
|
|
object_detector,
|
|
|
|
object_tracker,
|
|
|
|
detected_objects_queue,
|
2024-09-27 14:53:23 +02:00
|
|
|
camera_metrics,
|
2021-02-17 14:23:32 +01:00
|
|
|
objects_to_track,
|
|
|
|
object_filters,
|
|
|
|
stop_event,
|
2023-07-11 13:23:20 +02:00
|
|
|
ptz_metrics,
|
2023-10-19 01:21:52 +02:00
|
|
|
region_grid,
|
2021-02-17 14:23:32 +01:00
|
|
|
)
|
2020-08-22 14:05:20 +02:00
|
|
|
|
2024-06-07 01:54:38 +02:00
|
|
|
# empty the frame queue
|
|
|
|
logger.info(f"{name}: emptying frame queue")
|
|
|
|
while not frame_queue.empty():
|
2024-11-17 00:00:19 +01:00
|
|
|
(frame_name, _) = frame_queue.get(False)
|
|
|
|
frame_manager.delete(frame_name)
|
2024-06-07 01:54:38 +02:00
|
|
|
|
2020-11-04 04:26:39 +01:00
|
|
|
logger.info(f"{name}: exiting subprocess")
|
2020-08-22 14:05:20 +02:00
|
|
|
|
2021-02-17 14:23:32 +01:00
|
|
|
|
|
|
|
def detect(
|
2022-09-12 18:54:50 +02:00
|
|
|
detect_config: DetectConfig,
|
|
|
|
object_detector,
|
|
|
|
frame,
|
2022-11-04 03:23:09 +01:00
|
|
|
model_config,
|
2022-09-12 18:54:50 +02:00
|
|
|
region,
|
|
|
|
objects_to_track,
|
|
|
|
object_filters,
|
2021-02-17 14:23:32 +01:00
|
|
|
):
|
2022-11-04 03:23:09 +01:00
|
|
|
tensor_input = create_tensor_input(frame, model_config, region)
|
2020-08-22 14:05:20 +02:00
|
|
|
|
|
|
|
detections = []
|
|
|
|
region_detections = object_detector.detect(tensor_input)
|
|
|
|
for d in region_detections:
|
|
|
|
box = d[2]
|
2021-02-17 14:23:32 +01:00
|
|
|
size = region[2] - region[0]
|
2022-09-12 18:54:50 +02:00
|
|
|
x_min = int(max(0, (box[1] * size) + region[0]))
|
|
|
|
y_min = int(max(0, (box[0] * size) + region[1]))
|
2022-09-22 15:07:16 +02:00
|
|
|
x_max = int(min(detect_config.width - 1, (box[3] * size) + region[0]))
|
|
|
|
y_max = int(min(detect_config.height - 1, (box[2] * size) + region[1]))
|
|
|
|
|
|
|
|
# ignore objects that were detected outside the frame
|
|
|
|
if (x_min >= detect_config.width - 1) or (y_min >= detect_config.height - 1):
|
|
|
|
continue
|
|
|
|
|
2022-04-10 15:25:18 +02:00
|
|
|
width = x_max - x_min
|
|
|
|
height = y_max - y_min
|
|
|
|
area = width * height
|
2023-08-10 12:51:30 +02:00
|
|
|
ratio = width / max(1, height)
|
2021-02-17 14:23:32 +01:00
|
|
|
det = (
|
|
|
|
d[0],
|
2020-08-22 14:05:20 +02:00
|
|
|
d[1],
|
|
|
|
(x_min, y_min, x_max, y_max),
|
2022-04-10 15:25:18 +02:00
|
|
|
area,
|
|
|
|
ratio,
|
2021-02-17 14:23:32 +01:00
|
|
|
region,
|
|
|
|
)
|
2020-08-22 14:05:20 +02:00
|
|
|
# apply object filters
|
2023-10-19 01:21:52 +02:00
|
|
|
if is_object_filtered(det, objects_to_track, object_filters):
|
2020-08-22 14:05:20 +02:00
|
|
|
continue
|
|
|
|
detections.append(det)
|
|
|
|
return detections
|
|
|
|
|
2021-02-17 14:23:32 +01:00
|
|
|
|
|
|
|
def process_frames(
|
|
|
|
camera_name: str,
|
2024-02-15 01:24:36 +01:00
|
|
|
requestor: InterProcessRequestor,
|
2023-07-16 14:42:56 +02:00
|
|
|
frame_queue: mp.Queue,
|
2021-02-17 14:23:32 +01:00
|
|
|
frame_shape,
|
2023-07-06 14:25:37 +02:00
|
|
|
model_config: ModelConfig,
|
2021-10-31 17:48:49 +01:00
|
|
|
detect_config: DetectConfig,
|
2021-02-17 14:23:32 +01:00
|
|
|
frame_manager: FrameManager,
|
|
|
|
motion_detector: MotionDetector,
|
|
|
|
object_detector: RemoteObjectDetector,
|
|
|
|
object_tracker: ObjectTracker,
|
2023-07-16 14:42:56 +02:00
|
|
|
detected_objects_queue: mp.Queue,
|
2024-09-27 14:53:23 +02:00
|
|
|
camera_metrics: CameraMetrics,
|
2022-04-16 17:38:07 +02:00
|
|
|
objects_to_track: list[str],
|
2021-02-17 14:23:32 +01:00
|
|
|
object_filters,
|
|
|
|
stop_event,
|
2024-09-27 14:53:23 +02:00
|
|
|
ptz_metrics: PTZMetrics,
|
2023-10-19 01:21:52 +02:00
|
|
|
region_grid,
|
2021-02-17 14:23:32 +01:00
|
|
|
exit_on_empty: bool = False,
|
|
|
|
):
|
2023-11-04 03:21:29 +01:00
|
|
|
next_region_update = get_tomorrow_at_time(2)
|
2024-02-19 14:26:59 +01:00
|
|
|
config_subscriber = ConfigSubscriber(f"config/detect/{camera_name}")
|
2020-10-25 16:05:21 +01:00
|
|
|
|
2020-02-16 04:07:54 +01:00
|
|
|
fps_tracker = EventsPerSecond()
|
|
|
|
fps_tracker.start()
|
2020-08-22 14:05:20 +02:00
|
|
|
|
2023-10-19 01:21:52 +02:00
|
|
|
startup_scan = True
|
2023-10-20 00:14:33 +02:00
|
|
|
stationary_frame_counter = 0
|
2022-02-05 14:10:00 +01:00
|
|
|
|
2023-07-06 14:25:37 +02:00
|
|
|
region_min_size = get_min_region_size(model_config)
|
2023-06-11 15:45:11 +02:00
|
|
|
|
2021-05-21 17:39:14 +02:00
|
|
|
while not stop_event.is_set():
|
2024-02-19 14:26:59 +01:00
|
|
|
# check for updated detect config
|
|
|
|
_, updated_detect_config = config_subscriber.check_for_update()
|
|
|
|
|
|
|
|
if updated_detect_config:
|
|
|
|
detect_config = updated_detect_config
|
|
|
|
|
2023-10-19 01:21:52 +02:00
|
|
|
if (
|
|
|
|
datetime.datetime.now().astimezone(datetime.timezone.utc)
|
|
|
|
> next_region_update
|
|
|
|
):
|
2024-02-15 01:24:36 +01:00
|
|
|
region_grid = requestor.send_data(REQUEST_REGION_GRID, camera_name)
|
2023-11-04 03:21:29 +01:00
|
|
|
next_region_update = get_tomorrow_at_time(2)
|
2023-10-19 01:21:52 +02:00
|
|
|
|
2020-08-22 14:05:20 +02:00
|
|
|
try:
|
2023-07-06 15:18:39 +02:00
|
|
|
if exit_on_empty:
|
2024-11-17 00:00:19 +01:00
|
|
|
frame_name, frame_time = frame_queue.get(False)
|
2023-07-06 15:18:39 +02:00
|
|
|
else:
|
2024-11-17 00:00:19 +01:00
|
|
|
frame_name, frame_time = frame_queue.get(True, 1)
|
2020-08-22 14:05:20 +02:00
|
|
|
except queue.Empty:
|
2023-07-06 15:18:39 +02:00
|
|
|
if exit_on_empty:
|
|
|
|
logger.info("Exiting track_objects...")
|
|
|
|
break
|
2020-03-14 21:32:51 +01:00
|
|
|
continue
|
2020-04-19 17:07:27 +02:00
|
|
|
|
2024-09-27 14:53:23 +02:00
|
|
|
camera_metrics.detection_frame.value = frame_time
|
|
|
|
ptz_metrics.frame_time.value = frame_time
|
2020-08-22 14:05:20 +02:00
|
|
|
|
2024-11-17 00:00:19 +01:00
|
|
|
frame = frame_manager.get(frame_name, (frame_shape[0] * 3 // 2, frame_shape[1]))
|
2020-09-14 14:40:26 +02:00
|
|
|
|
|
|
|
if frame is None:
|
2024-09-03 18:22:30 +02:00
|
|
|
logger.debug(f"{camera_name}: frame {frame_time} is not in memory store.")
|
2020-09-14 14:40:26 +02:00
|
|
|
continue
|
2020-08-22 14:05:20 +02:00
|
|
|
|
2023-10-14 14:05:44 +02:00
|
|
|
# look for motion if enabled
|
2024-02-19 14:26:59 +01:00
|
|
|
motion_boxes = motion_detector.detect(frame)
|
2020-02-16 04:07:54 +01:00
|
|
|
|
2022-02-06 21:49:54 +01:00
|
|
|
regions = []
|
2023-06-17 16:56:22 +02:00
|
|
|
consolidated_detections = []
|
2020-08-22 14:05:20 +02:00
|
|
|
|
2022-02-06 21:49:54 +01:00
|
|
|
# if detection is disabled
|
2024-02-19 14:26:59 +01:00
|
|
|
if not detect_config.enabled:
|
2024-11-17 00:00:19 +01:00
|
|
|
object_tracker.match_and_update(frame_name, frame_time, [])
|
2022-02-06 21:49:54 +01:00
|
|
|
else:
|
|
|
|
# get stationary object ids
|
|
|
|
# check every Nth frame for stationary objects
|
|
|
|
# disappeared objects are not stationary
|
|
|
|
# also check for overlapping motion boxes
|
2023-10-20 00:14:33 +02:00
|
|
|
if stationary_frame_counter == detect_config.stationary.interval:
|
|
|
|
stationary_frame_counter = 0
|
|
|
|
stationary_object_ids = []
|
|
|
|
else:
|
|
|
|
stationary_frame_counter += 1
|
|
|
|
stationary_object_ids = [
|
|
|
|
obj["id"]
|
|
|
|
for obj in object_tracker.tracked_objects.values()
|
|
|
|
# if it has exceeded the stationary threshold
|
|
|
|
if obj["motionless_count"] >= detect_config.stationary.threshold
|
|
|
|
# and it hasn't disappeared
|
|
|
|
and object_tracker.disappeared[obj["id"]] == 0
|
|
|
|
# and it doesn't overlap with any current motion boxes when not calibrating
|
|
|
|
and not intersects_any(
|
|
|
|
obj["box"],
|
|
|
|
[] if motion_detector.is_calibrating() else motion_boxes,
|
|
|
|
)
|
|
|
|
]
|
2022-02-06 21:49:54 +01:00
|
|
|
|
|
|
|
# get tracked object boxes that aren't stationary
|
|
|
|
tracked_object_boxes = [
|
2023-10-21 01:21:34 +02:00
|
|
|
(
|
|
|
|
# use existing object box for stationary objects
|
|
|
|
obj["estimate"]
|
|
|
|
if obj["motionless_count"] < detect_config.stationary.threshold
|
|
|
|
else obj["box"]
|
|
|
|
)
|
2022-02-06 21:49:54 +01:00
|
|
|
for obj in object_tracker.tracked_objects.values()
|
2023-05-29 12:31:17 +02:00
|
|
|
if obj["id"] not in stationary_object_ids
|
2022-02-06 21:49:54 +01:00
|
|
|
]
|
2023-10-24 02:50:22 +02:00
|
|
|
object_boxes = tracked_object_boxes + object_tracker.untracked_object_boxes
|
2022-02-06 21:49:54 +01:00
|
|
|
|
2023-10-19 01:21:52 +02:00
|
|
|
# get consolidated regions for tracked objects
|
2022-02-06 21:49:54 +01:00
|
|
|
regions = [
|
2023-06-11 15:45:11 +02:00
|
|
|
get_cluster_region(
|
2023-10-24 02:50:22 +02:00
|
|
|
frame_shape, region_min_size, candidate, object_boxes
|
2023-10-19 01:21:52 +02:00
|
|
|
)
|
|
|
|
for candidate in get_cluster_candidates(
|
2023-10-24 02:50:22 +02:00
|
|
|
frame_shape, region_min_size, object_boxes
|
2021-02-17 14:23:32 +01:00
|
|
|
)
|
2022-02-06 21:49:54 +01:00
|
|
|
]
|
|
|
|
|
2023-10-25 01:25:22 +02:00
|
|
|
# only add in the motion boxes when not calibrating and a ptz is not moving via autotracking
|
|
|
|
# ptz_moving_at_frame_time() always returns False for non-autotracking cameras
|
|
|
|
if not motion_detector.is_calibrating() and not ptz_moving_at_frame_time(
|
|
|
|
frame_time,
|
2024-09-27 14:53:23 +02:00
|
|
|
ptz_metrics.start_time.value,
|
|
|
|
ptz_metrics.stop_time.value,
|
2023-10-25 01:25:22 +02:00
|
|
|
):
|
2023-10-19 01:21:52 +02:00
|
|
|
# find motion boxes that are not inside tracked object regions
|
|
|
|
standalone_motion_boxes = [
|
|
|
|
b for b in motion_boxes if not inside_any(b, regions)
|
|
|
|
]
|
|
|
|
|
|
|
|
if standalone_motion_boxes:
|
|
|
|
motion_clusters = get_cluster_candidates(
|
2022-02-06 21:49:54 +01:00
|
|
|
frame_shape,
|
|
|
|
region_min_size,
|
2023-10-19 01:21:52 +02:00
|
|
|
standalone_motion_boxes,
|
2022-02-06 21:49:54 +01:00
|
|
|
)
|
2023-10-19 01:21:52 +02:00
|
|
|
motion_regions = [
|
|
|
|
get_cluster_region_from_grid(
|
|
|
|
frame_shape,
|
|
|
|
region_min_size,
|
|
|
|
candidate,
|
|
|
|
standalone_motion_boxes,
|
|
|
|
region_grid,
|
|
|
|
)
|
|
|
|
for candidate in motion_clusters
|
|
|
|
]
|
|
|
|
regions += motion_regions
|
|
|
|
|
|
|
|
# if starting up, get the next startup scan region
|
|
|
|
if startup_scan:
|
|
|
|
for region in get_startup_regions(
|
|
|
|
frame_shape, region_min_size, region_grid
|
|
|
|
):
|
|
|
|
regions.append(region)
|
|
|
|
startup_scan = False
|
2021-02-17 14:23:32 +01:00
|
|
|
|
2022-02-06 21:49:54 +01:00
|
|
|
# resize regions and detect
|
|
|
|
# seed with stationary objects
|
|
|
|
detections = [
|
|
|
|
(
|
|
|
|
obj["label"],
|
|
|
|
obj["score"],
|
|
|
|
obj["box"],
|
|
|
|
obj["area"],
|
2022-04-10 15:25:18 +02:00
|
|
|
obj["ratio"],
|
2022-02-06 21:49:54 +01:00
|
|
|
obj["region"],
|
|
|
|
)
|
|
|
|
for obj in object_tracker.tracked_objects.values()
|
|
|
|
if obj["id"] in stationary_object_ids
|
|
|
|
]
|
|
|
|
|
|
|
|
for region in regions:
|
|
|
|
detections.extend(
|
|
|
|
detect(
|
2022-09-12 18:54:50 +02:00
|
|
|
detect_config,
|
2022-02-06 21:49:54 +01:00
|
|
|
object_detector,
|
|
|
|
frame,
|
2022-11-04 03:23:09 +01:00
|
|
|
model_config,
|
2022-02-06 21:49:54 +01:00
|
|
|
region,
|
|
|
|
objects_to_track,
|
|
|
|
object_filters,
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
2023-10-24 02:20:21 +02:00
|
|
|
consolidated_detections = reduce_detections(frame_shape, detections)
|
2022-02-06 21:49:54 +01:00
|
|
|
|
|
|
|
# if detection was run on this frame, consolidate
|
|
|
|
if len(regions) > 0:
|
2023-06-17 16:56:22 +02:00
|
|
|
tracked_detections = [
|
|
|
|
d
|
|
|
|
for d in consolidated_detections
|
2024-09-28 15:49:04 +02:00
|
|
|
if d[0] not in model_config.all_attributes
|
2023-06-17 16:56:22 +02:00
|
|
|
]
|
2022-02-06 21:49:54 +01:00
|
|
|
# now that we have refined our detections, we need to track objects
|
2024-11-17 00:00:19 +01:00
|
|
|
object_tracker.match_and_update(
|
|
|
|
frame_name, frame_time, tracked_detections
|
|
|
|
)
|
2022-02-06 21:49:54 +01:00
|
|
|
# else, just update the frame times for the stationary objects
|
|
|
|
else:
|
2024-11-17 00:00:19 +01:00
|
|
|
object_tracker.update_frame_times(frame_name, frame_time)
|
2020-02-16 04:07:54 +01:00
|
|
|
|
2023-06-17 16:56:22 +02:00
|
|
|
# group the attribute detections based on what label they apply to
|
2024-10-17 18:02:27 +02:00
|
|
|
attribute_detections: dict[str, list[TrackedObjectAttribute]] = {}
|
2024-09-28 15:49:04 +02:00
|
|
|
for label, attribute_labels in model_config.attributes_map.items():
|
2023-06-17 16:56:22 +02:00
|
|
|
attribute_detections[label] = [
|
2024-10-17 18:02:27 +02:00
|
|
|
TrackedObjectAttribute(d)
|
2024-10-01 15:31:03 +02:00
|
|
|
for d in consolidated_detections
|
|
|
|
if d[0] in attribute_labels
|
2023-06-17 16:56:22 +02:00
|
|
|
]
|
|
|
|
|
2024-10-01 15:31:03 +02:00
|
|
|
# build detections
|
2023-06-17 16:56:22 +02:00
|
|
|
detections = {}
|
|
|
|
for obj in object_tracker.tracked_objects.values():
|
2024-10-01 15:31:03 +02:00
|
|
|
detections[obj["id"]] = {**obj, "attributes": []}
|
|
|
|
|
|
|
|
# find the best object for each attribute to be assigned to
|
|
|
|
all_objects: list[dict[str, any]] = object_tracker.tracked_objects.values()
|
|
|
|
for attributes in attribute_detections.values():
|
|
|
|
for attribute in attributes:
|
|
|
|
filtered_objects = filter(
|
2024-10-01 15:54:27 +02:00
|
|
|
lambda o: attribute.label
|
|
|
|
in model_config.attributes_map.get(o["label"], []),
|
|
|
|
all_objects,
|
2024-10-01 15:31:03 +02:00
|
|
|
)
|
|
|
|
selected_object_id = attribute.find_best_object(filtered_objects)
|
|
|
|
|
|
|
|
if selected_object_id is not None:
|
|
|
|
detections[selected_object_id]["attributes"].append(
|
|
|
|
attribute.get_tracking_data()
|
|
|
|
)
|
2023-06-17 16:56:22 +02:00
|
|
|
|
2023-06-11 15:45:11 +02:00
|
|
|
# debug object tracking
|
2023-05-31 16:12:43 +02:00
|
|
|
if False:
|
|
|
|
bgr_frame = cv2.cvtColor(
|
|
|
|
frame,
|
|
|
|
cv2.COLOR_YUV2BGR_I420,
|
|
|
|
)
|
|
|
|
object_tracker.debug_draw(bgr_frame, frame_time)
|
|
|
|
cv2.imwrite(
|
|
|
|
f"debug/frames/track-{'{:.6f}'.format(frame_time)}.jpg", bgr_frame
|
|
|
|
)
|
2023-06-11 15:45:11 +02:00
|
|
|
# debug
|
|
|
|
if False:
|
|
|
|
bgr_frame = cv2.cvtColor(
|
|
|
|
frame,
|
|
|
|
cv2.COLOR_YUV2BGR_I420,
|
|
|
|
)
|
2023-05-31 16:12:43 +02:00
|
|
|
|
2023-06-11 15:45:11 +02:00
|
|
|
for m_box in motion_boxes:
|
|
|
|
cv2.rectangle(
|
|
|
|
bgr_frame,
|
|
|
|
(m_box[0], m_box[1]),
|
|
|
|
(m_box[2], m_box[3]),
|
|
|
|
(0, 0, 255),
|
|
|
|
2,
|
|
|
|
)
|
|
|
|
|
|
|
|
for b in tracked_object_boxes:
|
|
|
|
cv2.rectangle(
|
|
|
|
bgr_frame,
|
|
|
|
(b[0], b[1]),
|
|
|
|
(b[2], b[3]),
|
|
|
|
(255, 0, 0),
|
|
|
|
2,
|
|
|
|
)
|
|
|
|
|
|
|
|
for obj in object_tracker.tracked_objects.values():
|
|
|
|
if obj["frame_time"] == frame_time:
|
|
|
|
thickness = 2
|
|
|
|
color = model_config.colormap[obj["label"]]
|
|
|
|
else:
|
|
|
|
thickness = 1
|
|
|
|
color = (255, 0, 0)
|
|
|
|
|
|
|
|
# draw the bounding boxes on the frame
|
|
|
|
box = obj["box"]
|
|
|
|
|
|
|
|
draw_box_with_label(
|
|
|
|
bgr_frame,
|
|
|
|
box[0],
|
|
|
|
box[1],
|
|
|
|
box[2],
|
|
|
|
box[3],
|
|
|
|
obj["label"],
|
|
|
|
obj["id"],
|
|
|
|
thickness=thickness,
|
|
|
|
color=color,
|
|
|
|
)
|
|
|
|
|
|
|
|
for region in regions:
|
|
|
|
cv2.rectangle(
|
|
|
|
bgr_frame,
|
|
|
|
(region[0], region[1]),
|
|
|
|
(region[2], region[3]),
|
|
|
|
(0, 255, 0),
|
|
|
|
2,
|
|
|
|
)
|
|
|
|
|
|
|
|
cv2.imwrite(
|
|
|
|
f"debug/frames/{camera_name}-{'{:.6f}'.format(frame_time)}.jpg",
|
|
|
|
bgr_frame,
|
|
|
|
)
|
2020-10-24 18:36:04 +02:00
|
|
|
# add to the queue if not full
|
2021-02-17 14:23:32 +01:00
|
|
|
if detected_objects_queue.full():
|
2024-11-17 00:00:19 +01:00
|
|
|
frame_manager.close(frame_name)
|
2021-01-16 03:52:59 +01:00
|
|
|
continue
|
2020-10-24 18:36:04 +02:00
|
|
|
else:
|
2021-01-16 03:52:59 +01:00
|
|
|
fps_tracker.update()
|
2024-09-27 14:53:23 +02:00
|
|
|
camera_metrics.process_fps.value = fps_tracker.eps()
|
2021-02-17 14:23:32 +01:00
|
|
|
detected_objects_queue.put(
|
|
|
|
(
|
|
|
|
camera_name,
|
2024-11-17 00:00:19 +01:00
|
|
|
frame_name,
|
2021-02-17 14:23:32 +01:00
|
|
|
frame_time,
|
2023-06-17 16:56:22 +02:00
|
|
|
detections,
|
2021-02-17 14:23:32 +01:00
|
|
|
motion_boxes,
|
|
|
|
regions,
|
|
|
|
)
|
|
|
|
)
|
2024-09-27 14:53:23 +02:00
|
|
|
camera_metrics.detection_fps.value = object_detector.fps.eps()
|
2024-11-17 00:00:19 +01:00
|
|
|
frame_manager.close(frame_name)
|
2024-02-15 01:24:36 +01:00
|
|
|
|
2024-02-19 14:26:59 +01:00
|
|
|
motion_detector.stop()
|
2024-02-15 01:24:36 +01:00
|
|
|
requestor.stop()
|
2024-02-19 14:26:59 +01:00
|
|
|
config_subscriber.stop()
|