-
Notifications
You must be signed in to change notification settings - Fork 80
/
app.py
201 lines (168 loc) · 6.93 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
# -*- coding: utf-8 -*-
"""
Created on Sat Mar 17 15:40:29 2018
@author: Kaushik
"""
from flask import Flask, render_template, request, session, redirect, url_for, flash
import os
from werkzeug.utils import secure_filename
from tensorflow.keras.models import load_model
import matplotlib.pyplot as plt
import cv2
import numpy as np
UPLOAD_FOLDER = './flask_app/assets/images'
ALLOWED_EXTENSIONS = set(['png', 'jpg', 'jpeg', 'gif'])
# Create Database if it doesnt exist
app = Flask(__name__,static_url_path='/assets',
static_folder='./flask_app/assets',
template_folder='./flask_app')
app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER
@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('/news.html')
def news():
return render_template('news.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')
@app.route('/uploaded_chest', methods = ['POST', 'GET'])
def uploaded_chest():
if request.method == 'POST':
# check if the post request has the file part
if 'file' not in request.files:
flash('No file part')
return redirect(request.url)
file = request.files['file']
# if user does not select file, browser also
# submit a empty part without filename
if file.filename == '':
flash('No selected file')
return redirect(request.url)
if file:
# filename = secure_filename(file.filename)
file.save(os.path.join(app.config['UPLOAD_FOLDER'], 'upload_chest.jpg'))
resnet_chest = load_model('models/resnet50.h5')
vgg_chest = load_model('models/resnet50.h5')
inception_chest = load_model('models/inceptionv3_chest.h5')
xception_chest = load_model('models/xception_chest.h5')
image = cv2.imread('./flask_app/assets/images/upload_chest.jpg') # read file
image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB) # arrange format as per keras
image = cv2.resize(image,(224,224))
image = np.array(image) / 255
image = np.expand_dims(image, axis=0)
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')
else:
resnet_chest_pred = str('%.2f' % ((1-probability[0])*100) + '% NonCOVID')
print(resnet_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')
else:
vgg_chest_pred = str('%.2f' % ((1-probability[0])*100) + '% NonCOVID')
print(vgg_chest_pred)
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')
else:
inception_chest_pred = str('%.2f' % ((1-probability[0])*100) + '% NonCOVID')
print(inception_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')
else:
xception_chest_pred = str('%.2f' % ((1-probability[0])*100) + '% NonCOVID')
print(xception_chest_pred)
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)
@app.route('/uploaded_ct', methods = ['POST', 'GET'])
def uploaded_ct():
if request.method == 'POST':
# check if the post request has the file part
if 'file' not in request.files:
flash('No file part')
return redirect(request.url)
file = request.files['file']
# if user does not select file, browser also
# submit a empty part without filename
if file.filename == '':
flash('No selected file')
return redirect(request.url)
if file:
# filename = secure_filename(file.filename)
file.save(os.path.join(app.config['UPLOAD_FOLDER'], 'upload_ct.jpg'))
resnet_ct = load_model('models/resnet_ct.h5')
vgg_ct = load_model('models/vgg_ct.h5')
inception_ct = load_model('models/inception_ct.h5')
xception_ct = load_model('models/xception_ct.h5')
image = cv2.imread('./flask_app/assets/images/upload_ct.jpg') # read file
image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB) # arrange format as per keras
image = cv2.resize(image,(224,224))
image = np.array(image) / 255
image = np.expand_dims(image, axis=0)
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')
else:
resnet_ct_pred = str('%.2f' % ((1-probability[0])*100) + '% NonCOVID')
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')
else:
vgg_ct_pred = str('%.2f' % ((1-probability[0])*100) + '% NonCOVID')
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')
else:
inception_ct_pred = str('%.2f' % ((1-probability[0])*100) + '% NonCOVID')
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')
else:
xception_ct_pred = str('%.2f' % ((1-probability[0])*100) + '% NonCOVID')
print(xception_ct_pred)
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)
# if __name__ == '__main__':
# app.secret_key = ".."
# app.run()