-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
97 lines (85 loc) · 3.08 KB
/
app.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
89
90
91
92
93
94
95
96
97
#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
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 (khong chay)
def convertImage(imgData1):
imgstr = re.search(r'base64,(.*)',imgData1).group(1)
#print(imgstr)
with open('output.png','wb') as output:
output.write(imgstr.decode('base64'))
def convertImage1(imgData1):
imgstr = re.search(b'base64,(.*)',imgData1).group(1)
#print(imgstr)
with open('output.png','wb') as output: output.write(base64.b64decode(imgstr))
@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, nhan data tu brower gui qua va xu ly
imgData = request.get_data()
#neu la hinh ve thi encode it into a suitable format (ham nay se tao ra anh tu chung ta ve va luu xuong dia voi ten ouput.png)
convertImage1(imgData)
print ("debug")
#read the image into memory
x = imread("output.png",mode='L')
#neu la anh load len thi
#x = imread(imgData,mode='L')
#compute a bit-wise inversion so black becomes white and vice versa
x = np.invert(x)
#imshow(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) #in hinh
print(np.argmax(out,axis=1))#lay vector voi 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)
#optional if we want to run in debugging mode
#app.run(debug=True)