Spaces:
Build error
Build error
Update ocr_engine.py
Browse files- ocr_engine.py +24 -16
ocr_engine.py
CHANGED
|
@@ -1,10 +1,9 @@
|
|
| 1 |
-
import
|
| 2 |
from PIL import Image
|
| 3 |
import cv2
|
| 4 |
import numpy as np
|
| 5 |
import re
|
| 6 |
from typing import List, Tuple
|
| 7 |
-
import logging
|
| 8 |
|
| 9 |
# Configure logging
|
| 10 |
logging.basicConfig(level=logging.INFO)
|
|
@@ -31,12 +30,7 @@ class OCREngine:
|
|
| 31 |
# Apply thresholding
|
| 32 |
_, thresh = cv2.threshold(denoised, 0, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU)
|
| 33 |
|
| 34 |
-
|
| 35 |
-
kernel = np.ones((1, 1), np.uint8)
|
| 36 |
-
processed = cv2.morphologyEx(thresh, cv2.MORPH_CLOSE, kernel)
|
| 37 |
-
processed = cv2.morphologyEx(processed, cv2.MORPH_OPEN, kernel)
|
| 38 |
-
|
| 39 |
-
return processed
|
| 40 |
|
| 41 |
except Exception as e:
|
| 42 |
logger.error(f"Error preprocessing image: {e}")
|
|
@@ -47,28 +41,42 @@ class OCREngine:
|
|
| 47 |
def extract_text(self, image_path: str, preprocess: bool = True) -> str:
|
| 48 |
"""Extract text from image using OCR"""
|
| 49 |
try:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 50 |
if preprocess:
|
| 51 |
processed_image = self.preprocess_image(image_path)
|
| 52 |
pil_image = Image.fromarray(processed_image)
|
| 53 |
else:
|
| 54 |
pil_image = Image.open(image_path)
|
| 55 |
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 61 |
|
| 62 |
-
logger.info(f"Successfully extracted text from {image_path}")
|
| 63 |
return text.strip()
|
| 64 |
|
| 65 |
except Exception as e:
|
| 66 |
logger.error(f"OCR extraction failed: {e}")
|
| 67 |
return ""
|
| 68 |
-
|
| 69 |
def extract_text_with_confidence(self, image_path: str) -> Tuple[str, float]:
|
| 70 |
"""Extract text with confidence score"""
|
| 71 |
try:
|
|
|
|
| 72 |
pil_image = Image.open(image_path)
|
| 73 |
|
| 74 |
# Get data with confidence
|
|
@@ -86,4 +94,4 @@ class OCREngine:
|
|
| 86 |
|
| 87 |
except Exception as e:
|
| 88 |
logger.error(f"Confidence-based extraction failed: {e}")
|
| 89 |
-
return
|
|
|
|
| 1 |
+
import logging
|
| 2 |
from PIL import Image
|
| 3 |
import cv2
|
| 4 |
import numpy as np
|
| 5 |
import re
|
| 6 |
from typing import List, Tuple
|
|
|
|
| 7 |
|
| 8 |
# Configure logging
|
| 9 |
logging.basicConfig(level=logging.INFO)
|
|
|
|
| 30 |
# Apply thresholding
|
| 31 |
_, thresh = cv2.threshold(denoised, 0, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU)
|
| 32 |
|
| 33 |
+
return thresh
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 34 |
|
| 35 |
except Exception as e:
|
| 36 |
logger.error(f"Error preprocessing image: {e}")
|
|
|
|
| 41 |
def extract_text(self, image_path: str, preprocess: bool = True) -> str:
|
| 42 |
"""Extract text from image using OCR"""
|
| 43 |
try:
|
| 44 |
+
# Try to use pytesseract if available
|
| 45 |
+
try:
|
| 46 |
+
import pytesseract
|
| 47 |
+
tesseract_available = True
|
| 48 |
+
except ImportError:
|
| 49 |
+
tesseract_available = False
|
| 50 |
+
logger.warning("pytesseract not available, using fallback method")
|
| 51 |
+
|
| 52 |
if preprocess:
|
| 53 |
processed_image = self.preprocess_image(image_path)
|
| 54 |
pil_image = Image.fromarray(processed_image)
|
| 55 |
else:
|
| 56 |
pil_image = Image.open(image_path)
|
| 57 |
|
| 58 |
+
if tesseract_available:
|
| 59 |
+
# Configure Tesseract
|
| 60 |
+
custom_config = r'--oem 3 --psm 6'
|
| 61 |
+
|
| 62 |
+
# Extract text
|
| 63 |
+
text = pytesseract.image_to_string(pil_image, config=custom_config)
|
| 64 |
+
logger.info(f"Successfully extracted text using Tesseract from {image_path}")
|
| 65 |
+
else:
|
| 66 |
+
# Fallback: Return empty text
|
| 67 |
+
text = ""
|
| 68 |
+
logger.warning("Using fallback OCR method - no text extraction")
|
| 69 |
|
|
|
|
| 70 |
return text.strip()
|
| 71 |
|
| 72 |
except Exception as e:
|
| 73 |
logger.error(f"OCR extraction failed: {e}")
|
| 74 |
return ""
|
| 75 |
+
|
| 76 |
def extract_text_with_confidence(self, image_path: str) -> Tuple[str, float]:
|
| 77 |
"""Extract text with confidence score"""
|
| 78 |
try:
|
| 79 |
+
import pytesseract
|
| 80 |
pil_image = Image.open(image_path)
|
| 81 |
|
| 82 |
# Get data with confidence
|
|
|
|
| 94 |
|
| 95 |
except Exception as e:
|
| 96 |
logger.error(f"Confidence-based extraction failed: {e}")
|
| 97 |
+
return self.extract_text(image_path), 0.0
|