-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
56 lines (44 loc) · 1.45 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
import re
import base64
from flask import Flask, render_template,request
import cv2
import joblib
import numpy as np
from keras.models import Sequential
from keras.layers import Flatten,Dense
import keras.backend as K
app = Flask(__name__)
def load_model():
K.clear_session()
model = Sequential()
model.add(Flatten(input_shape=(28, 28)))
model.add(Dense(512, activation='relu'))
model.add(Dense(256, activation='relu'))
model.add(Dense(10, activation='softmax'))
model.compile(optimizer='adam',loss='categorical_crossentropy',metrics=['accuracy'])
model.load_weights('FFNN-MNIST.h5')
return model
@app.route('/') #home page / represents root page. this will return the html page.
def index():
return render_template('drawdigits.html')
@app.route('/predictdigits/', methods=['GET','POST'])
def predict_digits():
model=load_model()
parseImage(request.get_data())
img=cv2.imread('output.png')
img=255-img
img=cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
img=cv2.resize(img,(28,28))
img=img.reshape(1,28,28)
img=(img/255.0)
result=model.predict(img)
label=np.argmax(result,axis=1)[0]
print(label)
return str(label)
def parseImage(imgData):
# parse canvas bytes and save as output.png
imgstr = re.search(b'base64,(.*)', imgData).group(1)
with open('output.png','wb') as output:
output.write(base64.decodebytes(imgstr))
if __name__ == '__main__':
app.run(debug=True)