forked from navdeeshahuja/TensorFlow-Character-Recognition
-
Notifications
You must be signed in to change notification settings - Fork 0
/
predict.py
executable file
·61 lines (43 loc) · 1.6 KB
/
predict.py
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
import sys
import tensorflow as tf
from PIL import Image,ImageFilter
def predictLetter(imvalue):
x = tf.placeholder(tf.float32, [None, 784])
W = tf.Variable(tf.zeros([784, 10]))
b = tf.Variable(tf.zeros([10]))
y = tf.nn.softmax(tf.matmul(x, W) + b)
init_op = tf.initialize_all_variables()
saver = tf.train.Saver()
with tf.Session() as sess:
sess.run(init_op)
saver.restore(sess, "model.ckpt")
prediction=tf.argmax(y,1)
return prediction.eval(feed_dict={x: [imvalue]}, session=sess)
def imageprepare(argv):
im = Image.open(argv).convert('L')
width = float(im.size[0])
height = float(im.size[1])
newImage = Image.new('L', (28, 28), (255))
if width > height:
nheight = int(round((20.0/width*height),0))
if (nheight == 0):
nheight = 1
img = im.resize((20,nheight), Image.ANTIALIAS).filter(ImageFilter.SHARPEN)
wtop = int(round(((28 - nheight)/2),0))
newImage.paste(img, (4, wtop))
else:
nwidth = int(round((20.0/height*width),0))
if (nwidth == 0):
nwidth = 1
img = im.resize((nwidth,20), Image.ANTIALIAS).filter(ImageFilter.SHARPEN)
wleft = int(round(((28 - nwidth)/2),0))
newImage.paste(img, (wleft, 4))
tv = list(newImage.getdata())
tva = [ (255-x)*1.0/255.0 for x in tv]
return tva
def main(argv):
imvalue = imageprepare(argv)
predictedLetter = predictLetter(imvalue)
print (predictedLetter[0])
if __name__ == "__main__":
main(sys.argv[1])