|
1 | 1 | import glob |
| 2 | +import json |
| 3 | +import os.path |
| 4 | + |
2 | 5 | import pandas as pd |
3 | 6 | from PIL import Image |
4 | | -import os.path |
5 | | -import json |
6 | 7 |
|
7 | 8 |
|
8 | | -class Lidar: |
9 | | - def __init__(self, directory): |
| 9 | +class Sensor: |
| 10 | + def __init__(self, directory, data_file_extension): |
10 | 11 | self._directory = directory |
| 12 | + self._data_file_extension = data_file_extension |
11 | 13 | self._data_structure = None |
12 | 14 | self.data = None |
13 | | - self._pose_structure = None |
| 15 | + self._poses_structure = None |
14 | 16 | self.poses = None |
15 | 17 | self._timestamps_structure = None |
16 | 18 | self.timestamps = None |
17 | 19 | self._load_data_structure() |
18 | 20 |
|
| 21 | + def __getitem__(self, item): |
| 22 | + return self.data[item] |
| 23 | + |
19 | 24 | def _load_data_structure(self): |
20 | | - self._data_structure = sorted(glob.glob(f'{self._directory}/*.pkl.gz')) |
| 25 | + self._data_structure = sorted(glob.glob(f'{self._directory}/*.{self._data_file_extension}')) |
21 | 26 |
|
22 | | - positions_file = f'{self._directory}/positions.json' |
23 | | - if os.path.isfile(positions_file): |
24 | | - self._pose_structure = positions_file |
| 27 | + poses_file = f'{self._directory}/poses.json' |
| 28 | + if os.path.isfile(poses_file): |
| 29 | + self._poses_structure = poses_file |
25 | 30 |
|
26 | 31 | timestamps_file = f'{self._directory}/timestamps.json' |
27 | 32 | if os.path.isfile(timestamps_file): |
28 | 33 | self._timestamps_structure = timestamps_file |
29 | 34 |
|
30 | | - def load_data(self, sl): |
| 35 | + def load(self): |
| 36 | + self._load_data() |
| 37 | + self._load_poses() |
| 38 | + self._load_timestamps() |
| 39 | + |
| 40 | + def _load_data(self): |
31 | 41 | self.data = [] |
32 | | - for fp in self._data_structure[sl]: |
| 42 | + for fp in self._data_structure: |
33 | 43 | self.data.append( |
34 | | - pd.read_pickle(fp) |
| 44 | + self._load_data_file(fp) |
35 | 45 | ) |
36 | | - self.load_positions(sl) |
37 | | - self.load_timestamps(sl) |
38 | 46 |
|
39 | | - def load_positions(self, sl): |
| 47 | + def _load_poses(self): |
40 | 48 | self.poses = [] |
41 | | - with open(self._pose_structure, 'r') as f: |
| 49 | + with open(self._poses_structure, 'r') as f: |
42 | 50 | file_data = json.load(f) |
43 | | - for entry in file_data[sl]: |
| 51 | + for entry in file_data: |
44 | 52 | self.poses.append( |
45 | 53 | entry |
46 | 54 | ) |
47 | 55 |
|
48 | | - def load_timestamps(self, sl): |
| 56 | + def _load_timestamps(self): |
49 | 57 | self.timestamps = [] |
50 | 58 | with open(self._timestamps_structure, 'r') as f: |
51 | 59 | file_data = json.load(f) |
52 | | - for entry in file_data[sl]: |
| 60 | + for entry in file_data: |
53 | 61 | self.timestamps.append( |
54 | 62 | entry |
55 | 63 | ) |
56 | 64 |
|
| 65 | + def _load_data_file(self, fp): |
| 66 | + return None |
57 | 67 |
|
58 | | -class Camera: |
59 | | - def __init__(self, directory): |
60 | | - self._directory = directory |
61 | | - self._data_structure = None |
62 | | - self.data = None |
63 | | - self._pose_structure = None |
64 | | - self.poses = None |
65 | | - self._timestamps_structure = None |
66 | | - self.timestamps = None |
67 | | - self._load_data_structure() |
68 | 68 |
|
69 | | - def _load_data_structure(self): |
70 | | - self._data_structure = sorted(glob.glob(f'{self._directory}/*.jpg')) |
| 69 | +class Lidar(Sensor): |
71 | 70 |
|
72 | | - positions_file = f'{self._directory}/positions.json' |
73 | | - if os.path.isfile(positions_file): |
74 | | - self._pose_structure = positions_file |
| 71 | + def __init__(self, directory): |
| 72 | + Sensor.__init__(self, directory, 'pkl.gz') |
75 | 73 |
|
76 | | - timestamps_file = f'{self._directory}/timestamps.json' |
77 | | - if os.path.isfile(timestamps_file): |
78 | | - self._timestamps_structure = timestamps_file |
| 74 | + def _load_data_file(self, fp): |
| 75 | + return pd.read_pickle(fp) |
79 | 76 |
|
80 | | - def load_data(self, sl): |
81 | | - self.data = [] |
82 | | - for fp in self._data_structure[sl]: |
83 | | - self.data.append( |
84 | | - Image.open(fp) |
85 | | - ) |
86 | | - self.load_positions(sl) |
87 | | - self.load_timestamps(sl) |
88 | 77 |
|
89 | | - def load_positions(self, sl): |
90 | | - self.poses = [] |
91 | | - with open(self._pose_structure, 'r') as f: |
92 | | - file_data = json.load(f) |
93 | | - for entry in file_data[sl]: |
94 | | - self.poses.append( |
95 | | - entry |
96 | | - ) |
| 78 | +class Camera(Sensor): |
| 79 | + def __init__(self, directory): |
| 80 | + Sensor.__init__(self, directory, 'jpg') |
97 | 81 |
|
98 | | - def load_timestamps(self, sl): |
99 | | - self.timestamps = [] |
100 | | - with open(self._timestamps_structure, 'r') as f: |
101 | | - file_data = json.load(f) |
102 | | - for entry in file_data[sl]: |
103 | | - self.timestamps.append( |
104 | | - entry |
105 | | - ) |
| 82 | + def _load_data_file(self, fp): |
| 83 | + return Image.open(fp) |
0 commit comments