"""ONNX Runtime sequence recognizer loaded only when a promoted model is configured.""" from io import BytesIO import json import math from pathlib import Path from .contracts import BeamCandidate, SemanticClass from .ctc_beam import BeamDecodeResult from .ctc_frames import search_ctc_frames from .ctc_topk import frames_from_logits from .gpu_exec import gpu_call from .ort_session import open_numeric_session from .model_loader import ( LoadStatus, RecognizerLoadResult, load_recognizer_from_env, load_recognizer_result_from_env, ) from .preprocess_variants import VARIANTS def apply_temperature(probability: float, temperature: float) -> float: """Calibrate decoded field correctness with the fitted binary temperature.""" bounded = min(max(probability, 1e-9), 1 - 1e-9) logit = math.log(bounded / (1 - bounded)) return 1 / (1 + math.exp(-logit / temperature)) def ctc_decode(indices: list[int], probabilities: list[float], vocabulary: str = "0123456789٠١٢٣٤٥٦٧٨٩۰۱۲۳۴۵۶۷۸۹/-.,:% "): blank = len(vocabulary) characters, selected, char_confidences = [], [], [] previous = blank for index, probability in zip(indices, probabilities): if index != previous and index != blank: characters.append(vocabulary[index]) selected.append(probability) char_confidences.append(probability) previous = index mean = sum(selected) / len(selected) if selected else 0.0 return "".join(characters), mean, min(selected, default=0.0), tuple(char_confidences) def _decode_beam_margin(probabilities, indices, vocabulary: str) -> float: blank = len(vocabulary) margins = [] previous = blank for row, index in zip(probabilities, indices): if index != previous and index != blank: sorted_probs = sorted(float(value) for value in row) margin = sorted_probs[-1] - sorted_probs[-2] if len(sorted_probs) > 1 else 1.0 margins.append(margin) previous = index return sum(margins) / len(margins) if margins else 1.0 def _result_to_beam(result: BeamDecodeResult, temperature: float, variant: str) -> BeamCandidate: calibrated = apply_temperature(result.probability, temperature) return BeamCandidate( text=result.text, probability=calibrated, min_character_probability=apply_temperature(result.min_character_probability, temperature), beam_margin=result.beam_margin, character_confidences=result.character_confidences, character_confidence_sources=tuple("engine" for _ in result.character_confidences), preprocessing_variant=variant, character_alternatives=result.character_alternatives, ) class OnnxNumericRecognizer: def __init__(self, model_path: Path, temperature: float): metadata_path = model_path.with_suffix(".meta.json") metadata = json.loads(metadata_path.read_text(encoding="utf-8")) self.vocabulary = metadata["vocabulary"] self.version = metadata.get("sha256", model_path.name)[:16] self.temperature = temperature self.session = open_numeric_session(str(model_path)) def _stack_images(self, images: list[bytes]): import numpy as np planes = [] owners = [] for image_index, image_bytes in enumerate(images): for variant_fn in VARIANTS: tensor, variant_name = variant_fn(image_bytes) planes.append(tensor[0, 0]) owners.append((image_index, variant_name)) height = planes[0].shape[0] max_width = max(plane.shape[1] for plane in planes) stacked = np.zeros((len(planes), 1, height, max_width), dtype=np.float32) for index, plane in enumerate(planes): stacked[index, 0, :, : plane.shape[1]] = plane return stacked, owners def _beams_from_logits(self, row, variant: str) -> list[BeamCandidate]: decoded = search_ctc_frames(frames_from_logits(row, self.vocabulary), self.vocabulary) return [_result_to_beam(item, self.temperature, variant) for item in decoded if item.text] def recognize_batch( self, images: list[bytes], semantic_class: SemanticClass | list[SemanticClass], ) -> list[list[BeamCandidate]]: del semantic_class if not images: return [] try: return self._forward(images) except Exception: if len(images) == 1: raise return [self._forward([image])[0] for image in images] def _forward(self, images: list[bytes]) -> list[list[BeamCandidate]]: stacked, owners = self._stack_images(images) try: logits = gpu_call(lambda: self.session.run(["logits"], {"image": stacked})[0]) finally: stacked.fill(0) grouped: list[list[BeamCandidate]] = [[] for _ in images] seen = [set() for _ in images] for index, row in enumerate(logits): owner, variant = owners[index] for candidate in self._beams_from_logits(row, variant): if candidate.text in seen[owner]: continue seen[owner].add(candidate.text) grouped[owner].append(candidate) return [sorted(group, key=lambda item: item.probability, reverse=True) for group in grouped] def recognize(self, image_bytes: bytes, semantic_class: SemanticClass) -> list[BeamCandidate]: return self.recognize_batch([image_bytes], semantic_class)[0]