Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,73 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
import tensorflow as tf
|
3 |
+
from tensorflow.keras.preprocessing import image
|
4 |
+
import numpy as np
|
5 |
+
from PIL import Image
|
6 |
+
import base64
|
7 |
+
|
8 |
+
# Load the pre-trained brain tumor segmentation model
|
9 |
+
model_path = 'brain_tumor_segmentation_model.h5' # Replace with your actual model path
|
10 |
+
model = tf.keras.models.load_model(model_path)
|
11 |
+
img_size = (256, 256) # Adjust based on your model's input size
|
12 |
+
|
13 |
+
st.set_page_config(
|
14 |
+
page_title="Brain Tumor Segmentation App",
|
15 |
+
page_icon=":brain:",
|
16 |
+
layout="wide"
|
17 |
+
)
|
18 |
+
|
19 |
+
custom_style = """
|
20 |
+
<style>
|
21 |
+
div[data-testid="stToolbar"],
|
22 |
+
div[data-testid="stDecoration"],
|
23 |
+
div[data-testid="stStatusWidget"],
|
24 |
+
#MainMenu,
|
25 |
+
header,
|
26 |
+
footer {
|
27 |
+
visibility: hidden;
|
28 |
+
height: 0%;
|
29 |
+
}
|
30 |
+
</style>
|
31 |
+
"""
|
32 |
+
|
33 |
+
st.markdown(custom_style, unsafe_allow_html=True)
|
34 |
+
|
35 |
+
def preprocess_image(img):
|
36 |
+
img = img.resize(img_size)
|
37 |
+
img_array = image.img_to_array(img)
|
38 |
+
img_array = img_array / 255.0
|
39 |
+
img_array = np.expand_dims(img_array, axis=0)
|
40 |
+
return img_array
|
41 |
+
|
42 |
+
def predict_tumor_segmentation(img):
|
43 |
+
img_array = preprocess_image(img)
|
44 |
+
segmentation_mask = model.predict(img_array)
|
45 |
+
segmentation_mask = np.squeeze(segmentation_mask, axis=0)
|
46 |
+
return segmentation_mask
|
47 |
+
|
48 |
+
def display_segmentation_result(original_image, segmentation_mask):
|
49 |
+
original_image = np.array(original_image)
|
50 |
+
segmentation_mask = (segmentation_mask > 0.5).astype(np.uint8) * 255
|
51 |
+
segmentation_mask = Image.fromarray(segmentation_mask, 'L')
|
52 |
+
|
53 |
+
st.image([original_image, segmentation_mask], caption=['Original Image', 'Segmentation Mask'], use_column_width=True)
|
54 |
+
|
55 |
+
def main():
|
56 |
+
st.title("Brain Tumor Segmentation App")
|
57 |
+
|
58 |
+
uploaded_file = st.file_uploader("Upload an MRI image for tumor segmentation...", type=["jpg", "png", "jpeg"])
|
59 |
+
|
60 |
+
if uploaded_file is not None:
|
61 |
+
original_image = Image.open(uploaded_file)
|
62 |
+
st.image(original_image, caption="Uploaded Image", use_column_width=True)
|
63 |
+
|
64 |
+
st.markdown("## Tumor Segmentation Result")
|
65 |
+
|
66 |
+
# Perform segmentation
|
67 |
+
segmentation_mask = predict_tumor_segmentation(original_image)
|
68 |
+
|
69 |
+
# Display the segmentation result
|
70 |
+
display_segmentation_result(original_image, segmentation_mask)
|
71 |
+
|
72 |
+
if __name__ == "__main__":
|
73 |
+
main()
|