File size: 4,974 Bytes
7b68e51
 
 
 
 
 
 
 
 
fac7e00
7b68e51
 
44df331
7b68e51
 
 
 
 
 
 
 
94da9e4
7b68e51
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e31d9fe
7b68e51
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a757df5
7b68e51
 
 
532f3a6
7b68e51
 
 
 
 
 
532f3a6
7b68e51
 
44df331
7b68e51
 
 
dee051a
3938e54
7b68e51
 
 
 
 
 
 
 
49661a6
7b68e51
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172



import tensorflow as tf
from matplotlib import pyplot as plt
from skimage.transform import rescale, resize
import pickle as pkl
import numpy as np
import os
import cv2
from PIL import Image,ImageFont, ImageDraw
import CALTextModel
import gradio as gr


#### training setup parameters ####


lambda_val=1e-4
gamma_val=1

os.environ['CUDA_VISIBLE_DEVICES'] = '-1'



################################### Utility functions###################################



def load_dict_picklefile(dictFile):
    fp=open(dictFile,'rb')
    lexicon=pkl.load(fp)
    fp.close()
    return lexicon,lexicon[' ']

def preprocess_img(img):
    if len(img.shape)>2:
      img= cv2.cvtColor(img.astype('float32'), cv2.COLOR_BGR2GRAY)
    height=img.shape[0]
    width=img.shape[1]

    if(width<300):
      result = np.ones([img.shape[0], img.shape[1]*2])*255
      result[0:img.shape[0],img.shape[1]:img.shape[1]*2]=img
      img=result

    img=cv2.resize(img, dsize=(800,100), interpolation = cv2.INTER_AREA)
    img=(img-img.min())/(img.max()-img.min())
    xx_pad = np.zeros((100, 800), dtype='float32')
    xx_pad[:,:] =1

    xx_pad = xx_pad[None, :, :]
    img=img[None, :, :]
    return img, xx_pad


worddicts,_ = load_dict_picklefile('vocabulary.pkl')
worddicts_r = [None] * len(worddicts)
i=1
for kk, vv in worddicts.items():
    if(i<len(worddicts)):
        worddicts_r[vv] = kk
    else:
        break
    i=i+1

# Create an instance of the model
CALTEXT = CALTextModel.CALTEXT_Model(training=False)
#CALTEXT.load_weights('CALTextModel/cp-0037.ckpt')
test_loss = tf.keras.metrics.Mean(name='test_loss')


@tf.function(experimental_relax_shapes=True)
def execute_model(xx,xx_mask,CALTEXT):

  anno = CALTEXT(xx,xx_mask, training=False)
  hidden_state_0 = CALTEXT.get_hidden_state_0(anno)
  return anno,hidden_state_0


def test_error( images, x_mask):
  # training=False is only needed if there are layers with different
  # behavior during training versus inference (e.g. Dropout).
  batch_loss=0
  img_ind=1
  for img_ind in range(len(images)):
    xx = images[img_ind][tf.newaxis, ... ]
    xx_mask = x_mask[img_ind][tf.newaxis, ... ]
    anno,hidden_state_0=execute_model(xx,xx_mask,CALTEXT)

    sample, score,hypalpha=CALTextModel.get_sample(anno, hidden_state_0,10, 130, False, False, CALTEXT)


    score = score / np.array([len(s) for s in sample])
    ss = sample[score.argmin()]
    img_ind=img_ind+1

    ind=0
    num=int(len(ss)/2)




    ####   output string
    ind=0
    outstr=u''
    frames = []
    #font = ImageFont.truetype("Jameel Noori Nastaleeq.ttf",60)
    while (ind<len(ss)-1):
      k=(len(ss)-2)-ind
      outstr=outstr+worddicts_r[int(ss[k])]
      '''textimg = Image.new('RGB', (1400,100),(255,255,255))
      drawtext = ImageDraw.Draw(textimg)
      drawtext.text((20, 20), outstr ,(0,0,0),font=font)
      fig,axes=plt.subplots(2,1)
      axes[0].imshow(textimg)
      axes[0].axis('off')
      axes[1].axis('off')
      axes[1].imshow(xx[0,:,:],cmap='gray')
      visualization=resize(hypalpha[k], (100,800),anti_aliasing=True)
      axes[1].imshow(255-(255 * visualization), alpha=0.2)
      plt.axis('off')

      plt.savefig('/content/gdrive/My Drive/CALText_Demo/res.png')
      frames.append(Image.fromarray(cv2.imread('/content/gdrive/My Drive/CALText_Demo/res.png'), 'RGB'))'''
      ind=ind+1
    '''frame_one = frames[0]
    frame_one.save("/content/gdrive/My Drive/CALText_Demo/'vis.gif", format="GIF", append_images=frames,save_all=True, duration=300, loop=0)
    gif_image="/content/gdrive/My Drive/CALText_Demo/'vis.gif"'''
  return outstr



'''examples = [
    ['/content/gdrive/My Drive/CALText_Demo/sample_test_images/59-11.png'],
		['/content/gdrive/My Drive/CALText_Demo/sample_test_images/59-21.png'],
		['/content/gdrive/My Drive/CALText_Demo/sample_test_images/59-32.png'],
		['/content/gdrive/My Drive/CALText_Demo/sample_test_images/59-37.png'],
		['/content/gdrive/My Drive/CALText_Demo/sample_test_images/91-47.png'],
		['/content/gdrive/My Drive/CALText_Demo/sample_test_images/91-49.png'],
    ]'''




def recognize_text(input_image):
    x, x_mask=preprocess_img(input_image)
    #output_str=test_error(x, x_mask)
    return "output_str"


title = "CALText Demo"
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>"
article = "<p style='text-align: center'></p>"
css = "#0 {object-fit: contain;} #1 {object-fit: contain;}"
inputs = gr.inputs.Image(label="Input Image")

demo = gr.Interface(fn=recognize_text,inputs=inputs,outputs=[gr.Textbox(label="Output")],title=title,
                    description=description,
                    article=article,allow_flagging='never')

demo.launch()