-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
63 lines (50 loc) · 1.92 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
from flask import Flask, render_template, request
from tensorflow.keras.models import load_model
import numpy as np
from tensorflow.keras.preprocessing.image import load_img
from tensorflow.keras.applications.vgg16 import preprocess_input
import os
from tensorflow.keras.preprocessing import image
from pathlib import Path
app = Flask(__name__)
model_path = Path() / "model" / "Fruits_Classification.h5"
model = load_model(model_path)
target_img = os.path.join(os.getcwd(), "static/images")
@app.route("/")
def index_view():
return render_template("index.html")
# Allow files with extension png, jpg and jpeg
ALLOWED_EXT = set(["jpg", "jpeg", "png"])
def allowed_file(filename):
return "." in filename and filename.rsplit(".", 1)[1] in ALLOWED_EXT
# Function to load and prepare the image in right shape
def read_image(filename):
img = load_img(filename, target_size=(224, 224))
x = image.img_to_array(img)
x = np.expand_dims(x, axis=0)
x = preprocess_input(x)
return x
@app.route("/predict", methods=["GET", "POST"])
def predict():
if request.method == "POST":
file = request.files["file"]
if file and allowed_file(file.filename):
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)
classes_x = np.argmax(class_prediction, axis=1)
if classes_x == 0:
fruit = "Apple"
elif classes_x == 1:
fruit = "Banana"
else:
fruit = "Orange"
return render_template(
"predict.html", fruit=fruit, prob=class_prediction, user_image=file_path
)
else:
return "Unable to read the file. Please check file extension"
if __name__ == "__main__":
app.run(debug=True, use_reloader=False, port=8000)