Visual Servoing Platform  version 3.3.1 under development (2020-12-16)
record_helper.cpp
1 #include <visp3/core/vpIoTools.h>
2 #include <visp3/core/vpDisplay.h>
3 #include <visp3/io/vpImageIo.h>
4 
5 #include "record_helper.h"
6 
15 bool record_helper(const std::string &opt_seqname, int opt_record_mode,
16  const vpImage<unsigned char> &I)
17 {
18  static bool start_record = false;
19  static std::string text_record_mode = std::string("Record mode: ") + (opt_record_mode ? std::string("single") : std::string("continuous"));
20 
21  if (! opt_seqname.empty()) {
22  if (! opt_record_mode) { // continuous
23  if (start_record) {
24  vpDisplay::displayText(I, 20, 10, "Left click: stop recording", vpColor::red);
25  }
26  else {
27  vpDisplay::displayText(I, 20, 10, "Left click: start recording", vpColor::red);
28  }
29  }
30  else {
31  vpDisplay::displayText(I, 20, 10, "Left click: record image", vpColor::red);
32  }
33  vpDisplay::displayText(I, 40, 10, "Right click: quit", vpColor::red);
34  }
35  else {
36  vpDisplay::displayText(I, 20, 10, "Click to quit", vpColor::red);
37  }
38 
39  if (! opt_seqname.empty()) {
40  vpDisplay::displayText(I, 60, 10, text_record_mode, vpColor::red);
41  }
43  if (vpDisplay::getClick(I, button, false)) {
44  if (! opt_seqname.empty()) { // Recording requested
45  if (button == vpMouseButton::button1) { // enable/disable recording
46  start_record = !start_record;
47  }
48  else if (button == vpMouseButton::button3) { // quit
49  return true;
50  }
51  }
52  else { // any button to quit
53  return true;
54  }
55  }
56  if (start_record) {
57  static unsigned int counter = 1;
58  char filename[FILENAME_MAX];
59  sprintf(filename, opt_seqname.c_str(), counter);
60  {
61  // check if parent folder exists. Create otherwise
62  static bool parent_exists = false;
63  if (! parent_exists) {
64  std::string parent = vpIoTools::getParent(filename);
65  if (! parent.empty()) {
66  if (! vpIoTools::checkDirectory(parent)) {
68  }
69  }
70  parent_exists = true;
71  }
72  }
73 
74  counter ++;
75  std::string text = std::string("Save: ") + std::string(filename);
76  vpDisplay::displayText(I, 80, 10, text, vpColor::red);
77  std::cout << text << std::endl;
78  vpImageIo::write(I, filename);
79  if (opt_record_mode == 1) { // single shot mode
80  start_record = false;
81  }
82  }
83 
84  return false;
85 }
86 
98 bool record_helper(const std::string &opt_seqname_1, const std::string &opt_seqname_2, const int opt_record_mode,
100 {
101  static bool start_record = false;
102  static std::string text_record_mode = std::string("Record mode: ") + (opt_record_mode ? std::string("single") : std::string("continuous"));
103 
104  if (! opt_seqname_1.empty() || ! opt_seqname_2.empty()) {
105  if (! opt_record_mode) { // continuous
106  if (start_record) {
107  vpDisplay::displayText(I1, 20, 10, "Left click: stop recording", vpColor::red);
108  vpDisplay::displayText(I2, 20, 10, "Left click: stop recording", vpColor::red);
109  }
110  else {
111  vpDisplay::displayText(I1, 20, 10, "Left click: start recording", vpColor::red);
112  vpDisplay::displayText(I2, 20, 10, "Left click: start recording", vpColor::red);
113  }
114  }
115  else {
116  vpDisplay::displayText(I1, 20, 10, "Left click: record image", vpColor::red);
117  vpDisplay::displayText(I2, 20, 10, "Left click: record image", vpColor::red);
118  }
119  vpDisplay::displayText(I1, 40, 10, "Right click: quit", vpColor::red);
120  vpDisplay::displayText(I2, 40, 10, "Right click: quit", vpColor::red);
121  }
122  else {
123  vpDisplay::displayText(I1, 20, 10, "Click to quit", vpColor::red);
124  vpDisplay::displayText(I2, 20, 10, "Click to quit", vpColor::red);
125  }
126 
127  if (! opt_seqname_1.empty() || ! opt_seqname_2.empty()) {
128  vpDisplay::displayText(I1, 60, 10, text_record_mode, vpColor::red);
129  vpDisplay::displayText(I2, 60, 10, text_record_mode, vpColor::red);
130  }
132  if (vpDisplay::getClick(I1, button, false) || vpDisplay::getClick(I2, button, false)) {
133  if (! opt_seqname_1.empty() || !opt_seqname_2.empty()) { // Recording requested
134  if (button == vpMouseButton::button1) { // enable/disable recording
135  start_record = !start_record;
136  }
137  else if (button == vpMouseButton::button3) { // quit
138  return true;
139  }
140  }
141  else { // any button to quit
142  return true;
143  }
144  }
145  if (start_record) {
146  static unsigned int counter = 1;
147  char filename_1[FILENAME_MAX];
148  char filename_2[FILENAME_MAX];
149  sprintf(filename_1, opt_seqname_1.c_str(), counter);
150  {
151  // check if parent folder exists. Create otherwise
152  static bool parent_exists = false;
153  if (! parent_exists) {
154  std::string parent = vpIoTools::getParent(filename_1);
155  if (! parent.empty()) {
156  if (! vpIoTools::checkDirectory(parent)) {
157  vpIoTools::makeDirectory(parent);
158  }
159  }
160  parent_exists = true;
161  }
162  }
163 
164  sprintf(filename_2, opt_seqname_2.c_str(), counter);
165  {
166  // check if parent folder exists. Create otherwise
167  static bool parent_exists = false;
168  if (! parent_exists) {
169  std::string parent = vpIoTools::getParent(filename_2);
170  if (! parent.empty()) {
171  if (! vpIoTools::checkDirectory(parent)) {
172  vpIoTools::makeDirectory(parent);
173  }
174  }
175  parent_exists = true;
176  }
177  }
178 
179  counter ++;
180  std::string text_1 = std::string("Save: ") + std::string(filename_1);
181  std::string text_2 = std::string("Save: ") + std::string(filename_2);
182  vpDisplay::displayText(I1, 80, 10, text_1, vpColor::red);
183  vpDisplay::displayText(I2, 80, 10, text_2, vpColor::red);
184  std::cout << text_1 << std::endl;
185  std::cout << text_2 << std::endl;
186  vpImageIo::write(I1, filename_1);
187  vpImageIo::write(I2, filename_2);
188  if (opt_record_mode == 1) { // single shot mode
189  start_record = false;
190  }
191  }
192 
193  return false;
194 }
static void makeDirectory(const std::string &dirname)
Definition: vpIoTools.cpp:482
static bool getClick(const vpImage< unsigned char > &I, bool blocking=true)
static void displayText(const vpImage< unsigned char > &I, const vpImagePoint &ip, const std::string &s, const vpColor &color)
static std::string getParent(const std::string &pathname)
Definition: vpIoTools.cpp:1382
static const vpColor red
Definition: vpColor.h:217
static bool checkDirectory(const std::string &dirname)
Definition: vpIoTools.cpp:332
static void write(const vpImage< unsigned char > &I, const std::string &filename)
Definition: vpImageIo.cpp:447