diff --git a/src/core/processor/camera_motion.rs b/src/core/processor/camera_motion.rs index b13fbdd4..1861ccf6 100644 --- a/src/core/processor/camera_motion.rs +++ b/src/core/processor/camera_motion.rs @@ -67,47 +67,88 @@ pub async fn analyze_camera_motion( return Ok(CameraMotion::static_motion()); } - let pan = median(&mut pans); - let tilt = median(&mut tilts); - let zoom = median(&mut zooms); - let rot = median(&mut rots); - let confidence = median(&mut peaks).clamp(0.0, 1.0); + // Filter out unreliable pairs (low phase-correlation peak). A large-angle + // camera move breaks phase correlation on some pairs; those produce bogus + // displacements that would poison the median. Keep only confident pairs. + let mut good: Vec<(f64, f64, f64, f64, f64)> = Vec::new(); // (pan,tilt,zoom,rot,conf) + for i in 0..pans.len() { + if peaks[i] >= 0.15 { + good.push((pans[i], tilts[i], zooms[i], rots[i], peaks[i])); + } + } + debug!( + "[CAM-MOTION] pairs={} confident={} pans={:?} tilts={:?} zooms={:?} peaks={:?}", + pans.len(), good.len(), pans, tilts, zooms, peaks + ); + if good.is_empty() { + // All phase-correlation pairs unreliable — this itself indicates a + // large, fast camera move (or heavy scene change). Fall back to raw + // median but flag low confidence so caller can still add samples. + let pan = median(&mut pans); + let tilt = median(&mut tilts); + let zoom = median(&mut zooms); + let rot = median(&mut rots); + return Ok(CameraMotion { + motion_type: if pan.abs() > tilt.abs() { MotionType::Pan } else { MotionType::Tilt }, + level: if pan.abs() > 8.0 || tilt.abs() > 8.0 { MotionLevel::High } else { MotionLevel::Low }, + pan_px: pan, tilt_px: tilt, zoom_factor: zoom, rotation_rad: rot, + confidence: 0.2, + }); + } + let mut pans_g: Vec = good.iter().map(|x| x.0).collect(); + let mut tilts_g: Vec = good.iter().map(|x| x.1).collect(); + let mut zooms_g: Vec = good.iter().map(|x| x.2).collect(); + let mut rots_g: Vec = good.iter().map(|x| x.3).collect(); + let pan = median(&mut pans_g); + let tilt = median(&mut tilts_g); + let zoom = median(&mut zooms_g); + let rot = median(&mut rots_g); + let confidence = median(&mut good.iter().map(|x| x.4).collect()).clamp(0.0, 1.0); - // Classify: displacement relative to frame width, zoom/rotation thresholds. - let pan_abs = pan.abs(); - let tilt_abs = tilt.abs(); - let zoom_delta = (zoom - 1.0).abs(); - let rot_abs = rot.abs(); + // Classify using the LARGEST per-pair displacement magnitude, not the + // median of signed values. A pan/tilt that reverses direction (or a + // multi-angle sweep) cancels out in a signed median but is still real + // camera motion — we must not classify it as Static. + let pan_abs = good.iter().map(|x| x.0.abs()).fold(0.0f64, f64::max); + let tilt_abs = good.iter().map(|x| x.1.abs()).fold(0.0f64, f64::max); + let zoom_delta = good.iter().map(|x| (x.2 - 1.0).abs()).fold(0.0f64, f64::max); + let rot_abs = good.iter().map(|x| x.3.abs()).fold(0.0f64, f64::max); let max_shift = pan_abs.max(tilt_abs); - let (level, motion_type) = if max_shift < FFT_SIZE as f64 * 0.05 - && zoom_delta < 0.05 - && rot_abs < 0.02 + let (level, motion_type) = if max_shift < 3.0 + && zoom_delta < 0.03 + && rot_abs < 0.01 { (MotionLevel::None, MotionType::Static) } else { - let high = max_shift > FFT_SIZE as f64 * 0.20 - || zoom_delta > 0.30 - || rot_abs > 0.08; + // 256px frame: >10px shift is already a clearly visible camera move. + let high = max_shift > 10.0 || zoom_delta > 0.15 || rot_abs > 0.05; let level = if high { MotionLevel::High } else { MotionLevel::Low }; // Dominant type by normalized magnitude. let mut candidates = vec![ - (pan_abs / (FFT_SIZE as f64 * 0.2), MotionType::Pan), - (tilt_abs / (FFT_SIZE as f64 * 0.2), MotionType::Tilt), - (zoom_delta / 0.3, MotionType::Zoom), - (rot_abs / 0.08, MotionType::Rotation), + (pan_abs / 12.0, MotionType::Pan), + (tilt_abs / 12.0, MotionType::Tilt), + (zoom_delta / 0.15, MotionType::Zoom), + (rot_abs / 0.05, MotionType::Rotation), ]; candidates.sort_by(|a, b| b.0.partial_cmp(&a.0).unwrap_or(std::cmp::Ordering::Equal)); let motion_type = candidates.first().map(|x| x.1).unwrap_or(MotionType::Pan); (level, motion_type) }; + // Report the largest observed displacement per axis (signed from the pair + // with largest magnitude) so callers see the real camera-move amplitude. + let pan_signed = good.iter().max_by(|a, b| a.0.abs().partial_cmp(&b.0.abs()).unwrap()) + .map(|x| x.0).unwrap_or(pan); + let tilt_signed = good.iter().max_by(|a, b| a.1.abs().partial_cmp(&b.1.abs()).unwrap()) + .map(|x| x.1).unwrap_or(tilt); + Ok(CameraMotion { motion_type, level, - pan_px: pan, - tilt_px: tilt, + pan_px: pan_signed, + tilt_px: tilt_signed, zoom_factor: zoom, rotation_rad: rot, confidence,