-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathapp.py
160 lines (118 loc) · 4.91 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
from flask import Flask, request, render_template, send_file
import numpy as np
import tensorflow as tf
from tensorflow.keras.preprocessing import image
from io import BytesIO
from xai import limework
import tensorflow as tf
from tensorflow.keras.applications import ResNet50
from tensorflow.keras.layers import GlobalAveragePooling2D, Dense, Dropout
from tensorflow.keras import Model
import numpy as np
import matplotlib.pyplot as plt
from tensorflow.keras.preprocessing import image
from PIL import Image
import os
from flask import Flask, render_template, request, redirect, url_for, flash
import os
from werkzeug.utils import secure_filename
app = Flask(__name__)
app.config['UPLOAD_FOLDER'] = 'uploads'
app.config['ALLOWED_EXTENSIONS'] = {'png', 'jpg', 'jpeg', 'gif'}
app.config['OUTPUT_FOLDER'] = 'static/output'
app.secret_key = 'your_secret_key'
# Function to check allowed file extensions
def allowed_file(filename):
return '.' in filename and filename.rsplit('.', 1)[1].lower() in app.config['ALLOWED_EXTENSIONS']
model = tf.keras.models.load_model('prediction/brain_tumor_classifier.h5')
# Define class labels (ensure 'notumor' is in the last position for easy checking)
class_labels = ['glioma', 'meningioma', 'pituitary', 'notumor']
# Confidence threshold for detecting unknown tumors
CONFIDENCE_THRESHOLD = 0.50
@app.route('/')
def home():
return render_template('index.html')
@app.route('/resnet')
def resnet():
return render_template('resnet.html')
@app.route('/vgg')
def vgg():
return render_template('vgg.html')
@app.route('/gans')
def gans():
return render_template('gans.html')
@app.route('/detection')
def detection():
return render_template('detection.html')
@app.route('/gan_dataset')
def gan_dataset():
return render_template('gan_dataset.html')
@app.route('/xai')
def xai():
return render_template('xai.html')
@app.route('/predict', methods=['POST'])
def predict():
if 'file' not in request.files:
return 'No file uploaded!', 400
file = request.files['file']
# Read the image file and convert it to a format suitable for keras
img = image.load_img(BytesIO(file.read()), target_size=(224, 224))
img_array = image.img_to_array(img) / 255.0
img_array = np.expand_dims(img_array, axis=0)
# Make prediction
predictions = model.predict(img_array)
predicted_probabilities = predictions[0]
max_probability = np.max(predicted_probabilities)
predicted_class_index = np.argmax(predicted_probabilities)
predicted_class_label = class_labels[predicted_class_index]
# Check for 'notumor' class or low confidence
if predicted_class_label == 'notumor':
result = f'No tumor detected with confidence: {max_probability * 100:.2f}%'
elif max_probability < CONFIDENCE_THRESHOLD:
result = f'Tumor detected, but type is not in trained classes. Confidence: {max_probability * 100:.2f}%'
else:
result = f'Tumor detected: {predicted_class_label} with confidence: {max_probability * 100:.2f}%'
return render_template('detection_output.html', result=result)
@app.route('/upload', methods=['POST'])
def upload():
if 'image_files[]' not in request.files:
flash('No file part')
return redirect(request.url)
files = request.files.getlist('image_files[]')
uploaded_files = []
if not files:
flash('No selected file')
return redirect(request.url)
for file in files:
if file and allowed_file(file.filename):
filename = secure_filename(file.filename)
file_path = os.path.join(app.config['UPLOAD_FOLDER'], filename)
file.save(file_path)
uploaded_files.append(file_path)
else:
flash('Invalid file type')
return redirect(request.url)
if uploaded_files:
try:
explanations = []
for file_path in uploaded_files:
output_files = limework.apply_gradcam_and_lime(file_path, limework.model)
# Add URLs to static output folder
output_files = {
"original": url_for('static', filename='output/original_image.png'),
"heatmap": url_for('static', filename='output/heatmap.png'),
"superimposed": url_for('static', filename='output/superimposed_image.png'),
"lime": url_for('static', filename='output/lime_explanation_image.png')
}
explanations.append(output_files)
return render_template('xai.html', explanations=explanations)
except Exception as e:
flash(f"Error processing the image(s): {str(e)}")
return redirect(request.url)
flash('No valid images processed')
return redirect(request.url)
if __name__ == '__main__':
# Ensure upload folder exists
if not os.path.exists(app.config['UPLOAD_FOLDER']):
os.makedirs(app.config['UPLOAD_FOLDER'])
app.run(debug=True)