forked from unnumsykar/cataract-sys
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
117 lines (90 loc) · 3.53 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
import os
import uuid
import urllib
from tensorflow.keras.models import load_model
from flask import Flask, render_template, request
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("Cataract-Model")
ALLOWED_EXT = set(['jpg', 'jpeg', 'png', 'jfif'])
def allowed_file(filename):
return '.' in filename and \
filename.rsplit('.', 1)[1] in ALLOWED_EXT
classes = ["cataract", "normal"]
def predict(filename , model):
img = load_img(filename , target_size = (224 , 224))
img = img_to_array(img)
img = img.reshape(1, 224, 224, 3)
img = img.astype('float32')
# img = img/255.0
result = model.predict(img)
dict_result = {}
for i in range(2):
dict_result[result[0][i]] = classes[i]
res = result[0]
res.sort()
res = res[::-1]
prob = res[:2]
prob_result = []
class_result = []
for i in range(2):
prob_result.append((prob[i]*100).round(2))
class_result.append(dict_result[prob[i]])
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],
"prob1": prob_result[0],
"prob2": prob_result[1],
}
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],
"prob1": prob_result[0],
"prob2": prob_result[1],
}
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)