From cd498a6cb239e322d14eacff4990f36dc0eaf119 Mon Sep 17 00:00:00 2001 From: Josh Hawkins <32435876+hawkeye217@users.noreply.github.com> Date: Mon, 9 Mar 2026 12:34:04 -0500 Subject: [PATCH] add util for optimal detect resolution --- web/src/utils/cameraUtil.ts | 41 +++++++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) diff --git a/web/src/utils/cameraUtil.ts b/web/src/utils/cameraUtil.ts index 543605ad0..11c4988da 100644 --- a/web/src/utils/cameraUtil.ts +++ b/web/src/utils/cameraUtil.ts @@ -115,6 +115,47 @@ export type CameraAudioFeatures = { * @param requireSecureContext - If true, two-way audio requires secure context (default: true) * @returns CameraAudioFeatures object with detected capabilities */ +/** + * Calculates optimal detect dimensions from stream resolution. + * + * Scales dimensions to an efficient size for object detection while + * preserving the stream's aspect ratio. Does not upscale. + * + * @param streamWidth - Native stream width in pixels + * @param streamHeight - Native stream height in pixels + * @returns Detect dimensions with even values, or null if inputs are invalid + */ +export function calculateDetectDimensions( + streamWidth: number, + streamHeight: number, +): { width: number; height: number } | null { + if ( + !Number.isFinite(streamWidth) || + !Number.isFinite(streamHeight) || + streamWidth <= 0 || + streamHeight <= 0 + ) { + return null; + } + + const smallerDim = Math.min(streamWidth, streamHeight); + const target = Math.min(720, smallerDim); + const scale = target / smallerDim; + + let width = Math.round(streamWidth * scale); + let height = Math.round(streamHeight * scale); + + // Round down to even numbers (required for video processing) + width = width - (width % 2); + height = height - (height % 2); + + if (width < 2 || height < 2) { + return null; + } + + return { width, height }; +} + export function detectCameraAudioFeatures( metadata: LiveStreamMetadata | null | undefined, requireSecureContext: boolean = true,