Something went wrong. Try again.
Collection of macros
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209"""Cross-platform OCR.
Two primitives are exposed to macros:
recognize_text(image, lang) -> plain text of the whole image recognize_boxes(image) -> list of (box, text, score)
``box`` is a list of four ``[x, y]`` corners in clockwise order starting at thetop-left, matching RapidOCR's convention.
Backends are selected per platform, with a shared RapidOCR fallback everywhere:
Windows : text -> Windows OCR (winocr), boxes -> RapidOCR macOS : text + boxes -> Apple Vision (pyobjc) Linux : text + boxes -> RapidOCR
Engines are lazy so a macro never pays for (or imports) a backend it does notuse, and missing optional dependencies degrade gracefully."""
import sys
from PIL import Image
# ---------------------------------------------------------------------------# RapidOCR (cross-platform fallback, also the box engine on Windows/Linux)# ---------------------------------------------------------------------------class _RapidOcr: name = "rapidocr"
def __init__(self): # Imported lazily: rapidocr-onnxruntime pulls in onnxruntime, which is # heavy, so we only load it when we actually need it. from rapidocr_onnxruntime import RapidOCR
self._engine = RapidOCR( use_angle_cls=False, det_model_path=None, det_limit_side_len=736, det_limit_type="max", )
def boxes(self, image): res, _ = self._engine(image) if not res: return [] return res
def text(self, image, lang="en"): return "\n".join(text for _, text, _ in self.boxes(image))
# ---------------------------------------------------------------------------# Windows OCR (text only, used for the metadata panel)# ---------------------------------------------------------------------------class _WinOcr: name = "winocr"
def __init__(self): import winocr
self._winocr = winocr
def text(self, image, lang="en"): return self._winocr.recognize_pil_sync(image, lang)["text"]
def boxes(self, image): # winocr is only used for whole-image text; box OCR falls back to # RapidOCR on Windows. Keep this simple by delegating there. return _rapid_engine().boxes(image)
# ---------------------------------------------------------------------------# Apple Vision (text + boxes on macOS)# ---------------------------------------------------------------------------class _AppleVision: name = "apple-vision"
def __init__(self): import Vision
self._Vision = Vision
@staticmethod def _to_cgimage(image): import io
import Foundation import Quartz
buf = io.BytesIO() image.convert("RGB").save(buf, format="PNG") data = buf.getvalue() nsdata = Foundation.NSData.dataWithBytes_length_(data, len(data)) source = Quartz.CGImageSourceCreateWithData(nsdata, None) return Quartz.CGImageSourceCreateImageAtIndex(source, 0, None)
def _recognize(self, image, lang): import Quartz
cg = self._to_cgimage(image) width = Quartz.CGImageGetWidth(cg) height = Quartz.CGImageGetHeight(cg)
handler = self._Vision.VNImageRequestHandler.alloc().initWithCGImage_options_(cg, None) request = self._Vision.VNRecognizeTextRequest.alloc().init() request.setRecognitionLevel_(self._Vision.VNRequestTextRecognitionLevelAccurate) request.setRecognitionLanguages_([lang]) request.setUsesLanguageCorrection_(True) handler.performRequests_error_([request], None)
results = [] for obs in request.results() or []: text = obs.topCandidates_(1)[0].string() bb = obs.boundingBox() # Vision's box is normalised with the origin at the bottom-left; # convert to pixel coordinates with a top-left origin like RapidOCR. x = bb.origin.x * width y = (1.0 - bb.origin.y - bb.size.height) * height w = bb.size.width * width h = bb.size.height * height box = [[x, y], [x + w, y], [x + w, y + h], [x, y + h]] results.append((box, text, 1.0)) return results
def boxes(self, image): return self._recognize(image, "en")
def text(self, image, lang="en"): return "\n".join(text for _, text, _ in self._recognize(image, lang))
# ---------------------------------------------------------------------------# Engine selection# ---------------------------------------------------------------------------_rapid = None_text_engine = None_box_engine = None
def _ensure_image(image): """Accept both PIL Images and numpy arrays; return a PIL Image.""" if hasattr(image, "convert"): return image return Image.fromarray(image)
def _rapid_engine(): global _rapid if _rapid is None: _rapid = _RapidOcr() return _rapid
def _make_text_engine(): if sys.platform == "win32": try: return _WinOcr() except Exception: pass if sys.platform == "darwin": try: return _AppleVision() except Exception: pass return _rapid_engine()
def _make_box_engine(): if sys.platform == "darwin": try: return _AppleVision() except Exception: pass return _rapid_engine()
def get_text_engine(): global _text_engine if _text_engine is None: _text_engine = _make_text_engine() return _text_engine
def get_box_engine(): global _box_engine if _box_engine is None: _box_engine = _make_box_engine() return _box_engine
def recognize_text(image, lang="en"): """Return the plain text recognised across the whole image.""" return get_text_engine().text(_ensure_image(image), lang)
def recognize_boxes(image): """Return ``[(box, text, score), ...]`` for the image.""" return get_box_engine().boxes(_ensure_image(image))
def text_backend_name(): return get_text_engine().name
def box_backend_name(): return get_box_engine().name