Visual Servoing Platform  version 3.6.1 under development (2024-12-17)
tutorial-grabber-bebop2.cpp
1 
2 #include <visp3/core/vpConfig.h>
3 #include <visp3/core/vpImage.h>
4 #include <visp3/gui/vpDisplayGDI.h>
5 #include <visp3/gui/vpDisplayOpenCV.h>
6 #include <visp3/gui/vpDisplayX.h>
7 #include <visp3/io/vpImageStorageWorker.h>
8 
9 #ifdef VISP_HAVE_MODULE_ROBOT
10 #include <visp3/robot/vpRobotBebop2.h>
11 
12 void 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 
74 int 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  try {
81  std::string opt_seqname;
82  int opt_record_mode = 0;
83  int image_res = 0;
84  std::string ip_address = "192.168.42.1";
85  bool opt_display = true;
86 
87  for (int i = 1; i < argc; i++) {
88  if (std::string(argv[i]) == "--seqname") {
89  opt_seqname = std::string(argv[i + 1]);
90  i++;
91  }
92  else if (std::string(argv[i]) == "--record") {
93  opt_record_mode = std::atoi(argv[i + 1]);
94  i++;
95  }
96  else if (std::string(argv[i]) == "--ip" && i + 1 < argc) {
97  ip_address = std::string(argv[i + 1]);
98  i++;
99  }
100  else if (std::string(argv[i]) == "--hd-resolution") {
101  image_res = 1;
102  }
103  else if (std::string(argv[i]) == "--no-display") {
104  opt_display = false;
105  }
106  else if (std::string(argv[i]) == "--help" || std::string(argv[i]) == "-h") {
107  usage(argv, 0);
108  return EXIT_SUCCESS;
109  }
110  else {
111  usage(argv, i);
112  return EXIT_FAILURE;
113  }
114  }
115 
116  if ((!opt_display) && (!opt_seqname.empty())) {
117  opt_record_mode = 0;
118  }
119 
120  std::cout << "Recording : " << (opt_seqname.empty() ? "disabled" : "enabled") << std::endl;
121  std::cout << "Display : " << (opt_display ? "enabled" : "disabled") << std::endl;
122 
123  std::string text_record_mode =
124  std::string("Record mode: ") + (opt_record_mode ? std::string("single") : std::string("continuous"));
125 
126  if (!opt_seqname.empty()) {
127  std::cout << text_record_mode << std::endl;
128  std::cout << "Record name: " << opt_seqname << std::endl;
129  }
130  std::cout << "Image resolution : " << (image_res == 0 ? "480p." : "720p.") << std::endl << std::endl;
131 
132  vpImage<vpRGBa> I(1, 1, 0);
133 
134  vpRobotBebop2 drone(false, true, ip_address);
135 
136  if (drone.isRunning()) {
137 
138  drone.setVideoResolution(image_res);
139 
140  drone.startStreaming();
141  drone.setExposure(1.5f);
142  drone.getRGBaImage(I);
143  }
144  else {
145  std::cout << "Error : failed to setup drone control" << std::endl;
146  return EXIT_FAILURE;
147  }
148 
149  std::cout << "Image size : " << I.getWidth() << " " << I.getHeight() << std::endl;
150 
151  vpDisplay *d = nullptr;
152  if (opt_display) {
153 #if !(defined(VISP_HAVE_X11) || defined(VISP_HAVE_GDI) || defined(VISP_HAVE_OPENCV))
154  std::cout << "No image viewer is available..." << std::endl;
155  opt_display = false;
156 #endif
157  }
158  if (opt_display) {
159 #ifdef VISP_HAVE_X11
160  d = new vpDisplayX(I);
161 #elif defined(VISP_HAVE_GDI)
162  d = new vpDisplayGDI(I);
163 #elif defined(HAVE_OPENCV_HIGHGUI)
164  d = new vpDisplayOpenCV(I);
165 #endif
166  }
167 
168  vpImageQueue<vpRGBa> image_queue(opt_seqname, opt_record_mode);
169  vpImageStorageWorker<vpRGBa> image_storage_worker(std::ref(image_queue));
170  std::thread image_storage_thread(&vpImageStorageWorker<vpRGBa>::run, &image_storage_worker);
171 
172  bool quit = false;
173  while (!quit) {
174  double t = vpTime::measureTimeMs();
175  drone.getRGBaImage(I);
176 
178 
179  quit = image_queue.record(I);
180 
181  std::stringstream ss;
182  ss << "Acquisition time: " << std::setprecision(3) << vpTime::measureTimeMs() - t << " ms";
183  vpDisplay::displayText(I, static_cast<int>(I.getHeight()) - 20, 10, ss.str(), vpColor::red);
184  vpDisplay::flush(I);
185  }
186  image_queue.cancel();
187  image_storage_thread.join();
188 
189  if (d) {
190  delete d;
191  }
192  }
193  catch (const vpException &e) {
194  std::cout << "Caught an exception: " << e << std::endl;
195  }
196 #else
197  (void)argc;
198  (void)argv;
199 #ifndef VISP_HAVE_ARSDK
200  std::cout << "Install Parrot ARSDK3, configure and build ViSP again to use this example" << std::endl;
201 #endif
202 #ifndef VISP_HAVE_FFMPEG
203  std::cout << "Install ffmpeg, configure and build ViSP again to use this example" << std::endl;
204 #endif
205 #if (VISP_CXX_STANDARD < VISP_CXX_STANDARD_11)
206  std::cout << "This tutorial should be built with c++11 support" << std::endl;
207 #endif
208 #endif
209 }
210 #else
211 int main() { std::cout << "This tutorial needs visp_robot module that is not built." << std::endl; }
212 #endif
static const vpColor red
Definition: vpColor.h:217
Display for windows using GDI (available on any windows 32 platform).
Definition: vpDisplayGDI.h:130
The vpDisplayOpenCV allows to display image using the OpenCV library. Thus to enable this class OpenC...
Class that defines generic functionalities for display.
Definition: vpDisplay.h:178
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
unsigned int getWidth() const
Definition: vpImage.h:242
unsigned int getHeight() const
Definition: vpImage.h:181
VISP_EXPORT double measureTimeMs()