main.cpp
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::applications
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 // STD/STL
25 #include <iostream>
26 #include <string>
27 
28 // Boost
29 #include <filesystem>
30 #include <boost/program_options.hpp>
31 
32 // OpenCV
33 #include <opencv2/core/core.hpp>
34 
35 // VisionX
37 
38 
39 /**
40  * @brief Parses argv and writes the results to the out parameters description, help, progress, in_path, out_path
41  * @param argc Number of arguments
42  * @param argv Arguments array
43  * @param description Description of the program, listing all arguments etc
44  * @param help True, if help was requested while invoking the program
45  * @param progress True, if progress reporting was requested while invoking the program
46  * @param in_path Input path defined when invoking the program
47  * @param out_path Output path defined when invoking the program
48  */
49 void init_args(int argc, char* argv[], std::string& description, bool& help, bool& progress, std::filesystem::path& in_path, std::filesystem::path& out_path);
50 
51 
52 /**
53  * @brief Performs the actual conversion
54  * @param in Path to the input file
55  * @param out Path to the output file
56  */
57 void convert(const std::filesystem::path& in, const std::filesystem::path& out, bool print_progress);
58 
59 
60 int
61 main(int argc, char* argv[])
62 {
63  std::string description = "";
64  const std::string usage = "Usage: cvtrec input_file output_file\n"
65  " input_file: Path to input file\n"
66  " output_file: Path to output file";
67  bool help = false;
68  bool progress = false;
69  std::filesystem::path in_path = "";
70  std::filesystem::path out_path = "";
71 
72  // Initialise arguments and options
73  ::init_args(argc, argv, description, help, progress, in_path, out_path);
74 
75  // Print options for --help and exit
76  if (help)
77  {
78  std::cout << "cvtrec - VisionX utility to convert recordings" << std::endl << std::endl;
79  std::cout << usage << std::endl << std::endl;
80  std::cout << "Example: (To convert a PNG image sequence to AVI)" << std::endl;
81  std::cout << " `cvtrec /path/to/img/seq/ ./videofile.avi`" << std::endl << std::endl;
82  std::cout << description << std::endl;
83  return EXIT_SUCCESS;
84  }
85 
86  // Ensure mandatory arguments were set
87  if (in_path == "" or out_path == "")
88  {
89  std::cerr << usage << std::endl;
90  return EXIT_FAILURE;
91  }
92 
93  // Ensure that target format is different from source format
94  if (in_path.extension() == out_path.extension())
95  {
96  std::cerr << "Source and target formats are the same" << std::endl;
97  return EXIT_FAILURE;
98  }
99 
100  // Try running conversion
101  try
102  {
103  ::convert(in_path, out_path, progress);
104  return EXIT_SUCCESS;
105  }
106  catch (const std::exception& e)
107  {
108  if (progress)
109  {
110  std::cerr << std::endl;
111  }
112  std::cerr << "Error while converting: " << e.what() << std::endl;
113  return EXIT_FAILURE;
114  }
115 }
116 
117 
118 void
119 init_args(int argc, char* argv[], std::string& description, bool& help, bool& progress, std::filesystem::path& in_path, std::filesystem::path& out_path)
120 {
121  boost::program_options::options_description desc("Arguments and options");
122  boost::program_options::variables_map vm;
123 
124  // Define options
125  desc.add_options()
126  ("help,h", "Show this message")
127  ("progress,p", "Print information about the conversion progress")
128  ("in", boost::program_options::value<std::filesystem::path>(&in_path), "Path to input file which should be converted")
129  ("out", boost::program_options::value<std::filesystem::path>(&out_path), "Path to output file where the converted recording should be written to");
130 
131  // Designate positional parameters
132  boost::program_options::positional_options_description pos;
133  pos.add("in", 1);
134  pos.add("out", 1);
135 
136  // Parse argv
137  boost::program_options::store(boost::program_options::command_line_parser(argc, argv).options(desc).positional(pos).run(), vm);
138  boost::program_options::notify(vm);
139 
140  // Set description
141  std::stringstream description_stream;
142  desc.print(description_stream);
143  description = description_stream.str();
144 
145  // Set help flag
146  help = vm.find("help") != vm.end();
147 
148  // Set progress flag
149  progress = vm.find("progress") != vm.end();
150 }
151 
152 
153 void
154 convert(const std::filesystem::path& in, const std::filesystem::path& out, bool print_progress)
155 {
156  // Initialise playback, recording and frame_buffer
158  if (playback == nullptr)
159  {
160  throw std::runtime_error("Could not read input file format");
161  }
162  visionx::imrec::Recording recording = visionx::imrec::newRecording(out, playback->getFps());
163  recording->startRecording();
164  cv::Mat frame_buffer;
165  const unsigned int frame_count = playback->getFrameCount();
166 
167  // Loop over playback and write each frame into recording
168  for (unsigned int i = 0; i < frame_count; ++i)
169  {
170  const bool success = playback->getNextFrame(frame_buffer);
171  if (!success)
172  {
173  throw std::runtime_error("Could not read frame");
174  }
175  recording->recordFrame(frame_buffer, std::chrono::microseconds{0});
176 
177  // Print progress
178  if (print_progress)
179  {
180  std::cerr << "\r" << std::flush;
181  std::cerr << "Converting... " << (i * 100 / (frame_count - 1)) << "%" << std::flush;
182  }
183  }
184  recording->stopRecording();
185 
186  if (print_progress)
187  {
188  std::cerr << std::endl;
189  }
190 }
convert
void convert(const std::filesystem::path &in, const std::filesystem::path &out, bool print_progress)
Performs the actual conversion.
Definition: main.cpp:154
visionx::imrec::Playback
std::shared_ptr< visionx::imrec::AbstractPlaybackStrategy > Playback
Convenience alias for an instance of any playback strategy.
Definition: AbstractPlaybackStrategy.h:48
armarx::armem::server::ltm::mongodb::util::store
void store(const mongocxx::database &db, const armem::wm::Memory &m)
Definition: operations.cpp:237
imrec.h
armarx::flush
const LogSender::manipulator flush
Definition: LogSender.h:251
main
int main(int argc, char *argv[])
Definition: main.cpp:31
visionx::imrec::newRecording
visionx::imrec::Recording newRecording(const std::filesystem::path &path, const std::string &name, const Format format, double fps)
Definition: public_api.cpp:237
init_args
void init_args(int argc, char *argv[], std::string &description, bool &help, bool &progress, std::filesystem::path &in_path, std::filesystem::path &out_path)
Parses argv and writes the results to the out parameters description, help, progress,...
Definition: main.cpp:119
visionx::imrec::Recording
std::shared_ptr< AbstractRecordingStrategy > Recording
Convenience alias for any recording strategy.
Definition: AbstractRecordingStrategy.h:181
visionx::imrec::newPlayback
visionx::imrec::Playback newPlayback(const std::filesystem::path &path)
Instantiates and returns a new playback strategy which is capable of replaying the file or collection...
Definition: public_api.cpp:205