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