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