Update app.py
Browse files
app.py
CHANGED
@@ -4,30 +4,16 @@ import keras.backend as K
|
|
4 |
import cv2
|
5 |
import numpy as np
|
6 |
|
7 |
-
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
|
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 |
-
char_list = "!\"#&'()*+,-./0123456789:;?ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
|
15 |
-
|
16 |
-
model = load_model('Text_recognizer_Using_CRNN.h5')
|
17 |
-
|
18 |
|
19 |
def process_image(img):
|
20 |
"""
|
21 |
Converts image to shape (32, 128, 1) & normalize
|
22 |
"""
|
23 |
w, h = img.shape
|
24 |
-
|
25 |
-
# _, img = cv2.threshold(img,
|
26 |
-
# 128,
|
27 |
-
# 255,
|
28 |
-
# cv2.THRESH_BINARY | cv2.THRESH_OTSU)
|
29 |
-
|
30 |
-
# Aspect Ratio Calculation
|
31 |
new_w = 32
|
32 |
new_h = int(h * (new_w / w))
|
33 |
img = cv2.resize(img, (new_h, new_w))
|
@@ -59,23 +45,28 @@ def process_image(img):
|
|
59 |
|
60 |
return img
|
61 |
|
62 |
-
|
63 |
-
|
64 |
-
img =
|
65 |
-
|
66 |
-
|
67 |
-
|
68 |
-
|
69 |
-
|
70 |
-
|
71 |
-
|
72 |
-
|
73 |
-
|
74 |
-
|
75 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
76 |
|
77 |
-
|
78 |
-
return "Hello " + temp_str + "!!"
|
79 |
|
80 |
-
demo = gr.Interface(
|
81 |
-
demo.launch()
|
|
|
4 |
import cv2
|
5 |
import numpy as np
|
6 |
|
7 |
+
try:
|
8 |
+
model = load_model('Text_recognizer_Using_CRNN.h5')
|
9 |
+
except Exception as e:
|
10 |
+
print("Not able to load the trained model.")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
11 |
|
12 |
def process_image(img):
|
13 |
"""
|
14 |
Converts image to shape (32, 128, 1) & normalize
|
15 |
"""
|
16 |
w, h = img.shape
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
17 |
new_w = 32
|
18 |
new_h = int(h * (new_w / w))
|
19 |
img = cv2.resize(img, (new_h, new_w))
|
|
|
45 |
|
46 |
return img
|
47 |
|
48 |
+
def predict_image_text(input_img):
|
49 |
+
try:
|
50 |
+
img = cv2.cvtColor(input_img, cv2.COLOR_RGB2GRAY)
|
51 |
+
img = process_image(img)
|
52 |
+
prediction =model.predict(np.asarray([img]))
|
53 |
+
decoded = K.ctc_decode(prediction,
|
54 |
+
input_length=np.ones(prediction.shape[0]) * prediction.shape[1],
|
55 |
+
greedy=True)[0][0]
|
56 |
+
out = K.get_value(decoded)
|
57 |
+
char_list = "!\"#&'()*+,-./0123456789:;?ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
|
58 |
+
|
59 |
+
# see the results
|
60 |
+
temp_text=""
|
61 |
+
for i, x in enumerate(out):
|
62 |
+
for p in x:
|
63 |
+
if int(p) != -1:
|
64 |
+
temp_text+=char_list[int(p)]
|
65 |
+
return temp_text
|
66 |
+
except:
|
67 |
+
return "Some error occured please try again with proper image."
|
68 |
|
69 |
+
import gradio as gr
|
|
|
70 |
|
71 |
+
demo = gr.Interface(predict_image_text, gr.Image(), "text",title="Image to Text Conversion",description="Upload an image and get the extracted text.",theme="compact",)
|
72 |
+
demo.launch(debug=True)
|