-
Notifications
You must be signed in to change notification settings - Fork 69
/
Copy pathsensors.py
395 lines (314 loc) · 12.6 KB
/
sensors.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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
#!/usr/bin/env python3
import glob
import json
import os.path
from typing import List, overload, TypeVar, Dict
from abc import ABCMeta, abstractmethod
import pandas as pd
from PIL import Image
from PIL.JpegImagePlugin import JpegImageFile
from pandas.core.frame import DataFrame
T = TypeVar('T')
class Sensor:
"""Meta class inherited by subclasses for more specific sensor types.
``Sensor`` provides generic preparation and loading methods for PandaSet folder structures. Subclasses
for specific sensor types must implement certain methods, as well as can override existing ones for extension.
Args:
directory: Absolute or relative path where sensor files are stored
Attributes:
data: List of sensor data objects. The type of list elements depends on the subclass implementation of protected method ``_load_data_file``
poses: List of sensor poses in world-coordinates
timestamps: List of recording timestamps for sensor
"""
__metaclass__ = ABCMeta
@property
@abstractmethod
def _data_file_extension(self) -> str:
...
@property
def data(self) -> List[T]:
"""Returns sensor data array.
Subclasses can use any type inside array.
"""
return self._data
@property
def poses(self) -> List[T]:
"""Returns sensor pose array.
Subclasses can use any type inside array.
"""
return self._poses
@property
def timestamps(self) -> List[T]:
"""Returns sensor timestamp array.
Subclasses can use any type inside array.
"""
return self._timestamps
def __init__(self, directory: str) -> None:
self._directory: str = directory
self._data_structure: List[str] = None
self._data: List[T] = None
self._poses_structure: str = None
self._poses: List[Dict[str, T]] = None
self._timestamps_structure: str = None
self._timestamps: List[float] = None
self._load_structure()
@overload
def __getitem__(self, item: int) -> T:
...
@overload
def __getitem__(self, item: slice) -> List[T]:
...
def __getitem__(self, item):
return self.data[item]
def _load_structure(self) -> None:
self._load_data_structure()
self._load_poses_structure()
self._load_timestamps_structure()
def _load_data_structure(self) -> None:
self._data_structure = sorted(
glob.glob(f'{self._directory}/*.{self._data_file_extension}'))
def _load_poses_structure(self) -> None:
poses_file = f'{self._directory}/poses.json'
if os.path.isfile(poses_file):
self._poses_structure = poses_file
def _load_timestamps_structure(self) -> None:
timestamps_file = f'{self._directory}/timestamps.json'
if os.path.isfile(timestamps_file):
self._timestamps_structure = timestamps_file
def load(self) -> None:
"""Loads all sensor files from disk into memory.
All sensor and associated meta data files are loaded into memory in filename order.
"""
self._load_data()
self._load_poses()
self._load_timestamps()
def _load_data(self) -> None:
self._data = []
for fp in self._data_structure:
self._data.append(self._load_data_file(fp))
def _load_poses(self) -> None:
self._poses = []
with open(self._poses_structure, 'r') as f:
file_data = json.load(f)
for entry in file_data:
self._poses.append(entry)
def _load_timestamps(self) -> None:
self._timestamps = []
with open(self._timestamps_structure, 'r') as f:
file_data = json.load(f)
for entry in file_data:
self._timestamps.append(entry)
@abstractmethod
def _load_data_file(self, fp: str) -> None:
...
class Lidar(Sensor):
def load(self) -> None:
super().load()
self._load_laser()
@property
def _data_file_extension(self) -> str:
return 'pkl.gz'
@property
def data(self) -> List[pd.DataFrame]:
"""Returns (filtered) LiDAR point cloud array.
Point cloud data is in a world-coordinate system, i.e., a static object which is a position `(10,10,0)` in frame 1, will be at position `(10,10,0)` in all other frames, too.
Returns:
List of point cloud data frames for each timestamp. Each data frame has columns as follows:
- index: `int`
- Ordered point cloud. When joining the raw point cloud with data from ``SemanticSegmentation``, it is important to keep the index order.
- `x`: `float`
- Position of point in world-coordinate system (x-axis) in meter
- `y`: `float`
- Position of point in world-coordinate system (y-axis) in meter
- `z`: `float`
- Position of point in world-coordinate system (z-axis) in meter
- `i`: `float`
- Reflection intensity in a range `[0,255]`
- `t`: `float`
- Recorded timestamp for specific point
- `d`: `int`
- Sensor ID. `0` -> mechnical 360° LiDAR, `1` -> forward-facing LiDAR
"""
if self._sensor_id in [0, 1]:
return [df.loc[df['d'] == self._sensor_id] for df in self._data]
else:
return self._data
@property
def poses(self) -> List[Dict[str, Dict[str, float]]]:
"""Returns LiDAR sensor pose array.
Returns:
A pose dictionary of the LiDAR sensor in world-coordinates for each frame. The dictionary keys return the following types:
- `position`: `dict`
- `x`: `float`
- Position of LiDAR sensor in world-coordinate system (x-axis) in meter
- `y`: `float`
- Position of LiDAR sensor in world-coordinate system (y-axis) in meter
- `z`: `float`
- Position of LiDAR sensor in world-coordinate system (z-axis) in meter
- `heading`: `dict`
- `w`: `float`
- Real part of _Quaternion_
- `x`: `float`
- First imaginary part of _Quaternion_
- `y`: `float`
- Second imaginary part of _Quaternion_
- `z`: `float`
- Third imaginary part of _Quaternion_
"""
return self._poses
@property
def timestamps(self) -> List[float]:
"""Returns LiDAR sensor recording timestamps array.
Returns:
A list of timestamps in `float` format for each point cloud recorded in this sequence. To get point-wise timestamps, please refer to column `t` in `data` property return values.
"""
return self._timestamps
def __init__(self, directory: str) -> None:
self._sensor_id = -1
Sensor.__init__(self, directory)
@overload
def __getitem__(self, item: int) -> DataFrame:
...
@overload
def __getitem__(self, item: slice) -> List[DataFrame]:
...
def __getitem__(self, item):
return super().__getitem__(item)
def set_sensor(self, sensor_id: int) -> None:
"""Specifies a sensor which should be returned exclusively in the data objects
Args:
sensor_id: Set `-1` for both LiDAR sensors, set `0` for mechanical 360° LiDAR, set `1` for front-facing LiDAR.
"""
self._sensor_id = sensor_id
def _load_data_file(self, fp: str) -> DataFrame:
return pd.read_pickle(fp)
def _load_laser(self):
lasers_file_location = f'{self._directory}/laser.pkl'
if not os.path.exists(lasers_file_location):
return
lasers = pd.read_pickle(lasers_file_location)
for laser, df in zip(lasers, self._data):
df.loc[df.index[df['d'] == 0], "laser"] = laser
class Camera(Sensor):
@property
def _data_file_extension(self) -> str:
return 'jpg'
@property
def data(self) -> List[JpegImageFile]:
"""Returns Camera image array.
Returns:
List of camera images for each timestamp. Camera images are loaded as [``JpegImageFile``](https://pillow.readthedocs.io/en/stable/reference/plugins.html#PIL.JpegImagePlugin.JpegImageFile).
"""
return self._data
@property
def poses(self) -> List[Dict[str, Dict[str, float]]]:
"""Returns Camera sensor pose array.
Returns:
A pose dictionary of the Camera sensor in world-coordinates for each frame. The dictionary keys return the following types:
- `position`: `dict`
- `x`: `float`
- Position of LiDAR sensor in world-coordinate system (x-axis) in meter
- `y`: `float`
- Position of LiDAR sensor in world-coordinate system (y-axis) in meter
- `z`: `float`
- Position of LiDAR sensor in world-coordinate system (z-axis) in meter
- `heading`: `dict`
- `w`: `float`
- Real part of _Quaternion_
- `x`: `float`
- First imaginary part of _Quaternion_
- `y`: `float`
- Second imaginary part of _Quaternion_
- `z`: `float`
- Third imaginary part of _Quaternion_
"""
return self._poses
@property
def timestamps(self) -> List[float]:
"""Returns Camera sensor recording timestamps array.
Returns:
A list of timestamps in `float` format for each camera image recorded in this sequence. To get point-wise timestamps, please refer to column `t` in `data` property return values.
"""
return self._timestamps
@property
def intrinsics(self) -> 'Intrinsics':
"""Camera specific intrinsic data.
Returns:
Instance of class ``Intrinsics``
"""
return self._intrinsics
def __init__(self, directory: str) -> None:
self._intrinsics_structure: str = None
self._intrinsics: Intrinsics = None
Sensor.__init__(self, directory)
@overload
def __getitem__(self, item: int) -> JpegImageFile:
...
@overload
def __getitem__(self, item: slice) -> List[JpegImageFile]:
...
def __getitem__(self, item):
return super().__getitem__(item)
def load(self) -> None:
super().load()
self._load_intrinsics()
def _load_structure(self) -> None:
super()._load_structure()
self._load_intrinsics_structure()
def _load_intrinsics_structure(self) -> None:
intrinsics_file = f'{self._directory}/intrinsics.json'
if os.path.isfile(intrinsics_file):
self._intrinsics_structure = intrinsics_file
def _load_data_file(self, fp: str) -> JpegImageFile:
# solve this bug: https://github.com/python-pillow/Pillow/issues/1237
img = Image.open(fp)
image = img.copy()
img.close()
return image
def _load_intrinsics(self) -> None:
with open(self._intrinsics_structure, 'r') as f:
file_data = json.load(f)
self._intrinsics = Intrinsics(fx=file_data['fx'],
fy=file_data['fy'],
cx=file_data['cx'],
cy=file_data['cy'])
class Intrinsics:
"""Camera intrinsics
Contains camera intrinsics with properties `fx`, `fy`, `cx`, `cy`, for easy usage with [OpenCV framework](https://docs.opencv.org/2.4/modules/calib3d/doc/camera_calibration_and_3d_reconstruction.html).
There is no `skew` factor in the camera recordings.
"""
@property
def fx(self) -> float:
"""Focal length x-axis
Returns:
Focal length x-axis component
"""
return self._fx
@property
def fy(self) -> float:
"""Focal length y-axis
Returns:
Focal length y-axis component
"""
return self._fy
@property
def cx(self) -> float:
"""Principal point x-axis
Returns:
Principal point x-axis component
"""
return self._cx
@property
def cy(self) -> float:
"""Principal point y-axis
Returns:
Principal point y-axis component
"""
return self._cy
def __init__(self, fx: float, fy: float, cx: float, cy: float):
self._fx: float = fx
self._fy: float = fy
self._cx: float = cx
self._cy: float = cy
if __name__ == '__main__':
pass