ChunkedImageSequencePlaybackStrategy.h
Go to the documentation of this file.
1/*
2 * This file is part of ArmarX.
3 *
4 * ArmarX is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2 as
6 * published by the Free Software Foundation.
7 *
8 * ArmarX is distributed in the hope that it will be useful, but
9 * WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
15 *
16 * @package visionx::imrec
17 * @author Christian R. G. Dreher <christian.dreher@student.kit.edu>
18 * @date 2018
19 * @copyright http://www.gnu.org/licenses/gpl-2.0.txt
20 * GNU General Public License
21 */
22
23
24#pragma once
25
26
27// STD/STL
28#include <filesystem>
29#include <map>
30#include <string>
31#include <tuple>
32#include <vector>
33
34// VisionX
36
38{
39 /**
40 * Strategy to mimic a video playback using a chunked image sequence as recorded by the ImageMonitor
41 */
43} // namespace visionx::imrec::strats
44
47{
48
49private:
50 /**
51 * @brief Path to the metadata file (without /metadata.csv)
52 */
53 std::filesystem::path metadataPath;
54
55 /**
56 * @brief Data structure to map variable names to a tuple of their type and value
57 */
58 std::map<std::string, std::tuple<std::string, std::string>> metadata;
59
60 /**
61 * @brief Ordered list of paths to each individual frame
62 */
63 std::vector<std::filesystem::path> framePaths;
64
65 /**
66 * @brief File extension of the frames
67 */
68 std::filesystem::path extension;
69
70 /**
71 * @brief Index of the current frame
72 */
73 unsigned int currentFrame;
74
75 /**
76 * @brief Amount of frames in total
77 */
78 unsigned int frameCount;
79
80 /**
81 * @brief Amount of frames per chunk
82 */
83 unsigned int framesPerChunk;
84
85 /**
86 * @brief Height of a frame
87 */
88 unsigned int frameHeight;
89
90 /**
91 * @brief Width of a frame
92 */
93 unsigned int frameWidth;
94
95 /**
96 * @brief Frames per second of the recording
97 */
98 unsigned int fps;
99
100 /**
101 * @brief Flag to indicate whether the instance is initialised to be able to play back
102 */
103 unsigned int playingBack;
104
105public:
106 /**
107 * @brief Default constructor to manually setup later
108 */
110
111 /**
112 * @brief Constructor initialising the playback immediately
113 * @param filePath Path to the recording
114 */
115 ChunkedImageSequencePlaybackStrategy(const std::filesystem::path& filePath);
116
117 /**
118 * @brief Destructor
119 */
120 virtual ~ChunkedImageSequencePlaybackStrategy() override;
121
122 /**
123 * @brief Indicates whether the instance is configured to be able to play back
124 * @return True if it is playing back, false otherwise
125 */
126 virtual bool isPlayingBack() const override;
127
128 /**
129 * @brief Gets the amount of frames per second of the recording
130 * @return Amount of frames per second of the recording
131 */
132 virtual unsigned int getFps() const override;
133
134 /**
135 * @brief Gets the total amout of frames in the recording
136 * @return Total amount of frames in the recording
137 */
138 virtual unsigned int getFrameCount() const override;
139
140 /**
141 * @brief Gets the height of a frame in pixel
142 * @return Height of a frame in pixel
143 */
144 virtual unsigned int getFrameHeight() const override;
145
146 /**
147 * @brief Gets the width of a frame in pixel
148 * @return Width of a frame in pixel
149 */
150 virtual unsigned int getFrameWidth() const override;
151
152 /**
153 * @brief Sets the frame from there the playback should resume afterwards (seek)
154 * @param frame Frame from where the playback should be resumed (e.g. "0" to replay from the beginning)
155 */
156 virtual void setCurrentFrame(unsigned int frame) override;
157
158 /**
159 * @brief Gets the current frame index of the playback
160 * @return The current frame index
161 */
162 virtual unsigned int getCurrentFrame() const override;
163
164 /**
165 * @brief Indicates whether the recording has a consecutive frame
166 * @return True, if there is a consecutive frame, false otherwise
167 */
168 virtual bool hasNextFrame() const override;
169
170 /**
171 * @brief Starts the playback
172 * @param filePath Path to the recording to play back
173 */
174 virtual void startPlayback(const std::filesystem::path& filePath) override;
175
176 /**
177 * @brief Writes the next frame into a buffer of any form (RGB)
178 * @param buffer Output parameter where the frame data of the next frame should be written to
179 * @return True on success, false otherwise (no consecutive frame, error while loading frame)
180 */
181 virtual bool getNextFrame(void* buffer) override;
182
183 /**
184 * @brief Writes the next frame into an IVT CByteImage buffer (RGB)
185 * @param buffer Output parameter where the frame data of the next frame should be written to
186 * @return True on success, false otherwise (no consecutive frame, error while loading frame)
187 */
188 virtual bool getNextFrame(::CByteImage& buffer) override;
189
190 /**
191 * @brief Writes the next frame into an OpenCV Mat buffer (BGR)
192 * @param buffer Output parameter where the frame data of the next frame should be written to
193 * @return True on success, false otherwise (no consecutive frame, error while loading frame)
194 */
195 virtual bool getNextFrame(cv::Mat& buffer) override;
196
197 /**
198 * @brief Stops the playback
199 */
200 virtual void stopPlayback() override;
201
202private:
203 /**
204 * @brief Initialises local datastructure representing the metadata.csv file
205 */
206 void initMetadata();
207
208 /**
209 * @brief Initialises the FPS from metadata file or uses a (guessed) fallback value if not present
210 *
211 * Metadata will be populated by the guessed value if it was not present
212 */
213 void initFps();
214
215 /**
216 * @brief Initialises the file extension from metadata file or tries to guess it by crawling the folders
217 *
218 * Metadata will be populated by the guessed value if it was not present
219 */
220 void initExtension();
221
222 /**
223 * @brief Initialises the frames per chunk from metadata file or tries to guess it by crawling the first folder
224 *
225 * Metadata will be populated by the guessed value if it was not present
226 */
227 void initFramesPerChunk();
228
229 /**
230 * @brief Initialises the frame count from metadata file or tries to guess it by crawling the folders
231 *
232 * Metadata will be populated by the guessed value if it was not present
233 */
234 void initFrameCount();
235
236 /**
237 * @brief Initialises the frame height from metadata file or guesses it by probing the first frame
238 *
239 * Metadata will be populated by the guessed value if it was not present
240 */
241 void initFrameHeight();
242
243 /**
244 * @brief Initialises the frame width from metadata file or guesses it by probing the first frame
245 *
246 * Metadata will be populated by the guessed value if it was not present
247 */
248 void initFrameWidth();
249
250 /**
251 * @brief Helper to populate the metadata file with missing values
252 * @param varName Name of the variable
253 * @param varType Type of the variable
254 * @param varValue Value of the variable
255 */
256 void updateMetadata(const std::string& varName,
257 const std::string& varType,
258 const std::string& varValue);
259};
virtual void setCurrentFrame(unsigned int frame) override
Sets the frame from there the playback should resume afterwards (seek)
virtual unsigned int getFrameWidth() const override
Gets the width of a frame in pixel.
virtual unsigned int getFps() const override
Gets the amount of frames per second of the recording.
virtual bool hasNextFrame() const override
Indicates whether the recording has a consecutive frame.
virtual unsigned int getFrameCount() const override
Gets the total amout of frames in the recording.
virtual unsigned int getCurrentFrame() const override
Gets the current frame index of the playback.
virtual bool getNextFrame(void *buffer) override
Writes the next frame into a buffer of any form (RGB)
virtual void startPlayback(const std::filesystem::path &filePath) override
Starts the playback.
virtual bool isPlayingBack() const override
Indicates whether the instance is configured to be able to play back.
virtual unsigned int getFrameHeight() const override
Gets the height of a frame in pixel.