File size: 9,074 Bytes
d520909 |
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 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 |
"""
Tesseract OCR Engine
Fallback OCR engine using Tesseract.
Provides broad language support and is widely available.
"""
import time
from typing import List, Optional, Dict, Any
import numpy as np
from loguru import logger
from .base import OCREngine, OCRConfig, OCRResult
from ..schemas.core import BoundingBox, OCRRegion
# Try to import pytesseract
try:
import pytesseract
from PIL import Image
HAS_TESSERACT = True
except ImportError:
HAS_TESSERACT = False
logger.warning(
"pytesseract not installed. Install with: pip install pytesseract "
"Also install Tesseract: apt-get install tesseract-ocr"
)
class TesseractOCREngine(OCREngine):
"""
OCR engine using Tesseract.
Features:
- Broad language support (100+ languages)
- Mature and well-tested
- No GPU required
- Page segmentation modes for different layouts
"""
# Tesseract language codes (subset of common ones)
LANGUAGE_MAP = {
"en": "eng",
"ch": "chi_sim",
"chinese_cht": "chi_tra",
"fr": "fra",
"german": "deu",
"es": "spa",
"it": "ita",
"pt": "por",
"ru": "rus",
"japan": "jpn",
"korean": "kor",
"ar": "ara",
"hi": "hin",
"latin": "lat",
}
# Page segmentation modes
PSM_AUTO = 3 # Fully automatic page segmentation
PSM_SINGLE_BLOCK = 6 # Assume single uniform block of text
PSM_SINGLE_LINE = 7 # Treat image as single line
PSM_SPARSE = 11 # Sparse text with no particular order
def __init__(self, config: Optional[OCRConfig] = None):
"""Initialize Tesseract OCR engine."""
super().__init__(config)
self._tesseract_cmd: Optional[str] = None
def initialize(self):
"""Initialize Tesseract engine."""
if not HAS_TESSERACT:
raise RuntimeError(
"pytesseract not installed. Install with: pip install pytesseract. "
"Also install Tesseract: apt-get install tesseract-ocr"
)
if self._initialized:
return
logger.info("Initializing Tesseract OCR engine...")
# Test Tesseract installation
try:
version = pytesseract.get_tesseract_version()
logger.info(f"Tesseract version: {version}")
self._initialized = True
except Exception as e:
logger.error(f"Tesseract not properly installed: {e}")
raise RuntimeError(
f"Tesseract not properly installed: {e}. "
"Install with: apt-get install tesseract-ocr"
)
def recognize(
self,
image: np.ndarray,
page_number: int = 0,
) -> OCRResult:
"""
Perform OCR on an image using Tesseract.
Args:
image: Image as numpy array (RGB, HWC format)
page_number: Page number for multi-page documents
Returns:
OCRResult with recognized text and regions
"""
if not self._initialized:
self.initialize()
start_time = time.time()
try:
# Convert numpy array to PIL Image
pil_image = Image.fromarray(image)
# Build language string
lang = self._get_tesseract_lang()
# Configure Tesseract
custom_config = self._build_config()
# Get detailed data with bounding boxes
data = pytesseract.image_to_data(
pil_image,
lang=lang,
config=custom_config,
output_type=pytesseract.Output.DICT,
)
# Process results
regions = []
all_texts = []
total_confidence = 0.0
valid_count = 0
height, width = image.shape[:2]
# Group words into lines
current_line_id = -1
word_id = 0
for i in range(len(data['text'])):
text = data['text'][i].strip()
conf = int(data['conf'][i])
# Skip empty or low confidence
if not text or conf < 0:
continue
confidence = conf / 100.0
if confidence < self.config.min_confidence:
continue
# Track line changes
block_num = data['block_num'][i]
line_num = data['line_num'][i]
line_id = block_num * 1000 + line_num
if line_id != current_line_id:
current_line_id = line_id
word_id = 0
else:
word_id += 1
# Get bounding box
x = data['left'][i]
y = data['top'][i]
w = data['width'][i]
h = data['height'][i]
bbox = BoundingBox(
x_min=float(x),
y_min=float(y),
x_max=float(x + w),
y_max=float(y + h),
normalized=False,
page_width=width,
page_height=height,
)
region = OCRRegion(
text=text,
confidence=confidence,
bbox=bbox,
page=page_number,
line_id=line_id,
word_id=word_id,
engine="tesseract",
)
regions.append(region)
all_texts.append(text)
total_confidence += confidence
valid_count += 1
# Also get full text for better formatting
full_text = pytesseract.image_to_string(
pil_image,
lang=lang,
config=custom_config,
)
processing_time = (time.time() - start_time) * 1000
return OCRResult(
regions=regions,
full_text=full_text.strip(),
confidence_avg=total_confidence / valid_count if valid_count > 0 else 0.0,
processing_time_ms=processing_time,
engine="tesseract",
success=True,
)
except Exception as e:
logger.error(f"Tesseract recognition failed: {e}")
return OCRResult(
regions=[],
full_text="",
confidence_avg=0.0,
processing_time_ms=(time.time() - start_time) * 1000,
engine="tesseract",
success=False,
error=str(e),
)
def _get_tesseract_lang(self) -> str:
"""Get Tesseract language string from config."""
langs = []
for lang in self.config.languages:
tess_lang = self.LANGUAGE_MAP.get(lang, "eng")
if tess_lang not in langs:
langs.append(tess_lang)
return "+".join(langs) if langs else "eng"
def _build_config(self) -> str:
"""Build Tesseract config string."""
config_parts = [
f"--psm {self.PSM_AUTO}", # Page segmentation mode
"--oem 3", # Use both legacy and LSTM engines
]
# Add more options as needed
if self.config.return_word_boxes:
config_parts.append("-c preserve_interword_spaces=1")
return " ".join(config_parts)
def get_supported_languages(self) -> List[str]:
"""Return list of supported language codes."""
return list(self.LANGUAGE_MAP.keys())
def get_installed_languages(self) -> List[str]:
"""Get list of languages installed in Tesseract."""
if not self._initialized:
self.initialize()
try:
langs = pytesseract.get_languages()
return langs
except Exception as e:
logger.warning(f"Could not get installed languages: {e}")
return ["eng"]
def recognize_with_hocr(
self,
image: np.ndarray,
page_number: int = 0,
) -> tuple:
"""
Perform OCR and return hOCR format for detailed layout.
Args:
image: Image as numpy array
page_number: Page number
Returns:
Tuple of (OCRResult, hOCR string)
"""
if not self._initialized:
self.initialize()
pil_image = Image.fromarray(image)
lang = self._get_tesseract_lang()
config = self._build_config()
# Get standard result
ocr_result = self.recognize(image, page_number)
# Get hOCR for layout analysis
try:
hocr = pytesseract.image_to_pdf_or_hocr(
pil_image,
lang=lang,
config=config,
extension='hocr',
)
return ocr_result, hocr.decode('utf-8')
except Exception as e:
logger.warning(f"Failed to generate hOCR: {e}")
return ocr_result, None
|