|
|
import os |
|
|
import io |
|
|
import easyocr |
|
|
import numpy as np |
|
|
from typing import Dict, List, Union |
|
|
from PIL import Image |
|
|
|
|
|
class EndpointHandler: |
|
|
def __init__(self, path: str): |
|
|
""" |
|
|
Initialize the OCR inference handler |
|
|
|
|
|
Args: |
|
|
path (str): Directory containing model artifacts |
|
|
""" |
|
|
try: |
|
|
|
|
|
|
|
|
self.reader = easyocr.Reader(['en']) |
|
|
except Exception as e: |
|
|
raise RuntimeError(f"Error initializing OCR model: {str(e)}") |
|
|
|
|
|
def __call__(self, input_data: Union[bytes, Image.Image]) -> Dict[str, List[Dict]]: |
|
|
""" |
|
|
Perform OCR inference |
|
|
|
|
|
Args: |
|
|
input_data (Union[bytes, Image.Image]): Input image to process |
|
|
|
|
|
Returns: |
|
|
Dict containing OCR results |
|
|
""" |
|
|
try: |
|
|
|
|
|
img_array = self._preprocess(input_data) |
|
|
|
|
|
|
|
|
results = self.reader.readtext(img_array) |
|
|
|
|
|
|
|
|
processed_results = [] |
|
|
for detection in results: |
|
|
bbox, text, confidence = detection |
|
|
processed_results.append({ |
|
|
'text': text, |
|
|
'confidence': float(confidence), |
|
|
'bbox': [list(map(int, coord)) for coord in bbox] |
|
|
}) |
|
|
|
|
|
return { |
|
|
'success': True, |
|
|
'results': processed_results |
|
|
} |
|
|
|
|
|
except Exception as e: |
|
|
return { |
|
|
'success': False, |
|
|
'error': str(e) |
|
|
} |
|
|
|
|
|
def _preprocess(self, input_data: Union[bytes, Image.Image]) -> np.ndarray: |
|
|
""" |
|
|
Preprocess the input image |
|
|
|
|
|
Args: |
|
|
input_data (Union[bytes, Image.Image]): Input image in bytes or PIL Image |
|
|
|
|
|
Returns: |
|
|
np.ndarray: Processed image array |
|
|
""" |
|
|
|
|
|
if isinstance(input_data, bytes): |
|
|
try: |
|
|
image = Image.open(io.BytesIO(input_data)) |
|
|
except Exception as e: |
|
|
raise ValueError(f"Invalid image format: {str(e)}") |
|
|
elif isinstance(input_data, Image.Image): |
|
|
image = input_data |
|
|
else: |
|
|
raise TypeError("Input must be bytes or PIL Image") |
|
|
|
|
|
|
|
|
return np.array(image) |