|
22 | 22 | # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
23 | 23 | # SOFTWARE. |
24 | 24 |
|
25 | | -import glob |
26 | 25 | import os |
27 | 26 | from typing import Optional |
28 | 27 |
|
29 | 28 | import numpy as np |
30 | 29 |
|
31 | 30 |
|
32 | | -def find_metadata_json(pcap_file: str) -> str: |
33 | | - """Attempts to resolve the metadata json file for a provided pcap file.""" |
34 | | - dir_path, filename = os.path.split(pcap_file) |
35 | | - if not filename: |
36 | | - return "" |
37 | | - if not dir_path: |
38 | | - dir_path = os.getcwd() |
39 | | - json_candidates = sorted(glob.glob(f"{dir_path}/*.json")) |
40 | | - if not json_candidates: |
41 | | - return "" |
42 | | - prefix_sizes = list( |
43 | | - map(lambda p: len(os.path.commonprefix((filename, os.path.basename(p)))), json_candidates) |
44 | | - ) |
45 | | - max_elem = max(range(len(prefix_sizes)), key=lambda i: prefix_sizes[i]) |
46 | | - return json_candidates[max_elem] |
47 | | - |
48 | | - |
49 | 31 | class OusterDataloader: |
50 | 32 | """Ouster pcap dataloader""" |
51 | 33 |
|
@@ -83,64 +65,42 @@ def __init__( |
83 | 65 | """ |
84 | 66 |
|
85 | 67 | try: |
86 | | - import ouster.pcap as pcap |
87 | | - from ouster import client |
| 68 | + from ouster.sdk import client, open_source |
88 | 69 | except ImportError: |
89 | | - print( |
90 | | - f'[ERROR] ouster-sdk is not installed on your system, run "pip install ouster-sdk"' |
91 | | - ) |
| 70 | + print(f'ouster-sdk is not installed on your system, run "pip install ouster-sdk"') |
92 | 71 | exit(1) |
93 | 72 |
|
94 | | - # since we import ouster-sdk's client module locally, we keep it locally as well |
95 | | - self._client = client |
96 | | - |
97 | 73 | assert os.path.isfile(data_dir), "Ouster pcap dataloader expects an existing PCAP file" |
98 | 74 |
|
99 | 75 | # we expect `data_dir` param to be a path to the .pcap file, so rename for clarity |
100 | 76 | pcap_file = data_dir |
101 | 77 |
|
102 | | - metadata_json = meta or find_metadata_json(pcap_file) |
103 | | - if not metadata_json: |
104 | | - print("[ERROR] Ouster pcap dataloader can't find metadata json file.") |
105 | | - exit(1) |
106 | | - print("[INFO] Ouster pcap dataloader: using metadata json: ", metadata_json) |
| 78 | + print("Indexing Ouster pcap to count the scans number ...") |
| 79 | + source = open_source(str(pcap_file), meta=[meta] if meta else [], index=True) |
107 | 80 |
|
108 | | - self.data_dir = os.path.dirname(data_dir) |
| 81 | + # since we import ouster-sdk's client module locally, we keep reference |
| 82 | + # to it locally as well |
| 83 | + self._client = client |
109 | 84 |
|
110 | | - with open(metadata_json) as json: |
111 | | - self._info_json = json.read() |
112 | | - self._info = client.SensorInfo(self._info_json) |
| 85 | + self.data_dir = os.path.dirname(data_dir) |
113 | 86 |
|
114 | 87 | # lookup table for 2D range image projection to a 3D point cloud |
115 | | - self._xyz_lut = client.XYZLut(self._info) |
| 88 | + self._xyz_lut = client.XYZLut(source.metadata) |
116 | 89 |
|
117 | 90 | self._pcap_file = str(data_dir) |
118 | 91 |
|
119 | | - # read pcap file for the first pass to count scans |
120 | | - print("[INFO] Pre-reading Ouster pcap to count the scans number ...") |
121 | | - self._source = pcap.Pcap(self._pcap_file, self._info) |
122 | | - self._scans_num = sum((1 for _ in client.Scans(self._source))) |
123 | | - print(f"[INFO] Ouster pcap total scans number: {self._scans_num}") |
| 92 | + self._scans_num = len(source) |
| 93 | + print(f"Ouster pcap total scans number: {self._scans_num}") |
124 | 94 |
|
125 | 95 | # frame timestamps array |
126 | 96 | self._timestamps = np.linspace(0, self._scans_num, self._scans_num, endpoint=False) |
127 | 97 |
|
128 | | - # start Scans iterator for consumption in __getitem__ |
129 | | - self._source = pcap.Pcap(self._pcap_file, self._info) |
130 | | - self._scans_iter = iter(client.Scans(self._source)) |
131 | | - self._next_idx = 0 |
| 98 | + self._source = source |
132 | 99 |
|
133 | 100 | def __getitem__(self, idx): |
134 | | - # we assume that users always reads sequentially and do not |
135 | | - # pass idx as for a random access collection |
136 | | - assert self._next_idx == idx, ( |
137 | | - "Ouster pcap dataloader supports only sequential reads. " |
138 | | - f"Expected idx: {self._next_idx}, but got {idx}" |
139 | | - ) |
140 | | - scan = next(self._scans_iter) |
141 | | - self._next_idx += 1 |
142 | | - |
143 | | - self._timestamps[self._next_idx - 1] = 1e-9 * scan.timestamp[0] |
| 101 | + scan = self._source[idx] |
| 102 | + |
| 103 | + self._timestamps[idx] = 1e-9 * scan.timestamp[0] |
144 | 104 |
|
145 | 105 | timestamps = np.tile(np.linspace(0, 1.0, scan.w, endpoint=False), (scan.h, 1)) |
146 | 106 |
|
|
0 commit comments