From 03e32118ae999c0d0cf1774d6e428eb54cf83991 Mon Sep 17 00:00:00 2001 From: Nicolas Mowen Date: Tue, 9 Jul 2024 06:36:15 -0600 Subject: [PATCH] Simplify empty shm error handling --- frigate/object_processing.py | 17 ++++++++--------- frigate/output/output.py | 7 +++---- frigate/util/image.py | 18 +++++++++++------- frigate/video.py | 9 +++------ 4 files changed, 25 insertions(+), 26 deletions(-) diff --git a/frigate/object_processing.py b/frigate/object_processing.py index f9c513d6b..aed850fe3 100644 --- a/frigate/object_processing.py +++ b/frigate/object_processing.py @@ -660,13 +660,9 @@ class CameraState: # get the new frame frame_id = f"{self.name}{frame_time}" - try: - current_frame = self.frame_manager.get( - frame_id, self.camera_config.frame_shape_yuv - ) - except FileNotFoundError: - logger.error(f"Failed to get {frame_id} from SHM") - return + current_frame = self.frame_manager.get( + frame_id, self.camera_config.frame_shape_yuv + ) tracked_objects = self.tracked_objects.copy() current_ids = set(current_detections.keys()) @@ -698,7 +694,7 @@ class CameraState: for c in self.callbacks["autotrack"]: c(self.name, updated_obj, frame_time) - if thumb_update: + if thumb_update and current_frame is not None: # ensure this frame is stored in the cache if ( updated_obj.thumbnail_data["frame_time"] == frame_time @@ -858,7 +854,10 @@ class CameraState: self.current_frame_time = frame_time self.motion_boxes = motion_boxes self.regions = regions - self._current_frame = current_frame + + if current_frame is not None: + self._current_frame = current_frame + if self.previous_frame_id is not None: self.frame_manager.close(self.previous_frame_id) self.previous_frame_id = frame_id diff --git a/frigate/output/output.py b/frigate/output/output.py index 5904f0b10..6339e2b9c 100644 --- a/frigate/output/output.py +++ b/frigate/output/output.py @@ -94,10 +94,9 @@ def output_frames( frame_id = f"{camera}{frame_time}" - try: - frame = frame_manager.get(frame_id, config.cameras[camera].frame_shape_yuv) - except FileNotFoundError: - logger.error(f"Failed to get {frame_id} from SHM") + frame = frame_manager.get(frame_id, config.cameras[camera].frame_shape_yuv) + + if frame is None: continue # send camera frame to ffmpeg process if websockets are connected diff --git a/frigate/util/image.py b/frigate/util/image.py index cb8fd18ce..a3619193f 100644 --- a/frigate/util/image.py +++ b/frigate/util/image.py @@ -694,13 +694,17 @@ class SharedMemoryFrameManager(FrameManager): self.shm_store[name] = shm return shm.buf - def get(self, name: str, shape): - if name in self.shm_store: - shm = self.shm_store[name] - else: - shm = shared_memory.SharedMemory(name=name) - self.shm_store[name] = shm - return np.ndarray(shape, dtype=np.uint8, buffer=shm.buf) + def get(self, name: str, shape) -> Optional[np.ndarray]: + try: + if name in self.shm_store: + shm = self.shm_store[name] + else: + shm = shared_memory.SharedMemory(name=name) + self.shm_store[name] = shm + return np.ndarray(shape, dtype=np.uint8, buffer=shm.buf) + except FileNotFoundError: + logger.error(f"Failed to get {name} from SHM") + return None def close(self, name: str): if name in self.shm_store: diff --git a/frigate/video.py b/frigate/video.py index 88a265381..3397de6e4 100755 --- a/frigate/video.py +++ b/frigate/video.py @@ -575,12 +575,9 @@ def process_frames( current_frame_time.value = frame_time ptz_metrics["ptz_frame_time"].value = frame_time - try: - frame = frame_manager.get( - f"{camera_name}{frame_time}", (frame_shape[0] * 3 // 2, frame_shape[1]) - ) - except FileNotFoundError: - frame = None + frame = frame_manager.get( + f"{camera_name}{frame_time}", (frame_shape[0] * 3 // 2, frame_shape[1]) + ) if frame is None: logger.info(f"{camera_name}: frame {frame_time} is not in memory store.")