Spaces:
Running
Running
Goodnight7
commited on
Commit
•
d377323
1
Parent(s):
c868da9
Update app.py
Browse files
app.py
CHANGED
@@ -1,45 +1,45 @@
|
|
1 |
-
import streamlit as st
|
2 |
-
import numpy as np
|
3 |
-
import cv2
|
4 |
-
from PIL import Image
|
5 |
-
import tensorflow as tf
|
6 |
-
from tensorflow.keras.models import load_model
|
7 |
-
|
8 |
-
@st.cache_resource
|
9 |
-
def load_unet_model():
|
10 |
-
return load_model('
|
11 |
-
|
12 |
-
model = load_unet_model()
|
13 |
-
|
14 |
-
def preprocess_image(image):
|
15 |
-
image = image.resize((256, 256))
|
16 |
-
image = np.array(image) / 255.0
|
17 |
-
image = np.expand_dims(image, axis=0)
|
18 |
-
return image
|
19 |
-
|
20 |
-
def predict_mask(image):
|
21 |
-
processed_image = preprocess_image(image)
|
22 |
-
predicted_mask = model.predict(processed_image)
|
23 |
-
predicted_mask = (predicted_mask > 0.5).astype(np.uint8)
|
24 |
-
return predicted_mask[0, :, :, 0]
|
25 |
-
|
26 |
-
st.title('Medical Image Segmentation with U-Net (Mohamed Arbi Nsibi)')
|
27 |
-
st.subheader("Note: The model's segmentation accuracy is not that accurate because of the small training dataset. Larger and more diverse data could improve performance ")
|
28 |
-
|
29 |
-
uploaded_file = st.file_uploader("Choose an image...", type=["jpg", "jpeg", "png"])
|
30 |
-
|
31 |
-
if uploaded_file is not None:
|
32 |
-
image = Image.open(uploaded_file)
|
33 |
-
st.image(image, caption='Uploaded Image', use_column_width=True)
|
34 |
-
|
35 |
-
if st.button('Segment Image'):
|
36 |
-
mask = predict_mask(image)
|
37 |
-
|
38 |
-
st.image(mask * 255, caption='Segmentation Mask', use_column_width=True)
|
39 |
-
|
40 |
-
overlay = np.zeros((256, 256, 3), dtype=np.uint8)
|
41 |
-
overlay[:,:,1] = mask * 255
|
42 |
-
original_resized = np.array(image.resize((256, 256)))
|
43 |
-
overlayed_image = cv2.addWeighted(original_resized, 0.7, overlay, 0.3, 0)
|
44 |
-
|
45 |
st.image(overlayed_image, caption='Segmentation Overlay', use_column_width=True)
|
|
|
1 |
+
import streamlit as st
|
2 |
+
import numpy as np
|
3 |
+
import cv2
|
4 |
+
from PIL import Image
|
5 |
+
import tensorflow as tf
|
6 |
+
from tensorflow.keras.models import load_model
|
7 |
+
|
8 |
+
@st.cache_resource
|
9 |
+
def load_unet_model():
|
10 |
+
return load_model('best_unet_model.keras')
|
11 |
+
|
12 |
+
model = load_unet_model()
|
13 |
+
|
14 |
+
def preprocess_image(image):
|
15 |
+
image = image.resize((256, 256))
|
16 |
+
image = np.array(image) / 255.0
|
17 |
+
image = np.expand_dims(image, axis=0)
|
18 |
+
return image
|
19 |
+
|
20 |
+
def predict_mask(image):
|
21 |
+
processed_image = preprocess_image(image)
|
22 |
+
predicted_mask = model.predict(processed_image)
|
23 |
+
predicted_mask = (predicted_mask > 0.5).astype(np.uint8)
|
24 |
+
return predicted_mask[0, :, :, 0]
|
25 |
+
|
26 |
+
st.title('Medical Image Segmentation with U-Net (Mohamed Arbi Nsibi)')
|
27 |
+
st.subheader("Note: The model's segmentation accuracy is not that accurate because of the small training dataset. Larger and more diverse data could improve performance ")
|
28 |
+
|
29 |
+
uploaded_file = st.file_uploader("Choose an image...", type=["jpg", "jpeg", "png"])
|
30 |
+
|
31 |
+
if uploaded_file is not None:
|
32 |
+
image = Image.open(uploaded_file)
|
33 |
+
st.image(image, caption='Uploaded Image', use_column_width=True)
|
34 |
+
|
35 |
+
if st.button('Segment Image'):
|
36 |
+
mask = predict_mask(image)
|
37 |
+
|
38 |
+
st.image(mask * 255, caption='Segmentation Mask', use_column_width=True)
|
39 |
+
|
40 |
+
overlay = np.zeros((256, 256, 3), dtype=np.uint8)
|
41 |
+
overlay[:,:,1] = mask * 255
|
42 |
+
original_resized = np.array(image.resize((256, 256)))
|
43 |
+
overlayed_image = cv2.addWeighted(original_resized, 0.7, overlay, 0.3, 0)
|
44 |
+
|
45 |
st.image(overlayed_image, caption='Segmentation Overlay', use_column_width=True)
|