Update handler.py
Browse files- handler.py +31 -59
handler.py
CHANGED
|
@@ -1,49 +1,26 @@
|
|
|
|
|
| 1 |
import io
|
| 2 |
import easyocr
|
| 3 |
import numpy as np
|
| 4 |
from typing import Dict, List, Union
|
| 5 |
from PIL import Image
|
| 6 |
|
| 7 |
-
class
|
| 8 |
-
def __init__(self,
|
| 9 |
"""
|
| 10 |
Initialize the OCR inference handler
|
| 11 |
|
| 12 |
Args:
|
| 13 |
-
|
| 14 |
"""
|
| 15 |
try:
|
| 16 |
-
#
|
| 17 |
-
# You can
|
| 18 |
self.reader = easyocr.Reader(['en'])
|
| 19 |
except Exception as e:
|
| 20 |
raise RuntimeError(f"Error initializing OCR model: {str(e)}")
|
| 21 |
|
| 22 |
-
def
|
| 23 |
-
"""
|
| 24 |
-
Preprocess the input image
|
| 25 |
-
|
| 26 |
-
Args:
|
| 27 |
-
input_data (Union[bytes, Image.Image]): Input image in bytes or PIL Image
|
| 28 |
-
|
| 29 |
-
Returns:
|
| 30 |
-
np.ndarray: Processed image array
|
| 31 |
-
"""
|
| 32 |
-
# Convert input to PIL Image if it's bytes
|
| 33 |
-
if isinstance(input_data, bytes):
|
| 34 |
-
try:
|
| 35 |
-
image = Image.open(io.BytesIO(input_data))
|
| 36 |
-
except Exception as e:
|
| 37 |
-
raise ValueError(f"Invalid image format: {str(e)}")
|
| 38 |
-
elif isinstance(input_data, Image.Image):
|
| 39 |
-
image = input_data
|
| 40 |
-
else:
|
| 41 |
-
raise TypeError("Input must be bytes or PIL Image")
|
| 42 |
-
|
| 43 |
-
# Convert to numpy array
|
| 44 |
-
return np.array(image)
|
| 45 |
-
|
| 46 |
-
def predict(self, input_data: Union[bytes, Image.Image]) -> Dict[str, List[Dict]]:
|
| 47 |
"""
|
| 48 |
Perform OCR inference
|
| 49 |
|
|
@@ -55,7 +32,7 @@ class OCRInferenceHandler:
|
|
| 55 |
"""
|
| 56 |
try:
|
| 57 |
# Preprocess the image
|
| 58 |
-
img_array = self.
|
| 59 |
|
| 60 |
# Perform OCR detection
|
| 61 |
results = self.reader.readtext(img_array)
|
|
@@ -80,32 +57,27 @@ class OCRInferenceHandler:
|
|
| 80 |
'success': False,
|
| 81 |
'error': str(e)
|
| 82 |
}
|
| 83 |
-
|
| 84 |
-
# Hugging Face Inference API handler
|
| 85 |
-
def handler(input_data):
|
| 86 |
-
"""
|
| 87 |
-
Main handler for Hugging Face Inference API
|
| 88 |
-
|
| 89 |
-
Args:
|
| 90 |
-
input_data: Input image data
|
| 91 |
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
#
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
import io
|
| 3 |
import easyocr
|
| 4 |
import numpy as np
|
| 5 |
from typing import Dict, List, Union
|
| 6 |
from PIL import Image
|
| 7 |
|
| 8 |
+
class EndpointHandler:
|
| 9 |
+
def __init__(self, path: str):
|
| 10 |
"""
|
| 11 |
Initialize the OCR inference handler
|
| 12 |
|
| 13 |
Args:
|
| 14 |
+
path (str): Directory containing model artifacts
|
| 15 |
"""
|
| 16 |
try:
|
| 17 |
+
# Detect preferred languages from the model directory name or default to English
|
| 18 |
+
# You can modify this logic to detect or configure languages
|
| 19 |
self.reader = easyocr.Reader(['en'])
|
| 20 |
except Exception as e:
|
| 21 |
raise RuntimeError(f"Error initializing OCR model: {str(e)}")
|
| 22 |
|
| 23 |
+
def __call__(self, input_data: Union[bytes, Image.Image]) -> Dict[str, List[Dict]]:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 24 |
"""
|
| 25 |
Perform OCR inference
|
| 26 |
|
|
|
|
| 32 |
"""
|
| 33 |
try:
|
| 34 |
# Preprocess the image
|
| 35 |
+
img_array = self._preprocess(input_data)
|
| 36 |
|
| 37 |
# Perform OCR detection
|
| 38 |
results = self.reader.readtext(img_array)
|
|
|
|
| 57 |
'success': False,
|
| 58 |
'error': str(e)
|
| 59 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 60 |
|
| 61 |
+
def _preprocess(self, input_data: Union[bytes, Image.Image]) -> np.ndarray:
|
| 62 |
+
"""
|
| 63 |
+
Preprocess the input image
|
| 64 |
+
|
| 65 |
+
Args:
|
| 66 |
+
input_data (Union[bytes, Image.Image]): Input image in bytes or PIL Image
|
| 67 |
+
|
| 68 |
+
Returns:
|
| 69 |
+
np.ndarray: Processed image array
|
| 70 |
+
"""
|
| 71 |
+
# Convert input to PIL Image if it's bytes
|
| 72 |
+
if isinstance(input_data, bytes):
|
| 73 |
+
try:
|
| 74 |
+
image = Image.open(io.BytesIO(input_data))
|
| 75 |
+
except Exception as e:
|
| 76 |
+
raise ValueError(f"Invalid image format: {str(e)}")
|
| 77 |
+
elif isinstance(input_data, Image.Image):
|
| 78 |
+
image = input_data
|
| 79 |
+
else:
|
| 80 |
+
raise TypeError("Input must be bytes or PIL Image")
|
| 81 |
+
|
| 82 |
+
# Convert to numpy array
|
| 83 |
+
return np.array(image)
|