dropbop commited on
Commit
4f33c08
·
verified ·
1 Parent(s): d82e8e5

Update app.py

Browse files

Just messing with this until I get a working demo

Files changed (1) hide show
  1. app.py +51 -53
app.py CHANGED
@@ -1,83 +1,81 @@
1
  import os
2
- import glob
3
  import pandas as pd
4
  from PIL import Image
5
  from itertools import islice
6
- import earthview as ev
7
  import gradio as gr
8
 
9
- # this only gets the first file in the first shard - you can download more by editing this line
10
  filename = os.path.join("dataset", "satellogic", "train-00000-of-00065.parquet")
11
- # this returns an iterator for all files, not sorted
12
- # filenames = glob.glob(os.path.join("dataset", "satellogic", "*.parquet"))
13
 
14
- # loads a dataset with pandas, this loads a single file
15
- # for larger datasets you want to use `dask` which is significantly faster,
16
- # but I wanted to provide a simple version which only uses dependencies that have already been imported.
 
 
17
  data = pd.read_parquet(filename)
18
 
19
- # transforms a metadata sample to bounds and timestamps, handling revisits
 
 
 
 
 
 
 
 
20
  def item_to_bounds_timestamps(sample):
21
- # unpack metadata
22
  bounds_list = sample["metadata"]["bounds"]
23
  timestamp_list = sample["metadata"]["timestamp"]
24
- # each sample contains nested metadata
25
  bounds = []
26
  timestamps = []
27
- # return two flat lists
28
  for b, t in zip(bounds_list, timestamp_list):
29
  bounds.append(b)
30
  timestamps.append(t)
31
  return bounds, timestamps
32
 
33
- # Create an empty list to store ratings
34
- ratings = []
35
- image_id = 0 # Initialize image ID counter
36
- bounds = []
37
- timestamps = []
38
 
39
- # Limit the number of images to process for the test
40
- num_images_to_process = 5
41
 
42
- # Iterate through the samples, display, rate, and store info
43
- data_iter = islice(data.iterrows(), num_images_to_process)
44
- for index, sample in data_iter:
45
- rgb = sample["rgb"]
46
  bounds_sample, timestamps_sample = item_to_bounds_timestamps(sample)
47
- # iterate through the RGB images (revisits)
48
- for i, img in enumerate(rgb):
49
- print("Image ID:", image_id)
50
- display(img)
51
 
52
- # Get the rating from the user
53
- while True:
54
- try:
55
- rating = int(input("Rate the image (0 or 1): "))
56
- if rating in [0, 1]:
57
- break
58
- else:
59
- print("Invalid rating. Please enter 0 or 1.")
60
- except ValueError:
61
- print("Invalid input. Please enter a number.")
62
 
63
- # Store the rating and other info
64
- ratings.append(rating)
65
 
66
- # Store the bounds and timestamp
67
- bounds.append(bounds_sample[i])
68
- timestamps.append(timestamps_sample[i])
69
 
70
- image_id += 1 # Increment image ID
 
 
 
 
71
 
72
- # Create a DataFrame from the collected data
73
- df = pd.DataFrame({
74
- "image_id": range(image_id),
75
- "rating": ratings,
76
- "bounds": bounds,
77
- "timestamp": timestamps
78
- })
79
 
80
- # Save the DataFrame to a CSV file
81
- df.to_csv("image_ratings_test.csv", index=False)
 
 
 
 
 
 
 
 
 
 
 
82
 
83
- print("Test complete. Ratings saved to image_ratings_test.csv")
 
 
1
  import os
 
2
  import pandas as pd
3
  from PIL import Image
4
  from itertools import islice
5
+ import earthview as ev # Your custom library
6
  import gradio as gr
7
 
8
+ # Path to the first shard of the Satellogic dataset
9
  filename = os.path.join("dataset", "satellogic", "train-00000-of-00065.parquet")
 
 
10
 
11
+ # Check if the file exists
12
+ if not os.path.isfile(filename):
13
+ raise FileNotFoundError(f"{filename} not found")
14
+
15
+ # Loads a dataset with pandas, this loads a single file
16
  data = pd.read_parquet(filename)
17
 
18
+ # Global variables to manage state
19
+ current_image = None
20
+ current_metadata_index = 0
21
+ ratings = []
22
+ image_id = 0
23
+ bounds = []
24
+ timestamps = []
25
+
26
+ # Function to transform a metadata sample to bounds and timestamps
27
  def item_to_bounds_timestamps(sample):
 
28
  bounds_list = sample["metadata"]["bounds"]
29
  timestamp_list = sample["metadata"]["timestamp"]
 
30
  bounds = []
31
  timestamps = []
 
32
  for b, t in zip(bounds_list, timestamp_list):
33
  bounds.append(b)
34
  timestamps.append(t)
35
  return bounds, timestamps
36
 
37
+ # Function to load and display the next image
38
+ def load_next_image():
39
+ global current_image, image_id, current_metadata_index, bounds, timestamps
 
 
40
 
41
+ if current_metadata_index >= len(data):
42
+ return None, "No more images", None
43
 
44
+ sample = data.iloc[current_metadata_index]
 
 
 
45
  bounds_sample, timestamps_sample = item_to_bounds_timestamps(sample)
 
 
 
 
46
 
47
+ # Use earthview library to convert arrays to PIL images
48
+ sample = ev.item_to_images("satellogic", sample.to_dict())
49
+ current_image = sample["rgb"][0] # Get the first image
50
+ image_id += 1
 
 
 
 
 
 
51
 
52
+ return current_image, f"Image ID: {image_id}", bounds_sample[0]
 
53
 
54
+ # Function to handle rating submission
55
+ def submit_rating(rating, bounds_str):
56
+ global image_id, current_metadata_index, ratings, bounds, timestamps
57
 
58
+ ratings.append(rating)
59
+ bounds.append(bounds_str)
60
+ timestamps.append("timestamp") # Use a valid timestamp if available
61
+
62
+ current_metadata_index += 1
63
 
64
+ return load_next_image()
 
 
 
 
 
 
65
 
66
+ # Gradio Interface Layout
67
+ with gr.Blocks() as demo:
68
+ with gr.Row():
69
+ with gr.Column():
70
+ image_display = gr.Image(label="Satellite Image", type="pil")
71
+ image_id_display = gr.Textbox(label="Image ID")
72
+ bounds_display = gr.Textbox(label="Bounds")
73
+ load_button = gr.Button("Load Next Image")
74
+ load_button.click(fn=load_next_image, outputs=[image_display, image_id_display, bounds_display])
75
+ with gr.Column():
76
+ rating_radio = gr.Radio(["0", "1"], label="Rating (0 = No, 1 = Yes)")
77
+ submit_button = gr.Button("Submit Rating")
78
+ submit_button.click(fn=lambda rating, bounds_str: submit_rating(rating, bounds_str), inputs=[rating_radio, bounds_display], outputs=[image_display, image_id_display, bounds_display])
79
 
80
+ # Launch the Gradio interface
81
+ demo.launch()