-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
88 lines (77 loc) · 2.75 KB
/
main.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
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
#our web app framework!
#you could also generate a skeleton from scratch via
#http://flask-appbuilder.readthedocs.io/en/latest/installation.html
#Generating HTML from within Python is not fun, and actually pretty cumbersome because you have to do the
#HTML escaping on your own to keep the application secure. Because of that Flask configures the Jinja2 template engine
#for you automatically.
#requests are objects that flask handles (get set post, etc)
from flask import Flask, render_template,request
#scientific computing library for saving, reading, and resizing images
from scipy.misc import imsave, imread, imresize
#for matrix math
import numpy as np
#for importing our keras model
import keras.models
#for regular expressions, saves time dealing with string data
import re
#for convert image work
import base64
#system level operations (like loading files)
import sys
#for reading operating system data
import os
#tell our app where our saved model is
sys.path.append(os.path.abspath("./model"))
from load import *
#initalize our flask app
app = Flask(__name__)
#global vars for easy reusability
global model, graph
#initialize these variables
model, graph = init()
#decoding an image from base64 into raw representation
def convertImage(imgData1):
imgstr = re.search(b'base64,(.*)',imgData1).group(1)
#print(imgstr)
with open('output.png','wb') as output:
output.write(base64.b64decode(imgstr)) #output.write(imgstr.decode('base64'))
@app.route('/')
def index():
#initModel()
#render out pre-built HTML file right on the index page
return render_template("index.html")
@app.route('/predict/',methods=['GET','POST'])
def predict():
#whenever the predict method is called, we're going
#to input the user drawn character as an image into the model
#perform inference, and return the classification
#get the raw data format of the image
imgData = request.get_data()
#encode it into a suitable format
convertImage(imgData)
#print "debug"
#read the image into memory
x = imread('output.png',mode='L')
#compute a bit-wise inversion so black becomes white and vice versa
x = np.invert(x)
#make it the right size
x = imresize(x,(28,28))
#imshow(x)
#convert to a 4D tensor to feed into our model
x = x.reshape(1,28,28,1)
#print "debug2"
#in our computation graph
with graph.as_default():
#perform the prediction
out = model.predict(x)
print(out)
print(np.argmax(out,axis=1))
#print "debug3"
#convert the response to a string
response = np.array_str(np.argmax(out,axis=1))
return response
if __name__ == "__main__":
#decide what port to run the app in
port = int(os.environ.get('PORT', 5000))
#run the app locally on the givn port
app.run(host='0.0.0.0', port=port)