MedaSaiCharan commited on
Commit
f12e6af
β€’
1 Parent(s): 2af1548

Add application file and models

Browse files
app.py ADDED
@@ -0,0 +1,98 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import pandas as pd
3
+ import tensorflow as tf
4
+ from keras.models import load_model
5
+ import numpy as np
6
+ import PIL
7
+ from PIL import Image, ImageOps
8
+ import cv2
9
+
10
+
11
+
12
+ def num_to_label(num):
13
+ alphabets = "ABCDEFGHIJKLMNOPQRSTUVWXYZ-' " # The set of valid characters
14
+ max_str_len = 24 # Maximum length of input labels
15
+ num_of_characters = len(alphabets) + 1 # Number of unique characters, plus 1 for CTC pseudo-blank
16
+ num_of_timestamps = 64 # Maximum length of predicted labels
17
+ ret = "" # Initialize an empty string to store the label
18
+ for ch in num: # Iterate over each character index in the input list
19
+ if ch == -1: # Check for the CTC Blank (a special case)
20
+ break
21
+ else:
22
+ ret += alphabets[ch] # Append the character from 'alphabets' based on the index
23
+ return ret
24
+
25
+
26
+ # Loading different models
27
+ mnist_cnn_model = load_model('./models/mnist_cnn_model.h5')
28
+ mnist_mcdnn_model = load_model('./models/mnist_mcdnn_model.h5')
29
+
30
+
31
+ # Data Preprocessing
32
+ def pre_process(image, model_name):
33
+ img = image.convert("L")
34
+
35
+ # Select model based on the dropdown selection
36
+ if model_name == "cnn_model_mnist":
37
+ model = mnist_cnn_model
38
+ img = img.resize((28, 28))
39
+ img = np.array(img) / 255.0
40
+ final_img = np.expand_dims(img, axis=0) # Add batch dimension
41
+ pred = model.predict(final_img)
42
+ result = np.argmax(pred)
43
+ return str(result)
44
+ elif model_name == "mcdnn_model_mnist":
45
+ model = mnist_mcdnn_model
46
+ img = img.resize((28, 28))
47
+ img = np.array(img) / 255.0
48
+ final_img = np.expand_dims(img, axis=0) # Add batch dimension
49
+ pred = model.predict(final_img)
50
+ result = np.argmax(pred)
51
+ return str(result)
52
+ elif model_name == "cnn_model_handwritten_text":
53
+ model = mnist_mcdnn_model
54
+ img_array = np.array(img)
55
+ if len(img_array.shape) > 2:
56
+ img_array = np.mean(img_array, axis=-1, dtype=np.uint8)
57
+ (h, w) = img_array.shape
58
+ final_img = np.ones([64, 256])*255
59
+ if h > 64:
60
+ img_array = img_array[:64, :]
61
+ if w > 256:
62
+ img_array = img_array[:, :256]
63
+ final_img[:h, :w] = img_array
64
+ final_img = cv2.rotate(final_img, cv2.ROTATE_90_CLOCKWISE)
65
+ final_img = final_img / 255.0
66
+ pred = model.predict(final_img.reshape(1, 256, 64, 1))
67
+ num_to_label(pred)
68
+ else:
69
+ return "Invalid model selection"
70
+
71
+ # Defining model options for dropdown
72
+ model_options = ["cnn_model_mnist", "mcdnn_model_mnist", "cnn_model_handwritten_text"]
73
+
74
+
75
+ with gr.Blocks() as demo:
76
+ with gr.Row():
77
+ gr.Label("ScriptSense", container=False)
78
+
79
+ with gr.Row():
80
+ with gr.Column():
81
+ image_file = gr.inputs.Image(type="pil", label="Upload an image")
82
+ model_select = gr.components.Dropdown(choices=model_options, label="Select Model")
83
+
84
+ with gr.Row():
85
+ predict_btn = gr.Button(value="Predict")
86
+
87
+ with gr.Row():
88
+ Answer = gr.Label("πŸ‘‹ Hello, Let us predict the Result πŸ’β€β™‚οΈ", container=False)
89
+
90
+ predict_btn.click(
91
+ pre_process,
92
+ inputs=[
93
+ image_file, model_select
94
+ ],
95
+ outputs=[Answer],
96
+ )
97
+
98
+ demo.launch()
models/mnist_cnn_model.h5 ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:c7357478eb9b9dcabce4a39e1ac1100658ecb84f2fe9ed1a0f41d07cfa2041bf
3
+ size 642592
models/mnist_mcdnn_model.h5 ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:c7357478eb9b9dcabce4a39e1ac1100658ecb84f2fe9ed1a0f41d07cfa2041bf
3
+ size 642592