use std::sync::Arc;
use async_trait::async_trait;
use oar_ocr::domain::tasks::text_detection::TextDetectionConfig;
use oar_ocr::oarocr::{OAROCR, OAROCRBuilder};
use serde::Deserialize;
use crate::backend::{BackendDescriptor, InputLimits, Locality};
use crate::cx::VisionCx;
use crate::digest::Digest;
use crate::error::{Result, VisionError};
use crate::geometry::{BBox, CoordFrame, Polygon, RawRegion, Shape};
use crate::manifest::VerifiedModel;
use crate::reading;
use crate::sent::SentImage;
use crate::traits::{BlockKind, DocumentPage, DocumentReader, ReadOptions, Word};
#[non_exhaustive]
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub enum PpOcrV6Size {
Tiny,
Small,
Medium,
}
impl PpOcrV6Size {
fn name(self) -> &'static str {
match self {
Self::Tiny => "tiny",
Self::Small => "small",
Self::Medium => "medium",
}
}
pub fn detection_manifest(self) -> String {
format!("pp-ocrv6-{}-det", self.name())
}
pub fn recognition_manifest(self) -> String {
format!("pp-ocrv6-{}-rec", self.name())
}
fn detection_config(self) -> TextDetectionConfig {
TextDetectionConfig {
score_threshold: 0.2,
box_threshold: if self == Self::Tiny { 0.4 } else { 0.45 },
unclip_ratio: 1.4,
max_candidates: 3000,
..TextDetectionConfig::default()
}
}
}
const ONNX_FILE: &str = "inference.onnx";
const PIPELINE_FILE: &str = "inference.yml";
#[derive(Deserialize)]
struct Pipeline {
#[serde(rename = "PostProcess")]
post_process: PostProcess,
}
#[derive(Deserialize)]
struct PostProcess {
character_dict: Vec<String>,
}
pub(crate) fn dictionary(yaml: &str) -> Result<Vec<String>> {
let pipeline: Pipeline = serde_saphyr::from_str(yaml).map_err(|e| VisionError::Parse {
backend: "pp-ocrv6".into(),
message: format!("the recognition model's inference.yml has no character dictionary: {e}"),
})?;
if pipeline.post_process.character_dict.is_empty() {
return Err(VisionError::Parse {
backend: "pp-ocrv6".into(),
message: "the recognition model's character dictionary is empty".into(),
});
}
Ok(pipeline.post_process.character_dict)
}
fn verified_bytes(model: &VerifiedModel, path: &str) -> Result<Vec<u8>> {
let id = &model.manifest().id;
let file = model.file(path).ok_or_else(|| VisionError::Manifest {
id: id.clone(),
reason: format!("the manifest pins no `{path}`"),
})?;
let bytes = std::fs::read(file.path())
.map_err(|e| VisionError::io("could not read", file.path(), e))?;
let actual = Digest::of(&bytes);
if actual != file.digest() {
return Err(VisionError::DigestMismatch {
path: file.path().to_path_buf(),
expected: file.digest(),
actual,
});
}
Ok(bytes)
}
pub struct PpOcrReader {
descriptor: BackendDescriptor,
engine: Arc<OAROCR>,
}
impl std::fmt::Debug for PpOcrReader {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("PpOcrReader").field("descriptor", &self.descriptor).finish_non_exhaustive()
}
}
impl PpOcrReader {
pub fn from_models(
size: PpOcrV6Size,
detection: &VerifiedModel,
recognition: &VerifiedModel,
) -> Result<Self> {
let det = verified_bytes(detection, ONNX_FILE)?;
let rec = verified_bytes(recognition, ONNX_FILE)?;
let yaml = verified_bytes(recognition, PIPELINE_FILE)?;
let yaml = String::from_utf8(yaml).map_err(|e| VisionError::Parse {
backend: "pp-ocrv6".into(),
message: format!("inference.yml is not UTF-8: {e}"),
})?;
let dict = dictionary(&yaml)?;
let id = format!("pp-ocrv6-{}", size.name());
let environment = adk_core::onnx::ort_environment().map_err(|e| VisionError::Backend {
backend: id.clone(),
message: format!("ONNX Runtime could not start: {e}"),
})?;
let _ = oar_ocr::core::inference::set_ort_environment(environment);
let engine = OAROCRBuilder::new(det, rec, "")
.character_dict_content(dict.join("\n"))
.text_detection_config(size.detection_config())
.return_word_box(true)
.build()
.map_err(|e| VisionError::Backend {
backend: id.clone(),
message: format!("ONNX Runtime could not load PP-OCRv6 {}: {e}", size.name()),
})?;
tracing::info!(
vision.backend = %id,
model.detection = %detection.manifest().id,
model.recognition = %recognition.manifest().id,
model.dictionary = dict.len(),
"pp-ocrv6 loaded"
);
let descriptor = BackendDescriptor::new(id, Locality::InProcess, CoordFrame::SentPixels)
.with_manifest(recognition.manifest().id.clone())
.with_limits(InputLimits::long_edge(4096));
Ok(Self { descriptor, engine: Arc::new(engine) })
}
#[cfg(feature = "fetch")]
pub async fn fetch(fetcher: &crate::ModelFetcher, size: PpOcrV6Size) -> Result<Self> {
let manifests = crate::ManifestSet::builtin()?;
let lookup = |id: String| {
manifests.get(&id).cloned().ok_or_else(|| VisionError::Manifest {
id,
reason: "not in the built-in manifest".into(),
})
};
let det = fetcher.fetch(&lookup(size.detection_manifest())?).await?;
let rec = fetcher.fetch(&lookup(size.recognition_manifest())?).await?;
Self::from_models(size, &det, &rec)
}
pub fn with_id(mut self, id: impl Into<String>) -> Self {
self.descriptor.id = crate::backend::BackendId::new(id);
self
}
}
fn words(text: &str, chars: &[BBox], confidence: Option<f32>) -> Vec<Word<RawRegion>> {
let letters: Vec<char> = text.chars().collect();
if letters.len() != chars.len() {
return Vec::new();
}
let mut out = Vec::new();
let mut current: Option<(String, BBox)> = None;
for (c, b) in letters.iter().zip(chars) {
if c.is_whitespace() {
out.extend(current.take());
continue;
}
current = Some(match current.take() {
Some((mut word, bbox)) => {
word.push(*c);
(
word,
BBox::new(
bbox.x0.min(b.x0),
bbox.y0.min(b.y0),
bbox.x1.max(b.x1),
bbox.y1.max(b.y1),
),
)
}
None => (c.to_string(), *b),
});
}
out.extend(current);
out.into_iter()
.map(|(text, bbox)| Word {
region: RawRegion {
shape: Shape::Box(bbox),
label: None,
score: confidence,
track: None,
},
text,
confidence,
})
.collect()
}
#[async_trait]
impl DocumentReader for PpOcrReader {
fn descriptor(&self) -> &BackendDescriptor {
&self.descriptor
}
async fn read(
&self,
cx: &VisionCx,
page: &SentImage,
options: &ReadOptions,
) -> Result<DocumentPage<RawRegion>> {
cx.check()?;
let (w, h) = page.size();
if w < 8 || h < 8 {
return Ok(DocumentPage::default());
}
let rgb = image::DynamicImage::ImageRgba8(page.pixels().inner().clone()).to_rgb8();
let engine = Arc::clone(&self.engine);
let backend = self.descriptor.id.to_string();
let results = tokio::task::spawn_blocking(move || engine.predict(vec![rgb]))
.await
.map_err(|e| VisionError::Backend {
backend: backend.clone(),
message: format!("the OCR task failed: {e}"),
})?
.map_err(|e| VisionError::Backend {
backend: backend.clone(),
message: e.to_string(),
})?;
let Some(result) = results.into_iter().next() else {
return Ok(DocumentPage::default());
};
let mut blocks = Vec::new();
let mut boxes = Vec::new();
for region in result.text_regions {
let Some(text) = region.text.as_deref().map(str::trim).filter(|t| !t.is_empty()) else {
continue;
};
let points: Vec<[f32; 2]> =
region.bounding_box.points.iter().map(|p| [p.x, p.y]).collect();
let Some(bbox) = BBox::covering(points.iter().copied()) else { continue };
let chars: Vec<BBox> = region
.word_boxes
.as_deref()
.unwrap_or_default()
.iter()
.filter_map(|b| BBox::covering(b.points.iter().map(|p| [p.x, p.y])))
.collect();
let full = region.text.as_deref().unwrap_or_default();
let mut block = reading::block(
BlockKind::Text,
RawRegion {
shape: Shape::Polygon(Polygon { points }),
label: None,
score: region.confidence,
track: None,
},
text.to_string(),
region.confidence,
);
block.words = words(full, &chars, region.confidence);
boxes.push(bbox);
blocks.push(block);
}
let reading_order = reading::reading_order(&boxes);
let mut out = DocumentPage { blocks, reading_order, markdown: None };
if options.markdown {
out.markdown = Some(reading::markdown(&out));
}
Ok(out)
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn the_dictionary_comes_from_the_pipeline_yaml() {
let yaml = "Global:\n model_name: PP-OCRv6_tiny_rec\nHpi:\n a: &id001\n - 1\n b: *id001\nPostProcess:\n name: CTCLabelDecode\n character_dict:\n - '!'\n - '\"'\n - ''''\n - y\n - n\n - '0'\n - ~\n";
assert_eq!(dictionary(yaml).unwrap(), vec!["!", "\"", "'", "y", "n", "0", "~"]);
}
#[test]
fn a_pipeline_without_a_dictionary_is_refused() {
assert!(matches!(dictionary("Global: {}\n"), Err(VisionError::Parse { .. })));
}
#[test]
fn character_boxes_merge_into_words_at_spaces() {
let chars: Vec<BBox> =
(0..7).map(|i| BBox::new(i as f32 * 10.0, 0.0, i as f32 * 10.0 + 10.0, 20.0)).collect();
let found = words("ab cd e", &chars, Some(0.9));
let texts: Vec<(&str, Option<BBox>)> =
found.iter().map(|w| (w.text.as_str(), w.region.shape.bbox())).collect();
assert_eq!(
texts,
vec![
("ab", Some(BBox::new(0.0, 0.0, 20.0, 20.0))),
("cd", Some(BBox::new(30.0, 0.0, 50.0, 20.0))),
("e", Some(BBox::new(60.0, 0.0, 70.0, 20.0))),
]
);
assert!(words("abc", &chars, None).is_empty());
}
}