Spaces:
Sleeping
Update app.py
Browse files# Key Changes and Explanations:
## Image Handling in save_labeled_data():
The save_labeled_data function now correctly converts the PIL Image object to bytes using image.convert("RGB").tobytes() before storing it in the labeled_data list. The image might be None, so we account for that possibility.
## Event Handling (Clicking Buttons):
In labeling_ui(), the cool_button.click() and not_cool_button.click() are now correctly set up.
The fn argument is set to a lambda function that calls save_labeled_data with the appropriate arguments.
The inputs argument specifies the components that will provide input values to the function.
The outputs argument specifies the components that will be updated by the function's return values.
## Initializing the UI:
In labeling_ui(), after getting the first sample with get_next_sample(), we now use:
image_component.update(value=image)
metadata_text.update(value=str(metadata["bounds"]))
label_count_text.update(value=f"Labeled {count} samples.")
This ensures that the components are updated with the initial values.
# With these changes, your Gradio interface should now:
Correctly display a random image from the dataset when it loads.
Allow you to label images by clicking "Cool" or "Not Cool".
Properly save the labeled data to labeled_data.json.
@@ -10,8 +10,8 @@ import json
|
|
10 |
DATASET_SUBSET = "satellogic"
|
11 |
NUM_SAMPLES_TO_LABEL = 100 # You can adjust this
|
12 |
LABELED_DATA_FILE = "labeled_data.json"
|
13 |
-
DISPLAY_N_COOL = 5
|
14 |
-
SAMPLE_SEED = 10
|
15 |
# --- Load Dataset ---
|
16 |
dataset = ev.load_dataset(DATASET_SUBSET, shards=[SAMPLE_SEED])
|
17 |
data_iter = iter(dataset)
|
@@ -42,8 +42,15 @@ def get_next_sample():
|
|
42 |
# --- Save Labeled Data ---
|
43 |
def save_labeled_data(image, metadata, label):
|
44 |
global labeled_data
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
45 |
labeled_data.append({
|
46 |
-
"image":
|
47 |
"metadata": metadata,
|
48 |
"label": label
|
49 |
})
|
@@ -54,7 +61,7 @@ def save_labeled_data(image, metadata, label):
|
|
54 |
image, metadata, count = get_next_sample()
|
55 |
|
56 |
if image is None:
|
57 |
-
|
58 |
|
59 |
return "", image, str(metadata["bounds"]), f"Labeled {count} samples."
|
60 |
|
@@ -62,26 +69,34 @@ def save_labeled_data(image, metadata, label):
|
|
62 |
|
63 |
# --- Labeling UI ---
|
64 |
def labeling_ui():
|
65 |
-
|
66 |
-
|
67 |
-
|
68 |
-
|
69 |
-
|
70 |
-
|
71 |
-
|
72 |
-
|
73 |
-
|
74 |
-
|
75 |
-
|
76 |
-
|
77 |
-
|
78 |
-
|
79 |
-
|
80 |
-
|
81 |
-
|
82 |
-
|
83 |
-
|
84 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
85 |
|
86 |
# --- Display UI ---
|
87 |
def display_ui():
|
@@ -131,8 +146,8 @@ def display_ui():
|
|
131 |
|
132 |
# Initialize
|
133 |
debug, image, gallery = refresh_display()
|
134 |
-
new_image_component.value
|
135 |
-
cool_images_gallery.value
|
136 |
|
137 |
# --- Main Interface ---
|
138 |
with gr.Blocks() as demo:
|
|
|
10 |
DATASET_SUBSET = "satellogic"
|
11 |
NUM_SAMPLES_TO_LABEL = 100 # You can adjust this
|
12 |
LABELED_DATA_FILE = "labeled_data.json"
|
13 |
+
DISPLAY_N_COOL = 5 # How many "cool" examples to display alongside each new image.
|
14 |
+
SAMPLE_SEED = 10 # The seed to use when sampling the dataset for the demo.
|
15 |
# --- Load Dataset ---
|
16 |
dataset = ev.load_dataset(DATASET_SUBSET, shards=[SAMPLE_SEED])
|
17 |
data_iter = iter(dataset)
|
|
|
42 |
# --- Save Labeled Data ---
|
43 |
def save_labeled_data(image, metadata, label):
|
44 |
global labeled_data
|
45 |
+
|
46 |
+
# Convert PIL Image to bytes before saving
|
47 |
+
if image is not None:
|
48 |
+
image_bytes = image.convert("RGB").tobytes()
|
49 |
+
else:
|
50 |
+
image_bytes = None
|
51 |
+
|
52 |
labeled_data.append({
|
53 |
+
"image": image_bytes,
|
54 |
"metadata": metadata,
|
55 |
"label": label
|
56 |
})
|
|
|
61 |
image, metadata, count = get_next_sample()
|
62 |
|
63 |
if image is None:
|
64 |
+
return "No more samples", gr.Image.update(value=None), "", f"Labeled {count} samples."
|
65 |
|
66 |
return "", image, str(metadata["bounds"]), f"Labeled {count} samples."
|
67 |
|
|
|
69 |
|
70 |
# --- Labeling UI ---
|
71 |
def labeling_ui():
|
72 |
+
with gr.Row():
|
73 |
+
with gr.Column():
|
74 |
+
image_component = gr.Image(label="Satellite Image", type="pil")
|
75 |
+
metadata_text = gr.Textbox(label="Metadata (Bounds)")
|
76 |
+
label_count_text = gr.Textbox(label="Label Count")
|
77 |
+
|
78 |
+
with gr.Row():
|
79 |
+
cool_button = gr.Button("Cool")
|
80 |
+
not_cool_button = gr.Button("Not Cool")
|
81 |
+
|
82 |
+
# Handle button clicks
|
83 |
+
cool_button.click(
|
84 |
+
fn=lambda image, metadata: save_labeled_data(image, metadata, "cool"),
|
85 |
+
inputs=[image_component, metadata_text],
|
86 |
+
outputs=[gr.Textbox(label="Debug"), image_component, metadata_text, label_count_text]
|
87 |
+
)
|
88 |
+
not_cool_button.click(
|
89 |
+
fn=lambda image, metadata: save_labeled_data(image, metadata, "not cool"),
|
90 |
+
inputs=[image_component, metadata_text],
|
91 |
+
outputs=[gr.Textbox(label="Debug"), image_component, metadata_text, label_count_text]
|
92 |
+
)
|
93 |
+
|
94 |
+
# Initialize with the first sample
|
95 |
+
image, metadata, count = get_next_sample()
|
96 |
+
if image is not None:
|
97 |
+
image_component.update(value=image) # Use .update() to set the value
|
98 |
+
metadata_text.update(value=str(metadata["bounds"])) # Use .update()
|
99 |
+
label_count_text.update(value=f"Labeled {count} samples.") # Use .update()
|
100 |
|
101 |
# --- Display UI ---
|
102 |
def display_ui():
|
|
|
146 |
|
147 |
# Initialize
|
148 |
debug, image, gallery = refresh_display()
|
149 |
+
new_image_component.update(value=image)
|
150 |
+
cool_images_gallery.update(value=gallery)
|
151 |
|
152 |
# --- Main Interface ---
|
153 |
with gr.Blocks() as demo:
|