diff --git a/bob/io/stream/stream.py b/bob/io/stream/stream.py index 12f23e2020bccbb09b0dafaa6716b8f2f0b89dbb..5dc9198816bd864cb0d70d716cb05e9663675896 100644 --- a/bob/io/stream/stream.py +++ b/bob/io/stream/stream.py @@ -129,7 +129,7 @@ class Stream: # load one or several frames def load(self, index=None): - indices = self.get_indices(index) + indices = self.get_frame_indices(index) # return buffered data OR load from file OR process data if self.__loaded == indices and self.__data is not None: # print('loaded', self.name) @@ -142,7 +142,7 @@ class Stream: return self.__data # get list of frame indices - def get_indices(self, index): + def get_frame_indices(self, index): # None index is equivalent to [:] i.e. slice(None, None, None) if index is None: index = slice(None, None, None) @@ -219,8 +219,8 @@ class StreamFilter(Stream): super().__init__(name=name, parent=parent) pass - def get_indices(self, index): - return super().get_indices(index) + def get_frame_indices(self, index): + return super().get_frame_indices(index) def process(self, data, indices): assert isinstance(indices, list) @@ -231,7 +231,7 @@ class StreamFilter(Stream): # load one or several frames def load(self, index): - indices = self.get_indices(index) + indices = self.get_frame_indices(index) # return buffered data OR load from file OR process data if self._Stream__loaded == indices and self._Stream__data is not None: # print('loaded', self.name) diff --git a/bob/io/stream/stream_file.py b/bob/io/stream/stream_file.py index 4b9c1d41a89f33d1af0b4e8ea162a055540409f6..74945a5ca3a5627bf7113b101671b0d9865c4666 100644 --- a/bob/io/stream/stream_file.py +++ b/bob/io/stream/stream_file.py @@ -29,6 +29,7 @@ class StreamFile: if self.hdf5_file is not None: self.hdf5_file.close() + # set source def set_source( self, hdf5_file_path=None, data_format_config_file_path=None, camera_config_file_path=None, mode="r" ): @@ -40,23 +41,24 @@ class StreamFile: mode=mode, ) - print("set set_source", hdf5_file_path, self.hdf5_file) - + # get available streams def get_available_streams(self): - return list(self.data_format_config.keys()) + if self.data_format_config is not None: + return list(self.data_format_config.keys()) + else: + # TODO list available datasets if no config present + return None + # get stream config def get_stream_config(self, stream_name): if self.data_format_config is not None: data_config = self.data_format_config[stream_name] else: # return a generic config if no config is present - # TODO: make formal - data_config = { 'array_format' : {}, - 'rotation' : None, - 'camera' : None, - 'path' : stream_name} + data_config = { 'path' : stream_name} return data_config + # get stream shape def get_stream_shape(self, stream_name): data_config = self.get_stream_config(stream_name) data_path = data_config["path"] @@ -65,6 +67,7 @@ class StreamFile: shape = descriptor[1][0][1] return shape + # get stream timestamps def get_stream_timestamps(self, stream_name): data_config = self.get_stream_config(stream_name) data_path = data_config["path"] @@ -80,26 +83,41 @@ class StreamFile: else: return timestamps + # get stream camera def get_stream_camera(self, stream_name): - # TODO cache camera objects data_config = self.get_stream_config(stream_name) if "use_config_from" in data_config: data_config = self.get_stream_config(data_config["use_config_from"]) - camera_name = data_config["camera"] - if camera_name is None: + if "camera" in data_config: + camera_name = data_config["camera"] + if camera_name in self.camera_config: + return self.camera_config[camera_name] + else: + Raise('invalid camera name') + else: return None - return self.camera_config[camera_name] + # load stream data def load_stream_data(self, stream_name, index): data_config = self.get_stream_config(stream_name) - data_path = data_config["path"] if "use_config_from" in data_config: data_config = self.get_stream_config(data_config["use_config_from"]) - array_format = data_config["array_format"] - if "flip" in array_format: - array_flip = array_format["flip"] + data_path = data_config["path"] + + # load only relevant data + if isinstance(index, int): + data = np.stack([self.hdf5_file.lread(data_path, index)]) + elif isinstance(index, list): + data = np.stack([self.hdf5_file.lread(data_path, i) for i in index]) else: - array_flip = None + raise Exception("index can only be int or list") + + # flip if requested + array_flip = None + if "array_format" in data_config: + array_format = data_config["array_format"] + if "flip" in array_format: + array_flip = array_format["flip"] def flip_axes(data, axes): if axes is not None: @@ -107,24 +125,7 @@ class StreamFile: data = np.flip(data, axis=int(array_format[axis_name])) return data - # TODO load only relevant data if cropped - data = None - if isinstance(index, tuple): - index = index[0] - print("WARNING: cropping not yet implemented") - if isinstance(index, int): - data = np.stack([self.hdf5_file.lread(data_path, index)]) - elif isinstance(index, slice): - if index.step == None: - indices = list(range(index.start, index.stop)) - else: - indices = list(range(index.start, index.stop, index.step)) - data = np.stack([self.hdf5_file.lread(data_path, i) for i in indices]) - elif isinstance(index, list): - data = np.stack([self.hdf5_file.lread(data_path, i) for i in index]) - else: - raise Exception("index can only be int, slice, tuple or list") - data = flip_axes(data, array_flip) - # TODO rotate + + # TODO rotate if requested return data diff --git a/bob/io/stream/stream_filters.py b/bob/io/stream/stream_filters.py index 1c50967b2f512a0482255e3a92a242702f18dba5..b2b1d4a132672c24aafda03f22dd7d210e4bf0b7 100644 --- a/bob/io/stream/stream_filters.py +++ b/bob/io/stream/stream_filters.py @@ -170,8 +170,8 @@ class StreamAdjust(StreamFilter): def timestamps(self): return self.adjust_to.timestamps - def get_indices(self, index): - return super().get_indices(index) + def get_frame_indices(self, index): + return super().get_frame_indices(index) def load(self, index): # TODO load only relevant data if cropped @@ -179,7 +179,7 @@ class StreamAdjust(StreamFilter): index = index[0] print("WARNING: cropping not yet implemented") # original stream indices - old_indices = self.get_indices(index) + old_indices = self.get_frame_indices(index) selected_timestamps = [self.adjust_to.timestamps[i] for i in old_indices] kdtree = cKDTree(self.parent.timestamps[:, np.newaxis])