Spaces:
Sleeping
Sleeping
Download app/ocr_engine.py from vamsi1103/flask-ocr-api: direct link, hf CLI and curl.
- Browser
- Download file 4.2 kB
-
https://huggingface.co/spaces/vamsi1103/flask-ocr-api/resolve/main/app/ocr_engine.py
- Command line
-
hf download hf://spaces/vamsi1103/flask-ocr-api/app/ocr_engine.py
-
curl -L -o ocr_engine.py https://huggingface.co/spaces/vamsi1103/flask-ocr-api/resolve/main/app/ocr_engine.py
4.2 kB
| import os | |
| import numpy as np | |
| from PIL import Image | |
| from rapidocr_onnxruntime import RapidOCR | |
| from .preprocess import preprocess_image | |
| # 1. Initialize Default RapidOCR (for general text, ASCII, numbers, and rates) | |
| default_engine = RapidOCR() | |
| # 2. Initialize Telugu RapidOCR (if Telugu model is present) | |
| telugu_model_path = os.getenv("REC_MODEL_PATH") or os.path.join("models", "telugu", "rec.onnx") | |
| telugu_keys_path = os.getenv("KEYS_PATH") or os.path.join("models", "telugu", "dict.txt") | |
| telugu_engine = None | |
| if os.path.exists(telugu_model_path) and os.path.exists(telugu_keys_path): | |
| try: | |
| telugu_engine = RapidOCR(rec_model_path=telugu_model_path, rec_keys_path=telugu_keys_path) | |
| print("Telugu RapidOCR model initialized successfully.") | |
| except Exception as e: | |
| print(f"Warning: Failed to initialize Telugu RapidOCR model: {e}") | |
| def run_ocr(image): | |
| """ | |
| Runs text detection (DBNet) and recognition (Default + Telugu SVTR/PP-OCR if available). | |
| Returns list of detected bounding boxes with both default and Telugu text recognitions. | |
| """ | |
| img_np = np.array(image) | |
| processed = preprocess_image(img_np) | |
| res_default, _ = default_engine(processed) | |
| res_telugu = None | |
| if telugu_engine is not None: | |
| try: | |
| res_telugu, _ = telugu_engine(processed) | |
| except Exception as e: | |
| print(f"Warning: Telugu OCR execution failed: {e}") | |
| extracted = [] | |
| if not res_default and not res_telugu: | |
| return extracted | |
| # Helper to calculate box metrics | |
| def get_center(box): | |
| xs = [p[0] for p in box] | |
| ys = [p[1] for p in box] | |
| return sum(xs)/len(xs), sum(ys)/len(ys) | |
| # If Telugu engine is not available, return default | |
| if not res_telugu: | |
| for item in res_default or []: | |
| if not item or len(item) < 3: | |
| continue | |
| box = item[0] | |
| extracted.append({ | |
| "text": str(item[1]).strip(), | |
| "default_text": str(item[1]).strip(), | |
| "telugu_text": "", | |
| "confidence": float(item[2]), | |
| "box": box.tolist() if hasattr(box, "tolist") else box | |
| }) | |
| return extracted | |
| # When both are available, pair fragments by bounding box centers | |
| tel_items = [] | |
| for item in res_telugu: | |
| if not item or len(item) < 3: | |
| continue | |
| cx, cy = get_center(item[0]) | |
| tel_items.append({ | |
| "text": str(item[1]).strip(), | |
| "conf": float(item[2]), | |
| "box": item[0].tolist() if hasattr(item[0], "tolist") else item[0], | |
| "cx": cx, | |
| "cy": cy | |
| }) | |
| def_items = [] | |
| for item in (res_default or []): | |
| if not item or len(item) < 3: | |
| continue | |
| cx, cy = get_center(item[0]) | |
| def_items.append({ | |
| "text": str(item[1]).strip(), | |
| "conf": float(item[2]), | |
| "box": item[0].tolist() if hasattr(item[0], "tolist") else item[0], | |
| "cx": cx, | |
| "cy": cy | |
| }) | |
| # Pair each Telugu box with nearest Default box | |
| for t in tel_items: | |
| best_d = None | |
| min_dist = 999999 | |
| for d in def_items: | |
| dist = (t["cx"] - d["cx"])**2 + (t["cy"] - d["cy"])**2 | |
| if dist < min_dist: | |
| min_dist = dist | |
| best_d = d | |
| def_txt = best_d["text"] if (best_d and min_dist < 150) else "" | |
| tel_txt = t["text"] | |
| extracted.append({ | |
| "text": tel_txt if tel_txt else def_txt, | |
| "telugu_text": tel_txt, | |
| "default_text": def_txt, | |
| "confidence": round(t["conf"], 2), | |
| "box": t["box"] | |
| }) | |
| # Also include any default items that had no nearby Telugu box | |
| for d in def_items: | |
| is_paired = any(((d["cx"] - t["cx"])**2 + (d["cy"] - t["cy"])**2) < 150 for t in tel_items) | |
| if not is_paired: | |
| extracted.append({ | |
| "text": d["text"], | |
| "telugu_text": "", | |
| "default_text": d["text"], | |
| "confidence": round(d["conf"], 2), | |
| "box": d["box"] | |
| }) | |
| return extracted |