-
Notifications
You must be signed in to change notification settings - Fork 121
/
app.py
288 lines (227 loc) · 8.82 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
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
#Important Modules
from flask import Flask,render_template, url_for ,flash , redirect
#from forms import RegistrationForm, LoginForm
from sklearn.externals import joblib
from flask import request
import numpy as np
import tensorflow
#from keras.layers import Conv2D, MaxPooling2D, Dense, Dropout, Input, Flatten, SeparableConv2D
#from flask_sqlalchemy import SQLAlchemy
#from model_class import DiabetesCheck, CancerCheck
#from tensorflow.keras.models import Sequential
#from tensorflow.keras.layers import Conv2D, MaxPooling2D, Dense, Dropout, Input, Flatten, SeparableConv2D
#from tensorflow.keras.layers import GlobalMaxPooling2D, Activation
#from tensorflow.keras.layers.normalization import BatchNormalization
#from tensorflow.keras.layers.merge import Concatenate
#from tensorflow.keras.models import Model
import os
from flask import send_from_directory
from tensorflow.keras.models import load_model
from tensorflow.keras.preprocessing import image
import tensorflow as tf
#from this import SQLAlchemy
app=Flask(__name__,template_folder='template')
# RELATED TO THE SQL DATABASE
app.config['SECRET_KEY'] = '5791628bb0b13ce0c676dfde280ba245'
#app.config["SQLALCHEMY_DATABASE_URI"] = "sqlite:///site.db"
#db=SQLAlchemy(app)
#from model import User,Post
#//////////////////////////////////////////////////////////
dir_path = os.path.dirname(os.path.realpath(__file__))
# UPLOAD_FOLDER = dir_path + '/uploads'
# STATIC_FOLDER = dir_path + '/static'
UPLOAD_FOLDER = 'uploads'
STATIC_FOLDER = 'static'
#graph = tf.get_default_graph()
#with graph.as_default():;
from tensorflow.keras.models import load_model
model = load_model('model111.h5')
model222=load_model("my_model.h5")
#FOR THE FIRST MODEL
# call model to predict an image
def api(full_path):
data = image.load_img(full_path, target_size=(50, 50, 3))
data = np.expand_dims(data, axis=0)
data = data * 1.0 / 255
#with graph.as_default():
predicted = model.predict(data)
return predicted
#FOR THE SECOND MODEL
def api1(full_path):
data = image.load_img(full_path, target_size=(64, 64, 3))
data = np.expand_dims(data, axis=0)
data = data * 1.0 / 255
#with graph.as_default():
predicted = model222.predict(data)
return predicted
# home page
#@app.route('/')
#def home():
# return render_template('index.html')
# procesing uploaded file and predict it
@app.route('/upload', methods=['POST','GET'])
def upload_file():
if request.method == 'GET':
return render_template('index.html')
else:
try:
file = request.files['image']
full_name = os.path.join(UPLOAD_FOLDER, file.filename)
file.save(full_name)
indices = {0: 'PARASITIC', 1: 'Uninfected', 2: 'Invasive carcinomar', 3: 'Normal'}
result = api(full_name)
print(result)
predicted_class = np.asscalar(np.argmax(result, axis=1))
accuracy = round(result[0][predicted_class] * 100, 2)
label = indices[predicted_class]
return render_template('predict.html', image_file_name = file.filename, label = label, accuracy = accuracy)
except:
flash("Please select the image first !!", "danger")
return redirect(url_for("Malaria"))
@app.route('/upload11', methods=['POST','GET'])
def upload11_file():
if request.method == 'GET':
return render_template('index2.html')
else:
try:
file = request.files['image']
full_name = os.path.join(UPLOAD_FOLDER, file.filename)
file.save(full_name)
indices = {0: 'Normal', 1: 'Pneumonia'}
result = api1(full_name)
if(result>50):
label= indices[1]
accuracy= result
else:
label= indices[0]
accuracy= 100-result
return render_template('predict1.html', image_file_name = file.filename, label = label, accuracy = accuracy)
except:
flash("Please select the image first !!", "danger")
return redirect(url_for("Pneumonia"))
@app.route('/uploads/<filename>')
def send_file(filename):
return send_from_directory(UPLOAD_FOLDER, filename)
#//////////////////////////////////////////////
#app.config["SQLALCHEMY_DATABASE_URI"] = "sqlite:///site.db"
#db=SQLAlchemy(app)
#class User(db.Model):
## username = db.Column(db.String(20), unique=True, nullable=False)
# email = db.Column(db.String(120), unique=True, nullable=False)
#image_file = db.Column(db.String(20), nullable=False, default='default.jpg')
# password = db.Column(db.String(60), nullable=False)
#posts = db.relationship('Post', backref='author', lazy=True)
#def __repr__(self):
# return f"User('{self.username}', '{self.email}', '{self.image_file}')"
@app.route("/")
@app.route("/home")
def home():
return render_template("home.html")
@app.route("/about")
def about():
return render_template("about.html")
@app.route("/cancer")
def cancer():
return render_template("cancer.html")
@app.route("/diabetes")
def diabetes():
#if form.validate_on_submit():
return render_template("diabetes.html")
@app.route("/heart")
def heart():
return render_template("heart.html")
@app.route("/liver")
def liver():
#if form.validate_on_submit():
return render_template("liver.html")
@app.route("/kidney")
def kidney():
#if form.validate_on_submit():
return render_template("kidney.html")
@app.route("/Malaria")
def Malaria():
return render_template("index.html")
@app.route("/Pneumonia")
def Pneumonia():
return render_template("index2.html")
"""
@app.route("/register", methods=["GET", "POST"])
def register():
form =RegistrationForm()
if form.validate_on_submit():
#flash("Account created for {form.username.data}!".format("success"))
flash("Account created","success")
return redirect(url_for("home"))
return render_template("register.html", title ="Register",form=form )
@app.route("/login", methods=["POST","GET"])
def login():
form =LoginForm()
if form.validate_on_submit():
#if form.email.data =="sho" and form.password.data=="password":
flash("You Have Logged in !","success")
return redirect(url_for("home"))
#else:
# flash("Login Unsuccessful. Please check username and password","danger")
return render_template("login.html", title ="Login",form=form )
def ValuePredictor1(to_predict_list):
to_predict = np.array(to_predict_list).reshape(1,30)
loaded_model = joblib.load("model")
result = loaded_model.predict(to_predict)
return result[0]
@app.route('/result1',methods = ["GET","POST"])
def result():
if request.method == 'POST':
to_predict_list = request.form.to_dict()
to_predict_list=list(to_predict_list.values())
to_predict_list = list(map(float, to_predict_list))
result = ValuePredictor(to_predict_list)
if int(result)==1:
prediction='cancer'
else:
prediction='Healthy'
return(render_template("result.html", prediction=prediction))"""
def ValuePredictor(to_predict_list, size):
to_predict = np.array(to_predict_list).reshape(1,size)
if(size==8):#Diabetes
loaded_model = joblib.load("model1")
result = loaded_model.predict(to_predict)
elif(size==30):#Cancer
loaded_model = joblib.load("model")
result = loaded_model.predict(to_predict)
elif(size==12):#Kidney
loaded_model = joblib.load("model3")
result = loaded_model.predict(to_predict)
elif(size==10):
loaded_model = joblib.load("model4")
result = loaded_model.predict(to_predict)
elif(size==11):#Heart
loaded_model = joblib.load("model2")
result =loaded_model.predict(to_predict)
return result[0]
@app.route('/result',methods = ["POST"])
def result():
if request.method == 'POST':
to_predict_list = request.form.to_dict()
to_predict_list=list(to_predict_list.values())
to_predict_list = list(map(float, to_predict_list))
if(len(to_predict_list)==30):#Cancer
result = ValuePredictor(to_predict_list,30)
elif(len(to_predict_list)==8):#Daiabtes
result = ValuePredictor(to_predict_list,8)
elif(len(to_predict_list)==12):
result = ValuePredictor(to_predict_list,12)
elif(len(to_predict_list)==11):
result = ValuePredictor(to_predict_list,11)
#if int(result)==1:
# prediction ='diabetes'
#else:
# prediction='Healthy'
elif(len(to_predict_list)==10):
result = ValuePredictor(to_predict_list,10)
if(int(result)==1):
prediction='Sorry ! Suffering'
else:
prediction='Congrats ! you are Healthy'
return(render_template("result.html", prediction=prediction))
if __name__ == "__main__":
app.run(debug=True)