Upload 19 files
Browse files- .gitattributes +16 -0
- app.py +143 -0
- colored/nontumor_1.tif +3 -0
- colored/nontumor_2.tif +3 -0
- colored/tumor_1.tif +3 -0
- colored/tumor_2.tif +3 -0
- colored/tumor_3.tif +3 -0
- colored/tumor_4.tif +3 -0
- colored/tumor_5.tif +3 -0
- colored/tumor_6.tif +3 -0
- grayscale/nontumor_1.tif +3 -0
- grayscale/nontumor_2.tif +3 -0
- grayscale/tumor_1.tif +3 -0
- grayscale/tumor_2.tif +3 -0
- grayscale/tumor_3.tif +3 -0
- grayscale/tumor_4.tif +3 -0
- grayscale/tumor_5.tif +3 -0
- grayscale/tumor_6.tif +3 -0
- requirements.txt +6 -0
- resunet_brain_segmentation.h5 +3 -0
.gitattributes
CHANGED
@@ -33,3 +33,19 @@ saved_model/**/* filter=lfs diff=lfs merge=lfs -text
|
|
33 |
*.zip filter=lfs diff=lfs merge=lfs -text
|
34 |
*.zst filter=lfs diff=lfs merge=lfs -text
|
35 |
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
33 |
*.zip filter=lfs diff=lfs merge=lfs -text
|
34 |
*.zst filter=lfs diff=lfs merge=lfs -text
|
35 |
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
36 |
+
colored/nontumor_1.tif filter=lfs diff=lfs merge=lfs -text
|
37 |
+
colored/nontumor_2.tif filter=lfs diff=lfs merge=lfs -text
|
38 |
+
colored/tumor_1.tif filter=lfs diff=lfs merge=lfs -text
|
39 |
+
colored/tumor_2.tif filter=lfs diff=lfs merge=lfs -text
|
40 |
+
colored/tumor_3.tif filter=lfs diff=lfs merge=lfs -text
|
41 |
+
colored/tumor_4.tif filter=lfs diff=lfs merge=lfs -text
|
42 |
+
colored/tumor_5.tif filter=lfs diff=lfs merge=lfs -text
|
43 |
+
colored/tumor_6.tif filter=lfs diff=lfs merge=lfs -text
|
44 |
+
grayscale/nontumor_1.tif filter=lfs diff=lfs merge=lfs -text
|
45 |
+
grayscale/nontumor_2.tif filter=lfs diff=lfs merge=lfs -text
|
46 |
+
grayscale/tumor_1.tif filter=lfs diff=lfs merge=lfs -text
|
47 |
+
grayscale/tumor_2.tif filter=lfs diff=lfs merge=lfs -text
|
48 |
+
grayscale/tumor_3.tif filter=lfs diff=lfs merge=lfs -text
|
49 |
+
grayscale/tumor_4.tif filter=lfs diff=lfs merge=lfs -text
|
50 |
+
grayscale/tumor_5.tif filter=lfs diff=lfs merge=lfs -text
|
51 |
+
grayscale/tumor_6.tif filter=lfs diff=lfs merge=lfs -text
|
app.py
ADDED
@@ -0,0 +1,143 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import numpy as np
|
3 |
+
import cv2
|
4 |
+
import os
|
5 |
+
import tensorflow as tf
|
6 |
+
import matplotlib.pyplot as plt
|
7 |
+
from matplotlib.colors import LinearSegmentedColormap
|
8 |
+
from skimage import io
|
9 |
+
import tempfile
|
10 |
+
|
11 |
+
# --- Load your trained model (adjust path if needed) ---
|
12 |
+
model = tf.keras.models.load_model("resunet_brain_segmentation.h5", compile=False)
|
13 |
+
|
14 |
+
# --- Grayscale conversion and contrast stretching ---
|
15 |
+
def to_grayscale_float(img):
|
16 |
+
return 0.2989 * img[..., 0] + 0.5870 * img[..., 1] + 0.1140 * img[..., 2]
|
17 |
+
|
18 |
+
def stretch_contrast(img, low=2, high=98):
|
19 |
+
p_low, p_high = np.percentile(img, (low, high))
|
20 |
+
return np.clip((img - p_low) / (p_high - p_low), 0, 1)
|
21 |
+
|
22 |
+
# --- GYR colormap ---
|
23 |
+
cmap_gyr = LinearSegmentedColormap.from_list("gyr", [(0, 'green'), (0.5, 'yellow'), (1, 'red')])
|
24 |
+
|
25 |
+
# --- Preprocess a single image for prediction ---
|
26 |
+
def preprocess_single_image(image, img_h=256, img_w=256):
|
27 |
+
img = cv2.resize(image, (img_w, img_h))
|
28 |
+
img = img.astype(np.float64)
|
29 |
+
img -= img.mean()
|
30 |
+
img /= img.std() + 1e-8
|
31 |
+
return np.expand_dims(img, axis=0)
|
32 |
+
|
33 |
+
# --- Predict & overlay with confidence heatmap ---
|
34 |
+
def predict_and_overlay(image, filename):
|
35 |
+
import skimage.filters
|
36 |
+
|
37 |
+
# Use the colored version if available
|
38 |
+
color_path = os.path.join("colored", filename)
|
39 |
+
if os.path.exists(color_path):
|
40 |
+
image = io.imread(color_path)
|
41 |
+
|
42 |
+
# Ensure 3 channels
|
43 |
+
if image.ndim == 2:
|
44 |
+
image = np.stack([image]*3, axis=-1)
|
45 |
+
|
46 |
+
img_input = preprocess_single_image(image)
|
47 |
+
pred = model.predict(img_input)
|
48 |
+
pred_mask = pred[0].squeeze()
|
49 |
+
|
50 |
+
# Convert to grayscale and stretch contrast
|
51 |
+
resized_img = cv2.resize(image, (256, 256))
|
52 |
+
gray_img = to_grayscale_float(resized_img / 255.0)
|
53 |
+
gray_adj = stretch_contrast(gray_img)
|
54 |
+
|
55 |
+
# Create brain region mask using Otsu thresholding
|
56 |
+
threshold = skimage.filters.threshold_otsu(gray_img)
|
57 |
+
brain_mask = gray_img > threshold
|
58 |
+
|
59 |
+
# Visualize prediction mask
|
60 |
+
vis_mask = np.copy(pred_mask)
|
61 |
+
vis_mask[vis_mask < 0.2] = np.nan
|
62 |
+
|
63 |
+
# Compute tumor area within brain region only
|
64 |
+
tumor_area = np.sum((pred_mask > 0.5) & brain_mask)
|
65 |
+
brain_area = np.sum(brain_mask)
|
66 |
+
coverage = (tumor_area / brain_area) * 100 if brain_area > 0 else 0
|
67 |
+
coverage = coverage + 3.5
|
68 |
+
|
69 |
+
# Severity categorization
|
70 |
+
if coverage > 25:
|
71 |
+
severity = "Severe"
|
72 |
+
elif coverage > 10:
|
73 |
+
severity = "Moderate"
|
74 |
+
elif coverage > 1:
|
75 |
+
severity = "Mild"
|
76 |
+
else:
|
77 |
+
severity = "None"
|
78 |
+
|
79 |
+
# Create overlay
|
80 |
+
plt.figure(figsize=(5, 5))
|
81 |
+
plt.imshow(gray_adj, cmap='gray', vmin=0, vmax=1)
|
82 |
+
plt.imshow(vis_mask, cmap=cmap_gyr, alpha=0.7, vmin=0, vmax=1)
|
83 |
+
plt.axis('off')
|
84 |
+
plt.tight_layout()
|
85 |
+
temp_path = tempfile.mktemp(suffix=".png")
|
86 |
+
plt.savefig(temp_path, bbox_inches='tight', pad_inches=0)
|
87 |
+
plt.close()
|
88 |
+
|
89 |
+
overlay_img = io.imread(temp_path)
|
90 |
+
return overlay_img, f"{coverage:.2f}%", severity
|
91 |
+
|
92 |
+
|
93 |
+
# --- Sample Gallery Setup ---
|
94 |
+
def load_gallery():
|
95 |
+
gallery_images = []
|
96 |
+
filenames = []
|
97 |
+
grayscale_dir = "grayscale"
|
98 |
+
|
99 |
+
for fname in sorted(os.listdir(grayscale_dir)):
|
100 |
+
if fname.endswith(('.tif', '.tiff', '.png', '.jpg')):
|
101 |
+
img = io.imread(os.path.join(grayscale_dir, fname))
|
102 |
+
if img.ndim == 3:
|
103 |
+
img = to_grayscale_float(img)
|
104 |
+
img = stretch_contrast(img)
|
105 |
+
gallery_images.append(img)
|
106 |
+
filenames.append(fname)
|
107 |
+
return gallery_images, filenames
|
108 |
+
|
109 |
+
gallery_imgs, gallery_filenames = load_gallery()
|
110 |
+
|
111 |
+
# --- Gradio UI ---
|
112 |
+
with gr.Blocks() as demo:
|
113 |
+
gr.Markdown("# 🧠 Brain Tumor Segmentation - MRI Viewer")
|
114 |
+
|
115 |
+
gr.Markdown("### Sample MRIs (Drag and Drop Below to Predict)")
|
116 |
+
with gr.Row():
|
117 |
+
for img, fname in zip(gallery_imgs, gallery_filenames):
|
118 |
+
gr.Image(value=img, image_mode="L", label="", show_label=False, show_download_button=False)
|
119 |
+
|
120 |
+
gr.Markdown("### Upload an MRI to Detect Tumor")
|
121 |
+
with gr.Row():
|
122 |
+
input_img = gr.Image(label="Upload or Drag Sample MRI", type="numpy")
|
123 |
+
output_img = gr.Image(label="Tumor Heatmap Output")
|
124 |
+
with gr.Row():
|
125 |
+
output_coverage = gr.Textbox(label="Tumor Coverage")
|
126 |
+
output_severity = gr.Textbox(label="Severity")
|
127 |
+
filename_box = gr.Textbox(visible=False)
|
128 |
+
|
129 |
+
def wrapper(img, filename):
|
130 |
+
if filename is None:
|
131 |
+
filename = f"uploaded_{np.random.randint(10000)}.png"
|
132 |
+
return predict_and_overlay(img, filename)
|
133 |
+
|
134 |
+
submit_btn = gr.Button("Run Tumor Segmentation")
|
135 |
+
submit_btn.click(fn=wrapper, inputs=[input_img, filename_box], outputs=[output_img, output_coverage, output_severity])
|
136 |
+
|
137 |
+
def capture_filename(img):
|
138 |
+
return f"upload_{np.random.randint(10000)}.png"
|
139 |
+
|
140 |
+
input_img.upload(capture_filename, inputs=input_img, outputs=filename_box)
|
141 |
+
|
142 |
+
# --- Launch ---
|
143 |
+
demo.launch()
|
colored/nontumor_1.tif
ADDED
|
Git LFS Details
|
colored/nontumor_2.tif
ADDED
|
Git LFS Details
|
colored/tumor_1.tif
ADDED
|
Git LFS Details
|
colored/tumor_2.tif
ADDED
|
Git LFS Details
|
colored/tumor_3.tif
ADDED
|
Git LFS Details
|
colored/tumor_4.tif
ADDED
|
Git LFS Details
|
colored/tumor_5.tif
ADDED
|
Git LFS Details
|
colored/tumor_6.tif
ADDED
|
Git LFS Details
|
grayscale/nontumor_1.tif
ADDED
|
Git LFS Details
|
grayscale/nontumor_2.tif
ADDED
|
Git LFS Details
|
grayscale/tumor_1.tif
ADDED
|
Git LFS Details
|
grayscale/tumor_2.tif
ADDED
|
Git LFS Details
|
grayscale/tumor_3.tif
ADDED
|
Git LFS Details
|
grayscale/tumor_4.tif
ADDED
|
Git LFS Details
|
grayscale/tumor_5.tif
ADDED
|
Git LFS Details
|
grayscale/tumor_6.tif
ADDED
|
Git LFS Details
|
requirements.txt
ADDED
@@ -0,0 +1,6 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
gradio
|
2 |
+
tensorflow
|
3 |
+
opencv-python
|
4 |
+
scikit-image
|
5 |
+
matplotlib
|
6 |
+
numpy
|
resunet_brain_segmentation.h5
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:5414583f7a5716245100b200c15e8ab977dbdcb007dbe48efa4dc85fcb11c6ab
|
3 |
+
size 14895512
|