Visual Servoing Platform  version 3.6.1 under development (2024-11-15)
tutorial-video-recorder.cpp
1 
2 #include <visp3/core/vpConfig.h>
3 #include <visp3/core/vpTime.h>
4 #include <visp3/gui/vpDisplayFactory.h>
5 #include <visp3/gui/vpDisplayX.h>
6 #include <visp3/io/vpVideoWriter.h>
7 #include <visp3/sensor/vpV4l2Grabber.h>
8 
9 #if defined(HAVE_OPENCV_VIDEOIO)
10 #include <opencv2/videoio.hpp>
11 #endif
12 
30 int main(int argc, const char *argv[])
31 {
32 #if (defined(VISP_HAVE_V4L2) || defined(HAVE_OPENCV_VIDEOIO)) && defined(VISP_HAVE_DISPLAY)
33 #ifdef ENABLE_VISP_NAMESPACE
34  using namespace VISP_NAMESPACE_NAME;
35 #endif
36  std::string opt_videoname = "video-recorded.mpg";
37  int opt_device = 0;
38 
39  for (int i = 0; i < argc; i++) {
40  if (std::string(argv[i]) == "--device")
41  opt_device = atoi(argv[i + 1]);
42  else if (std::string(argv[i]) == "--recorded-name")
43  opt_videoname = std::string(argv[i + 1]);
44  else if (std::string(argv[i]) == "--help" || std::string(argv[i]) == "-h") {
45  std::cout << "\nUsage: " << argv[0]
46  << " [--device <device number>] [--recorded-name <video name>] [--help][-h]\n"
47  << std::endl;
48  return EXIT_SUCCESS;
49  }
50  }
51 
52  std::cout << "Use device: " << opt_device << std::endl;
53  std::cout << "Record video in: " << opt_videoname << std::endl;
54 
55  try {
56  // vpImage<vpRGBa> I; // for color images
57  vpImage<unsigned char> I; // for gray images
58 
59 #if defined(VISP_HAVE_V4L2)
60  std::cout << "Use v4l2 grabber..." << std::endl;
61  vpV4l2Grabber g;
62  std::ostringstream device;
63  device << "/dev/video" << opt_device;
64  g.setDevice(device.str());
65  g.setScale(1); // Acquire full resolution images
66  g.open(I);
67  g.acquire(I);
68 #elif defined(HAVE_OPENCV_VIDEOIO)
69  std::cout << "Use OpenCV grabber..." << std::endl;
70  cv::VideoCapture g(opt_device);
71  if (!g.isOpened()) { // check if we succeeded
72  std::cout << "Failed to open the camera" << std::endl;
73  return EXIT_FAILURE;
74  }
75  cv::Mat frame;
76  g >> frame; // get a new frame from camera
77  vpImageConvert::convert(frame, I);
78 #endif
79 
80  std::cout << "Image size: " << I.getWidth() << " " << I.getHeight() << std::endl;
81 
82 #if defined(VISP_HAVE_DISPLAY)
84  d->init(I, 0, 0, "Camera view");
85 #endif
86  vpVideoWriter writer;
87 
88 #if defined(HAVE_OPENCV_VIDEOIO)
89 #if (VISP_HAVE_OPENCV_VERSION >= 0x030000)
90  writer.setCodec(cv::VideoWriter::fourcc('P', 'I', 'M', '1')); // MPEG-1 codec
91 #else
92  writer.setCodec(CV_FOURCC('P', 'I', 'M', '1'));
93 #endif
94 #endif
95  writer.setFileName(opt_videoname);
96  writer.open(I);
97  bool recording = false;
98 
99  for (;;) {
100 #if defined(VISP_HAVE_V4L2)
101  g.acquire(I);
102 #elif defined(HAVE_OPENCV_VIDEOIO)
103  g >> frame;
104  vpImageConvert::convert(frame, I);
105 #endif
107  if (recording == false) {
108  vpDisplay::displayText(I, 10, 10, "A click to start recording", vpColor::green);
109  if (vpDisplay::getClick(I, false))
110  recording = true;
111  }
112  else {
113  writer.saveFrame(I);
114  vpDisplay::displayText(I, 10, 10, "Recording: A click to stop and exit", vpColor::red);
115  if (vpDisplay::getClick(I, false))
116  break;
117  }
118 
119  vpDisplay::flush(I);
120  }
121  std::cout << "The video was recorded in \"" << opt_videoname << "\"" << std::endl;
122 #ifdef VISP_HAVE_DISPLAY
123  delete d;
124 #endif
125  }
126  catch (const vpException &e) {
127  std::cout << "Catch an exception: " << e << std::endl;
128  }
129 #else
130  (void)argc;
131  (void)argv;
132  std::cout << "Install OpenCV and rebuild ViSP to use this example." << std::endl;
133 #endif
134  return EXIT_SUCCESS;
135 }
static const vpColor red
Definition: vpColor.h:217
static const vpColor green
Definition: vpColor.h:220
void init(vpImage< unsigned char > &I, int winx=-1, int winy=-1, const std::string &title="") VP_OVERRIDE
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 void convert(const vpImage< unsigned char > &src, vpImage< vpRGBa > &dest)
unsigned int getWidth() const
Definition: vpImage.h:242
unsigned int getHeight() const
Definition: vpImage.h:181
Class that is a wrapper over the Video4Linux2 (V4L2) driver.
void open(vpImage< unsigned char > &I)
void setScale(unsigned scale=vpV4l2Grabber::DEFAULT_SCALE)
void setDevice(const std::string &devname)
void acquire(vpImage< unsigned char > &I)
Class that enables to write easily a video file or a sequence of images.
void saveFrame(vpImage< vpRGBa > &I)
void setFileName(const std::string &filename)
void open(vpImage< vpRGBa > &I)
void setCodec(const int fourcc_codec)
vpDisplay * allocateDisplay()
Return a newly allocated vpDisplay specialization if a GUI library is available or nullptr otherwise.