-
Notifications
You must be signed in to change notification settings - Fork 0
/
density_invariant_cmax.py
219 lines (198 loc) · 9.13 KB
/
density_invariant_cmax.py
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
import os
import concurrent.futures
import event_warping
import itertools
import json
import numpy as np
import matplotlib.pyplot
import PIL.Image
from typing import Tuple
from PIL import ImageDraw
class DensityInvariantCMax:
OBJECTIVE = {
"variance": event_warping.intensity_variance,
"weighted_variance": event_warping.intensity_weighted_variance,
"max": event_warping.intensity_maximum,
}
def __init__(
self,
filename: str,
heuristic: str,
velocity_range: Tuple[float, float],
resolution: int,
ratio: float,
tstart: float,
tfinish: float,
read_path: str,
save_path: str,
):
self.filename = filename
self.heuristic = heuristic
self.velocity_range = velocity_range
self.resolution = resolution
self.ratio = ratio
self.tstart = tstart
self.tfinish = tfinish
self.read_path = read_path
self.save_path = save_path
def load_and_preprocess_events(self):
possible_extensions = [".es", ".txt"]
file_extension = None
for ext in possible_extensions:
if os.path.exists(os.path.join(self.read_path, self.filename + ext)):
file_extension = ext
break
if file_extension is None:
raise ValueError(f"File with name {self.filename} not found in {self.read_path}")
if file_extension == ".es":
self.width, self.height, events = event_warping.read_es_file(
os.path.join(self.read_path, self.filename + file_extension)
)
elif file_extension == ".txt":
self.width, self.height, events = event_warping.read_txt_file(
os.path.join(self.read_path, self.filename + file_extension)
)
else:
raise ValueError(f"Unsupported data type: {file_extension}")
events = event_warping.without_most_active_pixels(events, ratio=self.ratio)
if events["t"][0] != 0:
events["t"] = events["t"] - events["t"][0]
self.events = events[
np.where(
np.logical_and(events["t"] > self.tstart, events["t"] < self.tfinish)
)
]
print(f"Number of events: {len(self.events)}")
self.size = (self.width, self.height)
def calculate_heuristic(self, velocity: Tuple[float, float]):
"""Calculate the heuristic based on the method specified"""
return self.OBJECTIVE[self.heuristic](self.size, self.events, velocity)
def process_velocity_grid(self):
"""Compute the heuristic for a grid of velocities using concurrent processing"""
scalar_velocities = np.linspace(*self.velocity_range, self.resolution)
values = []
with concurrent.futures.ThreadPoolExecutor() as executor:
for value in executor.map(
self.calculate_heuristic,
(
(vx * 1e-6, vy * 1e-6)
for vx, vy in itertools.product(
scalar_velocities, scalar_velocities
)
),
):
values.append(value)
print(
f"\rProcessed {len(values)} / {self.resolution ** 2} "
f"({(len(values) / (self.resolution ** 2) * 100):.2f} %)",
end="",
)
return values
def save_values(self, values: list):
"""Save the computed values to a JSON file"""
output_file = (
f"{self.save_path + self.filename}_{self.heuristic}_{self.velocity_range[0]}_"
f"{self.velocity_range[1]}_{self.resolution}_{self.ratio}.json"
)
with open(output_file, "w") as output:
json.dump(values, output)
return output_file
def generate_landscape(self, output_file: str):
"""Generate an image from the computed values and save it as a PNG"""
with open(output_file) as input:
pixels = np.reshape(json.load(input), (self.resolution, self.resolution))
colormap = matplotlib.pyplot.colormaps["magma"] # type:ignore
scaled_pixels = ((pixels - pixels.min()) / (pixels.max() - pixels.min())) ** (
1 / 2
)
image = PIL.Image.fromarray(
(colormap(scaled_pixels)[:, :, :3] * 255).astype(np.uint8)
)
img_gray_original = image.convert('L')
img_np_original = np.array(img_gray_original)
max_intensity_index_original = np.argmax(img_np_original)
max_intensity_coords_original = np.unravel_index(max_intensity_index_original, img_np_original.shape)
scalar_velocities = np.linspace(*self.velocity_range, self.resolution)
self.vx_original = scalar_velocities[max_intensity_coords_original[1]]
self.vy_original = scalar_velocities[max_intensity_coords_original[0]]
resized_image = image.rotate(90, PIL.Image.NEAREST).resize((500, 500))
scale_factor = resized_image.width / image.width
img_gray_resized = resized_image.convert('L')
img_np_resized = np.array(img_gray_resized)
max_intensity_index_resized = np.argmax(img_np_resized)
max_intensity_coords_resized = np.unravel_index(max_intensity_index_resized, img_np_resized.shape)
draw = ImageDraw.Draw(resized_image)
radius = 20
draw.ellipse((max_intensity_coords_resized[1]-radius, max_intensity_coords_resized[0]-radius,
max_intensity_coords_resized[1]+radius, max_intensity_coords_resized[0]+radius), outline='black')# type:ignore
self.vx_resized = scalar_velocities[int(max_intensity_coords_resized[1] / scale_factor)]
text = f'vx: {self.vy_original:.3f}, vy: {self.vx_original:.3f}'
text_size = (len(text) * 6, radius)
if max_intensity_coords_resized[1] + radius + text_size[0] < resized_image.width:
text_pos_x = max_intensity_coords_resized[1] + radius
else:
text_pos_x = max_intensity_coords_resized[1] - radius - text_size[0]
if max_intensity_coords_resized[0] + radius + text_size[1] < resized_image.height:
text_pos_y = max_intensity_coords_resized[0] + radius
else:
text_pos_y = max_intensity_coords_resized[0] - radius - text_size[1]
draw.text((text_pos_x, text_pos_y), text, fill='white')# type:ignore
resized_image.save(
self.save_path
+ self.filename
+ f"_{self.velocity_range[0]}_{self.velocity_range[1]}_{self.resolution}_{self.heuristic}.png")
def generate_motion_comp_img(self):
#flip vx and vy, because the img was rotated by 90
cumulative_map = event_warping.accumulate(
sensor_size=self.size,
events=self.events,
velocity=(self.vy_original / 1e6, self.vx_original / 1e6), # px/µs
)
image = event_warping.render(
cumulative_map,
colormap_name="magma",
gamma=lambda image: image ** (1 / 3),
)
image.save(
self.save_path
+ self.filename
+ f'_vx_{self.vy_original:.3f}_vy_{self.vx_original:.3f}_motion_comp_img.png'
)
def process(self):
"""Main method to run the processing pipeline"""
self.load_and_preprocess_events()
values = self.process_velocity_grid()
output_file = self.save_values(values)
self.generate_landscape(output_file)
self.generate_motion_comp_img()
if __name__ == "__main__":
# List of objective functions to use
OBJECTIVE = ["variance", "weighted_variance", "max"]
EVENTS = [
"FN034HRTEgyptB_NADIR",
"20220125_Brittany_211945_2022-01-25_21-21-18_NADIR",
"20220124_201028_Panama_2022-01-24_20_12_11_NADIR.h5",
"20230119_4_UK_Nadir_Night_2023-01-19_20~25~10_NADIR",
"20220217_Houston_IAH_1_2022-02-17_20-28-02_NADIR",
"20220121a_Salvador_2022-01-21_20~58~34_NADIR.h5",
"20220127_Biscay_Spain_Med_211912_2_2022-01-27_21-53-58_NADIR",
"20220201_DIA_201410_2022-02-01_20-15-58_NADIR",
"20220125_Mexican_Coast_205735_2022-01-25_20~58~52_NADIR.h5",
"20220125_New_Zealand_220728_2022-01-25_22-09-42_NADIR"
]
filename_array = np.array(EVENTS)
for file_name in filename_array:
data_used = file_name
objective_function = OBJECTIVE[0]
event_warping.print_message(f"Processing: {data_used}", color='yellow', style='bold')
event_warping.print_message(f"Objective function: {objective_function}", color='red', style='bold')
calculator = DensityInvariantCMax(filename=data_used,
heuristic=objective_function,
velocity_range=(-30, 30),
resolution=200, #the higher the better, but becomes computationally expensive!
ratio=0.0,
tstart=0.0e6,
tfinish=30.0e6,
read_path="data/",
save_path="img/project_page/")
calculator.process()