-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
179 lines (151 loc) · 6.26 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
import os
from flask import Flask, render_template, request
import pickle
import numpy as np
from PIL import Image
import tensorflow as tf
from tensorflow.keras.models import load_model
from werkzeug.utils import secure_filename
app = Flask(__name__)
def predict(values, dic):
# diabetes
if len(values) == 8:
dic2 = {'NewBMI_Obesity 1': 0, 'NewBMI_Obesity 2': 0, 'NewBMI_Obesity 3': 0, 'NewBMI_Overweight': 0,
'NewBMI_Underweight': 0, 'NewInsulinScore_Normal': 0, 'NewGlucose_Low': 0,
'NewGlucose_Normal': 0, 'NewGlucose_Overweight': 0, 'NewGlucose_Secret': 0}
if dic['BMI'] <= 18.5:
dic2['NewBMI_Underweight'] = 1
elif 18.5 < dic['BMI'] <= 24.9:
pass
elif 24.9 < dic['BMI'] <= 29.9:
dic2['NewBMI_Overweight'] = 1
elif 29.9 < dic['BMI'] <= 34.9:
dic2['NewBMI_Obesity 1'] = 1
elif 34.9 < dic['BMI'] <= 39.9:
dic2['NewBMI_Obesity 2'] = 1
elif dic['BMI'] > 39.9:
dic2['NewBMI_Obesity 3'] = 1
if 16 <= dic['Insulin'] <= 166:
dic2['NewInsulinScore_Normal'] = 1
if dic['Glucose'] <= 70:
dic2['NewGlucose_Low'] = 1
elif 70 < dic['Glucose'] <= 99:
dic2['NewGlucose_Normal'] = 1
elif 99 < dic['Glucose'] <= 126:
dic2['NewGlucose_Overweight'] = 1
elif dic['Glucose'] > 126:
dic2['NewGlucose_Secret'] = 1
dic.update(dic2)
values2 = list(map(float, list(dic.values())))
model = pickle.load(open('models/diabetes_rf_model.pkl', 'rb'))
values = np.asarray(values2)
return model.predict(values.reshape(1, -1))[0]
# breast_cancer
elif len(values) == 22:
model = pickle.load(open('models/breast_cancer.pkl', 'rb'))
values = np.asarray(values)
return model.predict(values.reshape(1, -1))[0]
# heart disease
elif len(values) == 13:
model = pickle.load(open('models/heart_rf_model.pkl', 'rb'))
values = np.asarray(values)
return model.predict(values.reshape(1, -1))[0]
# kidney disease
elif len(values) == 24:
model = pickle.load(open('models/kidney_disease.pkl', 'rb'))
values = np.asarray(values)
return model.predict(values.reshape(1, -1))[0]
# liver disease
elif len(values) == 10:
model = pickle.load(open('models/liver.pkl', 'rb'))
values = np.asarray(values)
return model.predict(values.reshape(1, -1))[0]
@app.route("/")
def home():
return render_template('home.html')
@app.route("/diabetes", methods=['GET', 'POST'])
def diabetesPage():
return render_template('diabetes.html')
@app.route("/cancer", methods=['GET', 'POST'])
def cancerPage():
return render_template('breast_cancer.html')
@app.route("/heart", methods=['GET', 'POST'])
def heartPage():
return render_template('heart.html')
@app.route("/kidney", methods=['GET', 'POST'])
def kidneyPage():
return render_template('kidney.html')
@app.route("/liver", methods=['GET', 'POST'])
def liverPage():
return render_template('liver.html')
@app.route("/malaria", methods=['GET', 'POST'])
def malariaPage():
return render_template('malaria.html')
@app.route("/pneumonia", methods=['GET', 'POST'])
def pneumoniaPage():
return render_template('pneumonia.html')
@app.route("/predict", methods=['POST', 'GET'])
def predictPage():
try:
if request.method == 'POST':
to_predict_dict = request.form.to_dict()
for key, value in to_predict_dict.items():
try:
to_predict_dict[key] = int(value)
except ValueError:
to_predict_dict[key] = float(value)
to_predict_list = list(map(float, list(to_predict_dict.values())))
print(to_predict_dict)
print(to_predict_list)
pred = predict(to_predict_list, to_predict_dict)
return render_template('predict.html', pred=pred)
except Exception as e:
message = f"Error: {e}"
return render_template("home.html", message=message)
@app.route("/malariapredict", methods=['POST', 'GET'])
def malariapredictPage():
if request.method == 'POST':
try:
# img = Image.open(request.files['image'])
# img_path = os.path.join('uploads', 'malaria_image.jpg')
# img.save(img_path)
img_file = request.files['image']
filename = secure_filename(img_file.filename)
img_path = os.path.join('uploads', filename)
img_file.save(img_path)
# Log the file path for debugging
print(f"Image saved to {img_path}")
img = tf.keras.utils.load_img(img_path, target_size=(150, 150)) # Update target_size to (150, 150)
img = tf.keras.utils.img_to_array(img)
img = np.expand_dims(img, axis=0)
print("load_model")
model = load_model("models/malaria_detect.h5")
print("predict")
pred = np.argmax(model.predict(img))
os.remove(img_path)
return render_template('malaria_predict.html', pred=pred)
except Exception as e:
message = f"Error: {e}"
return render_template('malaria.html', message=message)
return render_template('malaria.html')
@app.route("/pneumoniapredict", methods=['POST', 'GET'])
def pneumoniapredictPage():
if request.method == 'POST':
try:
img = Image.open(request.files['image']).convert('L')
img_path = os.path.join('uploads', 'pneumonia_image.jpg')
img.save(img_path)
img = tf.keras.utils.load_img(img_path, target_size=(298, 298)) # Resize image to match model's expected input
img = tf.keras.utils.img_to_array(img)
img = np.expand_dims(img, axis=0)
model = load_model("models/pneumonia.h5")
pred = np.argmax(model.predict(img))
return render_template('pneumonia_predict.html', pred=pred)
except Exception as e:
message = f"Error: {e}"
return render_template('pneumonia.html', message=message)
return render_template('pneumonia.html')
if __name__ == '__main__':
if not os.path.exists('uploads'):
os.makedirs('uploads')
app.run(debug=True)