Spaces:
Configuration error
Configuration error
File size: 1,928 Bytes
f1ba785 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 | """Fuse ONNX sequence beams with optional Tesseract spatial beams."""
from __future__ import annotations
from .contracts import BeamCandidate, SemanticClass
from .recognizer import NumericRecognizer
from .tesseract_numeric_recognizer import TesseractNumericRecognizer
class CompositeNumericRecognizer:
def __init__(
self,
primary: NumericRecognizer,
tesseract: TesseractNumericRecognizer | None = None,
):
self._primary = primary
self._tesseract = tesseract or TesseractNumericRecognizer()
self.version = f"{primary.version}+tesseract"
def recognize_batch(
self,
images: list[bytes],
semantic_class: SemanticClass | list[SemanticClass],
) -> list[list[BeamCandidate]]:
if not images:
return []
classes = semantic_class if isinstance(semantic_class, list) else [semantic_class] * len(images)
batch = getattr(self._primary, "recognize_batch", None)
if callable(batch):
grouped = [list(beams) for beams in batch(images, classes)]
else:
grouped = [
list(self._primary.recognize(image, classes[index]))
for index, image in enumerate(images)
]
for index, image_bytes in enumerate(images):
seen = {item.text for item in grouped[index]}
for candidate in self._tesseract.recognize(image_bytes, classes[index]):
if candidate.text in seen:
continue
seen.add(candidate.text)
grouped[index].append(candidate)
grouped[index].sort(key=lambda item: item.probability, reverse=True)
return grouped
def recognize(self, image_bytes: bytes, semantic_class: SemanticClass) -> list[BeamCandidate]:
return self.recognize_batch([image_bytes], semantic_class)[0]
|