Visual Servoing Platform version 3.7.0
Loading...
Searching...
No Matches
tutorial-video-recorder.cpp
1
2#include <iostream>
3
4#include <visp3/core/vpConfig.h>
5
7// Comment / uncomment following lines to use the specific 3rd party compatible with your camera
8#undef VISP_HAVE_V4L2
9// #undef HAVE_OPENCV_HIGHGUI
10// #undef HAVE_OPENCV_VIDEOIO
12
13#if defined(VISP_HAVE_DISPLAY) && \
14 (defined(VISP_HAVE_V4L2) || defined(VISP_HAVE_OPENCV) && \
15 (((VISP_HAVE_OPENCV_VERSION < 0x030000) && defined(HAVE_OPENCV_HIGHGUI)) || ((VISP_HAVE_OPENCV_VERSION >= 0x030000) && defined(HAVE_OPENCV_VIDEOIO))))
16
17#include <visp3/core/vpTime.h>
18#include <visp3/gui/vpDisplayFactory.h>
19#include <visp3/io/vpVideoWriter.h>
20#include <visp3/sensor/vpV4l2Grabber.h>
21
22#if defined(VISP_HAVE_OPENCV) &&(VISP_HAVE_OPENCV_VERSION < 0x030000) && defined(HAVE_OPENCV_HIGHGUI)
23#include <opencv2/highgui/highgui.hpp> // for cv::VideoCapture
24#elif defined(VISP_HAVE_OPENCV) &&(VISP_HAVE_OPENCV_VERSION >= 0x030000) && defined(HAVE_OPENCV_VIDEOIO)
25#include <opencv2/videoio/videoio.hpp> // for cv::VideoCapture
26#endif
27
45int main(int argc, const char *argv[])
46{
47#ifdef ENABLE_VISP_NAMESPACE
48 using namespace VISP_NAMESPACE_NAME;
49#endif
50 std::string opt_videoname = "video-recorded.mpg";
51 int opt_device = 0;
52
53 for (int i = 1; i < argc; i++) {
54 if (std::string(argv[i]) == "--device" && i + 1 < argc) {
55 opt_device = atoi(argv[i + 1]);
56 }
57 else if (std::string(argv[i]) == "--recorded-name" && i + 1 < argc) {
58 opt_videoname = std::string(argv[i + 1]);
59 }
60 else if (std::string(argv[i]) == "--help" || std::string(argv[i]) == "-h") {
61 std::cout << "\nUsage: " << argv[0]
62 << " [--device <device number>] [--recorded-name <video name>] [--help][-h]\n"
63 << std::endl;
64 return EXIT_SUCCESS;
65 }
66 }
67
68 std::cout << "Use device: " << opt_device << std::endl;
69 std::cout << "Record video in: " << opt_videoname << std::endl;
70
71 try {
72 // vpImage<vpRGBa> I; // for color images
73 vpImage<unsigned char> I; // for gray images
74
75#if defined(VISP_HAVE_V4L2)
76 std::cout << "Use v4l2 grabber..." << std::endl;
78 std::ostringstream device;
79 device << "/dev/video" << opt_device;
80 g.setDevice(device.str());
81 g.setScale(1); // Acquire full resolution images
82 g.open(I);
83 g.acquire(I);
84#elif defined(VISP_HAVE_OPENCV) && \
85 (((VISP_HAVE_OPENCV_VERSION < 0x030000) && defined(HAVE_OPENCV_HIGHGUI)) || \
86 ((VISP_HAVE_OPENCV_VERSION >= 0x030000) && defined(HAVE_OPENCV_VIDEOIO)))
87 std::cout << "Use OpenCV grabber..." << std::endl;
88 cv::VideoCapture g(opt_device);
89 if (!g.isOpened()) { // check if we succeeded
90 std::cout << "Failed to open the camera" << std::endl;
91 return EXIT_FAILURE;
92 }
93 cv::Mat frame;
94 g >> frame; // get a new frame from camera
96#endif
97
98 std::cout << "Image size: " << I.getWidth() << " " << I.getHeight() << std::endl;
99
100#if defined(VISP_HAVE_DISPLAY)
101 vpDisplay *d = vpDisplayFactory::allocateDisplay(I, 0, 0, "Camera view");
102#endif
103 vpVideoWriter writer;
104
105#if defined(VISP_HAVE_OPENCV) && defined(HAVE_OPENCV_VIDEOIO)
106#if (VISP_HAVE_OPENCV_VERSION >= 0x030000)
107 writer.setCodec(cv::VideoWriter::fourcc('P', 'I', 'M', '1')); // MPEG-1 codec
108#else
109 writer.setCodec(CV_FOURCC('P', 'I', 'M', '1'));
110#endif
111#endif
112 writer.setFileName(opt_videoname);
113 writer.open(I);
114 bool recording = false;
115
116 for (;;) {
117#if defined(VISP_HAVE_V4L2)
118 g.acquire(I);
119#elif defined(VISP_HAVE_OPENCV) && \
120 (((VISP_HAVE_OPENCV_VERSION < 0x030000) && defined(HAVE_OPENCV_HIGHGUI)) || \
121 ((VISP_HAVE_OPENCV_VERSION >= 0x030000) && defined(HAVE_OPENCV_VIDEOIO)))
122 g >> frame;
123 vpImageConvert::convert(frame, I);
124#endif
126 if (recording == false) {
127 vpDisplay::displayText(I, 10, 10, "A click to start recording", vpColor::green);
128 if (vpDisplay::getClick(I, false))
129 recording = true;
130 }
131 else {
132 writer.saveFrame(I);
133 vpDisplay::displayText(I, 10, 10, "Recording: A click to stop and exit", vpColor::red);
134 if (vpDisplay::getClick(I, false))
135 break;
136 }
137
139 }
140 std::cout << "The video was recorded in \"" << opt_videoname << "\"" << std::endl;
141#ifdef VISP_HAVE_DISPLAY
142 delete d;
143#endif
144 }
145 catch (const vpException &e) {
146 std::cout << "Catch an exception: " << e << std::endl;
147 }
148 return EXIT_SUCCESS;
149}
150
151#else
152
153int main()
154{
155#if !defined(VISP_HAVE_DISPLAY)
156 std::cout << "Install a 3rdparty to enable display feature (X11, GDI...) and rebuild ViSP to use this tutorial." << std::endl;
157#endif
158#if !(defined(VISP_HAVE_V4L2) || defined(VISP_HAVE_OPENCV) && \
159 (((VISP_HAVE_OPENCV_VERSION < 0x030000) && defined(HAVE_OPENCV_HIGHGUI)) || \
160 ((VISP_HAVE_OPENCV_VERSION >= 0x030000) && defined(HAVE_OPENCV_VIDEOIO))))
161 std::cout << "Install V4l2 or OpenCV 3rdparty and rebuild ViSP to use this tutorial." << std::endl;
162#endif
163}
164
165#endif
static const vpColor red
Definition vpColor.h:198
static const vpColor green
Definition vpColor.h:201
Class that defines generic functionalities for display.
Definition vpDisplay.h:171
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)
Definition of the vpImage class member functions.
Definition vpImage.h:131
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.