-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
179 lines (140 loc) · 4.85 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
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
173
174
175
176
177
178
179
from flask import Flask ,request, jsonify,render_template
import pandas as pd
import tensorflow as tf
import keras
from keras.models import load_model
from keras.models import Sequential
from keras.layers import Dense, Dropout, Flatten, Conv2D, MaxPool2D
from keras.optimizers import RMSprop
import base64
import json
import cv2
from io import BytesIO
import numpy as np
import requests
from keras.preprocessing import image
from flask_cors import CORS
import pickle
import base64
from io import BytesIO
from PIL import Image
from binascii import a2b_base64
app = Flask(__name__)
CORS(app)
model = None
def load_lenet(weights = 'Lenet_model_01.h5'):
'''
load LeNet 5 model
'''
model = Sequential()
model.add(Conv2D(filters = 32, kernel_size = (5,5),padding = 'Same',activation ='relu', input_shape = (28,28,1)))
model.add(Conv2D(filters = 32, kernel_size = (5,5),padding = 'Same', activation ='relu'))
model.add(Conv2D(filters = 64, kernel_size = (3,3),padding = 'Same',activation ='relu'))
model.add(MaxPool2D(pool_size=(2,2), strides=(2,2)))
model.add(Conv2D(filters = 64, kernel_size = (3,3),padding = 'Same',activation ='relu'))
model.add(Flatten())
model.add(Dense(256, activation = "relu"))
model.add(Dense(10))
model.load_weights(weights, by_name=True)
model._make_predict_function()
return model
def build_model():
#build model
global model
global graph
model = load_lenet(weights='Lenet_model_01.h5')
graph = tf.get_default_graph()
def pad_image(img, pad_t, pad_r, pad_b, pad_l):
"""
Add padding of zeroes to an image.
Add padding to an array image.
return padded square image
"""
height, width= img.shape
# Adding padding to the left side.
pad_left = np.zeros(( height, pad_l))
img = np.concatenate((pad_left, img), axis = 1)
# Adding padding to the top.
pad_up = np.zeros((pad_t, pad_l + width))
img = np.concatenate((pad_up, img), axis = 0)
# Adding padding to the right.
pad_right = np.zeros((height + pad_t, pad_r))
img = np.concatenate((img, pad_right), axis = 1)
# Adding padding to the bottom
pad_bottom = np.zeros((pad_b, pad_l + width + pad_r))
img = np.concatenate((img, pad_bottom), axis = 0)
return img
def center_image(img):
"""
Return a centered image.
"""
col_sum = np.where(np.sum(img, axis=0) > 0)
row_sum = np.where(np.sum(img, axis=1) > 0)
y1, y2 = row_sum[0][0], row_sum[0][-1]
x1, x2 = col_sum[0][0], col_sum[0][-1]
cropped_image = img[y1:y2, x1:x2]
zero_axis_fill = (img.shape[0] - cropped_image.shape[0])
one_axis_fill = (img.shape[1] - cropped_image.shape[1])
top = zero_axis_fill / 2
bottom = zero_axis_fill - top
left = one_axis_fill / 2
right = one_axis_fill - left
padded_image = pad_image(cropped_image, int(top), int(left), int(bottom), int(right))
return padded_image
def resize_image(img, size=(28,28)):
'''
resize image to 28*28 to fit input shape of model
'''
h, w = img.shape
if h == w:
return cv2.resize(img, size, cv2.INTER_AREA)
dif = h if h > w else w
if dif > (size[0]+size[1])//2:
interpolation = cv2.INTER_AREA
else:
interpolation = cv2.INTER_CUBIC
x_pos = (dif - w)//2
y_pos = (dif - h)//2
if len(img.shape) == 2:
mask = np.zeros((dif, dif), dtype=img.dtype)
mask[y_pos:y_pos+h, x_pos:x_pos+w] = img[:h, :w]
else:
mask = np.zeros((dif, dif, c), dtype=img.dtype)
mask[y_pos:y_pos+h, x_pos:x_pos+w, :] = img[:h, :w, :]
return cv2.resize(mask, size, interpolation)
def toencodeStr(arr):
retval, buffer = cv2.imencode('.jpg', pic_img)
pic_str = base64.b64encode(buffer)
pic_str = pic_str.decode()
return pic_str
@app.route("/app/recognize", methods=['POST'])
def regonize():
#post method
if request.method == 'POST':
data = str(request.data,encoding = "utf-8").split(';')[1]
encoded_data = data.split(',')[1][:-2]
#decoded_image = base64.b64decode(encoded_data)
binary_data = a2b_base64(encoded_data)
img = Image.open(BytesIO(binary_data)).resize((280, 280)).convert('LA')
pixels = np.asarray(img, dtype='uint8')[:,:,1]
img = center_image(pixels)
nninput = resize_image(img)/255
#print(np.maximum(nninput))
img = nninput.reshape(1, 28, 28, -1)
#print(img.shape)
with graph.as_default():
pred = model.predict(img)
result = np.argmax(pred)
payload = {"pred":pred.tolist(),
"result":str(result)
}
return jsonify(payload)
@app.route("/app")
def digit_reg():
return render_template("paint_app.html")
if __name__ == '__main__':
print('*loading lenet model...')
build_model()
print('*starting flask app...')
#host='0.0.0.0', port=80
app.run(debug=True,host='0.0.0.0', port=80)