-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathapp.py
executable file
·249 lines (203 loc) · 9.03 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
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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
from flask import Flask, flash, request, redirect, url_for, render_template
import urllib.request
import os
from werkzeug.utils import secure_filename
import cv2
import pickle
import imutils
import sklearn
from tensorflow.keras.models import load_model
import joblib
import numpy as np
from tensorflow.keras.applications.vgg16 import preprocess_input
# Loading Models
covid_model = load_model('models/covid.h5')
breastcancer_model = joblib.load('models/breast_cancer_model.pkl')
alzheimer_model = load_model('models/alzheimer_model.h5')
pneumonia_model = load_model('models/pneumonia_model.h5')
# Configuring Flask
UPLOAD_FOLDER = 'static/uploads'
ALLOWED_EXTENSIONS = set(['png', 'jpg', 'jpeg'])
app = Flask(__name__)
app.config['SEND_FILE_MAX_AGE_DEFAULT'] = 0
app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER
app.secret_key = "secret key"
def allowed_file(filename):
return '.' in filename and filename.rsplit('.', 1)[1] in ALLOWED_EXTENSIONS
############################################# BRAIN TUMOR FUNCTIONS ################################################
def preprocess_imgs(set_name, img_size):
"""
Resize and apply VGG-15 preprocessing
"""
set_new = []
for img in set_name:
img = cv2.resize(img,dsize=img_size,interpolation=cv2.INTER_CUBIC)
set_new.append(preprocess_input(img))
return np.array(set_new)
def crop_imgs(set_name, add_pixels_value=0):
"""
Finds the extreme points on the image and crops the rectangular out of them
"""
set_new = []
for img in set_name:
gray = cv2.cvtColor(img, cv2.COLOR_RGB2GRAY)
gray = cv2.GaussianBlur(gray, (5, 5), 0)
# threshold the image, then perform a series of erosions +
# dilations to remove any small regions of noise
thresh = cv2.threshold(gray, 45, 255, cv2.THRESH_BINARY)[1]
thresh = cv2.erode(thresh, None, iterations=2)
thresh = cv2.dilate(thresh, None, iterations=2)
# find contours in thresholded image, then grab the largest one
cnts = cv2.findContours(
thresh.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
cnts = imutils.grab_contours(cnts)
c = max(cnts, key=cv2.contourArea)
# find the extreme points
extLeft = tuple(c[c[:, :, 0].argmin()][0])
extRight = tuple(c[c[:, :, 0].argmax()][0])
extTop = tuple(c[c[:, :, 1].argmin()][0])
extBot = tuple(c[c[:, :, 1].argmax()][0])
ADD_PIXELS = add_pixels_value
new_img = img[extTop[1]-ADD_PIXELS:extBot[1]+ADD_PIXELS,
extLeft[0]-ADD_PIXELS:extRight[0]+ADD_PIXELS].copy()
set_new.append(new_img)
return np.array(set_new)
########################### Routing Functions ########################################
@app.route('/')
def home():
return render_template('homepage.html')
@app.route('/covid.html')
def covid():
return render_template('covid.html')
@app.route('/services.html')
def services():
return render_template('services.html')
@app.route('/breastcancer.html')
def brain_tumor():
return render_template('breastcancer.html')
@app.route('/contact.html')
def contact():
return render_template('contact.html')
@app.route('/alzheimer.html')
def alzheimer():
return render_template('alzheimer.html')
@app.route('/pneumonia.html')
def pneumonia():
return render_template('pneumonia.html')
@app.route('/about.html')
def about():
return render_template('about.html')
########################### Result Functions ########################################
@app.route('/resultc', methods=['POST'])
def resultc():
if request.method == 'POST':
firstname = request.form['firstname']
lastname = request.form['lastname']
email = request.form['email']
phone = request.form['phone']
gender = request.form['gender']
age = request.form['age']
file = request.files['file']
if file and allowed_file(file.filename):
filename = secure_filename(file.filename)
file.save(os.path.join(app.config['UPLOAD_FOLDER'], filename))
flash('Image successfully uploaded and displayed below')
img = cv2.imread('static/uploads/'+filename)
img = cv2.resize(img, (224, 224))
img = img.reshape(1, 224, 224, 3)
img = img/255.0
pred = covid_model.predict(img)
if pred < 0.5:
pred = 0
else:
pred = 1
# pb.push_sms(pb.devices[0],str(phone), 'Hello {},\nYour COVID-19 test results are ready.\nRESULT: {}'.format(firstname,['POSITIVE','NEGATIVE'][pred]))
return render_template('resultc.html', filename=filename, fn=firstname, ln=lastname, age=age, r=pred, gender=gender)
else:
flash('Allowed image types are - png, jpg, jpeg')
return redirect(request.url)
@app.route('/resultbc', methods=['POST'])
def resultbc():
if request.method == 'POST':
firstname = request.form['firstname']
lastname = request.form['lastname']
email = request.form['email']
phone = request.form['phone']
gender = request.form['gender']
age = request.form['age']
cpm = request.form['concave_points_mean']
am = request.form['area_mean']
rm = request.form['radius_mean']
pm = request.form['perimeter_mean']
cm = request.form['concavity_mean']
pred = breastcancer_model.predict(
np.array([cpm, am, rm, pm, cm]).reshape(1, -1))
# pb.push_sms(pb.devices[0],str(phone), 'Hello {},\nYour Breast Cancer test results are ready.\nRESULT: {}'.format(firstname,['NEGATIVE','POSITIVE'][pred]))
return render_template('resultbc.html', fn=firstname, ln=lastname, age=age, r=pred, gender=gender)
@app.route('/resulta', methods=['GET', 'POST'])
def resulta():
if request.method == 'POST':
print(request.url)
firstname = request.form['firstname']
lastname = request.form['lastname']
email = request.form['email']
phone = request.form['phone']
gender = request.form['gender']
age = request.form['age']
file = request.files['file']
if file and allowed_file(file.filename):
filename = secure_filename(file.filename)
file.save(os.path.join(app.config['UPLOAD_FOLDER'], filename))
flash('Image successfully uploaded and displayed below')
img = cv2.imread('static/uploads/'+filename)
img = cv2.resize(img, (176, 176))
img = img.reshape(1, 176, 176, 3)
img = img/255.0
pred = alzheimer_model.predict(img)
pred = pred[0].argmax()
print(pred)
# pb.push_sms(pb.devices[0],str(phone), 'Hello {},\nYour Alzheimer test results are ready.\nRESULT: {}'.format(firstname,['NonDemented','VeryMildDemented','MildDemented','ModerateDemented'][pred]))
return render_template('resulta.html', filename=filename, fn=firstname, ln=lastname, age=age, r=0, gender=gender)
else:
flash('Allowed image types are - png, jpg, jpeg')
return redirect('/')
@app.route('/resultp', methods=['POST'])
def resultp():
if request.method == 'POST':
firstname = request.form['firstname']
lastname = request.form['lastname']
email = request.form['email']
phone = request.form['phone']
gender = request.form['gender']
age = request.form['age']
file = request.files['file']
if file and allowed_file(file.filename):
filename = secure_filename(file.filename)
file.save(os.path.join(app.config['UPLOAD_FOLDER'], filename))
flash('Image successfully uploaded and displayed below')
img = cv2.imread('static/uploads/'+filename)
img = cv2.resize(img, (150, 150))
img = img.reshape(1, 150, 150, 3)
img = img/255.0
pred = pneumonia_model.predict(img)
if pred < 0.5:
pred = 0
else:
pred = 1
# pb.push_sms(pb.devices[0],str(phone), 'Hello {},\nYour COVID-19 test results are ready.\nRESULT: {}'.format(firstname,['POSITIVE','NEGATIVE'][pred]))
return render_template('resultp.html', filename=filename, fn=firstname, ln=lastname, age=age, r=pred, gender=gender)
else:
flash('Allowed image types are - png, jpg, jpeg')
return redirect(request.url)
# No caching at all for API endpoints.
@app.after_request
def add_header(response):
"""
Add headers to both force latest IE rendering engine or Chrome Frame,
and also to cache the rendered page for 10 minutes.
"""
response.headers['X-UA-Compatible'] = 'IE=Edge,chrome=1'
response.headers['Cache-Control'] = 'public, max-age=0'
return response
if __name__ == '__main__':
app.run(debug=True)