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 
37 
39 {
40  /**
41  * Strategy to mimic a video playback using a chunked image sequence as recorded by the ImageMonitor
42  */
44 }
45 
46 
47 
50 {
51 
52 private:
53 
54  /**
55  * @brief Path to the metadata file (without /metadata.csv)
56  */
57  std::filesystem::path metadataPath;
58 
59  /**
60  * @brief Data structure to map variable names to a tuple of their type and value
61  */
62  std::map<std::string, std::tuple<std::string, std::string>> metadata;
63 
64  /**
65  * @brief Ordered list of paths to each individual frame
66  */
67  std::vector<std::filesystem::path> framePaths;
68 
69  /**
70  * @brief File extension of the frames
71  */
72  std::filesystem::path extension;
73 
74  /**
75  * @brief Index of the current frame
76  */
77  unsigned int currentFrame;
78 
79  /**
80  * @brief Amount of frames in total
81  */
82  unsigned int frameCount;
83 
84  /**
85  * @brief Amount of frames per chunk
86  */
87  unsigned int framesPerChunk;
88 
89  /**
90  * @brief Height of a frame
91  */
92  unsigned int frameHeight;
93 
94  /**
95  * @brief Width of a frame
96  */
97  unsigned int frameWidth;
98 
99  /**
100  * @brief Frames per second of the recording
101  */
102  unsigned int fps;
103 
104  /**
105  * @brief Flag to indicate whether the instance is initialised to be able to play back
106  */
107  unsigned int playingBack;
108 
109 public:
110 
111  /**
112  * @brief Default constructor to manually setup later
113  */
115 
116  /**
117  * @brief Constructor initialising the playback immediately
118  * @param filePath Path to the recording
119  */
120  ChunkedImageSequencePlaybackStrategy(const std::filesystem::path& filePath);
121 
122  /**
123  * @brief Destructor
124  */
125  virtual ~ChunkedImageSequencePlaybackStrategy() override;
126 
127  /**
128  * @brief Indicates whether the instance is configured to be able to play back
129  * @return True if it is playing back, false otherwise
130  */
131  virtual bool isPlayingBack() const override;
132 
133  /**
134  * @brief Gets the amount of frames per second of the recording
135  * @return Amount of frames per second of the recording
136  */
137  virtual unsigned int getFps() const override;
138 
139  /**
140  * @brief Gets the total amout of frames in the recording
141  * @return Total amount of frames in the recording
142  */
143  virtual unsigned int getFrameCount() const override;
144 
145  /**
146  * @brief Gets the height of a frame in pixel
147  * @return Height of a frame in pixel
148  */
149  virtual unsigned int getFrameHeight() const override;
150 
151  /**
152  * @brief Gets the width of a frame in pixel
153  * @return Width of a frame in pixel
154  */
155  virtual unsigned int getFrameWidth() const override;
156 
157  /**
158  * @brief Sets the frame from there the playback should resume afterwards (seek)
159  * @param frame Frame from where the playback should be resumed (e.g. "0" to replay from the beginning)
160  */
161  virtual void setCurrentFrame(unsigned int frame) override;
162 
163  /**
164  * @brief Gets the current frame index of the playback
165  * @return The current frame index
166  */
167  virtual unsigned int getCurrentFrame() const override;
168 
169  /**
170  * @brief Indicates whether the recording has a consecutive frame
171  * @return True, if there is a consecutive frame, false otherwise
172  */
173  virtual bool hasNextFrame() const override;
174 
175  /**
176  * @brief Starts the playback
177  * @param filePath Path to the recording to play back
178  */
179  virtual void startPlayback(const std::filesystem::path& filePath) override;
180 
181  /**
182  * @brief Writes the next frame into a buffer of any form (RGB)
183  * @param buffer Output parameter where the frame data of the next frame should be written to
184  * @return True on success, false otherwise (no consecutive frame, error while loading frame)
185  */
186  virtual bool getNextFrame(void* buffer) override;
187 
188  /**
189  * @brief Writes the next frame into an IVT CByteImage buffer (RGB)
190  * @param buffer Output parameter where the frame data of the next frame should be written to
191  * @return True on success, false otherwise (no consecutive frame, error while loading frame)
192  */
193  virtual bool getNextFrame(::CByteImage& buffer) override;
194 
195  /**
196  * @brief Writes the next frame into an OpenCV Mat buffer (BGR)
197  * @param buffer Output parameter where the frame data of the next frame should be written to
198  * @return True on success, false otherwise (no consecutive frame, error while loading frame)
199  */
200  virtual bool getNextFrame(cv::Mat& buffer) override;
201 
202  /**
203  * @brief Stops the playback
204  */
205  virtual void stopPlayback() override;
206 
207 private:
208 
209  /**
210  * @brief Initialises local datastructure representing the metadata.csv file
211  */
212  void initMetadata();
213 
214  /**
215  * @brief Initialises the FPS from metadata file or uses a (guessed) fallback value if not present
216  *
217  * Metadata will be populated by the guessed value if it was not present
218  */
219  void initFps();
220 
221  /**
222  * @brief Initialises the file extension from metadata file or tries to guess it by crawling the folders
223  *
224  * Metadata will be populated by the guessed value if it was not present
225  */
226  void initExtension();
227 
228  /**
229  * @brief Initialises the frames per chunk from metadata file or tries to guess it by crawling the first folder
230  *
231  * Metadata will be populated by the guessed value if it was not present
232  */
233  void initFramesPerChunk();
234 
235  /**
236  * @brief Initialises the frame count from metadata file or tries to guess it by crawling the folders
237  *
238  * Metadata will be populated by the guessed value if it was not present
239  */
240  void initFrameCount();
241 
242  /**
243  * @brief Initialises the frame height from metadata file or guesses it by probing the first frame
244  *
245  * Metadata will be populated by the guessed value if it was not present
246  */
247  void initFrameHeight();
248 
249  /**
250  * @brief Initialises the frame width from metadata file or guesses it by probing the first frame
251  *
252  * Metadata will be populated by the guessed value if it was not present
253  */
254  void initFrameWidth();
255 
256  /**
257  * @brief Helper to populate the metadata file with missing values
258  * @param varName Name of the variable
259  * @param varType Type of the variable
260  * @param varValue Value of the variable
261  */
262  void updateMetadata(const std::string& varName, const std::string& varType, const std::string& varValue);
263 
264 };
visionx::imrec::strats::ChunkedImageSequencePlaybackStrategy::getCurrentFrame
virtual unsigned int getCurrentFrame() const override
Gets the current frame index of the playback.
Definition: ChunkedImageSequencePlaybackStrategy.cpp:118
visionx::imrec::strats::ChunkedImageSequencePlaybackStrategy::getFrameHeight
virtual unsigned int getFrameHeight() const override
Gets the height of a frame in pixel.
Definition: ChunkedImageSequencePlaybackStrategy.cpp:97
visionx::imrec::strats::ChunkedImageSequencePlaybackStrategy::stopPlayback
virtual void stopPlayback() override
Stops the playback.
Definition: ChunkedImageSequencePlaybackStrategy.cpp:266
visionx::imrec::strats::ChunkedImageSequencePlaybackStrategy::~ChunkedImageSequencePlaybackStrategy
virtual ~ChunkedImageSequencePlaybackStrategy() override
Destructor.
Definition: ChunkedImageSequencePlaybackStrategy.cpp:69
AbstractPlaybackStrategy.h
visionx::imrec::strats::ChunkedImageSequencePlaybackStrategy
Definition: ChunkedImageSequencePlaybackStrategy.h:48
visionx::imrec::strats::ChunkedImageSequencePlaybackStrategy::getNextFrame
virtual bool getNextFrame(void *buffer) override
Writes the next frame into a buffer of any form (RGB)
Definition: ChunkedImageSequencePlaybackStrategy.cpp:214
visionx::imrec::strats::ChunkedImageSequencePlaybackStrategy::isPlayingBack
virtual bool isPlayingBack() const override
Indicates whether the instance is configured to be able to play back.
Definition: ChunkedImageSequencePlaybackStrategy.cpp:76
visionx::imrec::strats::ChunkedImageSequencePlaybackStrategy::ChunkedImageSequencePlaybackStrategy
ChunkedImageSequencePlaybackStrategy()
Default constructor to manually setup later.
Definition: ChunkedImageSequencePlaybackStrategy.cpp:51
visionx::imrec::strats::ChunkedImageSequencePlaybackStrategy::hasNextFrame
virtual bool hasNextFrame() const override
Indicates whether the recording has a consecutive frame.
Definition: ChunkedImageSequencePlaybackStrategy.cpp:125
visionx::imrec::strats::ChunkedImageSequencePlaybackStrategy::getFps
virtual unsigned int getFps() const override
Gets the amount of frames per second of the recording.
Definition: ChunkedImageSequencePlaybackStrategy.cpp:83
visionx::imrec::strats::ChunkedImageSequencePlaybackStrategy::setCurrentFrame
virtual void setCurrentFrame(unsigned int frame) override
Sets the frame from there the playback should resume afterwards (seek)
Definition: ChunkedImageSequencePlaybackStrategy.cpp:111
visionx::imrec::strats::ChunkedImageSequencePlaybackStrategy::getFrameWidth
virtual unsigned int getFrameWidth() const override
Gets the width of a frame in pixel.
Definition: ChunkedImageSequencePlaybackStrategy.cpp:104
visionx::imrec::strats::ChunkedImageSequencePlaybackStrategy::startPlayback
virtual void startPlayback(const std::filesystem::path &filePath) override
Starts the playback.
Definition: ChunkedImageSequencePlaybackStrategy.cpp:147
visionx::imrec::strats
Definition: ChunkedImageSequencePlaybackStrategy.h:38
visionx::imrec::strats::ChunkedImageSequencePlaybackStrategy::getFrameCount
virtual unsigned int getFrameCount() const override
Gets the total amout of frames in the recording.
Definition: ChunkedImageSequencePlaybackStrategy.cpp:90
visionx::imrec::AbstractPlaybackStrategy
Definition: AbstractPlaybackStrategy.h:52