File size: 4,199 Bytes
0e157b6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b6fadef
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
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