-
Notifications
You must be signed in to change notification settings - Fork 0
/
extract_echo4ch_obstacles.py
69 lines (50 loc) · 1.93 KB
/
extract_echo4ch_obstacles.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
import os
import sys
import h5py
import glob
import numpy as np
import PIL.Image as Image
from h5ds import H5DS
from utils import progress_bar
def main():
rootpath = os.environ.get("ECHO4CH")
if rootpath is None:
raise Exception(
"Please set the ECHO4CH environment variable to point to the ECHO4CH dataset root"
)
sys.stdout.write("Loading ECHO4CH...")
sys.stdout.flush()
digit = "[0-9]"
allfiles = sorted(
glob.glob(os.path.join(rootpath, f"T{digit * 6}_{digit * 2}_01_Label.png"))
# glob.glob(os.path.join(rootpath, f"T{digit * 6}_01_01_Label.png"))
)
sys.stdout.write(" done.\n")
sys.stdout.flush()
if len(allfiles) == 0:
raise Exception(
"The ECHO4CH environment variable points to a folder which contains no example files. Windows users: did you accidentally include quotes in the environment variable?"
)
print(f"{len(allfiles)} distinct obstacles were found")
path_to_h5file = "echo4ch_obstacles.h5"
# IMPORTANT: don't open the file in truncate mode or the dataset will be gone
h5file = h5py.File(path_to_h5file, "a")
obstacle_ds = H5DS(
name="obstacles", dtype=np.bool8, shape=(64, 64, 64), extensible=True
)
assert not obstacle_ds.exists(h5file)
obstacle_ds.create(h5file)
for i_file, file in enumerate(allfiles):
progress_bar(i_file, len(allfiles))
occupancy_unfolded = Image.open(os.path.join(rootpath, file))
# NOTE: PIL uses (width, height) convention
assert occupancy_unfolded.size == (64, 4096)
assert occupancy_unfolded.mode == "L"
occupancy_unfolded = np.array(occupancy_unfolded)
occupancy = occupancy_unfolded.reshape(64, 64, 64)
assert np.min(occupancy) == 0
assert np.max(occupancy) == 255
occupancy = occupancy > 127
obstacle_ds.append(h5file, occupancy)
if __name__ == "__main__":
main()