File size: 3,068 Bytes
465c443
63711ea
7d5835f
80acacc
7d5835f
63711ea
7c425bb
7d5835f
 
63711ea
 
7d5835f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import gradio as gr
import earthview as ev
import utils
import random
import pandas as pd
import os

# Load the Satellogic dataset
dataset = ev.load_dataset("satellogic", streaming=True).shuffle(seed=42)
data_iter = iter(dataset)

# File to store labels (will create if it doesn't exist)
label_file = "labels.csv"

# Initialize a DataFrame to hold labels (or load existing)
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

    while True:  # Keep iterating until we find an unlabeled image
        try:
            sample = next(data_iter)
        except StopIteration:
            #refresh the dataset if we reach the end
            dataset = ev.load_dataset("satellogic", streaming=True).shuffle(seed=random.randint(0, 1000000))
            data_iter = iter(dataset)
            continue

        sample = ev.item_to_images("satellogic", sample)
        image = sample["rgb"][0]  # Get the first RGB image
        metadata = sample["metadata"]

        bounds = metadata["bounds"]
        google_maps_link = utils.get_google_map_link(sample, "satellogic")
        #generate a unique image ID:
        image_id = (str(bounds))

        # Check if image is already labeled
        if image_id not in labels_df["image_id"].values:
            return image, image_id, bounds, google_maps_link

def rate_image(image_id, bounds, rating, google_maps_link):
    global labels_df

    # Add the rating to the DataFrame
    new_row = pd.DataFrame({"image_id": [image_id], "bounds": [bounds], "rating": [rating], "google_maps_link": [google_maps_link]})
    labels_df = pd.concat([labels_df, new_row], ignore_index=True)

    # Save the DataFrame to CSV
    labels_df.to_csv(label_file, index=False)

    # Get the next image and its details
    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

# Define the 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"),
    ],
    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"),
    ],
    title="TerraNomaly - Satellite Image Labeling",
    description="Rate satellite images as 'Cool' or 'Not Cool'.",
    live=False,
)

# Get the first image and its details
initial_image, initial_image_id, initial_bounds, initial_google_maps_link = get_next_image()

# Set the initial values for the output components
iface.launch(
    share=True,
    initial_outputs=[
        initial_image,
        initial_image_id,
        initial_bounds,
        initial_google_maps_link,
    ],

)