1 /*! \example tutorial-video-recorder.cpp */
2 #include <visp3/core/vpTime.h>
3 #include <visp3/gui/vpDisplayGDI.h>
4 #include <visp3/gui/vpDisplayGTK.h>
5 #include <visp3/gui/vpDisplayOpenCV.h>
6 #include <visp3/gui/vpDisplayX.h>
7 #include <visp3/io/vpVideoWriter.h>
8 #include <visp3/sensor/vpV4l2Grabber.h>
9
10 /*!
11 This example allows to record a video from a camera.
12 It only requires that ViSP is build with OpenCV.
13
14 Example to save an mpeg video:
15
16 ./tutorial-video-recorder --device 0 --name myvideo.mp4
17
18 Example to save a sequence of png images:
19
20 ./tutorial-video-recorder --device 0 --name image%04d.png
21
22 Example to save one imags:
23
24 ./tutorial-video-recorder --device 0 --name image.jpeg
25
26 */
main(int argc,const char * argv[])27 int main(int argc, const char *argv[])
28 {
29 #if ((defined(VISP_HAVE_V4L2) || (VISP_HAVE_OPENCV_VERSION >= 0x020100)) && \
30 (defined(VISP_HAVE_X11) || defined(VISP_HAVE_GDI) || defined(VISP_HAVE_OPENCV) || defined(VISP_HAVE_GTK)))
31 std::string opt_videoname = "video-recorded.mpg";
32 int opt_device = 0;
33
34 for (int i = 0; i < argc; i++) {
35 if (std::string(argv[i]) == "--device")
36 opt_device = atoi(argv[i + 1]);
37 else if (std::string(argv[i]) == "--name")
38 opt_videoname = std::string(argv[i + 1]);
39 else if (std::string(argv[i]) == "--help" || std::string(argv[i]) == "-h") {
40 std::cout << "\nUsage: " << argv[0]
41 << " [--device <device number>] [--name <video name>] [--help][-h]\n"
42 << std::endl;
43 return 0;
44 }
45 }
46
47 std::cout << "Use device: " << opt_device << std::endl;
48 std::cout << "Record video in: " << opt_videoname << std::endl;
49
50 try {
51 // vpImage<vpRGBa> I; // for color images
52 vpImage<unsigned char> I; // for gray images
53
54 #if defined(VISP_HAVE_V4L2)
55 vpV4l2Grabber g;
56 std::ostringstream device;
57 device << "/dev/video" << opt_device;
58 g.setDevice(device.str());
59 g.setScale(1); // Acquire full resolution images
60 g.open(I);
61 g.acquire(I);
62 #elif defined(VISP_HAVE_OPENCV)
63 cv::VideoCapture g(opt_device);
64 if (!g.isOpened()) { // check if we succeeded
65 std::cout << "Failed to open the camera" << std::endl;
66 return -1;
67 }
68 cv::Mat frame;
69 g >> frame; // get a new frame from camera
70 vpImageConvert::convert(frame, I);
71 #endif
72
73 std::cout << "Image size: " << I.getWidth() << " " << I.getHeight() << std::endl;
74
75 #if defined(VISP_HAVE_X11)
76 vpDisplayX d;
77 #elif defined(VISP_HAVE_GDI)
78 vpDisplayGDI d;
79 #elif defined(VISP_HAVE_OPENCV)
80 vpDisplayOpenCV d;
81 #elif defined(VISP_HAVE_GTK)
82 vpDisplayGTK d;
83 #endif
84 d.init(I, 0, 0, "Camera view");
85 vpVideoWriter writer;
86
87 #if VISP_HAVE_OPENCV_VERSION >= 0x030000
88 writer.setCodec(cv::VideoWriter::fourcc('P', 'I', 'M', '1')); // MPEG-1 codec
89 #elif VISP_HAVE_OPENCV_VERSION >= 0x020100
90 writer.setCodec(CV_FOURCC('P', 'I', 'M', '1'));
91 #endif
92 writer.setFileName(opt_videoname);
93 writer.open(I);
94 bool recording = false;
95
96 for (;;) {
97 #if defined(VISP_HAVE_V4L2)
98 g.acquire(I);
99 #elif defined(VISP_HAVE_OPENCV)
100 g >> frame;
101 vpImageConvert::convert(frame, I);
102 #endif
103 vpDisplay::display(I);
104 if (recording == false) {
105 vpDisplay::displayText(I, 10, 10, "A click to start recording", vpColor::green);
106 if (vpDisplay::getClick(I, false))
107 recording = true;
108 } else {
109 writer.saveFrame(I);
110 vpDisplay::displayText(I, 10, 10, "Recording: A click to stop and exit", vpColor::red);
111 if (vpDisplay::getClick(I, false))
112 break;
113 }
114
115 vpDisplay::flush(I);
116 }
117 std::cout << "The video was recorded in \"" << opt_videoname << "\"" << std::endl;
118 } catch (const vpException &e) {
119 std::cout << "Catch an exception: " << e << std::endl;
120 }
121 #else
122 (void)argc;
123 (void)argv;
124 std::cout << "Install OpenCV and rebuild ViSP to use this example." << std::endl;
125 #endif
126 }
127