Update app.py
Browse files
app.py
CHANGED
|
@@ -12,6 +12,49 @@ def ctc_lambda_func(args):
|
|
| 12 |
# Load the model, providing the custom loss function in the custom_objects dictionary
|
| 13 |
loaded_model = load_model('Text_recognizer_Using_CRNN_model.keras', custom_objects={'ctc': ctc_lambda_func},safe_mode=False)
|
| 14 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 15 |
filepath="test_img.png"
|
| 16 |
img = cv2.imread(filepath, cv2.IMREAD_GRAYSCALE)
|
| 17 |
img = process_image(img)
|
|
|
|
| 12 |
# Load the model, providing the custom loss function in the custom_objects dictionary
|
| 13 |
loaded_model = load_model('Text_recognizer_Using_CRNN_model.keras', custom_objects={'ctc': ctc_lambda_func},safe_mode=False)
|
| 14 |
|
| 15 |
+
def process_image(img):
|
| 16 |
+
"""
|
| 17 |
+
Converts image to shape (32, 128, 1) & normalize
|
| 18 |
+
"""
|
| 19 |
+
w, h = img.shape
|
| 20 |
+
|
| 21 |
+
# _, img = cv2.threshold(img,
|
| 22 |
+
# 128,
|
| 23 |
+
# 255,
|
| 24 |
+
# cv2.THRESH_BINARY | cv2.THRESH_OTSU)
|
| 25 |
+
|
| 26 |
+
# Aspect Ratio Calculation
|
| 27 |
+
new_w = 32
|
| 28 |
+
new_h = int(h * (new_w / w))
|
| 29 |
+
img = cv2.resize(img, (new_h, new_w))
|
| 30 |
+
w, h = img.shape
|
| 31 |
+
|
| 32 |
+
img = img.astype('float32')
|
| 33 |
+
|
| 34 |
+
# Converts each to (32, 128, 1)
|
| 35 |
+
if w < 32:
|
| 36 |
+
add_zeros = np.full((32-w, h), 255)
|
| 37 |
+
img = np.concatenate((img, add_zeros))
|
| 38 |
+
w, h = img.shape
|
| 39 |
+
|
| 40 |
+
if h < 128:
|
| 41 |
+
add_zeros = np.full((w, 128-h), 255)
|
| 42 |
+
img = np.concatenate((img, add_zeros), axis=1)
|
| 43 |
+
w, h = img.shape
|
| 44 |
+
|
| 45 |
+
if h > 128 or w > 32:
|
| 46 |
+
dim = (128,32)
|
| 47 |
+
img = cv2.resize(img, dim)
|
| 48 |
+
|
| 49 |
+
img = cv2.subtract(255, img)
|
| 50 |
+
|
| 51 |
+
img = np.expand_dims(img, axis=2)
|
| 52 |
+
|
| 53 |
+
# Normalize
|
| 54 |
+
img = img / 255
|
| 55 |
+
|
| 56 |
+
return img
|
| 57 |
+
|
| 58 |
filepath="test_img.png"
|
| 59 |
img = cv2.imread(filepath, cv2.IMREAD_GRAYSCALE)
|
| 60 |
img = process_image(img)
|