Spaces:
Sleeping
Sleeping
File size: 1,286 Bytes
3c25c17 | 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 46 47 48 49 50 51 52 | from __future__ import annotations
_reader = None
def _get_reader():
global _reader
if _reader is None:
try:
import easyocr
_reader = easyocr.Reader(["en"], gpu=False)
except ImportError:
raise RuntimeError(
"EasyOCR is not installed. Install it with: pip install easyocr"
)
except Exception as e:
raise RuntimeError(
f"Failed to load EasyOCR (possible OOM on free tier): {e}"
)
return _reader
def handle_image_input(image_path: str) -> dict:
reader = _get_reader()
results = reader.readtext(image_path)
if not results:
return {
"text": "",
"confidence": 0.0,
"input_type": "image",
"bboxes": [],
}
lines: list[str] = []
confidences: list[float] = []
bboxes: list[list] = []
for bbox, text, conf in results:
lines.append(text)
confidences.append(float(conf))
bboxes.append(bbox)
avg_confidence = sum(confidences) / len(confidences) if confidences else 0.0
return {
"text": " ".join(lines),
"confidence": round(float(avg_confidence), 3),
"input_type": "image",
"bboxes": [],
}
|