1mod native;
15mod router;
16#[cfg(feature = "onnx")]
17mod support;
18mod types;
19pub mod worker;
20
21#[cfg(all(target_os = "macos", feature = "native"))]
22pub use native::vision;
23#[cfg(all(windows, feature = "native"))]
24pub use native::win_native;
25pub use native::{canary_png, native_backend};
26pub use router::{
27 BackendPreference, Isolation, ModelLeaseHook, NativeHealthHook, Ocr, OcrOptions, OcrOutcome,
28 OcrResult, OnnxConfig,
29};
30pub use types::{
31 decode_encoded, is_backend_unavailable, OcrBackend, OcrErrorKind, OcrLine, OcrRect,
32 MACOS_VISION_UNAVAILABLE, MAX_PIXELS, OCR_UNSUPPORTED_PLATFORM, WINDOWS_OCR_UNAVAILABLE,
33};
34pub use worker::{RunRecord, WorkerCommand, WorkerEvent, WorkerEvents, WorkerFailure, WorkerRun};
35
36#[cfg(feature = "onnx")]
37use image::{imageops, RgbImage};
38#[cfg(feature = "onnx")]
39use rightkit_onnx::ort;
40#[cfg(feature = "onnx")]
41use rightkit_onnx::ort::{session::Session, value::Tensor};
42#[cfg(feature = "onnx")]
43use rightkit_onnx::ProviderDiagnostic;
44#[cfg(feature = "onnx")]
45use sha2::{Digest, Sha256};
46use std::fmt;
47#[cfg(feature = "onnx")]
48use std::path::Path;
49use std::path::PathBuf;
50#[cfg(all(test, feature = "onnx"))]
51use support::ctc_greedy_decode;
52#[cfg(feature = "onnx")]
53use support::{
54 build_session, crop_box, ctc_greedy_decode_scored, dedup_boxes, det_chw_data, det_net_size,
55 extract_boxes, group_into_lines, load_image, load_vocab, rec_chw_data, BBox, DET_BAND_OVERLAP,
56 DET_MAX_W, DET_TILE_H,
57};
58
59#[derive(Debug, Clone, PartialEq, Eq)]
64pub enum OcrProvenance {
65 Resolved {
66 label: String,
67 source: String,
68 license: String,
69 },
70 Conflicting {
71 labels: Vec<String>,
72 },
73}
74
75#[derive(Debug, Clone, PartialEq, Eq)]
76pub struct OcrArtifactManifest {
77 pub provenance: OcrProvenance,
78 pub detector_sha256: String,
79 pub recognizer_sha256: String,
80 pub dictionary_sha256: String,
81 pub fixture_sha256: String,
82 pub expected_fixture_output: String,
83}
84
85impl OcrArtifactManifest {
86 pub fn characterized_windows_contract() -> Self {
87 Self {
88 provenance: OcrProvenance::Conflicting {
89 labels: vec!["PP-OCRv5".to_string(), "PP-OCRv6".to_string()],
90 },
91 detector_sha256: "eb13b44b25bb36f89528b68720af8a61d9cf381176107f465db1757b65d086e1"
92 .to_string(),
93 recognizer_sha256: "5435fd747c9e0efe15a96d0b378d5bd157e9492ed8fd80edf08f30d02fa24634"
94 .to_string(),
95 dictionary_sha256: "b5f2bfe2bdd9448429e3e82b51c789775d9b42f2403d082b00662eb77e401c5d"
96 .to_string(),
97 fixture_sha256: "803e102100c91a077607cd2d4750d9d748d86ced48fabc0f77062f26dcba0a4b"
98 .to_string(),
99 expected_fixture_output: "RIGHTKIT OCR 2026\nShared parity fixture".to_string(),
100 }
101 }
102
103 pub fn pp_ocr_v6() -> Self {
114 let mut manifest = Self::characterized_windows_contract();
115 manifest.provenance = OcrProvenance::Resolved {
116 label: "PP-OCRv6".to_string(),
117 source: "https://huggingface.co/PaddlePaddle/PP-OCRv6_medium_det_onnx (det) + \
118 https://huggingface.co/PaddlePaddle/PP-OCRv6_small_rec_onnx (rec + dict)"
119 .to_string(),
120 license: "Apache-2.0".to_string(),
121 };
122 manifest
123 }
124
125 pub fn with_resolved_provenance(
128 mut self,
129 label: impl Into<String>,
130 source: impl Into<String>,
131 license: impl Into<String>,
132 ) -> Result<Self, OcrError> {
133 self.provenance = OcrProvenance::Resolved {
134 label: label.into(),
135 source: source.into(),
136 license: license.into(),
137 };
138 self.validate_metadata()?;
139 Ok(self)
140 }
141
142 pub fn validate_metadata(&self) -> Result<(), OcrError> {
143 match &self.provenance {
144 OcrProvenance::Conflicting { labels } => Err(OcrError::UnresolvedProvenance {
145 labels: labels.clone(),
146 }),
147 OcrProvenance::Resolved {
148 label,
149 source,
150 license,
151 } if label.trim().is_empty()
152 || source.trim().is_empty()
153 || license.trim().is_empty() =>
154 {
155 Err(OcrError::InvalidProvenance)
156 }
157 OcrProvenance::Resolved { .. } => Ok(()),
158 }
159 }
160
161 #[cfg(feature = "onnx")]
162 pub fn validate_model_dir(&self, model_dir: &Path) -> Result<VerifiedOcrArtifacts, OcrError> {
163 self.validate_metadata()?;
164 if !model_dir.is_absolute() {
165 return Err(OcrError::UnsafeArtifactPath {
166 path: model_dir.to_path_buf(),
167 });
168 }
169 let root = model_dir
170 .canonicalize()
171 .map_err(|_| OcrError::MissingArtifact {
172 path: model_dir.to_path_buf(),
173 })?;
174 if !root.is_dir() {
175 return Err(OcrError::MissingArtifact { path: root });
176 }
177 Ok(VerifiedOcrArtifacts {
178 detector: verify_artifact(&root, "det.onnx", &self.detector_sha256)?,
179 recognizer: verify_artifact(&root, "rec.onnx", &self.recognizer_sha256)?,
180 dictionary: verify_artifact(&root, "en_dict.txt", &self.dictionary_sha256)?,
181 })
182 }
183}
184
185#[derive(Debug, Clone, PartialEq, Eq)]
186pub struct VerifiedOcrArtifacts {
187 pub detector: PathBuf,
188 pub recognizer: PathBuf,
189 pub dictionary: PathBuf,
190}
191
192#[derive(Debug, Clone, PartialEq, Eq)]
193pub enum OcrError {
194 UnresolvedProvenance {
195 labels: Vec<String>,
196 },
197 InvalidProvenance,
198 UnsafeArtifactPath {
199 path: PathBuf,
200 },
201 MissingArtifact {
202 path: PathBuf,
203 },
204 DigestMismatch {
205 path: PathBuf,
206 expected: String,
207 actual: String,
208 },
209 Runtime(String),
210 BackendUnavailable(String),
212 NoFallbackConfigured(String),
214}
215
216impl fmt::Display for OcrError {
217 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
218 match self {
219 Self::UnresolvedProvenance { labels } => write!(
220 f,
221 "OCR provenance conflict is unresolved: {}",
222 labels.join(" vs ")
223 ),
224 Self::InvalidProvenance => {
225 write!(
226 f,
227 "OCR provenance label, source, and license must be non-empty"
228 )
229 }
230 Self::UnsafeArtifactPath { path } => {
231 write!(f, "OCR artifact path must be absolute: {}", path.display())
232 }
233 Self::MissingArtifact { path } => {
234 write!(f, "OCR artifact is missing: {}", path.display())
235 }
236 Self::DigestMismatch {
237 path,
238 expected,
239 actual,
240 } => write!(
241 f,
242 "OCR artifact digest mismatch for {}: expected {expected}, got {actual}",
243 path.display()
244 ),
245 Self::Runtime(message) => write!(f, "OCR runtime error: {message}"),
246 Self::BackendUnavailable(message) => write!(f, "OCR backend unavailable: {message}"),
247 Self::NoFallbackConfigured(message) => {
248 write!(
249 f,
250 "OCR native backend unavailable and no ONNX fallback configured: {message}"
251 )
252 }
253 }
254 }
255}
256
257impl OcrError {
258 pub fn kind(&self) -> OcrErrorKind {
264 match self {
265 Self::UnresolvedProvenance { .. } | Self::InvalidProvenance => OcrErrorKind::Provenance,
266 Self::UnsafeArtifactPath { .. }
267 | Self::MissingArtifact { .. }
268 | Self::DigestMismatch { .. } => OcrErrorKind::Artifact,
269 Self::Runtime(_) => OcrErrorKind::Runtime,
270 Self::BackendUnavailable(message) => {
271 OcrErrorKind::from_message(message).unwrap_or(OcrErrorKind::BackendUnavailable)
272 }
273 Self::NoFallbackConfigured(_) => OcrErrorKind::NoFallbackConfigured,
274 }
275 }
276}
277
278impl std::error::Error for OcrError {}
279impl From<String> for OcrError {
280 fn from(value: String) -> Self {
281 Self::Runtime(value)
282 }
283}
284
285#[cfg(feature = "onnx")]
287fn artifact_paths(model_dir: &Path) -> Vec<PathBuf> {
288 ["det.onnx", "rec.onnx", "en_dict.txt"]
289 .iter()
290 .map(|name| model_dir.join(name))
291 .collect()
292}
293
294#[cfg(feature = "onnx")]
295fn verify_artifact(root: &Path, name: &str, expected: &str) -> Result<PathBuf, OcrError> {
296 let path = root.join(name);
297 let canonical = path
298 .canonicalize()
299 .map_err(|_| OcrError::MissingArtifact { path: path.clone() })?;
300 if !canonical.starts_with(root) || !canonical.is_file() {
301 return Err(OcrError::UnsafeArtifactPath { path: canonical });
302 }
303 let bytes = std::fs::read(&canonical).map_err(|_| OcrError::MissingArtifact {
304 path: canonical.clone(),
305 })?;
306 let actual = format!("{:x}", Sha256::digest(bytes));
307 if actual != expected {
308 return Err(OcrError::DigestMismatch {
309 path: canonical,
310 expected: expected.to_string(),
311 actual,
312 });
313 }
314 Ok(canonical)
315}
316
317#[cfg(feature = "onnx")]
318pub fn ocr_image_with_manifest(
319 path: &Path,
320 model_dir: &Path,
321 manifest: &OcrArtifactManifest,
322) -> Result<String, OcrError> {
323 let mut engine = OcrEngine::load_with_manifest(model_dir, manifest)?;
324 let img = load_image(path)?;
325 engine.run(&img)
326}
327
328#[cfg(all(test, feature = "onnx"))]
331mod tests;
332#[cfg(feature = "onnx")]
340pub struct OcrEngine {
341 det: Session,
342 rec: Session,
343 vocab: Vec<char>,
348 provider_diagnostics: Vec<ProviderDiagnostic>,
349 _lease: Option<Box<dyn std::any::Any + Send>>,
351}
352
353#[cfg(feature = "onnx")]
354impl OcrEngine {
355 pub fn load(model_dir: &Path) -> Result<Self, OcrError> {
358 Self::load_with_manifest(model_dir, &OcrArtifactManifest::pp_ocr_v6())
359 }
360
361 pub fn load_with_manifest(
370 model_dir: &Path,
371 manifest: &OcrArtifactManifest,
372 ) -> Result<Self, OcrError> {
373 Self::load_with_lease(model_dir, manifest, None)
374 }
375
376 pub fn load_with_lease(
382 model_dir: &Path,
383 manifest: &OcrArtifactManifest,
384 lease_hook: Option<&dyn ModelLeaseHook>,
385 ) -> Result<Self, OcrError> {
386 let lease = match lease_hook {
387 Some(hook) => Some(hook.acquire_model_lease(&artifact_paths(model_dir))?),
388 None => None,
389 };
390 let artifacts = manifest.validate_model_dir(model_dir)?;
391 let vocab = load_vocab(&artifacts.dictionary)?;
392 let (det, mut provider_diagnostics) = build_session(&artifacts.detector)?;
393 let (rec, rec_diagnostics) = build_session(&artifacts.recognizer)?;
394 provider_diagnostics.extend(rec_diagnostics);
395 Ok(OcrEngine {
396 det,
397 rec,
398 vocab,
399 provider_diagnostics,
400 _lease: lease,
401 })
402 }
403
404 pub fn validate_sessions(
409 model_dir: &Path,
410 manifest: &OcrArtifactManifest,
411 ) -> Result<(), OcrError> {
412 let artifacts = manifest.validate_model_dir(model_dir)?;
413 load_vocab(&artifacts.dictionary)?;
414 build_session(&artifacts.detector)?;
415 build_session(&artifacts.recognizer)?;
416 Ok(())
417 }
418
419 pub fn provider_diagnostics(&self) -> &[ProviderDiagnostic] {
420 &self.provider_diagnostics
421 }
422
423 pub fn run(&mut self, img: &RgbImage) -> Result<String, OcrError> {
425 let lines = self.run_lines(img)?;
426 Ok(lines
427 .into_iter()
428 .map(|l| l.text)
429 .collect::<Vec<_>>()
430 .join("\n"))
431 }
432
433 pub fn run_lines(&mut self, img: &RgbImage) -> Result<Vec<OcrLine>, OcrError> {
438 let boxes = self.detect(img)?;
439 if boxes.is_empty() {
440 return Ok(Vec::new());
441 }
442 let (iw, ih) = (img.width().max(1) as f32, img.height().max(1) as f32);
443 let rows = group_into_lines(boxes);
447 let mut out = Vec::new();
448 for row in rows {
449 let mut words = Vec::new();
450 let (mut x0, mut y0, mut x1, mut y1) = (u32::MAX, u32::MAX, 0u32, 0u32);
451 let (mut conf_sum, mut conf_n, mut conf_ok) = (0.0f32, 0u32, true);
452 for bbox in &row {
453 let crop = crop_box(img, bbox);
454 if crop.width() == 0 || crop.height() == 0 {
455 continue;
456 }
457 let (text, conf) = self.recognize(&crop)?;
458 let t = text.trim().to_string();
459 if t.is_empty() {
460 continue;
461 }
462 words.push(t);
463 x0 = x0.min(bbox.x0);
464 y0 = y0.min(bbox.y0);
465 x1 = x1.max(bbox.x1);
466 y1 = y1.max(bbox.y1);
467 match conf {
468 Some(c) => {
469 conf_sum += c;
470 conf_n += 1;
471 }
472 None => conf_ok = false,
473 }
474 }
475 if words.is_empty() {
476 continue;
477 }
478 let (x0, y0) = (x0 as f32 / iw, y0 as f32 / ih);
479 let (x1, y1) = ((x1 as f32 / iw).min(1.0), (y1 as f32 / ih).min(1.0));
480 out.push(OcrLine {
481 text: words.join(" "),
482 rect: OcrRect {
483 x: x0,
484 y: y0,
485 width: (x1 - x0).max(0.0),
486 height: (y1 - y0).max(0.0),
487 },
488 confidence: (conf_ok && conf_n > 0).then(|| conf_sum / conf_n as f32),
489 });
490 }
491 Ok(out)
492 }
493
494 fn detect(&mut self, img: &RgbImage) -> Result<Vec<BBox>, String> {
508 let (orig_w, orig_h) = (img.width(), img.height());
509
510 let ws = if orig_w > DET_MAX_W {
512 DET_MAX_W as f32 / orig_w as f32
513 } else {
514 1.0
515 };
516 let work = if ws < 1.0 {
517 let nh = ((orig_h as f32 * ws).round() as u32).max(1);
518 imageops::resize(img, DET_MAX_W, nh, imageops::FilterType::Lanczos3)
519 } else {
520 img.clone()
521 };
522 let (work_w, work_h) = (work.width(), work.height());
523
524 let mut bands: Vec<(u32, u32)> = Vec::new();
527 if work_h <= DET_TILE_H {
528 bands.push((0, work_h));
529 } else {
530 let step = DET_TILE_H - DET_BAND_OVERLAP;
531 let mut y = 0u32;
532 loop {
533 let end = (y + DET_TILE_H).min(work_h);
534 bands.push((y, end));
535 if end >= work_h {
536 break;
537 }
538 y += step;
539 }
540 }
541
542 let mut all: Vec<BBox> = Vec::new();
543 for (ys, ye) in bands {
544 let band = imageops::crop_imm(&work, 0, ys, work_w, ye - ys).to_image();
545 for mut b in self.detect_one(&band)? {
546 b.y0 += ys;
547 b.y1 += ys;
548 all.push(b);
549 }
550 }
551
552 if ws < 1.0 {
554 let inv = 1.0 / ws;
555 for b in all.iter_mut() {
556 b.x0 = (b.x0 as f32 * inv) as u32;
557 b.y0 = (b.y0 as f32 * inv) as u32;
558 b.x1 = ((b.x1 as f32 * inv) as u32).min(orig_w);
559 b.y1 = ((b.y1 as f32 * inv) as u32).min(orig_h);
560 }
561 }
562
563 let mut boxes = dedup_boxes(all);
564 boxes.sort_by(|a, b| a.y0.cmp(&b.y0).then(a.x0.cmp(&b.x0)));
565 Ok(boxes)
566 }
567
568 fn detect_one(&mut self, band: &RgbImage) -> Result<Vec<BBox>, String> {
570 let (bw, bh) = (band.width(), band.height());
571 let (net_w, net_h) = det_net_size(bw, bh);
572 let resized = imageops::resize(band, net_w, net_h, imageops::FilterType::Lanczos3);
573
574 let data = det_chw_data(&resized, net_w, net_h);
575 let shape = [1i64, 3, net_h as i64, net_w as i64];
576 let tensor =
577 Tensor::<f32>::from_array((shape, data)).map_err(|e| format!("det tensor: {e}"))?;
578
579 let outputs = self
580 .det
581 .run(ort::inputs![tensor])
582 .map_err(|e| format!("det run: {e}"))?;
583
584 let (out_shape, prob_flat) = outputs[0]
586 .try_extract_tensor::<f32>()
587 .map_err(|e| format!("det extract: {e}"))?;
588 let dims: &[i64] = out_shape;
590 let (oh, ow) = match dims {
591 [_, _, h, w] => (*h as usize, *w as usize),
592 s => return Err(format!("unexpected det output shape: {s:?}")),
593 };
594 let prob_2d: &[f32] = &prob_flat[0..oh * ow];
595
596 let scale_x = bw as f32 / ow as f32;
598 let scale_y = bh as f32 / oh as f32;
599 Ok(extract_boxes(
600 prob_2d, ow, oh, scale_x, scale_y, 0.3, bw, bh,
601 ))
602 }
603
604 fn recognize(&mut self, crop: &RgbImage) -> Result<(String, Option<f32>), String> {
609 let (h, w) = (crop.height(), crop.width());
610 let new_w = ((48.0 / h as f32) * w as f32).max(4.0) as u32;
611 let strip = imageops::resize(crop, new_w, 48, imageops::FilterType::Lanczos3);
612
613 let data = rec_chw_data(&strip, new_w);
614 let shape = [1i64, 3, 48, new_w as i64];
615 let tensor =
616 Tensor::<f32>::from_array((shape, data)).map_err(|e| format!("rec tensor: {e}"))?;
617
618 let outputs = self
619 .rec
620 .run(ort::inputs![tensor])
621 .map_err(|e| format!("rec run: {e}"))?;
622
623 let (out_shape, logits_flat) = outputs[0]
625 .try_extract_tensor::<f32>()
626 .map_err(|e| format!("rec extract: {e}"))?;
627 let dims: &[i64] = out_shape;
629 let (t_len, vocab_size) = match dims {
630 [_, t, v] => (*t as usize, *v as usize),
631 s => return Err(format!("unexpected rec output shape: {s:?}")),
632 };
633 let logits = &logits_flat[0..t_len * vocab_size];
635
636 Ok(ctc_greedy_decode_scored(
637 logits,
638 t_len,
639 vocab_size,
640 &self.vocab,
641 ))
642 }
643}
644
645