kishoreb4 commited on
Commit
a0f8422
·
verified ·
1 Parent(s): 822fcd2

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +89 -8
app.py CHANGED
@@ -1,4 +1,12 @@
1
  import streamlit as st
 
 
 
 
 
 
 
 
2
  import tensorflow as tf
3
  from tensorflow.keras import backend
4
  import numpy as np
@@ -23,13 +31,6 @@ try:
23
  except Exception as e:
24
  st.sidebar.error(f"GPU config error: {e}")
25
 
26
- st.set_page_config(
27
- page_title="Pet Segmentation with SegFormer",
28
- page_icon="🐶",
29
- layout="wide",
30
- initial_sidebar_state="expanded"
31
- )
32
-
33
  # Constants for image preprocessing
34
  IMAGE_SIZE = 512
35
  OUTPUT_SIZE = 128
@@ -281,6 +282,54 @@ def create_overlay(image, mask, alpha=0.5):
281
 
282
  return overlay
283
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
284
  def main():
285
  st.title("🐶 Pet Segmentation with SegFormer")
286
  st.markdown("""
@@ -416,6 +465,9 @@ def main():
416
  # Calculate IoU if ground truth is uploaded
417
  if uploaded_mask is not None:
418
  try:
 
 
 
419
  # Read the mask file
420
  mask_data = uploaded_mask.read()
421
  mask_io = io.BytesIO(mask_data)
@@ -443,10 +495,39 @@ def main():
443
  with col3:
444
  fg_iou = calculate_iou(gt_mask, resized_mask, 2)
445
  st.metric("Foreground IoU", f"{fg_iou:.4f}")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
446
  except Exception as e:
447
  st.error(f"Error processing ground truth mask: {e}")
448
  st.write("Please ensure the mask is valid and has the correct format.")
449
-
 
 
 
 
 
 
 
 
 
 
 
 
 
 
450
  # Download buttons
451
  col1, col2 = st.columns(2)
452
 
 
1
  import streamlit as st
2
+ # THIS MUST BE THE FIRST STREAMLIT COMMAND
3
+ st.set_page_config(
4
+ page_title="Pet Segmentation with SegFormer",
5
+ page_icon="🐶",
6
+ layout="wide",
7
+ initial_sidebar_state="expanded"
8
+ )
9
+
10
  import tensorflow as tf
11
  from tensorflow.keras import backend
12
  import numpy as np
 
31
  except Exception as e:
32
  st.sidebar.error(f"GPU config error: {e}")
33
 
 
 
 
 
 
 
 
34
  # Constants for image preprocessing
35
  IMAGE_SIZE = 512
36
  OUTPUT_SIZE = 128
 
282
 
283
  return overlay
284
 
285
+ def display_results_side_by_side(original_image, ground_truth_mask=None, predicted_mask=None):
286
+ """
287
+ Display results in a side-by-side format similar to colab_code.py
288
+
289
+ Args:
290
+ original_image: Original input image
291
+ ground_truth_mask: Optional ground truth segmentation mask
292
+ predicted_mask: Predicted segmentation mask
293
+ """
294
+ # Determine how many images to display
295
+ cols = 1 + (ground_truth_mask is not None) + (predicted_mask is not None)
296
+
297
+ # Create a figure with multiple columns
298
+ st.write("### Segmentation Results Comparison")
299
+
300
+ col_list = st.columns(cols)
301
+
302
+ # Display original image
303
+ with col_list[0]:
304
+ st.markdown("**Original Image**")
305
+ st.image(original_image, use_column_width=True)
306
+
307
+ # Display ground truth if available
308
+ if ground_truth_mask is not None:
309
+ with col_list[1]:
310
+ st.markdown("**Ground Truth Mask**")
311
+
312
+ # Colorize ground truth if needed
313
+ if len(ground_truth_mask.shape) == 2:
314
+ gt_display = colorize_mask(ground_truth_mask)
315
+ else:
316
+ gt_display = ground_truth_mask
317
+
318
+ st.image(gt_display, use_column_width=True)
319
+
320
+ # Display prediction
321
+ if predicted_mask is not None:
322
+ with col_list[2 if ground_truth_mask is not None else 1]:
323
+ st.markdown("**Predicted Mask**")
324
+
325
+ # Colorize prediction if needed
326
+ if len(predicted_mask.shape) == 2:
327
+ pred_display = colorize_mask(predicted_mask)
328
+ else:
329
+ pred_display = predicted_mask
330
+
331
+ st.image(pred_display, use_column_width=True)
332
+
333
  def main():
334
  st.title("🐶 Pet Segmentation with SegFormer")
335
  st.markdown("""
 
465
  # Calculate IoU if ground truth is uploaded
466
  if uploaded_mask is not None:
467
  try:
468
+ # Reset the file pointer to the beginning
469
+ uploaded_mask.seek(0)
470
+
471
  # Read the mask file
472
  mask_data = uploaded_mask.read()
473
  mask_io = io.BytesIO(mask_data)
 
495
  with col3:
496
  fg_iou = calculate_iou(gt_mask, resized_mask, 2)
497
  st.metric("Foreground IoU", f"{fg_iou:.4f}")
498
+
499
+ # For display (original size)
500
+ # Reset the file pointer again
501
+ uploaded_mask.seek(0)
502
+ mask_data = uploaded_mask.read()
503
+ mask_io = io.BytesIO(mask_data)
504
+ gt_mask_for_display = np.array(Image.open(mask_io))
505
+
506
+ # Side-by-side display
507
+ display_results_side_by_side(
508
+ original_img,
509
+ ground_truth_mask=gt_mask_for_display,
510
+ predicted_mask=colorized_mask
511
+ )
512
+
513
  except Exception as e:
514
  st.error(f"Error processing ground truth mask: {e}")
515
  st.write("Please ensure the mask is valid and has the correct format.")
516
+
517
+ # Even with an error, try to display results without the ground truth
518
+ display_results_side_by_side(
519
+ original_img,
520
+ ground_truth_mask=None,
521
+ predicted_mask=colorized_mask
522
+ )
523
+ else:
524
+ # No ground truth, just display original and prediction
525
+ display_results_side_by_side(
526
+ original_img,
527
+ ground_truth_mask=None,
528
+ predicted_mask=colorized_mask
529
+ )
530
+
531
  # Download buttons
532
  col1, col2 = st.columns(2)
533