-
Notifications
You must be signed in to change notification settings - Fork 0
/
fashion_item_predict.py
165 lines (149 loc) · 5.67 KB
/
fashion_item_predict.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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
import os
from flask import Flask, flash, request, redirect, send_from_directory
from werkzeug.utils import secure_filename
from keras.models import load_model
from keras.utils import load_img
from tensorflow.keras.preprocessing.image import img_to_array
from keras.applications.imagenet_utils import preprocess_input
import numpy as np
from class_names import class_names
UPLOAD_FOLDER = './uploads/'
ALLOWED_EXTENSIONS = {'png', 'jpg', 'jpeg', 'gif'}
model = load_model('./fashion_mnist.hd5', compile=False)
def allowed_file(filename):
return '.' in filename and \
filename.rsplit('.', 1)[1].lower() in ALLOWED_EXTENSIONS
def preprocess_image(image):
image = img_to_array(image)
image = np.array([image])
image = preprocess_input(image, mode='tf')
return image
def predict_class(image):
yhat = model.predict(image)
print(yhat)
percentage = max(yhat[0])
predict_index = np.argmax(yhat[0])
print(predict_index, class_names, yhat[0])
prediction = class_names[predict_index]
percentage = '%.2f%%' % (percentage*100)
return prediction, percentage
app = Flask(__name__)
app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER
if(not os.path.exists(UPLOAD_FOLDER)):
os. makedirs(UPLOAD_FOLDER)
@app.route('/uploads/<name>')
def download_file(name):
return send_from_directory(app.config["UPLOAD_FOLDER"], name)
@app.route('/', methods=['GET', 'POST'])
def upload_file():
if request.method == 'POST':
# check if the post request has the file part
if 'file' not in request.files:
flash('No file part')
return redirect(request.url)
file = request.files['file']
# If the user does not select a file, the browser submits an
# empty file without a filename.
if file.filename == '':
return redirect(request.url)
if file and allowed_file(file.filename):
filename = secure_filename(file.filename)
file_path = os.path.join(app.config['UPLOAD_FOLDER'], filename)
file.save(file_path)
image = load_img(file_path, target_size=(28, 28), color_mode='grayscale')
image = preprocess_image(image)
prediction, percentage = predict_class(image)
return f'''
<!doctype html>
<html>
<head>
<title>Fashion Item Prediction</title>
<link rel="icon" type="image/ico" href="static/icon.ico">
<style>
.wrapper {{
position: absolute;
top: 0;
bottom: 0;
left: 0;
right: 0;
display: flex;
justify-content: center;
align-items: center;
}}
.panel {{
border-radius: 10px;
padding: 20px;
background-color: #ffc30033;
box-shadow: 1px 1px 7px #9e9e9e;
user-select: none;
}}
.back_btn {{
color: white;
text-decoration: none;
font-weight: bold;
border: 1px solid grey;
border-radius: 3px;
padding: 3px 5px;
background-color: #888;
box-shadow: 4px 2px 7px #482e2e;
}}
.pred {{
font-weight: bold;
}}
</style>
</head>
<body>
<div class="wrapper">
<div class="panel">
<div>
<span>My prediction is: <span class="pred">{prediction}</span></span> <span>(with {percentage} precision)</span>
</div>
<div style="margin-top: 20px;">
<a href="/" class="back_btn">Try Again</a>
</div>
</div>
</div>
</body>
</html>
'''
return f'''
<!doctype html>
<html>
<head>
<title>Predict Fashion Item</title>
<link rel="icon" type="image/ico" href="static/icon.ico">
<style>
.wrapper {{
position: absolute;
top: 0;
bottom: 0;
left: 0;
right: 0;
display: flex;
justify-content: center;
align-items: center;
}}
.panel {{
border-radius: 10px;
padding: 20px;
background-color: #ffc30033;
box-shadow: 1px 1px 7px #9e9e9e;
}}
h1 {{
margin: 0 0 20px 0;
color: #bb2020;
}}
</style>
</head>
<body class="wrapper">
<div class="panel">
<h1>Upload New Picture</h1>
<form method=post enctype=multipart/form-data>
<input type=file name=file accept=".png, .jpg, .jpeg, .gif">
<input type=submit value=Upload>
</form>
</div>
</body>
</html>
'''
app.run(host='0.0.0.0', debug=True)