From 3a877894f1de0ffbadfe8f7a0a838657683e2be9 Mon Sep 17 00:00:00 2001 From: Nick Mowen Date: Mon, 12 Jun 2023 14:13:45 -0600 Subject: [PATCH] Handle vertical cameras more optimally in 2 camera layout --- frigate/output.py | 108 +++++++++++++++++++++++++++++++++++----------- 1 file changed, 83 insertions(+), 25 deletions(-) diff --git a/frigate/output.py b/frigate/output.py index ea0a19b90..689c4af2e 100644 --- a/frigate/output.py +++ b/frigate/output.py @@ -269,10 +269,90 @@ class BirdsEyeFrameManager: def update_frame(self): """Update to a new frame for birdseye.""" + def calculate_two_cam_layout(canvas, cameras_to_add: list[str]) -> tuple[any]: + """Calculate the optimal layout for 2 cameras.""" + first_camera = cameras_to_add[0] + first_camera_dims = self.cameras[first_camera]["dimensions"].copy() + second_camera = cameras_to_add[1] + second_camera_dims = self.cameras[second_camera]["dimensions"].copy() + + # check for optimal layout + if first_camera_dims[0] + second_camera_dims[0] < canvas_width: + # place cameras horizontally + first_scaled_width = int( + canvas_height * first_camera_dims[0] / first_camera_dims[1] + ) + second_scaled_width = int( + canvas_height * second_camera_dims[0] / second_camera_dims[1] + ) + first_height = canvas_height + second_height = canvas_height + + if first_scaled_width + second_scaled_width > canvas_width: + if first_scaled_width > second_scaled_width: + first_scaled_width = canvas_width - second_scaled_width + first_height = int( + first_scaled_width + * first_camera_dims[1] + / first_camera_dims[0] + ) + else: + second_scaled_width = canvas_width - first_scaled_width + second_height = int( + second_scaled_width + * second_camera_dims[1] + / second_camera_dims[0] + ) + + return [ + [ + ( + first_camera, + (0, 0, first_scaled_width, first_height), + ), + ( + second_camera, + ( + first_scaled_width + 1, + 0, + second_scaled_width, + second_height, + ), + ), + ], + ] + elif first_camera_dims[1] + second_camera_dims[1] < canvas_height: + # place cameras vertically + top_scaled_width = int( + (canvas_height / 2) * first_camera_dims[0] / first_camera_dims[1] + ) + bottom_scaled_width = int( + (canvas_height / 2) * second_camera_dims[0] / second_camera_dims[1] + ) + return [ + [ + ( + first_camera, + (0, 0, top_scaled_width, int(canvas_height / 2)), + ) + ], + [ + ( + second_camera, + ( + 0, + int(canvas_height / 2), + bottom_scaled_width, + int(canvas_height / 2), + ), + ) + ], + ] + def calculate_layout( canvas, cameras_to_add: list[str], coefficient ) -> tuple[any]: - """Calculate the optimal layout for cameras.""" + """Calculate the optimal layout for 3+ cameras.""" camera_layout: list[list[any]] = [] camera_layout.append([]) canvas_aspect = canvas[0] / canvas[1] @@ -403,31 +483,9 @@ class BirdsEyeFrameManager: ] ] elif len(active_cameras) == 2: - # split canvas in half for 2 cameras - top_camera = active_cameras_to_add[0] - top_camera_dims = self.cameras[top_camera]["dimensions"].copy() - bottom_camera = active_cameras_to_add[1] - bottom_camera_dims = self.cameras[bottom_camera]["dimensions"].copy() - top_scaled_width = int( - (canvas_height / 2) * top_camera_dims[0] / top_camera_dims[1] + self.camera_layout = calculate_two_cam_layout( + (canvas_width, canvas_height), active_cameras_to_add ) - bottom_scaled_width = int( - (canvas_height / 2) * bottom_camera_dims[0] / bottom_camera_dims[1] - ) - self.camera_layout = [ - [(top_camera, (0, 0, top_scaled_width, int(canvas_height / 2)))], - [ - ( - bottom_camera, - ( - 0, - int(canvas_height / 2), - bottom_scaled_width, - int(canvas_height / 2), - ), - ) - ], - ] else: # calculate optimal layout coefficient = 1.0