-
Notifications
You must be signed in to change notification settings - Fork 5
/
app.py
70 lines (49 loc) · 1.58 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
from flask import Flask, request, url_for, redirect, render_template
from werkzeug.utils import secure_filename
import numpy as np
import tensorflow
import os
from tensorflow.keras.preprocessing import image
MODEL_PATH = ""
CATEGORIES = []
UPLOAD_FOLDER = "uploads"
app = Flask(__name__)
app.config["UPLOAD_FOLDER"] = UPLOAD_FOLDER
def predResult(result):
"""Returns the predicted output class.
Arguments:
result -- array obtained from model's predict method
"""
if result.size > 1:
index = np.argmax(result)
else:
if result[0][0] < 0.5:
index = 0
else:
index = 1
output = CATEGORIES[index]
return output
@app.route("/")
def input_image():
return render_template("index.html")
@app.route("/predict", methods=["POST"])
def predict():
inputImage = request.files["img"]
if not inputImage:
return "No image uploaded!", 400
filename = secure_filename(inputImage.filename)
mimetype = inputImage.mimetype
if not filename or not mimetype:
return "Bad upload!", 400
model = tensorflow.keras.models.load_model(MODEL_PATH)
inputImage.save(os.path.join(app.config["UPLOAD_FOLDER"], filename))
path = "uploads/" + filename
kwargs = {}
test_image = image.load_img(path, **kwargs)
test_image = image.img_to_array(test_image)
test_image = np.expand_dims(test_image, axis=0)
result = model.predict(test_image)
prediction = predResult(result)
return render_template("result.html", prediction=prediction)
if __name__ == "__main__":
app.run(debug=True)