File size: 1,989 Bytes
67e7598
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import numpy as np

try:
    import easyocr
    EASYOCR_AVAILABLE = True
except ImportError:
    EASYOCR_AVAILABLE = False

_reader = None

def get_ocr_reader():
    """Lazy initializer for EasyOCR reader."""
    global _reader
    if not EASYOCR_AVAILABLE:
        return None
        
    if _reader is None:
        try:
            # Initialize with English and Hindi, CPU only for maximum compatibility
            _reader = easyocr.Reader(['hi', 'en'], gpu=False)
        except Exception as e:
            print(f"Error initializing EasyOCR: {e}")
            return None
    return _reader

def detect_text_in_image(img_bgr: np.ndarray) -> list:
    """
    Detect all text strings and their bounding boxes in the BGR image.
    
    Returns:
    A list of dictionaries:
    [
        {
            "text": str,
            "bbox": (x, y, w, h),
            "confidence": float
        }
    ]
    """
    reader = get_ocr_reader()
    if reader is None:
        print("OCR Engine not available. Skipping automatic text detection.")
        return []
        
    try:
        # EasyOCR requires RGB or BGR numpy array
        # It returns: [([[x0,y0], [x1,y1], [x2,y2], [x3,y3]], text, confidence), ...]
        raw_results = reader.readtext(img_bgr)
        
        processed_results = []
        for coords, text, confidence in raw_results:
            # Map four-point coordinates to standard (x, y, w, h) bounding box
            xs = [p[0] for p in coords]
            ys = [p[1] for p in coords]
            
            x = int(min(xs))
            y = int(min(ys))
            w = int(max(xs) - x)
            h = int(max(ys) - y)
            
            processed_results.append({
                "text": text.strip(),
                "bbox": (x, y, w, h),
                "confidence": float(confidence)
            })
            
        return processed_results
    except Exception as e:
        print(f"OCR detection encountered an error: {e}")
        return []