subek commited on
Commit
8e43b37
·
verified ·
1 Parent(s): bccd11b

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +44 -5
app.py CHANGED
@@ -4,6 +4,9 @@ from tensorflow.keras.preprocessing import image
4
  import numpy as np
5
  from PIL import Image
6
  import base64
 
 
 
7
 
8
  H = 256
9
  W = 256
@@ -36,16 +39,52 @@ custom_style = """
36
  st.markdown(custom_style, unsafe_allow_html=True)
37
 
38
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
39
  def main():
40
- st.title("Brain Tumor Segmentation")
41
 
42
- uploaded_file = st.file_uploader("Upload an MRI image for tumor segmentation...", type=["jpg", "png", "jpeg"])
 
43
 
44
  if uploaded_file is not None:
45
- original_image = Image.open(uploaded_file)
46
- st.image(original_image, caption="Uploaded Image", use_column_width=True)
47
 
48
- st.markdown("## Tumor Segmentation Result")
 
 
 
 
 
 
 
 
 
 
49
 
50
  if __name__ == "__main__":
51
  main()
 
 
4
  import numpy as np
5
  from PIL import Image
6
  import base64
7
+ import cv2
8
+ import numpy as np
9
+ import matplotlib.pyplot as plt
10
 
11
  H = 256
12
  W = 256
 
39
  st.markdown(custom_style, unsafe_allow_html=True)
40
 
41
 
42
+ # Function to perform inference
43
+ def perform_inference(img):
44
+ image = cv2.imread(img, cv2.IMREAD_COLOR)
45
+ original_shape = image.shape[:2]
46
+ original_image = image.copy()
47
+ image = cv2.resize(image, (W, H))
48
+ image = image/255.0
49
+ image = np.expand_dims(image, axis=0)
50
+
51
+ mask = model.predict(image, verbose=0)[0]
52
+ mask = cv2.resize(mask, (original_shape[1], original_shape[0]))
53
+ mask = mask >= 0.5
54
+ mask = np.expand_dims(mask, axis=-1)
55
+ segmented_image = original_image * mask
56
+ return original_image, mask, segmented_image
57
+
58
+ # Function to display images using Matplotlib
59
+ def show_image(image, title="Image"):
60
+ plt.imshow(image, cmap='gray')
61
+ plt.title(title)
62
+ plt.axis('off')
63
+ st.pyplot()
64
+
65
+ # Streamlit app
66
  def main():
67
+ st.title("Brain Tumor Segmentation App")
68
 
69
+ # Allow user to upload an image
70
+ uploaded_file = st.file_uploader("Upload a brain tumor image...", type=["jpg", "png", "jpeg"])
71
 
72
  if uploaded_file is not None:
73
+ # Perform inference on the uploaded image
74
+ original_image, mask, segmented_image = perform_inference(uploaded_file)
75
 
76
+ # Display original image
77
+ st.subheader("Original Image")
78
+ show_image(original_image, title="Original Image")
79
+
80
+ # Display mask
81
+ st.subheader("Mask")
82
+ show_image(mask, title="Mask")
83
+
84
+ # Display segmented image
85
+ st.subheader("Segmented Brain Tumor")
86
+ show_image(segmented_image, title="Segmented Brain Tumor")
87
 
88
  if __name__ == "__main__":
89
  main()
90
+