-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
41 lines (33 loc) · 1.33 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, render_template, request
from tensorflow.keras.preprocessing.image import img_to_array
from tensorflow.keras.utils import load_img
from tensorflow.keras.models import load_model
import os
import numpy as np
app = Flask(__name__)
model = load_model("models/mnist.h5")
app.config["MAX_CONTENT_LENGTH"] = 10 * 1024 * 1024
ALLOWED_EXTENSIONS = ["jpeg", "png", "jpg"]
def read_image(filename):
img = load_img(filename, color_mode="grayscale", target_size=(28, 28))
img = img_to_array(img)
img = img.reshape((1, 28, 28, 1))
img = img / 255.0
return img
@app.route("/", methods=["GET", "POST"])
def home():
return render_template("index.html")
@app.route("/predict", methods=["GET", "POST"])
def predict():
if request.method == "POST":
file = request.files["img"]
if file and '.' in file.filename and file.filename.rsplit('.', 1)[1].lower() in ALLOWED_EXTENSIONS:
filename = file.filename
file_path = os.path.join("static/images", filename)
file.save(file_path)
img = read_image(file_path)
class_prediction = model.predict(img)
predicted_class_index = np.argmax(class_prediction)
return render_template("index.html", prediction=predicted_class_index)
if __name__ == "__main__":
app.run(port=5000, debug=True)