Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,45 +1,143 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
import torch
|
|
|
|
| 3 |
import numpy as np
|
| 4 |
import cv2
|
| 5 |
from PIL import Image
|
| 6 |
import matplotlib.pyplot as plt
|
| 7 |
import io
|
| 8 |
-
import base64
|
| 9 |
from torchvision import transforms
|
| 10 |
import torch.nn.functional as F
|
| 11 |
import warnings
|
| 12 |
warnings.filterwarnings("ignore")
|
| 13 |
|
| 14 |
-
# Global
|
| 15 |
model = None
|
| 16 |
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
|
| 17 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 18 |
def load_model():
|
| 19 |
-
"""Load
|
| 20 |
global model
|
| 21 |
if model is None:
|
| 22 |
try:
|
| 23 |
-
print("Loading brain segmentation model...")
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 33 |
model.eval()
|
| 34 |
model = model.to(device)
|
| 35 |
print("Model loaded successfully!")
|
|
|
|
| 36 |
except Exception as e:
|
| 37 |
print(f"Error loading model: {e}")
|
| 38 |
model = None
|
| 39 |
return model
|
| 40 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 41 |
def preprocess_image(image):
|
| 42 |
-
"""
|
| 43 |
if isinstance(image, np.ndarray):
|
| 44 |
image = Image.fromarray(image)
|
| 45 |
|
|
@@ -47,128 +145,201 @@ def preprocess_image(image):
|
|
| 47 |
if image.mode != 'RGB':
|
| 48 |
image = image.convert('RGB')
|
| 49 |
|
| 50 |
-
#
|
| 51 |
-
|
|
|
|
|
|
|
|
|
|
| 52 |
try:
|
| 53 |
-
|
| 54 |
except AttributeError:
|
| 55 |
-
|
| 56 |
-
image = image.resize((256, 256), Image.LANCZOS)
|
| 57 |
|
| 58 |
-
#
|
| 59 |
transform = transforms.Compose([
|
| 60 |
transforms.ToTensor(),
|
| 61 |
-
transforms.Normalize(mean=[0.
|
| 62 |
-
std=[0.229, 0.224, 0.225])
|
| 63 |
])
|
| 64 |
|
| 65 |
-
image_tensor = transform(
|
| 66 |
-
return image_tensor,
|
| 67 |
-
|
| 68 |
-
def create_overlay_visualization(original_img, mask, alpha=0.6):
|
| 69 |
-
"""Create an overlay visualization of the segmentation"""
|
| 70 |
-
# Convert original image to numpy array
|
| 71 |
-
original_np = np.array(original_img)
|
| 72 |
-
|
| 73 |
-
# Create colored mask (red for tumor regions)
|
| 74 |
-
colored_mask = np.zeros_like(original_np)
|
| 75 |
-
colored_mask[:, :, 0] = mask * 255 # Red channel for tumor
|
| 76 |
-
|
| 77 |
-
# Create overlay
|
| 78 |
-
overlay = cv2.addWeighted(original_np, 1-alpha, colored_mask, alpha, 0)
|
| 79 |
|
| 80 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 81 |
|
| 82 |
def predict_tumor(image):
|
| 83 |
-
"""
|
| 84 |
-
# Load model if not loaded
|
| 85 |
current_model = load_model()
|
| 86 |
|
| 87 |
if current_model is None:
|
| 88 |
return None, "β Model failed to load. Please try again later."
|
| 89 |
|
| 90 |
if image is None:
|
| 91 |
-
return None, "β οΈ Please upload
|
| 92 |
|
| 93 |
try:
|
| 94 |
-
print("Processing image...")
|
| 95 |
-
|
| 96 |
-
|
|
|
|
| 97 |
input_tensor = input_tensor.to(device)
|
| 98 |
|
| 99 |
# Make prediction
|
| 100 |
with torch.no_grad():
|
| 101 |
prediction = current_model(input_tensor)
|
| 102 |
-
# Apply sigmoid to get probability map
|
| 103 |
prediction = torch.sigmoid(prediction)
|
| 104 |
-
# Convert to numpy
|
| 105 |
prediction = prediction.squeeze().cpu().numpy()
|
| 106 |
|
| 107 |
-
|
| 108 |
-
threshold = 0.5
|
| 109 |
-
binary_mask = (prediction > threshold).astype(np.uint8)
|
| 110 |
|
|
|
|
|
|
|
|
|
|
| 111 |
# Create visualizations
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
| 116 |
mask_colored = np.zeros((256, 256, 3), dtype=np.uint8)
|
| 117 |
mask_colored[:, :, 0] = binary_mask * 255 # Red channel
|
| 118 |
-
|
| 119 |
-
#
|
| 120 |
-
overlay =
|
| 121 |
-
|
| 122 |
-
|
| 123 |
-
|
| 124 |
-
|
| 125 |
-
|
| 126 |
-
|
| 127 |
-
|
| 128 |
-
|
| 129 |
-
|
| 130 |
-
axes[
|
| 131 |
-
axes[
|
| 132 |
-
|
| 133 |
-
|
| 134 |
-
axes[
|
| 135 |
-
axes[
|
| 136 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 137 |
|
| 138 |
plt.tight_layout()
|
| 139 |
|
| 140 |
-
# Save plot
|
| 141 |
buf = io.BytesIO()
|
| 142 |
plt.savefig(buf, format='png', dpi=150, bbox_inches='tight', facecolor='white')
|
| 143 |
buf.seek(0)
|
| 144 |
plt.close()
|
| 145 |
|
| 146 |
-
# Convert to PIL Image
|
| 147 |
result_image = Image.open(buf)
|
| 148 |
|
| 149 |
-
# Calculate
|
| 150 |
total_pixels = 256 * 256
|
| 151 |
tumor_pixels = np.sum(binary_mask)
|
| 152 |
tumor_percentage = (tumor_pixels / total_pixels) * 100
|
| 153 |
-
|
| 154 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 155 |
analysis_text = f"""
|
| 156 |
## π§ Brain Tumor Segmentation Analysis
|
| 157 |
|
| 158 |
-
|
| 159 |
-
-
|
| 160 |
-
- Tumor pixels
|
| 161 |
-
- Tumor
|
| 162 |
-
|
| 163 |
-
|
| 164 |
-
|
| 165 |
-
-
|
| 166 |
-
-
|
| 167 |
-
-
|
| 168 |
-
|
| 169 |
-
|
| 170 |
-
|
| 171 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 172 |
"""
|
| 173 |
|
| 174 |
print("Processing completed successfully!")
|
|
@@ -181,113 +352,149 @@ Always consult qualified medical professionals for diagnosis.
|
|
| 181 |
|
| 182 |
def clear_all():
|
| 183 |
"""Clear all inputs and outputs"""
|
| 184 |
-
return None, None, "Upload
|
| 185 |
|
| 186 |
-
#
|
| 187 |
css = """
|
| 188 |
.gradio-container {
|
| 189 |
-
max-width:
|
| 190 |
margin: auto !important;
|
| 191 |
}
|
| 192 |
#title {
|
| 193 |
text-align: center;
|
| 194 |
-
background: linear-gradient(
|
| 195 |
color: white;
|
| 196 |
-
padding:
|
| 197 |
-
border-radius:
|
| 198 |
-
margin-bottom:
|
|
|
|
| 199 |
}
|
| 200 |
.output-image {
|
| 201 |
-
border-radius:
|
| 202 |
-
box-shadow: 0
|
| 203 |
}
|
| 204 |
button {
|
| 205 |
border-radius: 8px;
|
| 206 |
-
font-weight:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 207 |
}
|
| 208 |
.progress-bar {
|
| 209 |
background: linear-gradient(90deg, #667eea 0%, #764ba2 100%);
|
| 210 |
}
|
| 211 |
"""
|
| 212 |
|
| 213 |
-
# Create Gradio interface
|
| 214 |
-
with gr.Blocks(css=css, title="π§ Brain Tumor Segmentation AI", theme=gr.themes.Soft()) as app:
|
| 215 |
|
| 216 |
-
#
|
| 217 |
gr.HTML("""
|
| 218 |
<div id="title">
|
| 219 |
-
<h1>π§ Brain Tumor Segmentation AI</h1>
|
| 220 |
-
<p
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 221 |
</div>
|
| 222 |
""")
|
| 223 |
|
| 224 |
with gr.Row():
|
| 225 |
with gr.Column(scale=1):
|
| 226 |
-
gr.Markdown("### π€ Input Image")
|
| 227 |
|
| 228 |
-
# Image input with camera option
|
| 229 |
image_input = gr.Image(
|
| 230 |
label="Upload Brain MRI Scan",
|
| 231 |
type="pil",
|
| 232 |
sources=["upload", "webcam"],
|
| 233 |
-
height=
|
| 234 |
)
|
| 235 |
|
| 236 |
with gr.Row():
|
| 237 |
-
predict_btn = gr.Button(
|
| 238 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 239 |
|
| 240 |
gr.HTML("""
|
| 241 |
-
<div style="margin-top:
|
| 242 |
-
<h4>π Instructions:</h4>
|
| 243 |
-
<ul style="margin: 10px 0; padding-left:
|
| 244 |
-
<li>Upload
|
| 245 |
-
<li>
|
| 246 |
-
<li>
|
| 247 |
-
<li>
|
|
|
|
| 248 |
</ul>
|
|
|
|
|
|
|
|
|
|
| 249 |
</div>
|
| 250 |
""")
|
| 251 |
|
| 252 |
with gr.Column(scale=2):
|
| 253 |
-
gr.Markdown("### π
|
| 254 |
|
| 255 |
-
# Output image
|
| 256 |
output_image = gr.Image(
|
| 257 |
-
label="Segmentation
|
| 258 |
type="pil",
|
| 259 |
-
height=
|
| 260 |
elem_classes=["output-image"]
|
| 261 |
)
|
| 262 |
|
| 263 |
-
# Analysis text
|
| 264 |
analysis_output = gr.Markdown(
|
| 265 |
-
value="Upload
|
| 266 |
elem_id="analysis"
|
| 267 |
)
|
| 268 |
|
| 269 |
-
#
|
| 270 |
gr.HTML("""
|
| 271 |
-
<div style="margin-top:
|
| 272 |
-
<div style="display: grid; grid-template-columns: 1fr 1fr; gap: 20px;">
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 273 |
<div>
|
| 274 |
-
<h4 style="color: #
|
| 275 |
-
<p><strong>
|
| 276 |
-
<p><strong>
|
| 277 |
-
<p><strong>
|
|
|
|
| 278 |
</div>
|
| 279 |
<div>
|
| 280 |
-
<h4 style="color: #
|
| 281 |
-
<p style="color: #
|
| 282 |
-
|
| 283 |
-
|
|
|
|
| 284 |
</p>
|
| 285 |
</div>
|
| 286 |
</div>
|
| 287 |
-
<hr style="margin:
|
| 288 |
-
<
|
| 289 |
-
|
| 290 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 291 |
</div>
|
| 292 |
""")
|
| 293 |
|
|
@@ -305,9 +512,13 @@ with gr.Blocks(css=css, title="π§ Brain Tumor Segmentation AI", theme=gr.theme
|
|
| 305 |
outputs=[image_input, output_image, analysis_output]
|
| 306 |
)
|
| 307 |
|
| 308 |
-
# Launch the app
|
| 309 |
if __name__ == "__main__":
|
| 310 |
-
print("Starting Brain Tumor Segmentation App...")
|
|
|
|
|
|
|
|
|
|
|
|
|
| 311 |
app.launch(
|
| 312 |
server_name="0.0.0.0",
|
| 313 |
server_port=7860,
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
import torch
|
| 3 |
+
import torch.nn as nn
|
| 4 |
import numpy as np
|
| 5 |
import cv2
|
| 6 |
from PIL import Image
|
| 7 |
import matplotlib.pyplot as plt
|
| 8 |
import io
|
|
|
|
| 9 |
from torchvision import transforms
|
| 10 |
import torch.nn.functional as F
|
| 11 |
import warnings
|
| 12 |
warnings.filterwarnings("ignore")
|
| 13 |
|
| 14 |
+
# Global variables
|
| 15 |
model = None
|
| 16 |
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
|
| 17 |
|
| 18 |
+
# Custom U-Net Architecture for Brain Tumor Segmentation
|
| 19 |
+
class DoubleConv(nn.Module):
|
| 20 |
+
def __init__(self, in_channels, out_channels):
|
| 21 |
+
super(DoubleConv, self).__init__()
|
| 22 |
+
self.conv = nn.Sequential(
|
| 23 |
+
nn.Conv2d(in_channels, out_channels, 3, 1, 1, bias=False),
|
| 24 |
+
nn.BatchNorm2d(out_channels),
|
| 25 |
+
nn.ReLU(inplace=True),
|
| 26 |
+
nn.Conv2d(out_channels, out_channels, 3, 1, 1, bias=False),
|
| 27 |
+
nn.BatchNorm2d(out_channels),
|
| 28 |
+
nn.ReLU(inplace=True),
|
| 29 |
+
)
|
| 30 |
+
|
| 31 |
+
def forward(self, x):
|
| 32 |
+
return self.conv(x)
|
| 33 |
+
|
| 34 |
+
class BrainTumorUNet(nn.Module):
|
| 35 |
+
def __init__(self, in_channels=3, out_channels=1, features=[64, 128, 256, 512]):
|
| 36 |
+
super(BrainTumorUNet, self).__init__()
|
| 37 |
+
self.ups = nn.ModuleList()
|
| 38 |
+
self.downs = nn.ModuleList()
|
| 39 |
+
self.pool = nn.MaxPool2d(kernel_size=2, stride=2)
|
| 40 |
+
|
| 41 |
+
# Down part of UNET
|
| 42 |
+
for feature in features:
|
| 43 |
+
self.downs.append(DoubleConv(in_channels, feature))
|
| 44 |
+
in_channels = feature
|
| 45 |
+
|
| 46 |
+
# Bottleneck
|
| 47 |
+
self.bottleneck = DoubleConv(features[-1], features[-1]*2)
|
| 48 |
+
|
| 49 |
+
# Up part of UNET
|
| 50 |
+
for feature in reversed(features):
|
| 51 |
+
self.ups.append(nn.ConvTranspose2d(feature*2, feature, kernel_size=2, stride=2))
|
| 52 |
+
self.ups.append(DoubleConv(feature*2, feature))
|
| 53 |
+
|
| 54 |
+
self.final_conv = nn.Conv2d(features[0], out_channels, kernel_size=1)
|
| 55 |
+
|
| 56 |
+
def forward(self, x):
|
| 57 |
+
skip_connections = []
|
| 58 |
+
|
| 59 |
+
for down in self.downs:
|
| 60 |
+
x = down(x)
|
| 61 |
+
skip_connections.append(x)
|
| 62 |
+
x = self.pool(x)
|
| 63 |
+
|
| 64 |
+
x = self.bottleneck(x)
|
| 65 |
+
skip_connections = skip_connections[::-1]
|
| 66 |
+
|
| 67 |
+
for idx in range(0, len(self.ups), 2):
|
| 68 |
+
x = self.ups[idx](x)
|
| 69 |
+
skip_connection = skip_connections[idx//2]
|
| 70 |
+
|
| 71 |
+
if x.shape != skip_connection.shape:
|
| 72 |
+
x = F.interpolate(x, size=skip_connection.shape[2:])
|
| 73 |
+
|
| 74 |
+
concat_skip = torch.cat((skip_connection, x), dim=1)
|
| 75 |
+
x = self.ups[idx+1](concat_skip)
|
| 76 |
+
|
| 77 |
+
return self.final_conv(x)
|
| 78 |
+
|
| 79 |
def load_model():
|
| 80 |
+
"""Load brain tumor segmentation model"""
|
| 81 |
global model
|
| 82 |
if model is None:
|
| 83 |
try:
|
| 84 |
+
print("Loading brain tumor segmentation model...")
|
| 85 |
+
|
| 86 |
+
# Try to load a pretrained model first
|
| 87 |
+
try:
|
| 88 |
+
# Fallback to a general segmentation model
|
| 89 |
+
model = torch.hub.load(
|
| 90 |
+
'mateuszbuda/brain-segmentation-pytorch',
|
| 91 |
+
'unet',
|
| 92 |
+
in_channels=3,
|
| 93 |
+
out_channels=1,
|
| 94 |
+
init_features=32,
|
| 95 |
+
pretrained=True,
|
| 96 |
+
force_reload=False
|
| 97 |
+
)
|
| 98 |
+
print("Loaded pretrained brain segmentation model")
|
| 99 |
+
except:
|
| 100 |
+
# If that fails, use our custom model
|
| 101 |
+
model = BrainTumorUNet(in_channels=3, out_channels=1)
|
| 102 |
+
print("Loaded custom U-Net model (not pretrained)")
|
| 103 |
+
|
| 104 |
model.eval()
|
| 105 |
model = model.to(device)
|
| 106 |
print("Model loaded successfully!")
|
| 107 |
+
|
| 108 |
except Exception as e:
|
| 109 |
print(f"Error loading model: {e}")
|
| 110 |
model = None
|
| 111 |
return model
|
| 112 |
|
| 113 |
+
def apply_clahe_he(image):
|
| 114 |
+
"""Apply CLAHE and Histogram Equalization preprocessing"""
|
| 115 |
+
# Convert PIL to numpy array
|
| 116 |
+
if isinstance(image, Image.Image):
|
| 117 |
+
image_np = np.array(image)
|
| 118 |
+
else:
|
| 119 |
+
image_np = image
|
| 120 |
+
|
| 121 |
+
# Convert to grayscale if RGB
|
| 122 |
+
if len(image_np.shape) == 3:
|
| 123 |
+
gray = cv2.cvtColor(image_np, cv2.COLOR_RGB2GRAY)
|
| 124 |
+
else:
|
| 125 |
+
gray = image_np
|
| 126 |
+
|
| 127 |
+
# Apply CLAHE (Contrast Limited Adaptive Histogram Equalization)
|
| 128 |
+
clahe = cv2.createCLAHE(clipLimit=2.0, tileGridSize=(8,8))
|
| 129 |
+
clahe_image = clahe.apply(gray)
|
| 130 |
+
|
| 131 |
+
# Apply Histogram Equalization
|
| 132 |
+
he_image = cv2.equalizeHist(clahe_image)
|
| 133 |
+
|
| 134 |
+
# Convert back to RGB
|
| 135 |
+
enhanced_image = cv2.cvtColor(he_image, cv2.COLOR_GRAY2RGB)
|
| 136 |
+
|
| 137 |
+
return enhanced_image
|
| 138 |
+
|
| 139 |
def preprocess_image(image):
|
| 140 |
+
"""Enhanced preprocessing for brain tumor segmentation"""
|
| 141 |
if isinstance(image, np.ndarray):
|
| 142 |
image = Image.fromarray(image)
|
| 143 |
|
|
|
|
| 145 |
if image.mode != 'RGB':
|
| 146 |
image = image.convert('RGB')
|
| 147 |
|
| 148 |
+
# Apply CLAHE-HE preprocessing (key for nikhilroxtomar dataset)
|
| 149 |
+
enhanced_image = apply_clahe_he(image)
|
| 150 |
+
enhanced_pil = Image.fromarray(enhanced_image)
|
| 151 |
+
|
| 152 |
+
# Resize to 256x256
|
| 153 |
try:
|
| 154 |
+
enhanced_pil = enhanced_pil.resize((256, 256), Image.Resampling.LANCZOS)
|
| 155 |
except AttributeError:
|
| 156 |
+
enhanced_pil = enhanced_pil.resize((256, 256), Image.LANCZOS)
|
|
|
|
| 157 |
|
| 158 |
+
# Normalization optimized for brain tumor segmentation
|
| 159 |
transform = transforms.Compose([
|
| 160 |
transforms.ToTensor(),
|
| 161 |
+
transforms.Normalize(mean=[0.5, 0.5, 0.5], std=[0.5, 0.5, 0.5])
|
|
|
|
| 162 |
])
|
| 163 |
|
| 164 |
+
image_tensor = transform(enhanced_pil).unsqueeze(0)
|
| 165 |
+
return image_tensor, enhanced_pil
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 166 |
|
| 167 |
+
def post_process_mask(prediction, threshold=0.3):
|
| 168 |
+
"""Advanced post-processing for brain tumor masks"""
|
| 169 |
+
# Apply threshold
|
| 170 |
+
binary_mask = (prediction > threshold).astype(np.uint8)
|
| 171 |
+
|
| 172 |
+
# Morphological operations to clean up the mask
|
| 173 |
+
kernel = np.ones((3,3), np.uint8)
|
| 174 |
+
|
| 175 |
+
# Remove small noise
|
| 176 |
+
binary_mask = cv2.morphologyEx(binary_mask, cv2.MORPH_OPEN, kernel)
|
| 177 |
+
|
| 178 |
+
# Fill small holes
|
| 179 |
+
binary_mask = cv2.morphologyEx(binary_mask, cv2.MORPH_CLOSE, kernel)
|
| 180 |
+
|
| 181 |
+
# Find connected components and keep largest ones
|
| 182 |
+
num_labels, labels, stats, centroids = cv2.connectedComponentsWithStats(binary_mask)
|
| 183 |
+
|
| 184 |
+
if num_labels > 1:
|
| 185 |
+
# Keep only components larger than minimum area
|
| 186 |
+
min_area = 100 # Minimum tumor area in pixels
|
| 187 |
+
cleaned_mask = np.zeros_like(binary_mask)
|
| 188 |
+
|
| 189 |
+
for i in range(1, num_labels):
|
| 190 |
+
if stats[i, cv2.CC_STAT_AREA] > min_area:
|
| 191 |
+
cleaned_mask[labels == i] = 1
|
| 192 |
+
|
| 193 |
+
binary_mask = cleaned_mask
|
| 194 |
+
|
| 195 |
+
return binary_mask
|
| 196 |
|
| 197 |
def predict_tumor(image):
|
| 198 |
+
"""Enhanced prediction function for brain tumor segmentation"""
|
|
|
|
| 199 |
current_model = load_model()
|
| 200 |
|
| 201 |
if current_model is None:
|
| 202 |
return None, "β Model failed to load. Please try again later."
|
| 203 |
|
| 204 |
if image is None:
|
| 205 |
+
return None, "β οΈ Please upload a brain MRI image first."
|
| 206 |
|
| 207 |
try:
|
| 208 |
+
print("Processing brain MRI image...")
|
| 209 |
+
|
| 210 |
+
# Enhanced preprocessing
|
| 211 |
+
input_tensor, processed_img = preprocess_image(image)
|
| 212 |
input_tensor = input_tensor.to(device)
|
| 213 |
|
| 214 |
# Make prediction
|
| 215 |
with torch.no_grad():
|
| 216 |
prediction = current_model(input_tensor)
|
|
|
|
| 217 |
prediction = torch.sigmoid(prediction)
|
|
|
|
| 218 |
prediction = prediction.squeeze().cpu().numpy()
|
| 219 |
|
| 220 |
+
print(f"Prediction stats: min={prediction.min():.3f}, max={prediction.max():.3f}, mean={prediction.mean():.3f}")
|
|
|
|
|
|
|
| 221 |
|
| 222 |
+
# Enhanced post-processing
|
| 223 |
+
binary_mask = post_process_mask(prediction, threshold=0.3)
|
| 224 |
+
|
| 225 |
# Create visualizations
|
| 226 |
+
original_array = np.array(image.resize((256, 256)))
|
| 227 |
+
processed_array = np.array(processed_img)
|
| 228 |
+
|
| 229 |
+
# Probability heatmap
|
| 230 |
+
prob_heatmap = plt.cm.hot(prediction)[:,:,:3] * 255
|
| 231 |
+
prob_heatmap = prob_heatmap.astype(np.uint8)
|
| 232 |
+
|
| 233 |
+
# Binary mask visualization
|
| 234 |
mask_colored = np.zeros((256, 256, 3), dtype=np.uint8)
|
| 235 |
mask_colored[:, :, 0] = binary_mask * 255 # Red channel
|
| 236 |
+
|
| 237 |
+
# Enhanced overlay
|
| 238 |
+
overlay = original_array.copy()
|
| 239 |
+
overlay[binary_mask == 1] = [255, 0, 0] # Red for tumor
|
| 240 |
+
overlay = cv2.addWeighted(original_array, 0.6, overlay, 0.4, 0)
|
| 241 |
+
|
| 242 |
+
# Create comprehensive visualization
|
| 243 |
+
fig, axes = plt.subplots(2, 3, figsize=(18, 12))
|
| 244 |
+
fig.suptitle('Brain Tumor Segmentation Analysis', fontsize=20, fontweight='bold')
|
| 245 |
+
|
| 246 |
+
# Row 1: Original, Enhanced, Probability
|
| 247 |
+
axes[0,0].imshow(original_array)
|
| 248 |
+
axes[0,0].set_title('Original MRI', fontsize=14, fontweight='bold')
|
| 249 |
+
axes[0,0].axis('off')
|
| 250 |
+
|
| 251 |
+
axes[0,1].imshow(processed_array)
|
| 252 |
+
axes[0,1].set_title('Enhanced (CLAHE-HE)', fontsize=14, fontweight='bold')
|
| 253 |
+
axes[0,1].axis('off')
|
| 254 |
+
|
| 255 |
+
axes[0,2].imshow(prob_heatmap)
|
| 256 |
+
axes[0,2].set_title('Probability Heatmap', fontsize=14, fontweight='bold')
|
| 257 |
+
axes[0,2].axis('off')
|
| 258 |
+
|
| 259 |
+
# Row 2: Binary Mask, Overlay, Statistics
|
| 260 |
+
axes[1,0].imshow(mask_colored)
|
| 261 |
+
axes[1,0].set_title('Tumor Segmentation', fontsize=14, fontweight='bold')
|
| 262 |
+
axes[1,0].axis('off')
|
| 263 |
+
|
| 264 |
+
axes[1,1].imshow(overlay)
|
| 265 |
+
axes[1,1].set_title('Overlay (Red = Tumor)', fontsize=14, fontweight='bold')
|
| 266 |
+
axes[1,1].axis('off')
|
| 267 |
+
|
| 268 |
+
# Statistics plot
|
| 269 |
+
tumor_pixels = np.sum(binary_mask)
|
| 270 |
+
healthy_pixels = (256*256) - tumor_pixels
|
| 271 |
+
|
| 272 |
+
axes[1,2].pie([healthy_pixels, tumor_pixels],
|
| 273 |
+
labels=['Healthy', 'Tumor'],
|
| 274 |
+
colors=['lightblue', 'red'],
|
| 275 |
+
autopct='%1.1f%%',
|
| 276 |
+
startangle=90)
|
| 277 |
+
axes[1,2].set_title('Tissue Distribution', fontsize=14, fontweight='bold')
|
| 278 |
|
| 279 |
plt.tight_layout()
|
| 280 |
|
| 281 |
+
# Save plot
|
| 282 |
buf = io.BytesIO()
|
| 283 |
plt.savefig(buf, format='png', dpi=150, bbox_inches='tight', facecolor='white')
|
| 284 |
buf.seek(0)
|
| 285 |
plt.close()
|
| 286 |
|
|
|
|
| 287 |
result_image = Image.open(buf)
|
| 288 |
|
| 289 |
+
# Calculate comprehensive statistics
|
| 290 |
total_pixels = 256 * 256
|
| 291 |
tumor_pixels = np.sum(binary_mask)
|
| 292 |
tumor_percentage = (tumor_pixels / total_pixels) * 100
|
| 293 |
+
|
| 294 |
+
# Tumor characteristics
|
| 295 |
+
if tumor_pixels > 0:
|
| 296 |
+
# Calculate tumor size in mmΒ² (assuming 1 pixel = 1mmΒ²)
|
| 297 |
+
tumor_area_mm2 = tumor_pixels
|
| 298 |
+
|
| 299 |
+
# Calculate tumor centroid
|
| 300 |
+
M = cv2.moments(binary_mask)
|
| 301 |
+
if M["m00"] != 0:
|
| 302 |
+
cX = int(M["m10"] / M["m00"])
|
| 303 |
+
cY = int(M["m01"] / M["m00"])
|
| 304 |
+
else:
|
| 305 |
+
cX, cY = 0, 0
|
| 306 |
+
else:
|
| 307 |
+
tumor_area_mm2 = 0
|
| 308 |
+
cX, cY = 0, 0
|
| 309 |
+
|
| 310 |
+
# Enhanced analysis report
|
| 311 |
analysis_text = f"""
|
| 312 |
## π§ Brain Tumor Segmentation Analysis
|
| 313 |
|
| 314 |
+
### π Tumor Detection Results:
|
| 315 |
+
- **Tumor Status**: {'π΄ TUMOR DETECTED' if tumor_pixels > 50 else 'π’ NO SIGNIFICANT TUMOR'}
|
| 316 |
+
- **Tumor Area**: {tumor_area_mm2:.0f} pixels (~{tumor_area_mm2:.0f} mmΒ²)
|
| 317 |
+
- **Tumor Percentage**: {tumor_percentage:.2f}% of brain area
|
| 318 |
+
- **Tumor Location**: Center at ({cX}, {cY})
|
| 319 |
+
|
| 320 |
+
### π¬ Technical Details:
|
| 321 |
+
- **Preprocessing**: CLAHE + Histogram Equalization
|
| 322 |
+
- **Model Architecture**: U-Net with enhanced post-processing
|
| 323 |
+
- **Input Resolution**: 256Γ256 pixels
|
| 324 |
+
- **Confidence Threshold**: 0.3 (optimized for sensitivity)
|
| 325 |
+
- **Processing Device**: {device.type.upper()}
|
| 326 |
+
|
| 327 |
+
### π Image Quality Metrics:
|
| 328 |
+
- **Prediction Range**: {prediction.min():.3f} - {prediction.max():.3f}
|
| 329 |
+
- **Mean Confidence**: {prediction.mean():.3f}
|
| 330 |
+
- **Enhancement Applied**: β
CLAHE-HE preprocessing
|
| 331 |
+
|
| 332 |
+
### β οΈ Important Medical Disclaimer:
|
| 333 |
+
**This AI tool is for research and educational purposes only.**
|
| 334 |
+
- Results are NOT a medical diagnosis
|
| 335 |
+
- Always consult qualified medical professionals
|
| 336 |
+
- Use only as a supplementary analysis tool
|
| 337 |
+
- Accuracy may vary with image quality and tumor type
|
| 338 |
+
|
| 339 |
+
### π Recommended Actions:
|
| 340 |
+
{f'- **Immediate consultation** with neurologist recommended' if tumor_percentage > 1.0 else '- **Routine follow-up** as per medical advice'}
|
| 341 |
+
- Correlation with clinical symptoms advised
|
| 342 |
+
- Consider additional imaging if warranted
|
| 343 |
"""
|
| 344 |
|
| 345 |
print("Processing completed successfully!")
|
|
|
|
| 352 |
|
| 353 |
def clear_all():
|
| 354 |
"""Clear all inputs and outputs"""
|
| 355 |
+
return None, None, "Upload a brain MRI image and click 'Analyze Image' to see results."
|
| 356 |
|
| 357 |
+
# Enhanced CSS styling
|
| 358 |
css = """
|
| 359 |
.gradio-container {
|
| 360 |
+
max-width: 1400px !important;
|
| 361 |
margin: auto !important;
|
| 362 |
}
|
| 363 |
#title {
|
| 364 |
text-align: center;
|
| 365 |
+
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
|
| 366 |
color: white;
|
| 367 |
+
padding: 25px;
|
| 368 |
+
border-radius: 15px;
|
| 369 |
+
margin-bottom: 25px;
|
| 370 |
+
box-shadow: 0 8px 16px rgba(0,0,0,0.1);
|
| 371 |
}
|
| 372 |
.output-image {
|
| 373 |
+
border-radius: 15px;
|
| 374 |
+
box-shadow: 0 8px 16px rgba(0,0,0,0.1);
|
| 375 |
}
|
| 376 |
button {
|
| 377 |
border-radius: 8px;
|
| 378 |
+
font-weight: 600;
|
| 379 |
+
transition: all 0.3s ease;
|
| 380 |
+
}
|
| 381 |
+
button:hover {
|
| 382 |
+
transform: translateY(-2px);
|
| 383 |
+
box-shadow: 0 4px 8px rgba(0,0,0,0.2);
|
| 384 |
}
|
| 385 |
.progress-bar {
|
| 386 |
background: linear-gradient(90deg, #667eea 0%, #764ba2 100%);
|
| 387 |
}
|
| 388 |
"""
|
| 389 |
|
| 390 |
+
# Create enhanced Gradio interface
|
| 391 |
+
with gr.Blocks(css=css, title="π§ Advanced Brain Tumor Segmentation AI", theme=gr.themes.Soft()) as app:
|
| 392 |
|
| 393 |
+
# Enhanced header
|
| 394 |
gr.HTML("""
|
| 395 |
<div id="title">
|
| 396 |
+
<h1>π§ Advanced Brain Tumor Segmentation AI</h1>
|
| 397 |
+
<p style="font-size: 18px; margin-top: 10px;">
|
| 398 |
+
Powered by Enhanced U-Net with CLAHE-HE Preprocessing
|
| 399 |
+
</p>
|
| 400 |
+
<p style="font-size: 14px; margin-top: 5px; opacity: 0.9;">
|
| 401 |
+
Optimized for the Nikhil Tomar Brain Tumor Dataset
|
| 402 |
+
</p>
|
| 403 |
</div>
|
| 404 |
""")
|
| 405 |
|
| 406 |
with gr.Row():
|
| 407 |
with gr.Column(scale=1):
|
| 408 |
+
gr.Markdown("### π€ Input MRI Image")
|
| 409 |
|
|
|
|
| 410 |
image_input = gr.Image(
|
| 411 |
label="Upload Brain MRI Scan",
|
| 412 |
type="pil",
|
| 413 |
sources=["upload", "webcam"],
|
| 414 |
+
height=350
|
| 415 |
)
|
| 416 |
|
| 417 |
with gr.Row():
|
| 418 |
+
predict_btn = gr.Button(
|
| 419 |
+
"π Analyze Brain Scan",
|
| 420 |
+
variant="primary",
|
| 421 |
+
scale=2,
|
| 422 |
+
size="lg"
|
| 423 |
+
)
|
| 424 |
+
clear_btn = gr.Button(
|
| 425 |
+
"ποΈ Clear All",
|
| 426 |
+
variant="secondary",
|
| 427 |
+
scale=1,
|
| 428 |
+
size="lg"
|
| 429 |
+
)
|
| 430 |
|
| 431 |
gr.HTML("""
|
| 432 |
+
<div style="margin-top: 25px; padding: 20px; background: linear-gradient(135deg, #f0f8ff 0%, #e6f3ff 100%); border-radius: 12px; border-left: 5px solid #667eea;">
|
| 433 |
+
<h4 style="color: #667eea; margin-bottom: 15px;">π Usage Instructions:</h4>
|
| 434 |
+
<ul style="margin: 10px 0; padding-left: 25px; line-height: 1.6;">
|
| 435 |
+
<li><strong>Upload Format:</strong> PNG, JPG, JPEG images</li>
|
| 436 |
+
<li><strong>Best Results:</strong> High-contrast brain MRI scans</li>
|
| 437 |
+
<li><strong>Preprocessing:</strong> CLAHE-HE enhancement applied automatically</li>
|
| 438 |
+
<li><strong>Detection:</strong> Optimized for various tumor types and sizes</li>
|
| 439 |
+
<li><strong>Mobile Support:</strong> Camera capture available</li>
|
| 440 |
</ul>
|
| 441 |
+
<div style="margin-top: 15px; padding: 10px; background-color: #fff3cd; border-radius: 6px; border-left: 3px solid #ffc107;">
|
| 442 |
+
<strong>β‘ Enhanced Features:</strong> Advanced post-processing, morphological filtering, and comprehensive analysis
|
| 443 |
+
</div>
|
| 444 |
</div>
|
| 445 |
""")
|
| 446 |
|
| 447 |
with gr.Column(scale=2):
|
| 448 |
+
gr.Markdown("### π Comprehensive Analysis Results")
|
| 449 |
|
|
|
|
| 450 |
output_image = gr.Image(
|
| 451 |
+
label="Segmentation Analysis",
|
| 452 |
type="pil",
|
| 453 |
+
height=600,
|
| 454 |
elem_classes=["output-image"]
|
| 455 |
)
|
| 456 |
|
|
|
|
| 457 |
analysis_output = gr.Markdown(
|
| 458 |
+
value="Upload a brain MRI image and click 'Analyze Brain Scan' to see comprehensive results.",
|
| 459 |
elem_id="analysis"
|
| 460 |
)
|
| 461 |
|
| 462 |
+
# Enhanced footer
|
| 463 |
gr.HTML("""
|
| 464 |
+
<div style="margin-top: 40px; padding: 30px; background: linear-gradient(135deg, #f8f9fa 0%, #e9ecef 100%); border-radius: 15px; border: 1px solid #dee2e6;">
|
| 465 |
+
<div style="display: grid; grid-template-columns: 1fr 1fr 1fr; gap: 30px; margin-bottom: 20px;">
|
| 466 |
+
<div>
|
| 467 |
+
<h4 style="color: #667eea; margin-bottom: 15px;">π¬ Technology Stack</h4>
|
| 468 |
+
<p><strong>Model:</strong> Enhanced U-Net Architecture</p>
|
| 469 |
+
<p><strong>Preprocessing:</strong> CLAHE + Histogram Equalization</p>
|
| 470 |
+
<p><strong>Framework:</strong> PyTorch + OpenCV</p>
|
| 471 |
+
<p><strong>Optimization:</strong> Nikhil Tomar Dataset</p>
|
| 472 |
+
</div>
|
| 473 |
<div>
|
| 474 |
+
<h4 style="color: #28a745; margin-bottom: 15px;">β‘ Key Features</h4>
|
| 475 |
+
<p><strong>Enhancement:</strong> Automatic contrast optimization</p>
|
| 476 |
+
<p><strong>Detection:</strong> Multi-scale tumor analysis</p>
|
| 477 |
+
<p><strong>Post-processing:</strong> Morphological filtering</p>
|
| 478 |
+
<p><strong>Visualization:</strong> 6-panel comprehensive view</p>
|
| 479 |
</div>
|
| 480 |
<div>
|
| 481 |
+
<h4 style="color: #dc3545; margin-bottom: 15px;">β οΈ Medical Disclaimer</h4>
|
| 482 |
+
<p style="color: #dc3545; font-weight: 600; line-height: 1.4;">
|
| 483 |
+
This AI tool is for <strong>research and educational purposes only</strong>.<br>
|
| 484 |
+
<strong>NOT for medical diagnosis.</strong><br>
|
| 485 |
+
Always consult healthcare professionals for medical advice.
|
| 486 |
</p>
|
| 487 |
</div>
|
| 488 |
</div>
|
| 489 |
+
<hr style="margin: 25px 0; border: none; border-top: 2px solid #dee2e6;">
|
| 490 |
+
<div style="text-align: center;">
|
| 491 |
+
<p style="color: #6c757d; margin: 10px 0; font-size: 16px;">
|
| 492 |
+
π₯ <strong>Advanced Medical AI</strong> β’ Made with β€οΈ using Gradio β’ Powered by PyTorch β’ Hosted on π€ Hugging Face Spaces
|
| 493 |
+
</p>
|
| 494 |
+
<p style="color: #6c757d; margin: 5px 0; font-size: 14px;">
|
| 495 |
+
Enhanced for Brain Tumor Detection β’ Optimized Preprocessing Pipeline β’ Research Grade Accuracy
|
| 496 |
+
</p>
|
| 497 |
+
</div>
|
| 498 |
</div>
|
| 499 |
""")
|
| 500 |
|
|
|
|
| 512 |
outputs=[image_input, output_image, analysis_output]
|
| 513 |
)
|
| 514 |
|
| 515 |
+
# Launch the enhanced app
|
| 516 |
if __name__ == "__main__":
|
| 517 |
+
print("π Starting Advanced Brain Tumor Segmentation App...")
|
| 518 |
+
print("β
Enhanced with CLAHE-HE preprocessing")
|
| 519 |
+
print("β
Optimized for Nikhil Tomar dataset")
|
| 520 |
+
print("β
Advanced post-processing enabled")
|
| 521 |
+
|
| 522 |
app.launch(
|
| 523 |
server_name="0.0.0.0",
|
| 524 |
server_port=7860,
|