-
Notifications
You must be signed in to change notification settings - Fork 31
/
extract_fog.py
151 lines (92 loc) · 4.15 KB
/
extract_fog.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
__author__ = "Martin Hahner"
__contact__ = "martin.hahner@pm.me"
__license__ = "CC BY-NC 4.0 (https://creativecommons.org/licenses/by-nc/4.0/)"
import os
import csv
import argparse
import numpy as np
from tqdm import tqdm
from typing import List
from pathlib import Path
MIN_DIST = 1.75 # in m
MAX_DIST = 10 # in m
MIN_HEIGHT = -1 # in m
MAX_HEIGHT = np.inf # in m
SPLIT = 'test_dense_fog_night'
TOPIC = 'lidar_hdl64_strongest'
def get_recordings(split: str) -> List[str]:
splits_folder = Path(__file__).parent.absolute() / 'SeeingThroughFog' / 'splits'
splits = sorted(os.listdir(splits_folder))
assert f'{split}.txt' in splits, f'{split} is undefined'
recordings = []
with open(f'{splits_folder / split}.txt') as csv_file:
csv_reader = csv.reader(csv_file, delimiter=',')
for row in csv_reader:
recordings.append(f'{row[0]}_{row[1]}.png')
return sorted(recordings)
def extract_fog(arguments: argparse.Namespace, recordings: List[str]) -> None:
points_before_sum = 0
points_after_sum = 0
avg_num_point_before = 0
avg_num_point_after = 0
prog_bar = tqdm(recordings)
save_dir = Path(arguments.root_path) / f'{arguments.topic}_fog_extraction'
save_dir.mkdir(parents=True, exist_ok=True)
for i, recording in enumerate(prog_bar):
file_name = recording.replace('.png', '.bin')
path = Path(arguments.root_path) / arguments.topic / file_name
pc = np.fromfile(path, dtype=np.float32)
pc = pc.reshape((-1, 5))
points_before = len(pc)
pc = filter_ego_point(pc)
pc = filter_by_distance(pc)
pc = filter_by_height(pc)
points_after = len(pc)
points_before_sum += points_before
points_after_sum += points_after
avg_num_point_before = (avg_num_point_before * i + points_before) / (i + 1)
avg_num_point_after = (avg_num_point_after * i + points_after) / (i + 1)
save_path = save_dir / file_name
pc.astype(np.float32).tofile(save_path)
prog_bar.set_description(f'{int(avg_num_point_after)}/{int(avg_num_point_before)}')
num_recordings = len(recordings)
avg_num_point_before = points_before_sum / num_recordings
avg_num_point_after = points_after_sum / num_recordings
print(f'average points before: {avg_num_point_before}')
print(f'average points after: {avg_num_point_after}')
def filter_ego_point(pc: np.ndarray, l: float = 5.116, w: float = 1.899, h: float = 1.496) -> np.ndarray:
# default dimensions are dimensions of W222 taken from wikipedia
x_mask_lower = -l / 2 < pc[:, 0]
x_mask_upper = pc[:, 0] < l / 2
x_mask = (x_mask_lower == 1) & (x_mask_upper == 1)
y_mask_lower = -w / 2 < pc[:, 1]
y_mask_upper = pc[:, 1] < w / 2
y_mask = (y_mask_lower == 1) & (y_mask_upper == 1)
z_mask_lower = -h < pc[:, 2]
z_mask_upper = pc[:, 2] < -h / 2
z_mask = (z_mask_lower == 1) & (z_mask_upper == 1)
inside_mask = (x_mask == 1) & (y_mask == 1) & (z_mask == 1)
outside_mask = ~inside_mask
pc = pc[outside_mask, :]
return pc
def filter_by_distance(pc: np.ndarray, min_dist: float = MIN_DIST, max_dist: float = MAX_DIST) -> np.ndarray:
min_dist_mask = np.linalg.norm(pc[:, 0:3], axis=1) > min_dist
pc = pc[min_dist_mask, :]
max_dist_mask = np.linalg.norm(pc[:, 0:3], axis=1) < max_dist
pc = pc[max_dist_mask, :]
return pc
def filter_by_height(pc: np.ndarray, min_height: float = MIN_HEIGHT, max_height: float = MAX_HEIGHT) -> np.ndarray:
min_height_mask = pc[:, 2] > min_height
pc = pc[min_height_mask, :]
max_height_mask = pc[:, 2] < max_height
pc = pc[max_height_mask, :]
return pc
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument('-p', '--path', type=str, default=str(Path.home() / 'datasets/DENSE/SeeingThroughFog'),
help='path to SeeingThroughFog dataset')
parser.add_argument('-s', '--split', type=str, default=SPLIT)
parser.add_argument('-t', '--topic', type=str, default=TOPIC)
args = parser.parse_args()
recs = get_recordings(args.split)
extract_fog(args, recs)