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