-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
41 lines (32 loc) · 1.31 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
from flask import Flask, request, jsonify
from flask_cors import CORS, cross_origin
from PIL import Image
import keras as kr
import numpy as np
app = Flask(__name__)
CORS(app)
# Pip3 install h5py
# Load the model from the h5 file
model = kr.models.load_model('mnist-neural.h5')
# Print the model summary to the terminal
model.summary()
@app.route('/upload', methods = ['POST'])
def mnist_prediction():
# Load the image file into an object using PIL Image lib
# Use .convert('L') to convert the file to (8-bit pixels, black and white) - grayscale
# Use .resize() to resize the width & height to 28 x 28
img = Image.open(request.files['file'].stream).convert('L')
img = img.resize((28, 28))
# Convert the PIL Image to a pixel array by passing it to np.array()
# And change the dtype to np.float32
pixel_array = np.array(img).astype(np.float32)
pixel_array = pixel_array.reshape(784)
# The models accepts an array of images to make predictions, so will return an array of predictions
# Use np.argmax() to find the prediction value in the One-Hot vector
prediction = list(model.predict(np.array([pixel_array])))
print(prediction[0])
prediction = np.argmax(prediction[0])
print(prediction)
return jsonify(str(prediction))
if __name__ == '__main__':
app.run()