junwann commited on
Commit
3025cf4
·
1 Parent(s): a860625
Files changed (1) hide show
  1. app.py +12 -16
app.py CHANGED
@@ -53,7 +53,7 @@ def prepare_masks(filtered_annotations):
53
  segmentation = annotation["segmentation"]
54
  mask_id = annotation["id"]
55
  decoded_mask = decode_segmentation(segmentation) # Decode binary mask
56
- masks.append((decoded_mask, f"mask {idx + 1}")) # Add mask and its label
57
  return masks
58
 
59
  # Load image and annotations dynamically
@@ -80,24 +80,20 @@ def load_image_and_masks(image_id):
80
  return image_path, masks
81
 
82
  # Gradio event function to display captions
83
- # def display_caption(evt: gr.SelectData, masks, image_id):
84
- # # Extract the mask ID from the label
85
- # mask_id = int(masks[evt.index][1]) # Get the label corresponding to the selected mask
86
- # caption_data = captions_data.get(image_id, {}).get(str(mask_id), "No caption found")
87
- # return caption_data
88
-
89
  def display_caption(evt: gr.SelectData, masks, image_id):
90
- # Get the label, e.g., "mask 1"
91
- label = masks[evt.index][1]
92
- # Extract the numeric part: split by "mask" and strip whitespace
93
- try:
94
- mask_number = int(label.split("mask")[1].strip())
95
- except (IndexError, ValueError):
96
- return "Invalid mask label"
97
-
98
- caption_data = captions_data.get(image_id, {}).get(str(mask_number), "No caption found")
99
  return caption_data
100
 
 
 
 
 
 
 
 
101
  # Gradio event function to update image
102
  def update_image(image_index):
103
  image_id = image_ids[image_index]
 
53
  segmentation = annotation["segmentation"]
54
  mask_id = annotation["id"]
55
  decoded_mask = decode_segmentation(segmentation) # Decode binary mask
56
+ masks.append((decoded_mask, f"{mask_id}")) # Add mask and its label
57
  return masks
58
 
59
  # Load image and annotations dynamically
 
80
  return image_path, masks
81
 
82
  # Gradio event function to display captions
 
 
 
 
 
 
83
  def display_caption(evt: gr.SelectData, masks, image_id):
84
+ # Extract the mask ID from the label
85
+ # mask_id = int(masks[evt.index][1]) # Get the label corresponding to the selected mask
86
+ mask_id = masks[evt.index][1] # Get the label corresponding to the selected mask
87
+ caption_data = captions_data.get(image_id, {}).get(str(mask_id), "No caption found")
 
 
 
 
 
88
  return caption_data
89
 
90
+ # def display_caption(evt: gr.SelectData, masks, image_id):
91
+ # # Get the label, e.g., "mask 1"
92
+ # label = masks[evt.index][1]
93
+ # # Use the label directly as key in the captions_data
94
+ # caption_data = captions_data.get(image_id, {}).get(label, "No caption found")
95
+ # return caption_data
96
+
97
  # Gradio event function to update image
98
  def update_image(image_index):
99
  image_id = image_ids[image_index]