-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
126 lines (96 loc) · 4.75 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
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
import os
import uuid
import urllib
from PIL import Image
from tensorflow.keras.models import load_model
from flask import Flask, render_template, request, send_file
from tensorflow.keras.preprocessing.image import load_img, img_to_array
app = Flask(__name__)
BASE_DIR = os.path.dirname(os.path.abspath(__file__))
model = load_model(os.path.join(BASE_DIR, 'model.h5'))
ALLOWED_EXT = set(['jpg', 'jpeg', 'png', 'jfif'])
def allowed_file(filename):
return '.' in filename and \
filename.rsplit('.', 1)[1] in ALLOWED_EXT
classes = ['ace of clubs', 'ace of diamonds', 'ace of hearts', 'ace of spades', 'eight of clubs', 'eight of diamonds', 'eight of hearts', 'eight of spades', 'five of clubs', 'five of diamonds', 'five of hearts', 'five of spades', 'four of clubs', 'four of diamonds', 'four of hearts', 'four of spades', 'jack of clubs', 'jack of diamonds', 'jack of hearts', 'jack of spades', 'king of clubs', 'king of diamonds', 'king of hearts', 'king of spades', 'nine of clubs', 'nine of diamonds', 'nine of hearts', 'nine of spades', 'queen of clubs', 'queen of diamonds', 'queen of hearts', 'queen of spades', 'seven of clubs', 'seven of diamonds', 'seven of hearts', 'seven of spades', 'six of clubs', 'six of diamonds', 'six of hearts', 'six of spades', 'ten of clubs', 'ten of diamonds', 'ten of hearts', 'ten of spades', 'three of clubs', 'three of diamonds', 'three of hearts', 'three of spades', 'two of clubs', 'two of diamonds', 'two of hearts', 'two of spades']
def predict(filename, model):
img = load_img(filename, target_size=(180, 180))
img = img_to_array(img)
img = img.reshape(1, 180, 180, 3)
img = img.astype('float32')
img = img / 255.0
result = model.predict(img)
dict_result = {}
for i in range(52):
dict_result[result[0][i]] = classes[i]
res = result[0]
res.sort()
res = res[::-1]
prob = res[:3]
prob_result = []
class_result = []
for i in range(3):
prob_result.append((prob[i] * 100).round(2))
class_result.append(dict_result[prob[i]])
print(class_result,prob_result)
return class_result, prob_result
@app.route('/')
def home():
return render_template("index.html")
@app.route('/success', methods=['GET', 'POST'])
def success():
error = ''
target_img = os.path.join(os.getcwd(), 'static/images')
if request.method == 'POST':
if (request.form):
link = request.form.get('link')
try:
resource = urllib.request.urlopen(link)
unique_filename = str(uuid.uuid4())
filename = unique_filename + ".jpg"
img_path = os.path.join(target_img, filename)
output = open(img_path, "wb")
output.write(resource.read())
output.close()
img = filename
class_result, prob_result = predict(img_path, model)
predictions = {
"class1": class_result[0],
"class2": class_result[1],
"class3": class_result[2],
"prob1": prob_result[0],
"prob2": prob_result[1],
"prob3": prob_result[2],
}
except Exception as e:
print(str(e))
error = 'This image from this site is not accesible or inappropriate input'
if (len(error) == 0):
return render_template('success.html', img=img, predictions=predictions)
else:
return render_template('index.html', error=error)
elif (request.files):
file = request.files['file']
if file and allowed_file(file.filename):
file.save(os.path.join(target_img, file.filename))
img_path = os.path.join(target_img, file.filename)
img = file.filename
class_result, prob_result = predict(img_path, model)
predictions = {
"class1": class_result[0],
"class2": class_result[1],
"class3": class_result[2],
"prob1": prob_result[0],
"prob2": prob_result[1],
"prob3": prob_result[2],
}
else:
error = "Please upload images of jpg , jpeg and png extension only"
if (len(error) == 0):
return render_template('success.html', img=img, predictions=predictions)
else:
return render_template('index.html', error=error)
else:
return render_template('index.html')
if __name__ == "__main__":
app.run(debug=True)