From f05ff552ffc41f677b0e6c2d6dbfb2d4c9bccd90 Mon Sep 17 00:00:00 2001 From: Nick Mowen Date: Tue, 17 Oct 2023 14:27:52 -0600 Subject: [PATCH] Use regions grid for startup scan --- frigate/util/object.py | 30 ++++++++++++++++++++++++++++++ frigate/video.py | 25 ++++++------------------- 2 files changed, 36 insertions(+), 19 deletions(-) diff --git a/frigate/util/object.py b/frigate/util/object.py index 8aefe45a7..046d6818e 100644 --- a/frigate/util/object.py +++ b/frigate/util/object.py @@ -114,6 +114,8 @@ def get_camera_regions_grid( std_dev = np.std(cell["sizes"]) mean = np.mean(cell["sizes"]) logger.debug(f"std dev: {std_dev} mean: {mean}") + cell["x"] = x + cell["y"] = y cell["std_dev"] = std_dev cell["mean"] = mean @@ -442,3 +444,31 @@ def get_consolidated_object_detections(detected_object_groups): consolidated_detections.append(sorted_by_area[current_detection_idx]) return consolidated_detections + + +def get_startup_regions(frame_shape: tuple[int], region_min_size: int, region_grid: list[list[dict[str, any]]]) -> list[list[int]]: + """Get a list of regions to run on startup.""" + # return 8 most popular regions for the camera + all_cells = np.concatenate(region_grid).flat + startup_cells = sorted(all_cells, key=lambda c: len(c["sizes"]), reverse=True)[0:8] + regions = [] + + for cell in startup_cells: + # rest of the cells are empty + if not cell["sizes"]: + break + + x = frame_shape[1] / GRID_SIZE * (0.5 + cell["x"]) + y = frame_shape[0] / GRID_SIZE * (0.5 + cell["y"]) + size = cell["mean"] * frame_shape[1] + regions.append(calculate_region( + frame_shape, + x - size / 2, + y - size / 2, + x + size / 2, + y + size / 2, + region_min_size, + multiplier=1, + )) + + return regions diff --git a/frigate/video.py b/frigate/video.py index 68c4175f8..33ace1ad2 100755 --- a/frigate/video.py +++ b/frigate/video.py @@ -26,7 +26,6 @@ from frigate.util.builtin import EventsPerSecond, get_tomorrow_at_2 from frigate.util.image import ( FrameManager, SharedMemoryFrameManager, - calculate_region, draw_box_with_label, ) from frigate.util.object import ( @@ -37,6 +36,7 @@ from frigate.util.object import ( get_cluster_region_from_grid, get_consolidated_object_detections, get_min_region_size, + get_startup_regions, inside_any, intersects_any, is_object_filtered, @@ -520,7 +520,7 @@ def process_frames( fps_tracker = EventsPerSecond() fps_tracker.start() - startup_scan_counter = 0 + startup_scan = True region_min_size = get_min_region_size(model_config) @@ -626,23 +626,10 @@ def process_frames( regions += motion_regions # if starting up, get the next startup scan region - if startup_scan_counter < 9: - ymin = int(frame_shape[0] / 3 * startup_scan_counter / 3) - ymax = int(frame_shape[0] / 3 + ymin) - xmin = int(frame_shape[1] / 3 * startup_scan_counter / 3) - xmax = int(frame_shape[1] / 3 + xmin) - regions.append( - calculate_region( - frame_shape, - xmin, - ymin, - xmax, - ymax, - region_min_size, - multiplier=1.2, - ) - ) - startup_scan_counter += 1 + if startup_scan: + for region in get_startup_regions(frame_shape, region_min_size, region_grid): + regions.append(region) + startup_scan = False # resize regions and detect # seed with stationary objects