Spaces:
Sleeping
Sleeping
File size: 1,033 Bytes
835eeaa | 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 | from __future__ import annotations
import os
import pytesseract
def configure_tesseract() -> None:
cmd = os.environ.get("TESSERACT_CMD", "").strip()
if cmd:
pytesseract.pytesseract.tesseract_cmd = cmd
return
if os.name != "nt":
return # on Linux/macOS, tesseract should be in PATH
common = [
r"C:\Program Files\Tesseract-OCR\tesseract.exe",
r"C:\Program Files (x86)\Tesseract-OCR\tesseract.exe",
]
for p in common:
if os.path.exists(p):
pytesseract.pytesseract.tesseract_cmd = p
return
def ocr_pil_image(img, lang: str = "mal", psm: int = 6, oem: int = 1) -> str:
"""
psm:
3 = fully automatic page segmentation
4 = assume single column of text
6 = assume a uniform block of text (often best for docs)
11 = sparse text
oem:
1 = LSTM engine
"""
config = f"--oem {oem} --psm {psm}"
text = pytesseract.image_to_string(img, lang=lang, config=config)
return (text or "").strip()
|