refactor: migrate OCR processor to Swift, bypass Python wrapper

- Modified ocr.rs to prioritize Swift binary over Python wrapper
- Swift OCR already compiled and tested
- Retained Python fallback for backward compatibility
- Created migration plan document for full Python → Rust transition

Phase 1 of processor architecture refactoring (user chose option C)

Related: once_cell::Lazy poisoning issue blocking Python processors
This commit is contained in:
Accusys
2026-09-26 00:48:47 +08:00
parent 7c54c7b7e7
commit e283b477d2
2 changed files with 189 additions and 22 deletions
+50 -22
View File
@@ -236,31 +236,59 @@ pub async fn process_ocr(
uuid: Option<&str>,
frames: Option<&[i64]>,
) -> Result<OcrResult> {
let executor = PythonExecutor::new()?;
let script_path = executor.script_path("ocr_processor.py");
tracing::info!("[OCR] Starting text recognition: {}", video_path);
if !script_path.exists() {
tracing::warn!("[OCR] Script not found, returning empty result");
return Ok(OcrResult {
frame_count: 0,
fps: 0.0,
frames: vec![],
});
}
let swift_ocr_path = std::path::Path::new("/Users/accusys/momentry_core/scripts/swift_processors/.build/release/swift_ocr");
if swift_ocr_path.exists() {
tracing::info!("[OCR] Using Swift processor: {:?}", swift_ocr_path);
let mut args = vec![video_path.to_string(), output_path.to_string()];
if let Some(u) = uuid {
args.push("--uuid".to_string());
args.push(u.to_string());
}
let output = tokio::process::Command::new(swift_ocr_path)
.args(&args)
.output()
.await
.context("Failed to execute swift_ocr")?;
if !output.status.success() {
let stderr = String::from_utf8_lossy(&output.stderr);
tracing::error!("[OCR] Swift processor failed: {}", stderr);
anyhow::bail!("Swift OCR failed: {}", stderr);
}
tracing::info!("[OCR] Swift processor completed successfully");
} else {
tracing::warn!("[OCR] Swift binary not found, falling back to Python");
let executor = PythonExecutor::new()?;
let script_path = executor.script_path("ocr_processor.py");
executor
.run_with_frames(
"ocr_processor.py",
&[video_path, output_path],
uuid,
"OCR",
Some(OCR_TIMEOUT),
frames,
)
.await
.with_context(|| format!("Failed to run {:?}", script_path))?;
if !script_path.exists() {
tracing::warn!("[OCR] Script not found, returning empty result");
return Ok(OcrResult {
frame_count: 0,
fps: 0.0,
frames: vec![],
});
}
executor
.run_with_frames(
"ocr_processor.py",
&[video_path, output_path],
uuid,
"OCR",
Some(OCR_TIMEOUT),
frames,
)
.await
.with_context(|| format!("Failed to run {:?}", script_path))?;
}
let json_str = std::fs::read_to_string(output_path).context("Failed to read OCR output")?;