-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapp.py
270 lines (220 loc) · 9.54 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
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
# Flask Application Script
''' This Application Script is created as a part of Mini-Project of 6th Semester
MVJ College of Engineering Under Visvesvaraya Technological University
Department of Electronics and Communication Engineering
Team Name - Team OneShot
1MJ18EC122 - Satyam Oza R
1MJ18EC123 - Shankar S'''
# Import necessary modules
import os
import cv2
import numpy as np
from time import time
from tensorflow.keras.models import load_model
from flask import Flask, render_template, request, session, redirect, url_for, flash
# Defining Path of Assets Folder
UPLOAD_FOLDER = './flask_app/assets/images'
ALLOWED_EXTENSIONS = set(['png', 'jpg', 'jpeg', 'gif'])
# Creating Application Object Using Flask
app = Flask(__name__, static_url_path='/assets', static_folder='./flask_app/assets', template_folder='./flask_app')
# Configuring the Assets Path
app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER
# Function to Set Cache-Configuration to 'no-cache' in our case
@app.after_request
def add_header(r):
r.headers["Cache-Control"] = "no-cache, no-store, must-revalidate"
r.headers["Pragma"] = "no-cache"
r.headers["Expires"] = "0"
r.headers['Cache-Control'] = 'public, max-age=0'
return r
# Routing default to 'index.html'
@app.route('/')
def root():
return render_template('index.html')
@app.route('/index.html')
def index():
return render_template('index.html')
@app.route('/contact.html')
def contact():
return render_template('contact.html')
@app.route('/about.html')
def about():
return render_template('about.html')
@app.route('/faqs.html')
def faqs():
return render_template('faqs.html')
@app.route('/prevention.html')
def prevention():
return render_template('prevention.html')
@app.route('/upload.html')
def upload():
return render_template('upload.html')
@app.route('/upload_chest.html')
def upload_chest():
return render_template('upload_chest.html')
@app.route('/upload_ct.html')
def upload_ct():
return render_template('upload_ct.html')
# When User Chooses the X-ray this method will be called
@app.route('/uploaded_chest', methods=['POST', 'GET'])
def uploaded_chest():
t_init = time()
if request.method == 'POST':
if 'file' not in request.files:
flash('No file part')
return redirect(request.url)
file = request.files['file']
if file.filename == '':
flash('No selected file')
return redirect(request.url)
if file:
file.save(os.path.join(
app.config['UPLOAD_FOLDER'], 'upload_chest.jpg'))
# Loading Models to the Runtime
inception_chest = load_model('inception_chest.h5')
resnet_chest = load_model('resnet_chest.h5')
vgg_chest = load_model('vgg_chest.h5')
xception_chest = load_model('xception_chest.h5')
# Converting Image to Processable format
img = cv2.imread('./flask_app/assets/images/upload_chest.jpg')
# Resizing the Input Image
cv2.resize(img, (224, 224))
# Performing the Image Processing Using OpenCV
transformed_chest = cv2.applyColorMap(img, cv2.COLORMAP_HSV)
transformed_chest = cv2.resize(transformed_chest, (224, 224))
image = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
image = cv2.resize(image, (224, 224))
image = np.array(image) / 255
image = np.expand_dims(image, axis=0)
gray_image = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
ret,thresh = cv2.threshold(gray_image,100,300,0)
contours,hierarchy = cv2.findContours(thresh,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)
contoured_chest = cv2.drawContours(img,contours,-1,(0,300,0),1)
contoured_chest = cv2.resize(contoured_chest, (224, 224))
os.remove('./flask_app/assets/images/transformed_chest.jpg')
os.remove('./flask_app/assets/images/contoured_chest.jpg')
cv2.imwrite('./flask_app/assets/images/transformed_chest.jpg', transformed_chest)
cv2.imwrite('./flask_app/assets/images/contoured_chest.jpg', contoured_chest)
# Predicting the Probability of the result case
inception_pred = inception_chest.predict(image)
probability = inception_pred[0]
print("Inception Predictions:")
if probability[0] > 0.5:
inception_chest_pred = str('%.2f' % (probability[0] * 100) + '% COVID POSITIVE')
else:
inception_chest_pred = str('%.2f' % ((1 - probability[0]) * 100) + '% COVID NEGATIVE')
print(inception_chest_pred)
resnet_pred = resnet_chest.predict(image)
probability = resnet_pred[0]
print("Resnet Predictions:")
if probability[0] > 0.5:
resnet_chest_pred = str('%.2f' % (probability[0] * 100) + '% COVID POSITIVE')
else:
resnet_chest_pred = str('%.2f' % ((1 - probability[0]) * 100) + '% COVID NEGATIVE')
print(resnet_chest_pred)
xception_pred = xception_chest.predict(image)
probability = xception_pred[0]
print("Xception Predictions:")
if probability[0] > 0.5:
xception_chest_pred = str('%.2f' % (probability[0] * 100) + '% COVID POSITIVE')
else:
xception_chest_pred = str(
'%.2f' % ((1 - probability[0]) * 100) + '% COVID NEGATIVE')
print(xception_chest_pred)
vgg_pred = vgg_chest.predict(image)
probability = vgg_pred[0]
print("VGG Predictions:")
if probability[0] > 0.5:
vgg_chest_pred = str('%.2f' % (probability[0] * 100) + '% COVID POSITIVE')
else:
vgg_chest_pred = str(
'%.2f' % ((1 - probability[0]) * 100) + '% COVID NEGATIVE')
print(vgg_chest_pred)
t_final = time()
elapsed_time = t_final - t_init
elapsed_min = int(elapsed_time // 60)
elapsed_sec = int(elapsed_time % 60)
return render_template('results_chest.html', resnet_chest_pred=resnet_chest_pred, vgg_chest_pred=vgg_chest_pred, inception_chest_pred=inception_chest_pred, xception_chest_pred=xception_chest_pred, elapsed_min=elapsed_min, elapsed_sec=elapsed_sec)
# When User Chooses the CT-scan this method will be called
@app.route('/uploaded_ct', methods=['POST', 'GET'])
def uploaded_ct():
t_init = time()
if request.method == 'POST':
if 'file' not in request.files:
flash('No file part')
return redirect(request.url)
file = request.files['file']
if file.filename == '':
flash('No selected file')
return redirect(request.url)
if file:
file.save(os.path.join(app.config['UPLOAD_FOLDER'], 'upload_ct.jpg'))
# Loading Models to the Runtime
resnet_ct = load_model('resnet_ct.h5')
vgg_ct = load_model('vgg_ct.h5')
inception_ct = load_model('inception_ct.h5')
xception_ct = load_model('xception_ct.h5')
# Converting Image to Processable format
img = cv2.imread('./flask_app/assets/images/upload_ct.jpg')
# Resizing the Input Image
cv2.resize(img, (224, 224))
# Performing the Image Processing Using OpenCV
transformed_ct = cv2.applyColorMap(img, cv2.COLORMAP_HSV)
transformed_ct = cv2.resize(transformed_ct, (224, 224))
image = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
image = cv2.resize(image, (224, 224))
image = np.array(image) / 255
image = np.expand_dims(image, axis=0)
gray_image = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
ret,thresh = cv2.threshold(gray_image,100,300,0)
contours,hierarchy = cv2.findContours(thresh,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)
contoured_ct = cv2.drawContours(img,contours,-1,(0,300,0),1)
contoured_ct = cv2.resize(contoured_ct, (224, 224))
os.remove('./flask_app/assets/images/transformed_ct.jpg')
os.remove('./flask_app/assets/images/contoured_ct.jpg')
cv2.imwrite('./flask_app/assets/images/transformed_ct.jpg', transformed_ct)
cv2.imwrite('./flask_app/assets/images/contoured_ct.jpg', contoured_ct)
# Predicting the Probability of the result case
resnet_pred = resnet_ct.predict(image)
probability = resnet_pred[0]
print("Resnet Predictions:")
if probability[0] > 0.5:
resnet_ct_pred = str('%.2f' % (probability[0] * 100) + '% COVID POSITIVE')
else:
resnet_ct_pred = str(
'%.2f' % ((1 - probability[0]) * 100) + '% COVID NEGATIVE')
print(resnet_ct_pred)
vgg_pred = vgg_ct.predict(image)
probability = vgg_pred[0]
print("VGG Predictions:")
if probability[0] > 0.5:
vgg_ct_pred = str('%.2f' % (probability[0] * 100) + '% COVID POSITIVE')
else:
vgg_ct_pred = str('%.2f' % ((1 - probability[0]) * 100) + '% COVID NEGATIVE')
print(vgg_ct_pred)
inception_pred = inception_ct.predict(image)
probability = inception_pred[0]
print("Inception Predictions:")
if probability[0] > 0.5:
inception_ct_pred = str('%.2f' % (probability[0] * 100) + '% COVID POSITIVE')
else:
inception_ct_pred = str(
'%.2f' % ((1 - probability[0]) * 100) + '% COVID NEGATIVE')
print(inception_ct_pred)
xception_pred = xception_ct.predict(image)
probability = xception_pred[0]
print("Xception Predictions:")
if probability[0] > 0.5:
xception_ct_pred = str('%.2f' % (probability[0] * 100) + '% COVID POSITIVE')
else:
xception_ct_pred = str(
'%.2f' % ((1 - probability[0]) * 100) + '% COVID NEGATIVE')
print(xception_ct_pred)
t_final = time()
elapsed_time = t_final - t_init
elapsed_min = int(elapsed_time // 60)
elapsed_sec = int(elapsed_time % 60)
return render_template('results_ct.html', resnet_ct_pred=resnet_ct_pred, vgg_ct_pred=vgg_ct_pred, inception_ct_pred=inception_ct_pred, xception_ct_pred=xception_ct_pred, elapsed_min=elapsed_min, elapsed_sec=elapsed_sec)
if __name__ == '__main__':
#app.run(host='0.0.0.0', debug=True, port=80)
app.run(host='0.0.0.0', port=8080)