Spaces:
Configuration error
Configuration error
Download axis_numeric/tesseract_numeric_recognizer.py from serety95/axis-numeric: direct link, hf CLI and curl.
- Browser
- Download file 2.94 kB
-
https://huggingface.co/spaces/serety95/axis-numeric/resolve/main/axis_numeric/tesseract_numeric_recognizer.py
- Command line
-
hf download hf://spaces/serety95/axis-numeric/axis_numeric/tesseract_numeric_recognizer.py
-
curl -L -o tesseract_numeric_recognizer.py https://huggingface.co/spaces/serety95/axis-numeric/resolve/main/axis_numeric/tesseract_numeric_recognizer.py
2.94 kB
| """ONNX-adjunct Tesseract beam producer for whitelisted numeric crops.""" | |
| from __future__ import annotations | |
| from .arabic_tesseract_engine import ( | |
| POLARITY_RETRY_MEAN_CONF, | |
| bytes_to_bgr, | |
| extract_structured_numeric_tokens, | |
| invert_polarity, | |
| mean_token_confidence, | |
| noir_filter_preprocess, | |
| tokens_to_text, | |
| ) | |
| from .contracts import BeamCandidate, SemanticClass | |
| def _tokens_to_beam( | |
| tokens: list[dict], | |
| text: str, | |
| variant: str, | |
| ) -> BeamCandidate | None: | |
| if not text: | |
| return None | |
| confidences = [max(0.0, min(1.0, float(item["confidence"]) / 100.0)) for item in tokens] | |
| if not confidences: | |
| confidences = [0.5] * len(text) | |
| elif len(confidences) < len(text): | |
| mean = sum(confidences) / len(confidences) | |
| confidences = confidences + [mean] * (len(text) - len(confidences)) | |
| else: | |
| confidences = confidences[: len(text)] | |
| mean_prob = sum(confidences) / len(confidences) | |
| min_prob = min(confidences) | |
| return BeamCandidate( | |
| text=text, | |
| probability=mean_prob, | |
| min_character_probability=min_prob, | |
| beam_margin=0.15, | |
| character_confidences=tuple(confidences), | |
| character_confidence_sources=tuple("engine" for _ in confidences), | |
| preprocessing_variant=variant, | |
| ) | |
| class TesseractNumericRecognizer: | |
| version = "tesseract-numeric-v1" | |
| def available() -> bool: | |
| try: | |
| import cv2 # noqa: F401 | |
| import pytesseract | |
| pytesseract.get_tesseract_version() | |
| return True | |
| except Exception: | |
| return False | |
| def recognize(self, image_bytes: bytes, semantic_class: SemanticClass) -> list[BeamCandidate]: | |
| if not self.available(): | |
| return [] | |
| try: | |
| bgr = bytes_to_bgr(image_bytes) | |
| except ValueError: | |
| return [] | |
| beams: list[BeamCandidate] = [] | |
| processed = noir_filter_preprocess(bgr) | |
| tokens = extract_structured_numeric_tokens( | |
| bgr, semantic_class=semantic_class, processed=processed | |
| ) | |
| text = tokens_to_text(tokens, semantic_class) | |
| primary = _tokens_to_beam(tokens, text, "tesseract_adaptive") | |
| if primary: | |
| beams.append(primary) | |
| if mean_token_confidence(tokens) < POLARITY_RETRY_MEAN_CONF: | |
| inverted = invert_polarity(processed) | |
| retry_tokens = extract_structured_numeric_tokens( | |
| bgr, semantic_class=semantic_class, processed=inverted | |
| ) | |
| retry_text = tokens_to_text(retry_tokens, semantic_class) | |
| retry = _tokens_to_beam(retry_tokens, retry_text, "tesseract_polarity_inverted") | |
| if retry and retry.text not in {item.text for item in beams}: | |
| beams.append(retry) | |
| return beams | |