tools/tcpRequest/main.cpp
Go to the documentation of this file.
1 /*
2 Copyright (c) 2011-2014, Mathieu Labbe - IntRoLab - Universite de Sherbrooke
3 All rights reserved.
4 
5 Redistribution and use in source and binary forms, with or without
6 modification, are permitted provided that the following conditions are met:
7  * Redistributions of source code must retain the above copyright
8  notice, this list of conditions and the following disclaimer.
9  * Redistributions in binary form must reproduce the above copyright
10  notice, this list of conditions and the following disclaimer in the
11  documentation and/or other materials provided with the distribution.
12  * Neither the name of the Universite de Sherbrooke nor the
13  names of its contributors may be used to endorse or promote products
14  derived from this software without specific prior written permission.
15 
16 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
17 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY
20 DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
23 ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 */
27 
28 #include <QtNetwork/QNetworkInterface>
29 #include <QtCore/QCoreApplication>
30 #include <QtCore/QFile>
31 #include <QtCore/QTime>
32 #include <opencv2/opencv.hpp>
33 #include <find_object/TcpServer.h>
34 #include "TcpResponse.h"
35 #include "find_object/JsonWriter.h"
36 
37 void showUsage()
38 {
39  printf("\ntcpRequest [options] --scene image.png --out # --in #\n"
40  "\ntcpRequest [options] --scene image.png --port #\n"
41  " \"out\" is the port to which the image is sent.\n"
42  " \"in\" is the port from which the detection is received.\n"
43  " \"port\" is the bidirectional port from which the image is sent AND the detection is received.\n"
44  " Options:\n"
45  " --host #.#.#.# Set host address.\n"
46  " --json \"path\" Path to an output JSON file.\n"
47  " --help Show this help.\n");
48  exit(-1);
49 }
50 
51 int main(int argc, char * argv[])
52 {
53  QString ipAddress;
54  QString scenePath;
55  QString jsonPath;
56  quint16 portOut = 0;
57  quint16 portIn = 0;
58  quint16 bidrectionalPort = 0;
59 
60  for(int i=1; i<argc; ++i)
61  {
62  if(strcmp(argv[i], "--host") == 0 || strcmp(argv[i], "-host") == 0)
63  {
64  ++i;
65  if(i < argc)
66  {
67  ipAddress = argv[i];
68  }
69  else
70  {
71  printf("error parsing --host\n");
72  showUsage();
73  }
74  continue;
75  }
76  if(strcmp(argv[i], "--scene") == 0 || strcmp(argv[i], "-scene") == 0)
77  {
78  ++i;
79  if(i < argc)
80  {
81  scenePath = argv[i];
82  }
83  else
84  {
85  printf("error parsing --scene\n");
86  showUsage();
87  }
88  continue;
89  }
90  if(strcmp(argv[i], "--out") == 0 || strcmp(argv[i], "-out") == 0)
91  {
92  ++i;
93  if(i < argc)
94  {
95  portOut = std::atoi(argv[i]);
96  }
97  else
98  {
99  printf("error parsing --out\n");
100  showUsage();
101  }
102  continue;
103  }
104  if(strcmp(argv[i], "--in") == 0 || strcmp(argv[i], "-in") == 0)
105  {
106  ++i;
107  if(i < argc)
108  {
109  portIn = std::atoi(argv[i]);
110  }
111  else
112  {
113  printf("error parsing --in\n");
114  showUsage();
115  }
116  continue;
117  }
118  if(strcmp(argv[i], "--port") == 0 || strcmp(argv[i], "-port") == 0)
119  {
120  ++i;
121  if(i < argc)
122  {
123  bidrectionalPort = std::atoi(argv[i]);
124  }
125  else
126  {
127  printf("error parsing --port\n");
128  showUsage();
129  }
130  continue;
131  }
132 
133  if(strcmp(argv[i], "--json") == 0 || strcmp(argv[i], "-json") == 0)
134  {
135  ++i;
136  if(i < argc)
137  {
138  jsonPath = argv[i];
139  }
140  else
141  {
142  printf("error parsing --json\n");
143  showUsage();
144  }
145  continue;
146  }
147 
148  if(strcmp(argv[i], "-help") == 0 ||
149  strcmp(argv[i], "--help") == 0)
150  {
151  showUsage();
152  }
153 
154  printf("Unrecognized option: %s\n", argv[i]);
155  showUsage();
156  }
157 
158  if(bidrectionalPort == 0 && portOut == 0 && portIn == 0)
159  {
160  printf("Arguments --port or [--in and --out] should be set.\n");
161  showUsage();
162  }
163  else if(portOut == 0 && bidrectionalPort == 0)
164  {
165  printf("Argument --out should be set.\n");
166  showUsage();
167  }
168  else if(portIn == 0 && bidrectionalPort == 0)
169  {
170  printf("Argument --in should be set.\n");
171  showUsage();
172  }
173  else if(scenePath.isEmpty())
174  {
175  printf("Argument --scene should be set.\n");
176  showUsage();
177  }
178 
179  if(ipAddress.isEmpty())
180  {
181  ipAddress = QHostAddress(QHostAddress::LocalHost).toString();
182  }
183 
184  cv::Mat image = cv::imread(scenePath.toStdString());
185  if(image.empty())
186  {
187  printf("Cannot read image from \"%s\".\n", scenePath.toStdString().c_str());
188  showUsage();
189  }
190 
191  QCoreApplication app(argc, argv);
192  QTcpSocket request;
194 
195  QObject::connect(&response, SIGNAL(detectionReceived()), &app, SLOT(quit()));
196  QObject::connect(&response, SIGNAL(disconnected()), &app, SLOT(quit()));
197  QObject::connect(&response, SIGNAL(error(QAbstractSocket::SocketError)), &app, SLOT(quit()));
198 
199  QTcpSocket * requestPtr = &request;
200  if(bidrectionalPort == 0)
201  {
202  QObject::connect(&request, SIGNAL(disconnected()), &app, SLOT(quit()));
203  QObject::connect(&request, SIGNAL(error(QAbstractSocket::SocketError)), &app, SLOT(quit()));
204 
205  request.connectToHost(ipAddress, portOut);
206  response.connectToHost(ipAddress, portIn);
207 
208  if(!request.waitForConnected())
209  {
210  printf("ERROR: Unable to connect to %s:%d\n", ipAddress.toStdString().c_str(), portOut);
211  return -1;
212  }
213  }
214  else
215  {
216  printf("Using bidirectional port\n");
217  requestPtr = &response;
218  response.connectToHost(ipAddress, bidrectionalPort);
219  }
220 
221 
222  if(!response.waitForConnected())
223  {
224  printf("ERROR: Unable to connect to %s:%d\n", ipAddress.toStdString().c_str(), bidrectionalPort?bidrectionalPort:portIn);
225  return -1;
226  }
227 
228  // publish image
229  std::vector<unsigned char> buf;
230  cv::imencode(".png", image, buf);
231 
232  QByteArray block;
233  QDataStream out(&block, QIODevice::WriteOnly);
234  out.setVersion(QDataStream::Qt_4_0);
235  out << (quint64)0;
236  if(bidrectionalPort)
237  {
239  }
240  out.writeRawData((char*)buf.data(), (int)buf.size());
241  out.device()->seek(0);
242  out << (quint64)(block.size() - sizeof(quint64));
243 
244  qint64 bytes = requestPtr->write(block);
245  printf("Image published (%d bytes), waiting for response...\n", (int)bytes);
246 
247  QTime time;
248  time.start();
249 
250  // wait for response
251  if(bidrectionalPort)
252  {
253  requestPtr->waitForBytesWritten();
254  response.waitForReadyRead();
255  }
256  else
257  {
258  app.exec();
259  }
260 
261  if(response.dataReceived())
262  {
263  printf("Response received! (%d ms)\n", time.elapsed());
264  // print detected objects
265  if(response.info().objDetected_.size())
266  {
267  QList<int> ids = response.info().objDetected_.uniqueKeys();
268  for(int i=0; i<ids.size(); ++i)
269  {
270  int count = response.info().objDetected_.count(ids[i]);
271  if(count == 1)
272  {
273  printf("Object %d detected.\n", ids[i]);
274  }
275  else
276  {
277  printf("Object %d detected %d times.\n", ids[i], count);
278  }
279  }
280  }
281  else
282  {
283  printf("No objects detected.\n");
284  }
285  // write json
286  if(!jsonPath.isEmpty())
287  {
288  find_object::JsonWriter::write(response.info(), jsonPath);
289  printf("JSON written to \"%s\"\n", jsonPath.toStdString().c_str());
290  }
291  }
292  else
293  {
294  printf("Failed to receive a response...\n");
295  return -1;
296  }
297 
298  return 0;
299 }
300 
app
void showUsage()
QMultiMap< int, QTransform > objDetected_
Definition: DetectionInfo.h:71
int main(int argc, char *argv[])
const find_object::DetectionInfo & info() const
Definition: TcpResponse.h:43
bool dataReceived() const
Definition: TcpResponse.h:44
static void write(const DetectionInfo &info, const QString &path)
Definition: JsonWriter.cpp:39
const std::string response


find_object_2d
Author(s): Mathieu Labbe
autogenerated on Thu Jun 6 2019 19:22:26