Spaces:
Sleeping
Sleeping
import gradio as gr | |
import earthview as ev | |
import utils | |
import random | |
import pandas as pd | |
import os | |
from itertools import islice | |
# Configuration | |
chunk_size = 100 # Size of the chunks to shuffle | |
label_file = os.path.join(os.path.dirname(__file__), "labels.csv") # Save CSV in the same directory as the script | |
# Load the Satellogic dataset (streaming) | |
dataset = ev.load_dataset("satellogic", streaming=True) | |
data_iter = iter(dataset) | |
shuffled_chunk = [] # Initialize an empty list to hold the current chunk | |
chunk_iter = None # Initialize the chunk iterator | |
# Initialize or load labels DataFrame | |
if os.path.exists(label_file): | |
labels_df = pd.read_csv(label_file) | |
else: | |
labels_df = pd.DataFrame(columns=["image_id", "bounds", "rating", "google_maps_link"]) | |
def get_next_image(): | |
global data_iter, labels_df, shuffled_chunk, chunk_iter | |
while True: | |
# If we don't have a current chunk or it's exhausted, get a new one | |
if not shuffled_chunk or chunk_iter is None: | |
chunk = list(islice(data_iter, chunk_size)) | |
if not chunk: # If the dataset is exhausted, reset the iterator | |
print("Dataset exhausted, resetting iterator.") | |
data_iter = iter(ev.load_dataset("satellogic", streaming=True)) | |
chunk = list(islice(data_iter, chunk_size)) | |
if not chunk: | |
print("Still no data after reset.") | |
return None, "Dataset exhausted", None, None | |
random.shuffle(chunk) | |
shuffled_chunk = chunk | |
chunk_iter = iter(shuffled_chunk) | |
try: | |
sample = next(chunk_iter) | |
sample = ev.item_to_images("satellogic", sample) | |
image = sample["rgb"][0] | |
metadata = sample["metadata"] | |
bounds = metadata["bounds"] | |
google_maps_link = utils.get_google_map_link(sample, "satellogic") | |
image_id = str(bounds) | |
if image_id not in labels_df["image_id"].values: | |
return image, image_id, bounds, google_maps_link | |
except StopIteration: | |
# Current chunk is exhausted, reset chunk variables to get a new one in the next iteration | |
shuffled_chunk = [] | |
chunk_iter = None | |
def rate_image(image_id, bounds, rating): | |
global labels_df | |
new_row = pd.DataFrame( | |
{ | |
"image_id": [image_id], | |
"bounds": [bounds], | |
"rating": [rating], | |
"google_maps_link": [""], # this isn't necessary to pass to the function since we aren't updating it here. | |
} | |
) | |
labels_df = pd.concat([labels_df, new_row], ignore_index=True) | |
labels_df.to_csv(label_file, index=False) | |
next_image, next_image_id, next_bounds, next_google_maps_link = get_next_image() | |
return next_image, next_image_id, next_bounds, next_google_maps_link | |
# Gradio interface | |
iface = gr.Interface( | |
fn=rate_image, | |
inputs=[ | |
gr.Textbox(label="Image ID", visible=False), | |
gr.Textbox(label="Bounds", visible=False), | |
gr.Radio(["Cool", "Not Cool"], label="Rating"), | |
#gr.Textbox(label="Google Maps Link"), # Remove google maps link as an input | |
], | |
outputs=[ | |
gr.Image(label="Satellite Image"), | |
gr.Textbox(label="Image ID", visible=False), | |
gr.Textbox(label="Bounds", visible=False), | |
gr.Textbox(label="Google Maps Link", visible=True), # Add google maps link as an output | |
], | |
title="TerraNomaly - Satellite Image Labeling", | |
description="Rate satellite images as 'Cool' or 'Not Cool'.", | |
live=False, | |
) | |
iface.launch( | |
share=True | |
) |