Update app.py
Browse files
app.py
CHANGED
|
@@ -1,73 +1,132 @@
|
|
| 1 |
-
import os
|
| 2 |
-
import numpy as np
|
| 3 |
-
from PIL import Image
|
| 4 |
-
import cv2
|
| 5 |
-
from flask import Flask, request, render_template
|
| 6 |
-
from werkzeug.utils import secure_filename
|
| 7 |
-
from tensorflow.keras.models import load_model
|
| 8 |
-
from gradcam_utils import generate_and_merge_heatmaps
|
| 9 |
-
|
| 10 |
-
app = Flask(__name__)
|
| 11 |
-
UPLOAD_FOLDER = 'static/uploads'
|
| 12 |
-
HEATMAP_PATH = 'static/heatmap.jpg'
|
| 13 |
-
os.makedirs(UPLOAD_FOLDER, exist_ok=True)
|
| 14 |
-
app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER
|
| 15 |
-
|
| 16 |
-
# Load your trained ensemble model
|
| 17 |
-
model = load_model('ensemble_model_best(92.3).h5')
|
| 18 |
-
|
| 19 |
-
# Load the three base models (if required for gradcam)
|
| 20 |
-
from models import create_vgg19_model, create_efficientnet_model, create_densenet_model
|
| 21 |
-
vgg_model = create_vgg19_model()
|
| 22 |
-
efficientnet_model = create_efficientnet_model()
|
| 23 |
-
densenet_model = create_densenet_model()
|
| 24 |
-
|
| 25 |
-
print('Model loaded. Visit http://127.0.0.1:5000/')
|
| 26 |
-
|
| 27 |
-
def get_className(classNo):
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
def getResult(img_path):
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
@app.route('/', methods=['GET'])
|
| 41 |
-
def index():
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
@app.route('/predict', methods=['POST'])
|
| 45 |
-
def upload():
|
| 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 |
-
if __name__ == '__main__':
|
| 73 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# import os
|
| 2 |
+
# import numpy as np
|
| 3 |
+
# from PIL import Image
|
| 4 |
+
# import cv2
|
| 5 |
+
# from flask import Flask, request, render_template
|
| 6 |
+
# from werkzeug.utils import secure_filename
|
| 7 |
+
# from tensorflow.keras.models import load_model
|
| 8 |
+
# from gradcam_utils import generate_and_merge_heatmaps
|
| 9 |
+
|
| 10 |
+
# app = Flask(__name__)
|
| 11 |
+
# UPLOAD_FOLDER = 'static/uploads'
|
| 12 |
+
# HEATMAP_PATH = 'static/heatmap.jpg'
|
| 13 |
+
# os.makedirs(UPLOAD_FOLDER, exist_ok=True)
|
| 14 |
+
# app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER
|
| 15 |
+
|
| 16 |
+
# # Load your trained ensemble model
|
| 17 |
+
# model = load_model('ensemble_model_best(92.3).h5')
|
| 18 |
+
|
| 19 |
+
# # Load the three base models (if required for gradcam)
|
| 20 |
+
# from models import create_vgg19_model, create_efficientnet_model, create_densenet_model
|
| 21 |
+
# vgg_model = create_vgg19_model()
|
| 22 |
+
# efficientnet_model = create_efficientnet_model()
|
| 23 |
+
# densenet_model = create_densenet_model()
|
| 24 |
+
|
| 25 |
+
# print('Model loaded. Visit http://127.0.0.1:5000/')
|
| 26 |
+
|
| 27 |
+
# def get_className(classNo):
|
| 28 |
+
# return "Normal" if classNo == 0 else "Pneumonia"
|
| 29 |
+
|
| 30 |
+
# def getResult(img_path):
|
| 31 |
+
# image = cv2.imread(img_path)
|
| 32 |
+
# image = Image.fromarray(image, 'RGB')
|
| 33 |
+
# image = image.resize((224, 224))
|
| 34 |
+
# image = np.array(image)
|
| 35 |
+
# input_img = np.expand_dims(image, axis=0) / 255.0
|
| 36 |
+
# result = model.predict(input_img)
|
| 37 |
+
# result01 = np.argmax(result, axis=1)
|
| 38 |
+
# return result01
|
| 39 |
+
|
| 40 |
+
# @app.route('/', methods=['GET'])
|
| 41 |
+
# def index():
|
| 42 |
+
# return render_template('index.html')
|
| 43 |
+
|
| 44 |
+
# @app.route('/predict', methods=['POST'])
|
| 45 |
+
# def upload():
|
| 46 |
+
# if request.method == 'POST':
|
| 47 |
+
# f = request.files['file']
|
| 48 |
+
# filename = secure_filename(f.filename)
|
| 49 |
+
# file_path = os.path.join(app.config['UPLOAD_FOLDER'], filename)
|
| 50 |
+
# f.save(file_path)
|
| 51 |
+
|
| 52 |
+
# # Get prediction
|
| 53 |
+
# value = getResult(file_path)
|
| 54 |
+
# result = get_className(value[0])
|
| 55 |
+
|
| 56 |
+
# # Generate Grad-CAM heatmap
|
| 57 |
+
# heatmap_img = generate_and_merge_heatmaps(
|
| 58 |
+
# file_path, vgg_model, efficientnet_model, densenet_model
|
| 59 |
+
# )
|
| 60 |
+
|
| 61 |
+
# # Save heatmap image
|
| 62 |
+
# cv2.imwrite(HEATMAP_PATH, cv2.cvtColor(heatmap_img, cv2.COLOR_RGB2BGR))
|
| 63 |
+
|
| 64 |
+
# return render_template(
|
| 65 |
+
# 'result.html',
|
| 66 |
+
# prediction=result,
|
| 67 |
+
# original_image=file_path,
|
| 68 |
+
# heatmap_image=HEATMAP_PATH
|
| 69 |
+
# )
|
| 70 |
+
# return None
|
| 71 |
+
|
| 72 |
+
# if __name__ == '__main__':
|
| 73 |
+
# app.run(host='0.0.0.0', port=5000, debug=True)
|
| 74 |
+
|
| 75 |
+
|
| 76 |
+
|
| 77 |
+
|
| 78 |
+
|
| 79 |
+
|
| 80 |
+
import gradio as gr
|
| 81 |
+
import numpy as np
|
| 82 |
+
import cv2
|
| 83 |
+
from PIL import Image
|
| 84 |
+
from tensorflow.keras.models import load_model
|
| 85 |
+
from models import create_vgg19_model, create_efficientnet_model, create_densenet_model
|
| 86 |
+
from gradcam_utils import generate_and_merge_heatmaps
|
| 87 |
+
|
| 88 |
+
# Load models
|
| 89 |
+
ensemble_model = load_model("ensemble_model_best(92.3).h5")
|
| 90 |
+
vgg_model = create_vgg19_model()
|
| 91 |
+
efficientnet_model = create_efficientnet_model()
|
| 92 |
+
densenet_model = create_densenet_model()
|
| 93 |
+
|
| 94 |
+
def get_class_name(class_id):
|
| 95 |
+
return "Normal" if class_id == 0 else "Pneumonia"
|
| 96 |
+
|
| 97 |
+
def predict_and_heatmap(image):
|
| 98 |
+
# Preprocess input image
|
| 99 |
+
img = image.resize((224, 224))
|
| 100 |
+
img_array = np.array(img) / 255.0
|
| 101 |
+
img_array = np.expand_dims(img_array, axis=0)
|
| 102 |
+
|
| 103 |
+
# Predict using ensemble model
|
| 104 |
+
prediction = ensemble_model.predict(img_array)
|
| 105 |
+
class_id = np.argmax(prediction[0])
|
| 106 |
+
result = get_class_name(class_id)
|
| 107 |
+
|
| 108 |
+
# Save uploaded image temporarily
|
| 109 |
+
temp_img_path = "temp_input.jpg"
|
| 110 |
+
image.save(temp_img_path)
|
| 111 |
+
|
| 112 |
+
# Generate Grad-CAM heatmap
|
| 113 |
+
heatmap_img = generate_and_merge_heatmaps(
|
| 114 |
+
temp_img_path, vgg_model, efficientnet_model, densenet_model
|
| 115 |
+
)
|
| 116 |
+
|
| 117 |
+
return result, Image.fromarray(heatmap_img)
|
| 118 |
+
|
| 119 |
+
# Gradio Interface
|
| 120 |
+
interface = gr.Interface(
|
| 121 |
+
fn=predict_and_heatmap,
|
| 122 |
+
inputs=gr.Image(type="pil", label="Upload Chest X-ray"),
|
| 123 |
+
outputs=[
|
| 124 |
+
gr.Label(label="Prediction"),
|
| 125 |
+
gr.Image(label="Grad-CAM Heatmap")
|
| 126 |
+
],
|
| 127 |
+
title="Pneumonia Detection Using Deep Learning",
|
| 128 |
+
description="Upload a chest X-ray to detect Pneumonia and see the heatmap visualization (Grad-CAM)."
|
| 129 |
+
)
|
| 130 |
+
|
| 131 |
+
if __name__ == "__main__":
|
| 132 |
+
interface.launch()
|