vjdevane commited on
Commit
77987e0
·
verified ·
1 Parent(s): 3f87f19

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +27 -36
app.py CHANGED
@@ -4,30 +4,16 @@ import keras.backend as K
4
  import cv2
5
  import numpy as np
6
 
7
- # # Define the custom loss function
8
- # def ctc_lambda_func(args):
9
- # y_pred, labels, input_length, label_length = args
10
- # return K.ctc_batch_cost(labels, y_pred, input_length, label_length)
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
- filepath="test_img.png"
63
- img = cv2.imread(filepath, cv2.IMREAD_GRAYSCALE)
64
- img = process_image(img)
65
- prediction =model.predict(np.asarray([img]))
66
- decoded = K.ctc_decode(prediction,
67
- input_length=np.ones(prediction.shape[0]) * prediction.shape[1],
68
- greedy=True)[0][0]
69
- out = K.get_value(decoded)
70
- # see the results
71
- temp_str=""
72
- for i, x in enumerate(out):
73
- for p in x:
74
- if int(p) != -1:
75
- temp_str+=char_list[int(p)]
 
 
 
 
 
 
76
 
77
- def greet(name):
78
- return "Hello " + temp_str + "!!"
79
 
80
- demo = gr.Interface(fn=greet, inputs="text", outputs="text")
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)