File size: 1,534 Bytes
ffd392f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""
ocr.py - Image OCR using Tesseract (offline, CPU-only).

Tesseract is a mature, fully offline OCR engine - no internet or cloud
calls are made. Requires the `tesseract` binary + `pytesseract` package.
"""
from PIL import Image
import pytesseract
import io


def ocr_image(image_bytes: bytes, lang: str = "eng") -> dict:
    """Run OCR on raw image bytes. Returns text + a rough confidence score."""
    img = Image.open(io.BytesIO(image_bytes))
    if img.mode != "RGB":
        img = img.convert("RGB")

    try:
        text = pytesseract.image_to_string(img, lang=lang)
    except pytesseract.TesseractNotFoundError:
        raise RuntimeError(
            "Tesseract OCR engine is not installed on this machine.\n"
            "Install it, then restart the app:\n"
            "  Windows: https://github.com/UB-Mannheim/tesseract/wiki\n"
            "  macOS:   brew install tesseract\n"
            "  Linux:   sudo apt install tesseract-ocr"
        )

    # image_to_data gives per-word confidence; average the valid ones
    try:
        data = pytesseract.image_to_data(img, lang=lang, output_type=pytesseract.Output.DICT)
        confs = [int(c) for c in data["conf"] if c not in ("-1", -1)]
        avg_conf = (sum(confs) / len(confs) / 100.0) if confs else 0.5
    except Exception:
        avg_conf = 0.5

    return {"text": text.strip(), "confidence": round(avg_conf, 3)}


def available_languages() -> list:
    try:
        return pytesseract.get_languages(config="")
    except Exception:
        return ["eng"]