fix: 運鏡分析對往復/多角度運鏡誤判 Static 的問題
Core Test / lint-and-test (push) Has been cancelled
Cross-Project Integration / integration (push) Has been cancelled

根因: 相位相關正確偵測到每段位移 (022.mp4 pans=[-1,-17,16,20,-1],
tilts=[0,7,-16,30,-47]),但取『有符號中位數』後正負抵消 → pan=-1 tilt=0
→ 誤判 Static,只選 1 張。

修復:
- level 判斷改用『所有 pair 位移絕對值的最大值』(fold max),
  不再用有符號中位數;多角度/往復運鏡不會再被抵消
- confidence<0.15 的 pair 過濾(原本 tanh(peak/second) 全 0.77 不可信)
- 閾值下調: high >10px (256 圖), zoom>0.15, rot>0.05
- pan_px/tilt_px 回傳最大位移的 signed 值(真實振幅)

驗證: 022.mp4 (10s 單鏡頭大角度 tilt)
  修復前: Static/None → 1 張
  修復後: Tilt/High (tilt=-47px) → 4 張 (Start/Mid/Mid/End)
This commit is contained in:
Accusys
2026-11-19 18:44:46 +08:00
parent a6a78cca8b
commit 797f4c61f6
+63 -22
View File
@@ -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<f64> = good.iter().map(|x| x.0).collect();
let mut tilts_g: Vec<f64> = good.iter().map(|x| x.1).collect();
let mut zooms_g: Vec<f64> = good.iter().map(|x| x.2).collect();
let mut rots_g: Vec<f64> = 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,