use visual_cortex_capture::Frame;
use visual_cortex_ocr_onnx::PaddleOcr;
use visual_cortex_vision::OcrEngine;
#[tokio::test]
#[ignore = "downloads models (~15 MB) and runs real inference"]
async fn recognizes_fixture_text() {
let png = include_bytes!("fixtures/hp_1234.png");
let img = image::load_from_memory(png)
.expect("decode fixture")
.to_rgba8();
let (w, h) = img.dimensions();
let bgra: Vec<u8> = img
.pixels()
.flat_map(|p| [p.0[2], p.0[1], p.0[0], p.0[3]])
.collect();
let frame = Frame::new(w, h, bgra).expect("frame");
let view = frame
.view(visual_cortex_capture::PxRect { x: 0, y: 0, w, h })
.expect("view");
let mut engine = PaddleOcr::new().await.expect("engine (network + cache)");
let spans = engine.recognize(&view).expect("recognize");
assert!(!spans.is_empty(), "no text found in fixture");
let joined = spans
.iter()
.map(|s| s.text.as_str())
.collect::<Vec<_>>()
.join(" ");
let normalized = joined.to_uppercase().replace(' ', "");
assert!(
normalized.contains("HP") && normalized.contains("1234"),
"expected 'HP 1234' in output, got: {joined:?}"
);
for s in &spans {
assert!(s.confidence.is_finite());
}
}
#[tokio::test]
#[ignore = "downloads models on cold cache"]
async fn cloned_engines_share_models_and_both_recognize() {
use visual_cortex_capture::PxRect;
use visual_cortex_vision::OcrEngine;
let mut a = visual_cortex_ocr_onnx::PaddleOcr::new().await.unwrap();
let mut b = a.clone();
let img = image::open(concat!(
env!("CARGO_MANIFEST_DIR"),
"/tests/fixtures/hp_1234.png"
))
.unwrap()
.to_rgba8();
let (w, h) = (img.width(), img.height());
let mut bgra = img.into_raw();
for px in bgra.chunks_exact_mut(4) {
px.swap(0, 2);
}
let frame = visual_cortex_capture::Frame::new(w, h, bgra).unwrap();
let view = frame.view(PxRect { x: 0, y: 0, w, h }).unwrap();
let ta: Vec<String> = a
.recognize(&view)
.unwrap()
.into_iter()
.map(|s| s.text)
.collect();
let tb: Vec<String> = b
.recognize(&view)
.unwrap()
.into_iter()
.map(|s| s.text)
.collect();
assert_eq!(ta, tb);
assert!(ta.iter().any(|t| t.contains("1234")), "got {ta:?}");
}