-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
93 lines (76 loc) · 3 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
85
86
87
88
89
90
91
92
from __future__ import division, print_function
# coding=utf-8
import os
import sys
import datetime
import glob
# Flask utils
import flask
from flask import Flask, redirect, url_for, request, render_template
from werkzeug.utils import secure_filename
from gevent.pywsgi import WSGIServer
# import keras
from keras.models import load_model, Model
import numpy as np
# from scipy.misc import imread, imresize
from imageio import imread
from skimage.transform import resize
# Define a flask app
app = Flask(__name__)
# Model saved with Keras model.save()
#MODEL_PATH = 'D:\IIITB\Rise Courses\Deployment in Machine Learning\docker-gesture-recognition\models\Colab.h5'
MODEL_PATH = r"D:\IIITB\Rise Courses\Deployment in Machine Learning\docker-gesture-recognition\models\Colab.h5"
gesture_class = ["Left Swipe", "Right Swipe", "Stop", "Thumbs Down", "Thumbs Up"]
def generator(folder_path):
print('folder_path :', folder_path)
img_idx = [0, 1, 2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 22, 24, 26, 27, 28, 29]
while True:
batch_data = np.zeros((1, 18, 84, 84, 3))
batch_labels = np.zeros((1, 5))
imgs = os.listdir(folder_path)
for idx, item in enumerate(img_idx):
image = imread(folder_path + '/' + imgs[item]).astype(np.float32)
if image.shape[1] == 160:
image = resize(image[:, 20:140, :], (84, 84)).astype(np.float32)
else:
image = resize(image, (84, 84)).astype(np.float32)
batch_data[0, idx, :, :, 0] = image[:, :, 0] - 104
batch_data[0, idx, :, :, 1] = image[:, :, 1] - 117
batch_data[0, idx, :, :, 2] = image[:, :, 2] - 123
yield batch_data
@app.route('/', methods=['GET'])
def index():
# Main page
return render_template('index.html')
@app.route('/predict', methods=['GET', 'POST'])
def upload():
if request.method == 'POST':
# Get the file from post request
all_files = flask.request.files.getlist("file[]")
# Save the file to ./uploads
basepath = os.path.dirname(__file__)
del_dir = os.path.join(basepath, 'uploads')
del_files = glob.glob(del_dir+"/*")
if os.path.exists(del_dir):
for df in del_files:
os.remove(df)
if not os.path.exists(del_dir):
os.makedirs(del_dir)
for f in all_files :
file_path = os.path.join(
basepath, 'uploads', secure_filename(f.filename))
f.save(file_path)
print("All Files Uploaded")
test_generator = generator(del_dir)
x = test_generator.__next__()
print("x :", x.shape)
# Load your trained model
model = load_model(MODEL_PATH)
model_func = Model(inputs=[model.input], outputs=[model.output])
pred_idx = np.argmax(model_func.predict_on_batch(x), axis=1)
print(" pred :", pred_idx)
result = gesture_class[pred_idx[0]]
return result
if __name__ == '__main__':
app.debug = True
app.run(host='0.0.0.0', port=5000)