-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
68 lines (50 loc) · 1.71 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
import glob
import numpy as np
import os
# Importing deep-learning related libraries:
from tensorflow.keras.applications.imagenet_utils import preprocess_input, decode_predictions
from tensorflow.keras.models import load_model
from tensorflow.keras.preprocessing import image
# Importing flask-related libraries:
from flask import Flask, redirect, url_for, request, render_template
from werkzeug.utils import secure_filename
# Initializing our flask application:
app = Flask(__name__)
modelPath = 'model_ResNet50.h5'
# Loading our model:
model = load_model(modelPath)
def modelPredict(img_path, model):
img = image.load_img(img_path, target_size=(224, 224))
# Preprocessing the image:
x = image.img_to_array(img)
x = x/255
x = np.expand_dims(x, axis=0)
predictions = model.predict(x)
predictions = np.argmax(predictions, axis=1)
if predictions == 0:
predictions = "The brand is Audi"
elif predictions == 1:
predictions = "The brand is Lamborghini"
else:
predictions = "The brand is Mercedes"
return predictions
@app.route('/', methods=['GET'])
def index():
return render_template('index.html')
@app.route('/predict', methods=['GET', 'POST'])
def upload():
if request.method == 'POST':
f = request.files['file']
# Saving the file to an "upload folder":
basePath = os.path.dirname(__file__)
filePath = os.path.join(
basePath, 'uploads', secure_filename(f.filename)
)
f.save(filePath)
predictions = modelPredict(filePath, model)
result = predictions
return result
else:
return None
if __name__ == '__main__':
app.run(debug=True)