Spaces:
Runtime error
Runtime error
File size: 8,279 Bytes
9fa9058 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 |
import gradio as gr
import json
import os
# root directory of image files
# each action should be saved under subdirectories
# input filename
image_root = "forward_dynamics_qa_pairs_v5_w_newline"
# output filename
output_file = "v5_forward_dynamics_user_choices.json"
# # input filename
# image_root = "/Users/bryan/Desktop/wkdir/VLM/src/human-annotation-interface/inverse_dynamics_qa_pairs_v5_w_newline"
# # output filename
# output_file = "/Users/bryan/Desktop/wkdir/VLM/src/human-annotation-interface/v5_inverse_dynamics_user_choices.json"
if not os.path.exists(output_file):
with open(output_file, 'w') as f:
json.dump({}, f)
def load_action_images():
action_images = {}
for action in os.listdir(image_root):
action_dir = os.path.join(image_root, action)
if os.path.isdir(action_dir):
images = [f for f in os.listdir(action_dir) if f.endswith('.jpg')]
images.sort() # Ensure files are sorted in ascending order
action_images[action] = images
return action_images
def load_user_choices():
with open(output_file, 'r') as f:
return json.load(f)
def save_user_choice(action, image_name, choice, ground_truth):
image_name_no_ext = os.path.splitext(image_name)[0] # remove ".jpg" ext
user_choices = load_user_choices()
is_correct = (choice == ground_truth)
# save result
if action not in user_choices:
user_choices[action] = {}
user_choices[action][image_name_no_ext] = {
"choice": choice,
"ground_truth": ground_truth,
"is_correct": is_correct
}
with open(output_file, 'w') as f:
json.dump(user_choices, f, indent=2)
def get_content_at_index(action, index):
if action not in action_images or index < 0 or index >= len(action_images[action]):
return None, None, "No more images", "", False, False
# image
image_name = action_images[action][index]
image_path = os.path.join(image_root, action, image_name)
# text prompt
text_prompt_path = image_path.replace(".jpg", ".txt")
text_prompt = (
open(text_prompt_path, 'r').read().strip()
if os.path.exists(text_prompt_path)
else "No text prompt available"
)
# Apply font size styling to text_prompt
text_prompt = f"<div style='font-size: 1.25em;'>{text_prompt}</div>"
# ground truth
ground_truth_path = image_path.replace(".jpg", "_answer.txt")
ground_truth = (
open(ground_truth_path, 'r').read().strip()
if os.path.exists(ground_truth_path)
else "No ground truth available"
)
# button states
enable_prev = index > 0
enable_next = index < len(action_images[action]) - 1
return image_path, image_name, text_prompt, ground_truth, enable_prev, enable_next
# navigate among questions
def navigate(action, index, direction):
# update index
new_index = max(0, min(index + direction, len(action_images[action]) - 1))
# retrieve context
image_path, _, text_prompt, ground_truth, enable_prev, enable_next = get_content_at_index(action, new_index)
# Apply font size styling to text_prompt
styled_text_prompt = f"<div style='font-size: 1.25em;'>{text_prompt}</div>"
return (
image_path,
styled_text_prompt,
ground_truth,
gr.update(value=""),
gr.update(interactive=enable_prev),
gr.update(interactive=enable_next),
new_index
)
# Handle user choice submission
def submit_choice(action, index, choice, ground_truth):
if action not in action_images or index < 0 or index >= len(action_images[action]):
return "Invalid demo or keyframe index."
image_name = action_images[action][index]
save_user_choice(action, image_name, choice, ground_truth) # Save user choice
if choice == ground_truth:
color = "green"
else:
color = "red"
return f'<div style="font-size: 1.25em; color:{color}">Ground Truth: {ground_truth}</div>'
def change_action(action):
if action not in action_images:
return None, "No images available", "No text prompt available", "", gr.update(interactive=False), gr.update(interactive=False), action, 0
# Get the first image of the new action
image_path, image_name, text_prompt, ground_truth, enable_prev, enable_next = get_content_at_index(action, 0)
# Apply font size styling to text_prompt
styled_text_prompt = f"<div style='font-size: 1.25em;'>{text_prompt}</div>"
# Reset states
enable_prev = gr.update(interactive=False) # Disable "Previous" as we're at the start
enable_next = gr.update(interactive=enable_next) # Enable "Next" if there are more images
return image_path, styled_text_prompt, ground_truth, gr.update(value=""), enable_prev, enable_next, action, 0
# initialize data
action_images = load_action_images()
def initialize_app():
if not action_images:
return None, None, "No actions available", "", gr.update(interactive=False), gr.update(interactive=False), "", 0
first_action = list(action_images.keys())[0]
image_path, image_name, text_prompt, ground_truth, enable_prev, enable_next = get_content_at_index(first_action, 0)
# Force the Previous button to be disabled during initialization
enable_prev = gr.update(interactive=False)
return image_path, image_name, text_prompt, ground_truth, enable_prev, gr.update(interactive=enable_next), first_action, 0
first_image, first_image_name, first_text_prompt, first_ground_truth, enable_prev, enable_next, first_action, first_index = initialize_app()
# Gradio interface
with gr.Blocks() as app:
gr.Markdown("# VLM Embodied Benchmark Human Annotation Interface")
# states: action, index, ground truth
current_action = gr.State(value=first_action)
current_index = gr.State(value=first_index)
current_ground_truth = gr.State(value=first_ground_truth)
# UI components
action_dropdown = gr.Dropdown(choices=list(action_images.keys()), value=first_action, label="Select Demo to Annotate")
# image = gr.Image(value=first_image, interactive=False, width=500)
image = gr.Image(value=first_image, interactive=False, width=1500)
text_prompt = gr.Markdown(value=f"<div style='font-size: 1.25em;'>{first_text_prompt}</div>")
with gr.Row(): # nav buttons
prev_button = gr.Button("Previous", interactive=False) # Explicitly disabled during initialization
next_button = gr.Button("Next", interactive=enable_next["interactive"])
with gr.Row(): # choice buttons
a_button = gr.Button("A")
b_button = gr.Button("B")
c_button = gr.Button("C")
d_button = gr.Button("D")
ground_truth_display = gr.Markdown(value="")
# change action dropdown
action_dropdown.change(
fn=change_action,
inputs=[action_dropdown],
outputs=[image, text_prompt, current_ground_truth, ground_truth_display, prev_button, next_button, current_action, current_index]
)
# click on navigation buttons
nav_input = [current_action, current_index]
nav_output = [image, text_prompt, current_ground_truth, ground_truth_display, prev_button, next_button, current_index]
prev_button.click(
fn=lambda action, index: navigate(action, index, -1),
inputs=nav_input, outputs=nav_output
)
next_button.click(
fn=lambda action, index: navigate(action, index, 1),
inputs=[current_action, current_index],
outputs=nav_output
)
# click on choice buttons
input_param = [current_action, current_index, current_ground_truth]
output_param= [ground_truth_display]
a_button.click(
fn=lambda action, index, gt: submit_choice(action, index, "A", gt),
inputs=input_param, outputs=output_param)
b_button.click(
fn=lambda action, index, gt: submit_choice(action, index, "B", gt),
inputs=input_param, outputs=output_param)
c_button.click(
fn=lambda action, index, gt: submit_choice(action, index, "C", gt),
inputs=input_param, outputs=output_param)
d_button.click(
fn=lambda action, index, gt: submit_choice(action, index, "D", gt),
inputs=input_param, outputs=output_param)
app.launch(share=True) |