#[cfg(test)]
mod tests {
use image::GenericImageView;
use std::path::PathBuf;
use uni_ocr::{Credentials, Language, OcrEngine, OcrOptions, OcrProvider};
#[tokio::test]
#[ignore]
async fn test_custom_ocr() {
println!("Starting custom OCR test...");
let mut path = PathBuf::from(env!("CARGO_MANIFEST_DIR"));
path.push("tests");
path.push("testing_OCR.png");
println!("Path to testing_OCR.png: {:?}", path);
if let Ok(metadata) = std::fs::metadata(&path) {
println!("File size: {} bytes", metadata.len());
}
let image = image::open(&path).expect("Failed to open test image");
println!("Image dimensions: {:?}", image.dimensions());
let credentials = Credentials {
api_url: "http://localhost:8000/ocr".to_string(),
api_key: "".to_string(),
timeout_ms: 5000,
};
let engine = OcrEngine::new(OcrProvider::Custom { credentials })
.unwrap()
.with_options(OcrOptions::default().languages(vec![Language::English]));
let (ocr_text, structured_data, confidence) = engine.recognize_image(&image).await.unwrap();
println!("OCR text: {:?}", ocr_text);
println!("Structured data: {:?}", structured_data);
println!("Confidence: {:?}", confidence);
assert!(
!ocr_text.is_empty(),
"Custom OCR did not return any recognized text."
);
}
#[tokio::test]
#[ignore]
async fn test_custom_ocr_chinese() {
println!("Starting custom OCR Chinese test...");
let mut path = PathBuf::from(env!("CARGO_MANIFEST_DIR"));
path.push("tests");
path.push("testing_OCR_chinese.png");
println!("Path to testing_OCR_chinese.png: {:?}", path);
if let Ok(metadata) = std::fs::metadata(&path) {
println!("File size: {} bytes", metadata.len());
}
let image = image::open(&path).expect("Failed to open Chinese test image");
println!("Image dimensions: {:?}", image.dimensions());
let credentials = Credentials {
api_url: "http://localhost:8000/ocr".to_string(),
api_key: "".to_string(),
timeout_ms: 30000000,
};
let engine = OcrEngine::new(OcrProvider::Custom { credentials })
.unwrap()
.with_options(OcrOptions::default().languages(vec![Language::Chinese]));
let (ocr_text, _, _) = engine.recognize_image(&image).await.unwrap();
println!("OCR text: {:?}", ocr_text);
assert!(
ocr_text.contains("管理分支"),
"OCR failed to recognize Chinese text: {:?}",
ocr_text
);
}
}