GeorgeSherif commited on
Commit
c74d7c6
·
1 Parent(s): 179fca4
Files changed (1) hide show
  1. app.py +19 -5
app.py CHANGED
@@ -36,6 +36,7 @@ lock = threading.Lock()
36
  with open('nearest_neighbors_with_captions.json', 'r') as f:
37
  results = json.load(f)
38
 
 
39
  def get_caption_for_image_id(image_path):
40
  """
41
  Retrieve the caption for a given image_id from the JSON data.
@@ -62,17 +63,27 @@ def get_caption_for_image_id(image_path):
62
  print("Caption not found for image_id:", image_id) # Debugging line
63
  return None
64
 
65
- # Function to get a random image that hasn’t been annotated or skipped
 
66
  def get_next_image(session_data):
67
  with lock:
68
- annotated_images = set(dataset["image_id"]) # Set of annotated images
69
- available_images = [img for img in image_files if img not in annotated_images]
 
 
 
 
 
 
 
 
70
  # Check if the user already has an image
71
  if session_data["current_image"] is None and available_images:
72
  # Assign a new random image to the user
73
  session_data["current_image"] = random.choice(available_images)
74
  return os.path.join(image_folder, session_data["current_image"]) if session_data["current_image"] else None
75
 
 
76
  # Function to save the annotation to Hugging Face dataset and fetch the next image
77
  def save_annotation(caption, session_data):
78
  global dataset # Declare global dataset at the start of the function
@@ -107,8 +118,9 @@ def save_annotation(caption, session_data):
107
  dataset.push_to_hub(dataset_name)
108
  print("Pushed updated dataset")
109
 
110
- # Clear user's current image so they get a new one next time
111
- session_data["current_image"] = None
 
112
 
113
  # Fetch the next image
114
  next_image = get_next_image(session_data)
@@ -118,6 +130,7 @@ def save_annotation(caption, session_data):
118
  else:
119
  return gr.update(visible=False), gr.update(value="All images have been annotated!"), gr.update(value="")
120
 
 
121
  def initialize_interface(session_data):
122
  next_image = get_next_image(session_data)
123
  if next_image:
@@ -127,6 +140,7 @@ def initialize_interface(session_data):
127
  else:
128
  return gr.update(visible=False), gr.update(value="All images have been annotated!")
129
 
 
130
  # Build the Gradio interface
131
  with gr.Blocks() as demo:
132
  gr.Markdown("# Image Captioning Tool")
 
36
  with open('nearest_neighbors_with_captions.json', 'r') as f:
37
  results = json.load(f)
38
 
39
+
40
  def get_caption_for_image_id(image_path):
41
  """
42
  Retrieve the caption for a given image_id from the JSON data.
 
63
  print("Caption not found for image_id:", image_id) # Debugging line
64
  return None
65
 
66
+
67
+ # Function to get a random image that hasn’t been fully annotated
68
  def get_next_image(session_data):
69
  with lock:
70
+ annotated_images = {item["image_id"]: item["annotation_count"] for item in dataset}
71
+
72
+ # Available images filter
73
+ available_images = [
74
+ img for img in image_files
75
+ if img not in annotated_images or
76
+ ("val" in img and annotated_images[img] < 5) or
77
+ ("val" not in img and annotated_images[img] == 0)
78
+ ]
79
+
80
  # Check if the user already has an image
81
  if session_data["current_image"] is None and available_images:
82
  # Assign a new random image to the user
83
  session_data["current_image"] = random.choice(available_images)
84
  return os.path.join(image_folder, session_data["current_image"]) if session_data["current_image"] else None
85
 
86
+
87
  # Function to save the annotation to Hugging Face dataset and fetch the next image
88
  def save_annotation(caption, session_data):
89
  global dataset # Declare global dataset at the start of the function
 
118
  dataset.push_to_hub(dataset_name)
119
  print("Pushed updated dataset")
120
 
121
+ # Clear user's current image if the validation image has been annotated five times
122
+ if ("val" not in image_id) or (annotation_count > 2):
123
+ session_data["current_image"] = None
124
 
125
  # Fetch the next image
126
  next_image = get_next_image(session_data)
 
130
  else:
131
  return gr.update(visible=False), gr.update(value="All images have been annotated!"), gr.update(value="")
132
 
133
+
134
  def initialize_interface(session_data):
135
  next_image = get_next_image(session_data)
136
  if next_image:
 
140
  else:
141
  return gr.update(visible=False), gr.update(value="All images have been annotated!")
142
 
143
+
144
  # Build the Gradio interface
145
  with gr.Blocks() as demo:
146
  gr.Markdown("# Image Captioning Tool")