NVR with realtime local object detection for IP cameras
TypeScript aicameragoogle-coralhome-assistanthome-automationhomeautomationmqttnvrobject-detectionrealtimertsptensorflow
Fix camera access and improve typing (#15272)
Nicolas Mowenee816b2251bf
diff --git a/frigate/api/event.py b/frigate/api/event.pyindex bff1edc1ac..3b38ff0720 100644--- a/frigate/api/event.py+++ b/frigate/api/event.py@@ -35,8 +35,9 @@ from frigate.const import (CLIPS_DIR,)from frigate.embeddings import EmbeddingsContext+from frigate.events.external import ExternalEventProcessorfrom frigate.models import Event, ReviewSegment, Timeline-from frigate.object_processing import TrackedObject+from frigate.object_processing import TrackedObject, TrackedObjectProcessorfrom frigate.util.builtin import get_tz_modifierslogger = logging.getLogger(__name__)@@ -1087,9 +1088,11 @@ def create_event()try:- frame = request.app.detected_frames_processor.get_current_frame(camera_name)+ frame_processor: TrackedObjectProcessor = request.app.detected_frames_processor+ external_processor: ExternalEventProcessor = request.app.external_processor- event_id = request.app.external_processor.create_manual_event(+ frame = frame_processor.get_current_frame(camera_name)+ event_id = external_processor.create_manual_event(camera_name,label,body.source_type,diff --git a/frigate/api/media.py b/frigate/api/media.pyindex dcfc44f897..a907668995 100644--- a/frigate/api/media.py+++ b/frigate/api/media.py@@ -36,6 +36,7 @@ from frigate.const import (RECORD_DIR,)from frigate.models import Event, Previews, Recordings, Regions, ReviewSegment+from frigate.object_processing import TrackedObjectProcessorfrom frigate.util.builtin import get_tz_modifiersfrom frigate.util.image import get_image_from_recording@@ -79,7 +80,11 @@ def mjpeg_feed(def imagestream(- detected_frames_processor, camera_name: str, fps: int, height: int, draw_options+ detected_frames_processor: TrackedObjectProcessor,+ camera_name: str,+ fps: int,+ height: int,+ draw_options: dict[str, any],):while True:# max out at specified FPS@@ -118,6 +123,7 @@ def latest_frame(extension: Extension,params: MediaLatestFrameQueryParams = Depends(),):+ frame_processor: TrackedObjectProcessor = request.app.detected_frames_processordraw_options = {"bounding_boxes": params.bbox,"timestamp": params.timestamp,@@ -129,17 +135,14 @@ def latest_frame(quality = params.qualityif camera_name in request.app.frigate_config.cameras:- frame = request.app.detected_frames_processor.get_current_frame(- camera_name, draw_options- )+ frame = frame_processor.get_current_frame(camera_name, draw_options)retry_interval = float(request.app.frigate_config.cameras.get(camera_name).ffmpeg.retry_intervalor 10)if frame is None or datetime.now().timestamp() > (- request.app.detected_frames_processor.get_current_frame_time(camera_name)- + retry_interval+ frame_processor.get_current_frame_time(camera_name) + retry_interval):if request.app.camera_error_image is None:error_image = glob.glob("/opt/frigate/frigate/images/camera-error.jpg")@@ -180,7 +183,7 @@ def latest_frame()elif camera_name == "birdseye" and request.app.frigate_config.birdseye.restream:frame = cv2.cvtColor(- request.app.detected_frames_processor.get_current_frame(camera_name),+ frame_processor.get_current_frame(camera_name),cv2.COLOR_YUV2BGR_I420,)@@ -813,15 +816,15 @@ def grid_snapshot():if camera_name in request.app.frigate_config.cameras:detect = request.app.frigate_config.cameras[camera_name].detect- frame = request.app.detected_frames_processor.get_current_frame(camera_name, {})+ frame_processor: TrackedObjectProcessor = request.app.detected_frames_processor+ frame = frame_processor.get_current_frame(camera_name, {})retry_interval = float(request.app.frigate_config.cameras.get(camera_name).ffmpeg.retry_intervalor 10)if frame is None or datetime.now().timestamp() > (- request.app.detected_frames_processor.get_current_frame_time(camera_name)- + retry_interval+ frame_processor.get_current_frame_time(camera_name) + retry_interval):return JSONResponse(content={"success": False, "message": "Unable to get valid frame"},diff --git a/frigate/events/external.py b/frigate/events/external.pyindex 52ff5ffb73..922917bb4d 100644--- a/frigate/events/external.py+++ b/frigate/events/external.py@@ -10,6 +10,7 @@ from enum import Enumfrom typing import Optionalimport cv2+from numpy import ndarrayfrom frigate.comms.detections_updater import DetectionPublisher, DetectionTypeEnumfrom frigate.comms.events_updater import EventUpdatePublisher@@ -45,7 +46,7 @@ class ExternalEventProcessor:duration: Optional[int],include_recording: bool,draw: dict[str, any],- snapshot_frame: any,+ snapshot_frame: Optional[ndarray],) -> str:now = datetime.datetime.now().timestamp()camera_config = self.config.cameras.get(camera)@@ -131,8 +132,11 @@ class ExternalEventProcessor:label: str,event_id: str,draw: dict[str, any],- img_frame: any,- ) -> str:+ img_frame: Optional[ndarray],+ ) -> Optional[str]:+ if not img_frame:+ return None+# write clean snapshot if enabledif camera_config.snapshots.clean_copy:ret, png = cv2.imencode(".png", img_frame)diff --git a/frigate/object_processing.py b/frigate/object_processing.pyindex 937c935baa..ef23c3de3f 100644--- a/frigate/object_processing.py+++ b/frigate/object_processing.py@@ -6,7 +6,7 @@ import queueimport threadingfrom collections import Counter, defaultdictfrom multiprocessing.synchronize import Event as MpEvent-from typing import Callable+from typing import Callable, Optionalimport cv2import numpy as np@@ -784,13 +784,18 @@ class TrackedObjectProcessor(threading.Thread):else:return {}- def get_current_frame(self, camera, draw_options={}):+ def get_current_frame(+ self, camera: str, draw_options: dict[str, any] = {}+ ) -> Optional[np.ndarray]:if camera == "birdseye":return self.frame_manager.get("birdseye",(self.config.birdseye.height * 3 // 2, self.config.birdseye.width),)+ if camera not in self.camera_states:+ return None+return self.camera_states[camera].get_current_frame(draw_options)def get_current_frame_time(self, camera) -> int: