Simplify empty shm error handling

This commit is contained in:
Nicolas Mowen 2024-07-09 06:36:15 -06:00
parent c4fbe6df65
commit 03e32118ae
4 changed files with 25 additions and 26 deletions

View File

@ -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

View File

@ -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

View File

@ -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:

View File

@ -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.")