File size: 9,638 Bytes
4bf0aa2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
52d0aa4
4bf0aa2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284

import logging
import sys
import numpy as np
import cv2
import base64
from flask import Flask, request, jsonify

# Configure logging
logging.basicConfig(
    level=logging.INFO,
    format='%(asctime)s - %(levelname)s - %(message)s',
    handlers=[
        logging.StreamHandler(sys.stdout)
    ]
)
logger = logging.getLogger(__name__)

app = Flask(__name__)

# Abstract OCR interface to handle multiple engines
ocr_engine = None
engine_type = None

def init_ocr():
    global ocr_engine, engine_type
    
    # Priority 1: PaddleOCR (Best quality)
    try:
        from paddleocr import PaddleOCR
        logger.info("Initializing PaddleOCR model...")
        # use_angle_cls=True allows orientation correction
        ocr_engine = PaddleOCR(use_textline_orientation=True, lang='en')
        engine_type = 'paddle'
        logger.info("PaddleOCR model initialized successfully.")
        return
    except ImportError:
        logger.warning("PaddleOCR not found. Falling back to EasyOCR.")
    except Exception as e:
        logger.warning(f"Failed to initialize PaddleOCR: {str(e)}. Falling back to EasyOCR.")

    # Priority 2: EasyOCR (Good fallback)
    try:
        import easyocr
        logger.info("Initializing EasyOCR model...")
        # gpu=False for compatibility, True if CUDA available
        ocr_engine = easyocr.Reader(['en'], gpu=False) 
        engine_type = 'easyocr'
        logger.info("EasyOCR model initialized successfully.")
        return
    except ImportError:
        logger.error("EasyOCR not found.")
    except Exception as e:
        logger.error(f"Failed to initialize EasyOCR: {str(e)}")

    if ocr_engine is None:
        logger.error("No OCR engine could be initialized. Please install paddleocr or easyocr.")
        # We don't exit here so the server can at least respond with health checks or errors
    
def enhance_image(img):
    """
    Apply image processing to improve OCR results
    """
    try:
        # 1. Convert to grayscale if it's color
        if len(img.shape) == 3:
            gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
        else:
            gray = img

        # 2. Rescale image if it's too small (OCR likes larger text)
        height, width = gray.shape
        if height < 1000 or width < 1000:
            scale_factor = 2.0
            gray = cv2.resize(gray, None, fx=scale_factor, fy=scale_factor, interpolation=cv2.INTER_CUBIC)

        # 3. Denoising
        denoised = cv2.fastNlMeansDenoising(gray, None, 10, 7, 21)

        # 4. Sharpening using Laplacian
        kernel = np.array([[-1, -1, -1], [-1, 9, -1], [-1, -1, -1]])
        sharpened = cv2.filter2D(denoised, -1, kernel)
        
        # 5. Contrast Limited Adaptive Histogram Equalization (CLAHE)
        clahe = cv2.createCLAHE(clipLimit=2.0, tileGridSize=(8, 8))
        enhanced = clahe.apply(sharpened)
        
        return enhanced
    except Exception as e:
        logger.warning(f"Image enhancement failed, using original: {str(e)}")
        return img

def process_with_paddle(img):
    # PaddleOCR returns a list of results per image (list of lists)
    result = ocr_engine.ocr(img, cls=True)
    
    structured_data = []
    
    if result and result[0]:
        detections = result[0]
        
        for line in detections:
            bbox = line[0]        # [[x1,y1], [x2,y2], [x3,y3], [x4,y4]]
            text_info = line[1]   # (text, confidence)
            text = text_info[0]
            confidence = text_info[1]
            
            # Transform bbox to list of [x, y]
            box = [[float(pt[0]), float(pt[1])] for pt in bbox]
            
            h = abs(bbox[0][1] - bbox[2][1])
            
            structured_data.append({
                "text": text,
                "confidence": float(confidence),
                "box": box,
                "height": h,
                "center_y": (bbox[0][1] + bbox[2][1]) / 2
            })
            
    return structured_data

def process_with_easyocr(img):
    result = ocr_engine.readtext(img)
    
    structured_data = []
    
    for item in result:
        bbox, text, confidence = item
        # bbox is [[x1,y1], [x2,y2], [x3,y3], [x4,y4]]
        
        h = abs(bbox[0][1] - bbox[2][1])
        
        box = [[float(pt[0]), float(pt[1])] for pt in bbox]
        
        structured_data.append({
            "text": text,
            "confidence": float(confidence),
            "box": box,
            "height": h,
            "center_y": (bbox[0][1] + bbox[2][1]) / 2
        })
        
    return structured_data

def process_image(image_base64):
    try:
        if ocr_engine is None:
             return {"success": False, "error": "OCR engine not initialized"}

        # Decode base64 image
        if ',' in image_base64:
            image_base64 = image_base64.split(',')[1]
        
        img_bytes = base64.b64decode(image_base64)
        nparr = np.frombuffer(img_bytes, np.uint8)
        img = cv2.imdecode(nparr, cv2.IMREAD_COLOR)

        if img is None:
            return {"success": False, "error": "Failed to decode image"}

        # Apply enhancement
        processed_img = enhance_image(img)

        # Perform OCR based on engine
        structured_data = []
        if engine_type == 'paddle':
            structured_data = process_with_paddle(processed_img)
        elif engine_type == 'easyocr':
            structured_data = process_with_easyocr(processed_img)
        
        # Reconstruct text with line grouping logic
        
        # Calculate average text height for dynamic thresholding
        total_height = sum(item['height'] for item in structured_data) if structured_data else 0
        count = len(structured_data)
        
        # Dynamic y_threshold based on average line height
        avg_height = (total_height / count) if count > 0 else 20
        y_threshold = avg_height * 0.5  # 50% of avg height as same-line threshold

        # Sort by Y-coordinate
        structured_data.sort(key=lambda x: x['box'][0][1])
        
        lines = []
        if structured_data:
            current_line = []
            current_y = -1

            for item in structured_data:
                y_center = item['center_y']
                
                if current_y == -1:
                    current_y = y_center
                    current_line.append(item)
                elif abs(y_center - current_y) < y_threshold:
                    current_line.append(item)
                else:
                    # Sort current line by X-coordinate
                    current_line.sort(key=lambda x: x['box'][0][0])
                    lines.append(" ".join([str(i.get('text', '')) for i in current_line]))
                    
                    current_line = [item]
                    current_y = y_center
            
            if current_line:
                current_line.sort(key=lambda x: x['box'][0][0])
                lines.append(" ".join([str(i.get('text', '')) for i in current_line]))

        # Build full text from lines with enhancement (User request)
        import re
        final_lines = []
        for line_str in lines:
            # 1. Filter out "thank you" and "visit" phrases
            junk_keywords = ["thank you", "visit again", "thanks", "welcome", "have a nice day", "visit us", "come again"]
            if any(k in line_str.lower() for k in junk_keywords):
                continue
            
            # 2. Remove any numbers (User request: "dont add any number")
            # This helps in cleaning up item names for real-time price fetching
            cleaned = re.sub(r'\d+', '', line_str)
            
            # Clean up extra whitespace and punctuation
            cleaned = re.sub(r'\s+', ' ', cleaned).strip()
            
            # Skip empty lines or lines with no alphanumeric characters
            if cleaned and any(c.isalnum() for c in cleaned):
                final_lines.append(cleaned)

        reconstructed_text = "\n".join(final_lines)

        return {
            "success": True,
            "text": reconstructed_text,
            "structured_data": [
                {k: v for k, v in item.items() if k != 'center_y' and k != 'height'}
                for item in structured_data
            ],
            "engine": engine_type
        }

    except Exception as e:
        logger.error(f"Error processing image: {str(e)}")
        import traceback
        traceback.print_exc()
        return {"success": False, "error": str(e)}

@app.route('/ocr', methods=['POST'])
def ocr_endpoint():
    try:
        data = request.get_json()
        if not data or 'imageBase64' not in data:
            return jsonify({"success": False, "error": "No imageBase64 provided"}), 400
        
        image_base64 = data['imageBase64']
        
        # Ensure OCR is ready
        if ocr_engine is None:
            init_ocr()
            
        result = process_image(image_base64)
        
        return jsonify(result)
    except Exception as e:
        logger.error(f"Endpoint error: {str(e)}")
        return jsonify({"success": False, "error": str(e)}), 500

@app.route('/health', methods=['GET'])
def health_check():
    status = "healthy" if ocr_engine is not None else "initializing_or_failed"
    return jsonify({
        "status": status, 
        "model": engine_type or "none", 
        "enhanced": True
    })

if __name__ == '__main__':
    # Initialize implementation on startup
    init_ocr()
    
    port = 5001
    logger.info(f"Starting Enhanced OCR Server ({engine_type or 'No Engine'}) on port {port}")
    app.run(host='0.0.0.0', port=port)