Visual Servoing Platform version 3.7.0
Loading...
Searching...
No Matches
tutorial-grabber-bebop2.cpp
1
2#include <visp3/core/vpConfig.h>
3#include <visp3/core/vpImage.h>
4#include <visp3/gui/vpDisplayFactory.h>
5#include <visp3/io/vpImageStorageWorker.h>
6
7#ifdef VISP_HAVE_MODULE_ROBOT
8#include <visp3/robot/vpRobotBebop2.h>
9
10void usage(const char *argv[], int error);
11
12void usage(const char *argv[], int error)
13{
14 std::cout << "SYNOPSIS" << std::endl
15 << " " << argv[0] << " [--ip <address>]"
16 << " [--hd-resolution]"
17 << " [--seqname <sequence name>]"
18 << " [--record <mode>]"
19 << " [--no-display]"
20 << " [--help] [-h]" << std::endl
21 << std::endl;
22 std::cout << "DESCRIPTION" << std::endl
23 << " --ip <address>" << std::endl
24 << " IP address of the drone to which you want to connect." << std::endl
25 << " Default: 192.168.42.1" << std::endl
26 << std::endl
27 << " --hd-resolution" << std::endl
28 << " Enables HD 720p images instead of default 480p." << std::endl
29 << " Caution : The camera settings are different depending on whether the resolution is 720p or 480p."
30 << std::endl
31 << std::endl
32 << " --seqname <sequence name>" << std::endl
33 << " Name of the sequence of image to create (ie: /tmp/image%04d.jpg)." << std::endl
34 << " Default: empty." << std::endl
35 << std::endl
36 << " --record <mode>" << std::endl
37 << " Allowed values for mode are:" << std::endl
38 << " 0: record all the captures images (continuous mode)," << std::endl
39 << " 1: record only images selected by a user click (single shot mode)." << std::endl
40 << " Default mode: 0" << std::endl
41 << std::endl
42 << " --no-display" << std::endl
43 << " Disable displaying captured images." << std::endl
44 << " When used and sequence name specified, record mode is internally set to 1 (continuous mode)."
45 << std::endl
46 << std::endl
47 << " --help, -h" << std::endl
48 << " Print this helper message." << std::endl
49 << std::endl;
50 std::cout << "USAGE" << std::endl
51 << " Example to visualize images:" << std::endl
52 << " " << argv[0] << std::endl
53 << std::endl
54 << " Examples to record a sequence of 720p images from drone with ip different from default:" << std::endl
55 << " " << argv[0] << " --seqname I%04d.png --ip 192.168.42.3 --hd_resolution" << std::endl
56 << " " << argv[0] << " --seqname folder/I%04d.png --record 0 --ip 192.168.42.3 --hd-resolution"
57 << std::endl
58 << std::endl
59 << " Examples to record single shot images:" << std::endl
60 << " " << argv[0] << " --seqname I%04d.png --record 1" << std::endl
61 << " " << argv[0] << " --seqname folder/I%04d.png --record 1" << std::endl
62 << std::endl;
63
64 if (error) {
65 std::cout << "Error" << std::endl
66 << " "
67 << "Unsupported parameter " << argv[error] << std::endl;
68 }
69}
70
74int main(int argc, const char **argv)
75{
76#if defined(VISP_HAVE_ARSDK) && defined(VISP_HAVE_FFMPEG) && defined(VISP_HAVE_THREADS)
77#ifdef ENABLE_VISP_NAMESPACE
78 using namespace VISP_NAMESPACE_NAME;
79#endif
80#if (VISP_CXX_STANDARD >= VISP_CXX_STANDARD_11)
81 std::shared_ptr<vpDisplay> display;
82#else
83 vpDisplay *display = nullptr;
84#endif
85 try {
86 std::string opt_seqname;
87 int opt_record_mode = 0;
88 int image_res = 0;
89 std::string ip_address = "192.168.42.1";
90 bool opt_display = true;
91
92 for (int i = 1; i < argc; i++) {
93 if (std::string(argv[i]) == "--seqname" && i + 1 < argc) {
94 opt_seqname = std::string(argv[++i]);
95 i++;
96 }
97 else if (std::string(argv[i]) == "--record" && i + 1 < argc) {
98 opt_record_mode = std::atoi(argv[++i]);
99 i++;
100 }
101 else if (std::string(argv[i]) == "--ip" && i + 1 < argc) {
102 ip_address = std::string(argv[++i]);
103 i++;
104 }
105 else if (std::string(argv[i]) == "--hd-resolution") {
106 image_res = 1;
107 }
108 else if (std::string(argv[i]) == "--no-display") {
109 opt_display = false;
110 }
111 else if (std::string(argv[i]) == "--help" || std::string(argv[i]) == "-h") {
112 usage(argv, 0);
113 return EXIT_SUCCESS;
114 }
115 else {
116 usage(argv, i);
117 return EXIT_FAILURE;
118 }
119 }
120
121 if ((!opt_display) && (!opt_seqname.empty())) {
122 opt_record_mode = 0;
123 }
124
125 std::cout << "Recording : " << (opt_seqname.empty() ? "disabled" : "enabled") << std::endl;
126 std::cout << "Display : " << (opt_display ? "enabled" : "disabled") << std::endl;
127
128 std::string text_record_mode =
129 std::string("Record mode: ") + (opt_record_mode ? std::string("single") : std::string("continuous"));
130
131 if (!opt_seqname.empty()) {
132 std::cout << text_record_mode << std::endl;
133 std::cout << "Record name: " << opt_seqname << std::endl;
134 }
135 std::cout << "Image resolution : " << (image_res == 0 ? "480p." : "720p.") << std::endl << std::endl;
136
137 vpImage<vpRGBa> I(1u, 1u, vpRGBa(0));
138
139 vpRobotBebop2 drone(false, true, ip_address);
140
141 if (drone.isRunning()) {
142
143 drone.setVideoResolution(image_res);
144
145 drone.startStreaming();
146 drone.setExposure(1.5f);
147 drone.getRGBaImage(I);
148 }
149 else {
150 std::cout << "Error : failed to setup drone control" << std::endl;
151 return EXIT_FAILURE;
152 }
153
154 std::cout << "Image size : " << I.getWidth() << " " << I.getHeight() << std::endl;
155
156 if (opt_display) {
157#if !(defined(VISP_HAVE_DISPLAY))
158 std::cout << "No image viewer is available..." << std::endl;
159 opt_display = false;
160#endif
161 }
162 if (opt_display) {
163#if (VISP_CXX_STANDARD >= VISP_CXX_STANDARD_11)
165#else
167#endif
168 }
169
170 vpImageQueue<vpRGBa> image_queue(opt_seqname, opt_record_mode);
171 vpImageStorageWorker<vpRGBa> image_storage_worker(std::ref(image_queue));
172 std::thread image_storage_thread(&vpImageStorageWorker<vpRGBa>::run, &image_storage_worker);
173
174 bool quit = false;
175 while (!quit) {
176 double t = vpTime::measureTimeMs();
177 drone.getRGBaImage(I);
178
180
181 quit = image_queue.record(I);
182
183 std::stringstream ss;
184 ss << "Acquisition time: " << std::setprecision(3) << vpTime::measureTimeMs() - t << " ms";
185 vpDisplay::displayText(I, static_cast<int>(I.getHeight()) - 20, 10, ss.str(), vpColor::red);
187 }
188 image_queue.cancel();
189 image_storage_thread.join();
190 }
191 catch (const vpException &e) {
192 std::cout << "Caught an exception: " << e << std::endl;
193 }
194#if (VISP_CXX_STANDARD < VISP_CXX_STANDARD_11)
195 if (display != nullptr) {
196 delete display;
197 }
198#endif
199#else
200 (void)argc;
201 (void)argv;
202#ifndef VISP_HAVE_ARSDK
203 std::cout << "Install Parrot ARSDK3, configure and build ViSP again to use this example" << std::endl;
204#endif
205#ifndef VISP_HAVE_FFMPEG
206 std::cout << "Install ffmpeg, configure and build ViSP again to use this example" << std::endl;
207#endif
208#if (VISP_CXX_STANDARD < VISP_CXX_STANDARD_11)
209 std::cout << "This tutorial should be built with c++11 support" << std::endl;
210#endif
211#endif
212}
213#else
214int main() { std::cout << "This tutorial needs visp_robot module that is not built." << std::endl; }
215#endif
static const vpColor red
Definition vpColor.h:198
Class that defines generic functionalities for display.
Definition vpDisplay.h:171
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
Definition of the vpImage class member functions.
Definition vpImage.h:131
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.
VISP_EXPORT double measureTimeMs()