gera-richarte commited on
Commit
196b164
1 Parent(s): ae1a3fb

moved code to earthview.py

Browse files
Files changed (2) hide show
  1. app.py +11 -87
  2. earthview.py +84 -0
app.py CHANGED
@@ -1,29 +1,15 @@
1
  from datasets import load_dataset, get_dataset_config_names
2
  from functools import partial
3
  from pandas import DataFrame
4
- from PIL import Image
5
  import gradio as gr
6
- import numpy as np
7
  import tqdm
8
- import json
9
  import os
10
 
11
- DATASET = "satellogic/EarthView"
12
  DEBUG = False
13
 
14
- sets = {
15
- "satellogic": {
16
- "shards" : 3676,
17
- },
18
- "sentinel_1": {
19
- "shards" : 1763,
20
- },
21
- "neon": {
22
- "config" : "default",
23
- "shards" : 607,
24
- "path" : "data",
25
- }
26
- }
27
 
28
  def open_dataset(dataset, set_name, split, batch_size, state, shard = -1):
29
  if shard == -1:
@@ -31,9 +17,9 @@ def open_dataset(dataset, set_name, split, batch_size, state, shard = -1):
31
  data_files = None
32
  shards = 100
33
  else:
34
- config = sets[set_name].get("config", set_name)
35
- shards = sets[set_name]["shards"]
36
- path = sets[set_name].get("path", set_name)
37
  data_files = {"train":[f"{path}/{split}-{shard:05d}-of-{shards:05d}.parquet"]}
38
 
39
  if DEBUG:
@@ -60,68 +46,6 @@ def open_dataset(dataset, set_name, split, batch_size, state, shard = -1):
60
  state
61
  )
62
 
63
- def item_to_images(config, item):
64
- metadata = item["metadata"]
65
- if type(metadata) == str:
66
- metadata = json.loads(metadata)
67
-
68
- item = {
69
- k: np.asarray(v).astype("uint8")
70
- for k,v in item.items()
71
- if k != "metadata"
72
- }
73
- item["metadata"] = metadata
74
-
75
- if config == "satellogic":
76
- item["rgb"] = [
77
- Image.fromarray(image.transpose(1,2,0))
78
- for image in item["rgb"]
79
- ]
80
- item["1m"] = [
81
- Image.fromarray(image[0,:,:])
82
- for image in item["1m"]
83
- ]
84
- elif config == "sentinel_1":
85
- # Mapping of V and H to RGB. May not be correct
86
- # https://gis.stackexchange.com/questions/400726/creating-composite-rgb-images-from-sentinel-1-channels
87
- i10m = item["10m"]
88
- i10m = np.concatenate(
89
- ( i10m,
90
- np.expand_dims(
91
- i10m[:,0,:,:]/(i10m[:,1,:,:]+0.01)*256,
92
- 1
93
- ).astype("uint8")
94
- ),
95
- 1
96
- )
97
- item["10m"] = [
98
- Image.fromarray(image.transpose(1,2,0))
99
- for image in i10m
100
- ]
101
- elif config == "default":
102
- item["rgb"] = [
103
- Image.fromarray(image.transpose(1,2,0))
104
- for image in item["rgb"]
105
- ]
106
- item["chm"] = [
107
- Image.fromarray(image[0])
108
- for image in item["chm"]
109
- ]
110
-
111
- # The next is a very arbitrary conversion from the 369 hyperspectral data to RGB
112
- # It just averages each 1/3 of the bads and assigns it to a channel
113
- item["1m"] = [
114
- Image.fromarray(
115
- np.concatenate((
116
- np.expand_dims(np.average(image[:124],0),2),
117
- np.expand_dims(np.average(image[124:247],0),2),
118
- np.expand_dims(np.average(image[247:],0),2))
119
- ,2).astype("uint8"))
120
- for image in item["1m"]
121
- ]
122
- return item
123
-
124
-
125
  def get_images(batch_size, state):
126
  config = state["config"]
127
 
@@ -138,7 +62,7 @@ def get_images(batch_size, state):
138
  except StopIteration:
139
  break
140
  metadata = item["metadata"]
141
- item = item_to_images(config, item)
142
 
143
  if config == "satellogic":
144
  images.extend(item["rgb"])
@@ -163,20 +87,20 @@ if __name__ == "__main__":
163
  with gr.Blocks(title="Dataset Explorer", fill_height = True) as demo:
164
  state = new_state()
165
 
166
- gr.Markdown(f"# Viewer for [{DATASET}](https://huggingface.co/datasets/satellogic/EarthView) Dataset")
167
  batch_size = gr.Number(10, label = "Batch Size", render=False)
168
  shard = gr.Slider(label="Shard", minimum=0, maximum=10000, step=1, render=False)
169
  table = gr.DataFrame(render = False)
170
  # headers=["Index","TimeStamp","Bounds","CRS"],
171
 
172
  gallery = gr.Gallery(
173
- label=DATASET,
174
  interactive=False,
175
  columns=5, rows=2, render=False)
176
 
177
  with gr.Row():
178
- dataset = gr.Textbox(label="Dataset", value=DATASET, interactive=False)
179
- config = gr.Dropdown(choices=sets.keys(), label="Config", value="satellogic", )
180
  split = gr.Textbox(label="Split", value="train")
181
  initial_shard = gr.Number(label = "Initial shard", value=0, info="-1 for whole dataset")
182
 
 
1
  from datasets import load_dataset, get_dataset_config_names
2
  from functools import partial
3
  from pandas import DataFrame
4
+ import earthview as ev
5
  import gradio as gr
 
6
  import tqdm
 
7
  import os
8
 
 
9
  DEBUG = False
10
 
11
+ if DEBUG:
12
+ import numpy as np
 
 
 
 
 
 
 
 
 
 
 
13
 
14
  def open_dataset(dataset, set_name, split, batch_size, state, shard = -1):
15
  if shard == -1:
 
17
  data_files = None
18
  shards = 100
19
  else:
20
+ config = ev.sets[set_name].get("config", set_name)
21
+ shards = ev.sets[set_name]["shards"]
22
+ path = ev.sets[set_name].get("path", set_name)
23
  data_files = {"train":[f"{path}/{split}-{shard:05d}-of-{shards:05d}.parquet"]}
24
 
25
  if DEBUG:
 
46
  state
47
  )
48
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
49
  def get_images(batch_size, state):
50
  config = state["config"]
51
 
 
62
  except StopIteration:
63
  break
64
  metadata = item["metadata"]
65
+ item = ev.item_to_images(config, item)
66
 
67
  if config == "satellogic":
68
  images.extend(item["rgb"])
 
87
  with gr.Blocks(title="Dataset Explorer", fill_height = True) as demo:
88
  state = new_state()
89
 
90
+ gr.Markdown(f"# Viewer for [{ev.DATASET}](https://huggingface.co/datasets/satellogic/EarthView) Dataset")
91
  batch_size = gr.Number(10, label = "Batch Size", render=False)
92
  shard = gr.Slider(label="Shard", minimum=0, maximum=10000, step=1, render=False)
93
  table = gr.DataFrame(render = False)
94
  # headers=["Index","TimeStamp","Bounds","CRS"],
95
 
96
  gallery = gr.Gallery(
97
+ label=ev.DATASET,
98
  interactive=False,
99
  columns=5, rows=2, render=False)
100
 
101
  with gr.Row():
102
+ dataset = gr.Textbox(label="Dataset", value=ev.DATASET, interactive=False)
103
+ config = gr.Dropdown(choices=ev.get_sets(), label="Config", value="satellogic", )
104
  split = gr.Textbox(label="Split", value="train")
105
  initial_shard = gr.Number(label = "Initial shard", value=0, info="-1 for whole dataset")
106
 
earthview.py ADDED
@@ -0,0 +1,84 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from PIL import Image
2
+ import numpy as np
3
+ import json
4
+
5
+ DATASET = "satellogic/EarthView"
6
+
7
+ sets = {
8
+ "satellogic": {
9
+ "shards" : 3676,
10
+ },
11
+ "sentinel_1": {
12
+ "shards" : 1763,
13
+ },
14
+ "neon": {
15
+ "config" : "default",
16
+ "shards" : 607,
17
+ "path" : "data",
18
+ }
19
+ }
20
+
21
+ def get_sets():
22
+ return sets.keys()
23
+
24
+ def item_to_images(config, item):
25
+ metadata = item["metadata"]
26
+ if type(metadata) == str:
27
+ metadata = json.loads(metadata)
28
+
29
+ item = {
30
+ k: np.asarray(v).astype("uint8")
31
+ for k,v in item.items()
32
+ if k != "metadata"
33
+ }
34
+ item["metadata"] = metadata
35
+
36
+ if config == "satellogic":
37
+ item["rgb"] = [
38
+ Image.fromarray(image.transpose(1,2,0))
39
+ for image in item["rgb"]
40
+ ]
41
+ item["1m"] = [
42
+ Image.fromarray(image[0,:,:])
43
+ for image in item["1m"]
44
+ ]
45
+ elif config == "sentinel_1":
46
+ # Mapping of V and H to RGB. May not be correct
47
+ # https://gis.stackexchange.com/questions/400726/creating-composite-rgb-images-from-sentinel-1-channels
48
+ i10m = item["10m"]
49
+ i10m = np.concatenate(
50
+ ( i10m,
51
+ np.expand_dims(
52
+ i10m[:,0,:,:]/(i10m[:,1,:,:]+0.01)*256,
53
+ 1
54
+ ).astype("uint8")
55
+ ),
56
+ 1
57
+ )
58
+ item["10m"] = [
59
+ Image.fromarray(image.transpose(1,2,0))
60
+ for image in i10m
61
+ ]
62
+ elif config == "default":
63
+ item["rgb"] = [
64
+ Image.fromarray(image.transpose(1,2,0))
65
+ for image in item["rgb"]
66
+ ]
67
+ item["chm"] = [
68
+ Image.fromarray(image[0])
69
+ for image in item["chm"]
70
+ ]
71
+
72
+ # The next is a very arbitrary conversion from the 369 hyperspectral data to RGB
73
+ # It just averages each 1/3 of the bads and assigns it to a channel
74
+ item["1m"] = [
75
+ Image.fromarray(
76
+ np.concatenate((
77
+ np.expand_dims(np.average(image[:124],0),2),
78
+ np.expand_dims(np.average(image[124:247],0),2),
79
+ np.expand_dims(np.average(image[247:],0),2))
80
+ ,2).astype("uint8"))
81
+ for image in item["1m"]
82
+ ]
83
+ return item
84
+