-
Notifications
You must be signed in to change notification settings - Fork 50
/
Copy pathreading.py
84 lines (66 loc) · 2.34 KB
/
reading.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
import os
from typing import Generator, List, Tuple, Union
import numpy as np
from ..proto import Scene
from .serialization import deserialize_numpy
def get_file_paths(dataset_path: str) -> List[str]:
"""Returns a sorted list with file paths to raw protobuf files.
Args:
dataset_path (str): path to datset directory
Returns:
List[str]: sorted list of file paths
"""
sub_dirs = sorted([
os.path.join(dataset_path, d) for d in os.listdir(dataset_path)
if os.path.isdir(os.path.join(dataset_path, d))
])
res = []
for d in sub_dirs:
file_names = sorted(os.listdir(d))
res += [
os.path.join(d, fname)
for fname in file_names
if fname.endswith('.pb')
]
return res
def scenes_generator(
file_paths: List[str], yield_fpath: bool = False
) -> Generator[Union[Tuple[Scene, str], Scene], None, None]:
"""Generates protobuf messages from files with serialized scenes.
Args:
file_paths (List[str]): Paths to serialized protobuf scenes.
Use the function get_file_paths() defined above to get file paths for the dataset.
yield_fpath (bool, optional): If set to True generator yield file path as well.
Defaults to False.
Yields:
Union[Scene, Tuple[Scene, str]]: scene protobuf message
or tuple of scene protobuf message and respective file path
"""
for fpath in file_paths:
scene = read_scene_from_file(fpath)
if yield_fpath:
yield scene, fpath
else:
yield scene
def read_feature_map_from_file(filepath: str) -> np.ndarray:
"""Reads and deserializes pre-rendered feature map from the given file.
Args:
filepath (str): path to a file with serialized feature map
Returns:
np.ndarray: feature map
"""
with open(filepath, 'rb') as f:
fm = deserialize_numpy(f.read(), decompress=True)
return fm
def read_scene_from_file(filepath: str) -> Scene:
"""Reads and deserializes one protobuf scene from the given file.
Args:
filepath (str): path to a file with serialized scene
Returns:
Scene: protobuf message
"""
with open(filepath, 'rb') as f:
scene_serialized = f.read()
scene = Scene()
scene.ParseFromString(scene_serialized)
return scene