-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.py
85 lines (60 loc) · 2.16 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
import os
from flask import Flask, render_template, request, send_from_directory
from keras_preprocessing import image
# from keras.models import load_model
import numpy as np
import tensorflow as tf
app = Flask(__name__)
STATIC_FOLDER = 'static'
# Path to the folder where we'll store the upload before prediction
UPLOAD_FOLDER = STATIC_FOLDER + '/uploads'
# Path to the folder where we store the different models
MODEL_FOLDER = STATIC_FOLDER + '/models'
def load__model():
"""Load model once at running time for all the predictions"""
print('[INFO] : Model loading ................')
global model
model = tf.keras.models.load_model(MODEL_FOLDER + '/cat_dog_classifier.h5')
global graph
graph = tf.compat.v1.get_default_graph()
print('[INFO] : Model loaded')
def predict(fullpath):
data = image.load_img(fullpath, target_size=(128, 128, 3))
# (224,224,3) ==> (128, 128, 3) - used a self trained model, not the mobilenet.
data = np.expand_dims(data, axis=0)
# Scaling
data = data.astype('float') / 255
# Prediction
result = model.predict(data)
return result
# Home Page
@app.route('/')
def index():
return render_template('index.html')
# Process file and predict his label
@app.route('/upload', methods=['GET', 'POST'])
def upload_file():
if request.method == 'GET':
return render_template('index.html')
else:
file = request.files['image']
fullname = os.path.join(UPLOAD_FOLDER, file.filename)
file.save(fullname)
result = predict(fullname)
pred_prob = result.item()
if pred_prob > .5:
label = 'Dog'
accuracy = round(pred_prob * 100, 2)
else:
label = 'Cat'
accuracy = round((1 - pred_prob) * 100, 2)
return render_template('predict.html', image_file_name=file.filename, label=label, accuracy=accuracy)
@app.route('/upload/<filename>')
def send_file(filename):
return send_from_directory(UPLOAD_FOLDER, filename)
def create_app():
load__model()
return app
if __name__ == '__main__':
app = create_app()
app.run(host='0.0.0.0', port=5001, debug=False)