Visual Servoing Platform  version 3.6.1 under development (2025-03-03)
videoReader.cpp
1 /****************************************************************************
2  *
3  * ViSP, open source Visual Servoing Platform software.
4  * Copyright (C) 2005 - 2023 by Inria. All rights reserved.
5  *
6  * This software is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  * See the file LICENSE.txt at the root directory of this source
11  * distribution for additional information about the GNU GPL.
12  *
13  * For using ViSP with software that can not be combined with the GNU
14  * GPL, please contact Inria about acquiring a ViSP Professional
15  * Edition License.
16  *
17  * See https://visp.inria.fr for more information.
18  *
19  * This software was developed at:
20  * Inria Rennes - Bretagne Atlantique
21  * Campus Universitaire de Beaulieu
22  * 35042 Rennes Cedex
23  * France
24  *
25  * If you have questions regarding the use of this file, please contact
26  * Inria at visp@inria.fr
27  *
28  * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
29  * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
30  *
31  * Description:
32  * Reading a video file.
33  *
34 *****************************************************************************/
35 
46 #include <visp3/core/vpConfig.h>
47 #include <visp3/core/vpDebug.h>
48 #include <visp3/core/vpImage.h>
49 #include <visp3/core/vpIoTools.h>
50 #include <visp3/gui/vpDisplayFactory.h>
51 #include <visp3/io/vpImageIo.h>
52 #include <visp3/io/vpParseArgv.h>
53 #include <visp3/io/vpVideoReader.h>
54 
55 #if defined(VISP_HAVE_DISPLAY)
56 
57 // List of allowed command line options
58 #define GETOPTARGS "cdi:p:h"
59 
60 #ifdef ENABLE_VISP_NAMESPACE
61 using namespace VISP_NAMESPACE_NAME;
62 #endif
63 
64 void usage(const char *name, const char *badparam, std::string ipath, std::string ppath);
65 bool getOptions(int argc, const char **argv, std::string &ipath, std::string &ppath, bool &click_allowed,
66  bool &display);
67 
78 void usage(const char *name, const char *badparam, std::string ipath, std::string ppath)
79 {
80  fprintf(stdout, "\n\
81 Read a video file on the disk.\n\
82 \n\
83 SYNOPSIS\n\
84  %s [-i <input video path>] \n\
85  [-h]\n\
86 ",
87 name);
88 
89  fprintf(stdout, "\n\
90 OPTIONS: Default\n\
91  -i <input video path> %s\n\
92  Set video input path.\n\
93  From this path read \"video/cube.mpeg\"\n\
94  video.\n\
95  Setting the VISP_INPUT_IMAGE_PATH environment\n\
96  variable produces the same behaviour than using\n\
97  this option.\n\
98 \n\
99  -p <personal video path> %s\n\
100  Specify a personal folder containing a video \n\
101  to process.\n\
102  Example : \"/Temp/visp-images/video/video.mpeg\"\n\
103 \n\
104  -c\n\
105  Disable the mouse click. Useful to automate the \n\
106  execution of this program without human intervention.\n\
107 \n\
108  -d \n\
109  Turn off the display.\n\
110 \n\
111  -h\n\
112  Print the help.\n\n",
113  ipath.c_str(), ppath.c_str());
114 
115  if (badparam) {
116  fprintf(stderr, "ERROR: \n");
117  fprintf(stderr, "\nBad parameter [%s]\n", badparam);
118  }
119 }
133 bool getOptions(int argc, const char **argv, std::string &ipath, std::string &ppath, bool &click_allowed, bool &display)
134 {
135  const char *optarg_;
136  int c;
137  while ((c = vpParseArgv::parse(argc, argv, GETOPTARGS, &optarg_)) > 1) {
138 
139  switch (c) {
140  case 'c':
141  click_allowed = false;
142  break;
143  case 'd':
144  display = false;
145  break;
146  case 'i':
147  ipath = optarg_;
148  break;
149  case 'p':
150  ppath = optarg_;
151  break;
152  case 'h':
153  usage(argv[0], nullptr, ipath, ppath);
154  return false;
155  break;
156 
157  default:
158  usage(argv[0], optarg_, ipath, ppath);
159  return false;
160  break;
161  }
162  }
163 
164  if ((c == 1) || (c == -1)) {
165  // standalone param or error
166  usage(argv[0], nullptr, ipath, ppath);
167  std::cerr << "ERROR: " << std::endl;
168  std::cerr << " Bad argument " << optarg_ << std::endl << std::endl;
169  return false;
170  }
171 
172  return true;
173 }
174 
175 int main(int argc, const char **argv)
176 {
177 #if (VISP_CXX_STANDARD >= VISP_CXX_STANDARD_11)
178  std::shared_ptr<vpDisplay> display;
179 #else
180  vpDisplay *display = nullptr;
181 #endif
182  try {
183  std::string env_ipath;
184  std::string opt_ipath;
185  std::string ipath;
186  std::string opt_ppath;
187  std::string filename;
188  bool opt_click_allowed = true;
189  bool opt_display = true;
190 
191  std::cout << "-------------------------------------------------------" << std::endl;
192  std::cout << " videoReader.cpp" << std::endl << std::endl;
193 
194  std::cout << " reading a video file" << std::endl;
195  std::cout << "-------------------------------------------------------" << std::endl;
196  std::cout << std::endl;
197 
198  // Get the visp-images-data package path or VISP_INPUT_IMAGE_PATH
199  // environment variable value
200  env_ipath = vpIoTools::getViSPImagesDataPath();
201 
202  // Set the default input path
203  if (!env_ipath.empty())
204  ipath = env_ipath;
205 
206  // Read the command line options
207  if (getOptions(argc, argv, opt_ipath, opt_ppath, opt_click_allowed, opt_display) == false) {
208  return EXIT_FAILURE;
209  }
210 
211  // Get the option values
212  if (!opt_ipath.empty())
213  ipath = opt_ipath;
214 
215  // Compare ipath and env_ipath. If they differ, we take into account
216  // the input path coming from the command line option
217  if (!opt_ipath.empty() && !env_ipath.empty() && opt_ppath.empty()) {
218  if (ipath != env_ipath) {
219  std::cout << std::endl << "WARNING: " << std::endl;
220  std::cout << " Since -i <visp image path=" << ipath << "> "
221  << " is different from VISP_IMAGE_PATH=" << env_ipath << std::endl
222  << " we skip the environment variable." << std::endl;
223  }
224  }
225 
226  // Test if an input path is set
227  if (opt_ipath.empty() && env_ipath.empty() && opt_ppath.empty()) {
228  usage(argv[0], nullptr, ipath, opt_ppath);
229  std::cerr << std::endl << "ERROR:" << std::endl;
230  std::cerr << " Use -i <visp image path> option or set VISP_INPUT_IMAGE_PATH " << std::endl
231  << " environment variable to specify the location of the " << std::endl
232  << " video path where test images are located." << std::endl
233  << std::endl;
234  return EXIT_FAILURE;
235  }
236 
238 
239  // vpImage is a template class you can declare vpImage of ...
240  // everything...
241  vpImage<vpRGBa> I;
242 
243  // Create the video Reader
244  vpVideoReader reader;
245 
246  if (opt_ppath.empty()) {
247  filename = vpIoTools::createFilePath(ipath, "video/cube.mpeg");
248  }
249  else {
250  filename.assign(opt_ppath);
251  }
252 
253  // Initialize the reader and get the first frame.
254  std::cout << "Process video in " << filename << std::endl;
255  reader.setFileName(filename);
256  reader.open(I);
257 
258  if (opt_display) {
259  // Display size is automatically defined by the image (I) size
260 #if (VISP_CXX_STANDARD >= VISP_CXX_STANDARD_11)
261  display = vpDisplayFactory::createDisplay(I, 100, 100, "Display video frame");
262 #else
263  display = vpDisplayFactory::allocateDisplay(I, 100, 100, "Display video frame");
264 #endif
266  vpDisplay::flush(I);
267  }
268 
269  // if (opt_display && opt_click_allowed)
270  // {
271  // std::cout << "Click on the image to read and display the last key
272  // frame" << std::endl; vpDisplay::getClick(I);
273  // }
274  //
275  // reader.getFrame(I,reader.getLastFrameIndex());
276  //
277  // if (opt_display)
278  // {
279  // vpDisplay::display(I) ;
280  // vpDisplay::flush(I);
281  // }
282 
283  if (opt_display && opt_click_allowed) {
284  std::cout << "Click to see the video" << std::endl;
286  }
287 
288  while (!reader.end()) {
289  reader.acquire(I);
290  std::cout << "Display frame: " << reader.getFrameIndex() << std::endl;
291  if (opt_display) {
293  if (opt_click_allowed) {
294  vpDisplay::displayText(I, 15, 15, "A click to stop...", vpColor::red);
295 
296  if (vpDisplay::getClick(I, false)) {
297  break;
298  }
299  }
300  vpDisplay::flush(I);
301  }
302  }
303 
304  if (opt_display && opt_click_allowed) {
305  std::cout << "Click to exit this example" << std::endl;
307  }
308  }
309  catch (const vpException &e) {
310  std::cout << "Catch an exception: " << e << std::endl;
311  }
312 #if (VISP_CXX_STANDARD < VISP_CXX_STANDARD_11)
313  if (display != nullptr) {
314  delete display;
315  }
316 #endif
317  return EXIT_SUCCESS;
318 }
319 #else
320 int main()
321 {
322  std::cout << "Sorry, no display is available. We quit this example." << std::endl;
323  return EXIT_SUCCESS;
324 }
325 #endif
static const vpColor red
Definition: vpColor.h:198
Class that defines generic functionalities for display.
Definition: vpDisplay.h:178
static bool getClick(const vpImage< unsigned char > &I, bool blocking=true)
static void display(const vpImage< unsigned char > &I)
static void flush(const vpImage< unsigned char > &I)
static void displayText(const vpImage< unsigned char > &I, const vpImagePoint &ip, const std::string &s, const vpColor &color)
error that can be emitted by ViSP classes.
Definition: vpException.h:60
static std::string getViSPImagesDataPath()
Definition: vpIoTools.cpp:1053
static std::string createFilePath(const std::string &parent, const std::string &child)
Definition: vpIoTools.cpp:1439
static bool parse(int *argcPtr, const char **argv, vpArgvInfo *argTable, int flags)
Definition: vpParseArgv.cpp:70
Class that enables to manipulate easily a video file or a sequence of images. As it inherits from the...
void acquire(vpImage< vpRGBa > &I)
void open(vpImage< vpRGBa > &I)
void setFileName(const std::string &filename)
long getFrameIndex() const
std::shared_ptr< vpDisplay > createDisplay()
Return a smart pointer vpDisplay specialization if a GUI library is available or nullptr otherwise.
vpDisplay * allocateDisplay()
Return a newly allocated vpDisplay specialization if a GUI library is available or nullptr otherwise.