Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,123 +1,94 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
import earthview as ev
|
| 3 |
-
|
| 4 |
-
import numpy as np
|
| 5 |
import random
|
|
|
|
| 6 |
import os
|
| 7 |
-
import json
|
| 8 |
-
import utils
|
| 9 |
-
from pandas import DataFrame
|
| 10 |
-
|
| 11 |
-
# --- Configuration ---
|
| 12 |
-
DATASET_SUBSET = "satellogic"
|
| 13 |
-
LABELED_DATA_FILE = "labeled_data.json"
|
| 14 |
-
SAMPLE_SEED = 10 # The seed to use when sampling the dataset for the demo.
|
| 15 |
|
| 16 |
-
#
|
| 17 |
-
dataset = ev.load_dataset(
|
| 18 |
data_iter = iter(dataset)
|
| 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 |
-
state.value = initial_state
|
| 104 |
-
|
| 105 |
-
cool_button.click(
|
| 106 |
-
fn=lambda image, label, state: save_labeled_data(image, label, state),
|
| 107 |
-
inputs=[image_component, gr.Textbox(visible=False, value="cool"), state],
|
| 108 |
-
outputs=[gr.Textbox(label="Debug"), image_component, table]
|
| 109 |
-
)
|
| 110 |
-
not_cool_button.click(
|
| 111 |
-
fn=lambda image, label, state: save_labeled_data(image, label, state),
|
| 112 |
-
inputs=[image_component, gr.Textbox(visible=False, value="not cool"), state],
|
| 113 |
-
outputs=[gr.Textbox(label="Debug"), image_component, table]
|
| 114 |
-
)
|
| 115 |
-
|
| 116 |
-
# --- Main Interface ---
|
| 117 |
-
with gr.Blocks() as demo:
|
| 118 |
-
gr.Markdown("# TerraNomaly")
|
| 119 |
-
with gr.Tabs():
|
| 120 |
-
with gr.TabItem("Labeling"):
|
| 121 |
-
labeling_ui()
|
| 122 |
-
|
| 123 |
-
demo.launch(debug=True)
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
import earthview as ev
|
| 3 |
+
import utils
|
|
|
|
| 4 |
import random
|
| 5 |
+
import pandas as pd
|
| 6 |
import os
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 7 |
|
| 8 |
+
# Load the Satellogic dataset
|
| 9 |
+
dataset = ev.load_dataset("satellogic", streaming=True).shuffle(seed=42)
|
| 10 |
data_iter = iter(dataset)
|
| 11 |
|
| 12 |
+
# File to store labels (will create if it doesn't exist)
|
| 13 |
+
label_file = "labels.csv"
|
| 14 |
+
|
| 15 |
+
# Initialize a DataFrame to hold labels (or load existing)
|
| 16 |
+
if os.path.exists(label_file):
|
| 17 |
+
labels_df = pd.read_csv(label_file)
|
| 18 |
+
else:
|
| 19 |
+
labels_df = pd.DataFrame(columns=["image_id", "bounds", "rating", "google_maps_link"])
|
| 20 |
+
|
| 21 |
+
def get_next_image():
|
| 22 |
+
global data_iter, labels_df
|
| 23 |
+
|
| 24 |
+
while True: # Keep iterating until we find an unlabeled image
|
| 25 |
+
try:
|
| 26 |
+
sample = next(data_iter)
|
| 27 |
+
except StopIteration:
|
| 28 |
+
#refresh the dataset if we reach the end
|
| 29 |
+
dataset = ev.load_dataset("satellogic", streaming=True).shuffle(seed=random.randint(0, 1000000))
|
| 30 |
+
data_iter = iter(dataset)
|
| 31 |
+
continue
|
| 32 |
+
|
| 33 |
+
sample = ev.item_to_images("satellogic", sample)
|
| 34 |
+
image = sample["rgb"][0] # Get the first RGB image
|
| 35 |
+
metadata = sample["metadata"]
|
| 36 |
+
|
| 37 |
+
bounds = metadata["bounds"]
|
| 38 |
+
google_maps_link = utils.get_google_map_link(sample, "satellogic")
|
| 39 |
+
#generate a unique image ID:
|
| 40 |
+
image_id = (str(bounds))
|
| 41 |
+
|
| 42 |
+
# Check if image is already labeled
|
| 43 |
+
if image_id not in labels_df["image_id"].values:
|
| 44 |
+
return image, image_id, bounds, google_maps_link
|
| 45 |
+
|
| 46 |
+
def rate_image(image_id, bounds, rating, google_maps_link):
|
| 47 |
+
global labels_df
|
| 48 |
+
|
| 49 |
+
# Add the rating to the DataFrame
|
| 50 |
+
new_row = pd.DataFrame({"image_id": [image_id], "bounds": [bounds], "rating": [rating], "google_maps_link": [google_maps_link]})
|
| 51 |
+
labels_df = pd.concat([labels_df, new_row], ignore_index=True)
|
| 52 |
+
|
| 53 |
+
# Save the DataFrame to CSV
|
| 54 |
+
labels_df.to_csv(label_file, index=False)
|
| 55 |
+
|
| 56 |
+
# Get the next image and its details
|
| 57 |
+
next_image, next_image_id, next_bounds, next_google_maps_link = get_next_image()
|
| 58 |
+
|
| 59 |
+
return next_image, next_image_id, next_bounds, next_google_maps_link
|
| 60 |
+
|
| 61 |
+
# Define the Gradio interface
|
| 62 |
+
iface = gr.Interface(
|
| 63 |
+
fn=rate_image,
|
| 64 |
+
inputs=[
|
| 65 |
+
gr.Textbox(label="Image ID", visible=False),
|
| 66 |
+
gr.Textbox(label="Bounds", visible=False),
|
| 67 |
+
gr.Radio(["Cool", "Not Cool"], label="Rating"),
|
| 68 |
+
gr.Textbox(label="Google Maps Link"),
|
| 69 |
+
],
|
| 70 |
+
outputs=[
|
| 71 |
+
gr.Image(label="Satellite Image"),
|
| 72 |
+
gr.Textbox(label="Image ID", visible=False),
|
| 73 |
+
gr.Textbox(label="Bounds", visible=False),
|
| 74 |
+
gr.Textbox(label="Google Maps Link"),
|
| 75 |
+
],
|
| 76 |
+
title="TerraNomaly - Satellite Image Labeling",
|
| 77 |
+
description="Rate satellite images as 'Cool' or 'Not Cool'.",
|
| 78 |
+
live=False,
|
| 79 |
+
)
|
| 80 |
+
|
| 81 |
+
# Get the first image and its details
|
| 82 |
+
initial_image, initial_image_id, initial_bounds, initial_google_maps_link = get_next_image()
|
| 83 |
+
|
| 84 |
+
# Set the initial values for the output components
|
| 85 |
+
iface.launch(
|
| 86 |
+
share=True,
|
| 87 |
+
initial_outputs=[
|
| 88 |
+
initial_image,
|
| 89 |
+
initial_image_id,
|
| 90 |
+
initial_bounds,
|
| 91 |
+
initial_google_maps_link,
|
| 92 |
+
],
|
| 93 |
+
|
| 94 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|