Tayaba171 commited on
Commit
7b68e51
1 Parent(s): d259247

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +171 -0
app.py ADDED
@@ -0,0 +1,171 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+
2
+
3
+
4
+ import tensorflow as tf
5
+ from matplotlib import pyplot as plt
6
+ from skimage.transform import rescale, resize
7
+ import pickle as pkl
8
+ import numpy as np
9
+ import os
10
+ import cv2
11
+ from PIL import Image,ImageFont, ImageDraw
12
+ import CALTextModel
13
+
14
+
15
+
16
+ #### training setup parameters ####
17
+
18
+
19
+ lambda_val=1e-4
20
+ gamma_val=1
21
+
22
+
23
+
24
+
25
+
26
+ ################################### Utility functions###################################
27
+
28
+
29
+
30
+ def load_dict_picklefile(dictFile):
31
+ fp=open(dictFile,'rb')
32
+ lexicon=pkl.load(fp)
33
+ fp.close()
34
+ return lexicon,lexicon[' ']
35
+
36
+ def preprocess_img(img):
37
+ if len(img.shape)>2:
38
+ img= cv2.cvtColor(img.astype('float32'), cv2.COLOR_BGR2GRAY)
39
+ height=img.shape[0]
40
+ width=img.shape[1]
41
+
42
+ if(width<300):
43
+ result = np.ones([img.shape[0], img.shape[1]*2])*255
44
+ result[0:img.shape[0],img.shape[1]:img.shape[1]*2]=img
45
+ img=result
46
+
47
+ img=cv2.resize(img, dsize=(800,100), interpolation = cv2.INTER_AREA)
48
+ img=(img-img.min())/(img.max()-img.min())
49
+ xx_pad = np.zeros((100, 800), dtype='float32')
50
+ xx_pad[:,:] =1
51
+
52
+ xx_pad = xx_pad[None, :, :]
53
+ img=img[None, :, :]
54
+ return img, xx_pad
55
+
56
+
57
+ worddicts,_ = load_dict_picklefile('vocabulary.pkl')
58
+ worddicts_r = [None] * len(worddicts)
59
+ i=1
60
+ for kk, vv in worddicts.items():
61
+ if(i<len(worddicts)):
62
+ worddicts_r[vv] = kk
63
+ else:
64
+ break
65
+ i=i+1
66
+
67
+ # Create an instance of the model
68
+ CALTEXT = CALTextModel.CALTEXT_Model(training=False)
69
+ CALTEXT.load_weights('final_caltextModel/cp-0037.ckpt')
70
+ test_loss = tf.keras.metrics.Mean(name='test_loss')
71
+
72
+
73
+ @tf.function(experimental_relax_shapes=True)
74
+ def execute_model(xx,xx_mask,CALTEXT):
75
+
76
+ anno = CALTEXT(xx,xx_mask, training=False)
77
+ hidden_state_0 = CALTEXT.get_hidden_state_0(anno)
78
+ return anno,hidden_state_0
79
+
80
+
81
+ def test_error( images, x_mask):
82
+ # training=False is only needed if there are layers with different
83
+ # behavior during training versus inference (e.g. Dropout).
84
+ batch_loss=0
85
+ img_ind=1
86
+ for img_ind in range(len(images)):
87
+ xx = images[img_ind][tf.newaxis, ... ]
88
+ xx_mask = x_mask[img_ind][tf.newaxis, ... ]
89
+ anno,hidden_state_0=execute_model(xx,xx_mask,CALTEXT)
90
+
91
+ sample, score,hypalpha=CALTextModel.get_sample(anno, hidden_state_0,10, 130, False, False, CALTEXT)
92
+
93
+
94
+ score = score / np.array([len(s) for s in sample])
95
+ ss = sample[score.argmin()]
96
+ img_ind=img_ind+1
97
+
98
+ ind=0
99
+ num=int(len(ss)/2)
100
+
101
+
102
+
103
+
104
+ #### output string
105
+ ind=0
106
+ outstr=u''
107
+ frames = []
108
+ #font = ImageFont.truetype("Jameel Noori Nastaleeq.ttf",60)
109
+ while (ind<len(ss)-1):
110
+ k=(len(ss)-2)-ind
111
+ outstr=outstr+worddicts_r[int(ss[k])]
112
+ '''textimg = Image.new('RGB', (1400,100),(255,255,255))
113
+ drawtext = ImageDraw.Draw(textimg)
114
+ drawtext.text((20, 20), outstr ,(0,0,0),font=font)
115
+ fig,axes=plt.subplots(2,1)
116
+ axes[0].imshow(textimg)
117
+ axes[0].axis('off')
118
+ axes[1].axis('off')
119
+ axes[1].imshow(xx[0,:,:],cmap='gray')
120
+ visualization=resize(hypalpha[k], (100,800),anti_aliasing=True)
121
+ axes[1].imshow(255-(255 * visualization), alpha=0.2)
122
+ plt.axis('off')
123
+
124
+ plt.savefig('/content/gdrive/My Drive/CALText_Demo/res.png')
125
+ frames.append(Image.fromarray(cv2.imread('/content/gdrive/My Drive/CALText_Demo/res.png'), 'RGB'))'''
126
+ ind=ind+1
127
+ '''frame_one = frames[0]
128
+ frame_one.save("/content/gdrive/My Drive/CALText_Demo/'vis.gif", format="GIF", append_images=frames,save_all=True, duration=300, loop=0)
129
+ gif_image="/content/gdrive/My Drive/CALText_Demo/'vis.gif"'''
130
+ return outstr,gif_image
131
+
132
+
133
+
134
+ ''''''examples = [
135
+ ['/content/gdrive/My Drive/CALText_Demo/sample_test_images/59-11.png'],
136
+ ['/content/gdrive/My Drive/CALText_Demo/sample_test_images/59-21.png'],
137
+ ['/content/gdrive/My Drive/CALText_Demo/sample_test_images/59-32.png'],
138
+ ['/content/gdrive/My Drive/CALText_Demo/sample_test_images/59-37.png'],
139
+ ['/content/gdrive/My Drive/CALText_Demo/sample_test_images/91-47.png'],
140
+ ['/content/gdrive/My Drive/CALText_Demo/sample_test_images/91-49.png'],
141
+ ]''''''
142
+
143
+
144
+ import gradio as gr
145
+
146
+ def recognize_text(input_image):
147
+ x, x_mask=preprocess_img(input_image)
148
+ output_str,gif_image=test_error(x, x_mask)
149
+ return output_str,gif_image
150
+
151
+
152
+ title = "CALText Demo"
153
+ description = "<p style='text-align: center'>Gradio demo for an CALText model architecture <a href='https://github.com/nazar-khan/CALText'>[GitHub Code]</a> trained on the <a href='http://faculty.pucit.edu.pk/nazarkhan/work/urdu_ohtr/pucit_ohul_dataset.html'>PUCIT-OHUL</a> dataset. To use it, simply add your image, or click one of the examples to load them. </p>"
154
+ article = "<p style='text-align: center'></p>"
155
+ css = "#0 {object-fit: contain;} #1 {object-fit: contain;}"
156
+ inputs = gr.inputs.Image(label="Input Image")
157
+
158
+ demo = gr.Interface(fn=recognize_text,inputs=inputs,outputs=[gr.Textbox(label="Output"),gr.Image(label="Demonstration of attention")],title=title,
159
+ description=description,
160
+ article=article,allow_flagging='never')
161
+
162
+ demo.launch()
163
+
164
+
165
+
166
+
167
+
168
+
169
+
170
+
171
+