|
from PIL import Image |
|
import os |
|
import numpy as np |
|
import tensorflow as tf |
|
|
|
|
|
data = "DATA" |
|
|
|
exit() |
|
|
|
|
|
def predict(values, dic): |
|
|
|
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.pkl','rb')) |
|
values = np.asarray(values2) |
|
return model.predict(values.reshape(1, -1))[0] |
|
|
|
|
|
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] |
|
|
|
|
|
elif len(values) == 13: |
|
model = pickle.load(open('models/heart.pkl','rb')) |
|
values = np.asarray(values) |
|
return model.predict(values.reshape(1, -1))[0] |
|
|
|
|
|
elif len(values) == 24: |
|
model = pickle.load(open('models/kidney.pkl','rb')) |
|
values = np.asarray(values) |
|
return model.predict(values.reshape(1, -1))[0] |
|
|
|
|
|
elif len(values) == 10: |
|
model = pickle.load(open('models/liver.pkl','rb')) |
|
values = np.asarray(values) |
|
return model.predict(values.reshape(1, -1))[0] |
|
|
|
|
|
def predict_api(): |
|
try: |
|
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()))) |
|
pred = predict(to_predict_list, to_predict_dict) |
|
|
|
return {'prediction': pred} |
|
|
|
except Exception as e: |
|
return {'error': str(e)} |
|
|
|
|
|
def malariapredict_api(): |
|
try: |
|
img = Image.open(request.files['image']) |
|
img.save("uploads/image.jpg") |
|
img_path = os.path.join(os.path.dirname(__file__), 'uploads/image.jpg') |
|
os.path.isfile(img_path) |
|
img = tf.keras.utils.load_img(img_path, target_size=(128, 128)) |
|
img = tf.keras.utils.img_to_array(img) |
|
img = np.expand_dims(img, axis=0) |
|
|
|
model = tf.keras.models.load_model("models/malaria.h5") |
|
pred = np.argmax(model.predict(img)) |
|
|
|
return {'prediction': int(pred)} |
|
|
|
except Exception as e: |
|
return {'error': str(e)} |
|
|
|
|
|
def pneumoniapredict_api(): |
|
try: |
|
img = Image.open(request.files['image']).convert('L') |
|
img.save("uploads/image.jpg") |
|
img_path = os.path.join(os.path.dirname(__file__), 'uploads/image.jpg') |
|
os.path.isfile(img_path) |
|
img = tf.keras.utils.load_img(img_path, target_size=(128, 128)) |
|
img = tf.keras.utils.img_to_array(img) |
|
img = np.expand_dims(img, axis=0) |
|
|
|
model = tf.keras.models.load_model("models/pneumonia.h5") |
|
pred = np.argmax(model.predict(img)) |
|
|
|
return {'prediction': int(pred)} |
|
|
|
except Exception as e: |
|
return {'error': str(e)} |
|
|