Spaces:
Sleeping
Sleeping
Download ocr/ocr_engine.py from afueth/malayalam-ocr: direct link, hf CLI and curl.
- Browser
- Download file 1.03 kB
-
https://huggingface.co/spaces/afueth/malayalam-ocr/resolve/main/ocr/ocr_engine.py
- Command line
-
hf download hf://spaces/afueth/malayalam-ocr/ocr/ocr_engine.py
-
curl -L -o ocr_engine.py https://huggingface.co/spaces/afueth/malayalam-ocr/resolve/main/ocr/ocr_engine.py
1.03 kB
| 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() | |