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)