28 #include <QtNetwork/QNetworkInterface>
29 #include <QtCore/QCoreApplication>
30 #include <QtCore/QFile>
31 #include <QtCore/QTime>
32 #include <opencv2/opencv.hpp>
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"
45 " --host #.#.#.# Set host address.\n"
46 " --json \"path\" Path to an output JSON file.\n"
47 " --help Show this help.\n");
51 int main(
int argc,
char * argv[])
58 quint16 bidrectionalPort = 0;
60 for(
int i=1; i<argc; ++i)
62 if(strcmp(argv[i],
"--host") == 0 || strcmp(argv[i],
"-host") == 0)
71 printf(
"error parsing --host\n");
76 if(strcmp(argv[i],
"--scene") == 0 || strcmp(argv[i],
"-scene") == 0)
85 printf(
"error parsing --scene\n");
90 if(strcmp(argv[i],
"--out") == 0 || strcmp(argv[i],
"-out") == 0)
95 portOut = std::atoi(argv[i]);
99 printf(
"error parsing --out\n");
104 if(strcmp(argv[i],
"--in") == 0 || strcmp(argv[i],
"-in") == 0)
109 portIn = std::atoi(argv[i]);
113 printf(
"error parsing --in\n");
118 if(strcmp(argv[i],
"--port") == 0 || strcmp(argv[i],
"-port") == 0)
123 bidrectionalPort = std::atoi(argv[i]);
127 printf(
"error parsing --port\n");
133 if(strcmp(argv[i],
"--json") == 0 || strcmp(argv[i],
"-json") == 0)
142 printf(
"error parsing --json\n");
148 if(strcmp(argv[i],
"-help") == 0 ||
149 strcmp(argv[i],
"--help") == 0)
154 printf(
"Unrecognized option: %s\n", argv[i]);
158 if(bidrectionalPort == 0 && portOut == 0 && portIn == 0)
160 printf(
"Arguments --port or [--in and --out] should be set.\n");
163 else if(portOut == 0 && bidrectionalPort == 0)
165 printf(
"Argument --out should be set.\n");
168 else if(portIn == 0 && bidrectionalPort == 0)
170 printf(
"Argument --in should be set.\n");
173 else if(scenePath.isEmpty())
175 printf(
"Argument --scene should be set.\n");
179 if(ipAddress.isEmpty())
181 ipAddress = QHostAddress(QHostAddress::LocalHost).toString();
184 cv::Mat image = cv::imread(scenePath.toStdString());
187 printf(
"Cannot read image from \"%s\".\n", scenePath.toStdString().c_str());
191 QCoreApplication
app(argc, argv);
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()));
199 QTcpSocket * requestPtr = &request;
200 if(bidrectionalPort == 0)
202 QObject::connect(&request, SIGNAL(disconnected()), &
app, SLOT(quit()));
203 QObject::connect(&request, SIGNAL(error(QAbstractSocket::SocketError)), &
app, SLOT(quit()));
205 request.connectToHost(ipAddress, portOut);
206 response.connectToHost(ipAddress, portIn);
208 if(!request.waitForConnected())
210 printf(
"ERROR: Unable to connect to %s:%d\n", ipAddress.toStdString().c_str(), portOut);
216 printf(
"Using bidirectional port\n");
218 response.connectToHost(ipAddress, bidrectionalPort);
224 printf(
"ERROR: Unable to connect to %s:%d\n", ipAddress.toStdString().c_str(), bidrectionalPort?bidrectionalPort:portIn);
229 std::vector<unsigned char> buf;
230 cv::imencode(
".png", image, buf);
233 QDataStream out(&block, QIODevice::WriteOnly);
234 out.setVersion(QDataStream::Qt_4_0);
240 out.writeRawData((
char*)buf.data(), (int)buf.size());
241 out.device()->seek(0);
242 out << (quint64)(block.size() -
sizeof(quint64));
244 qint64 bytes = requestPtr->write(block);
245 printf(
"Image published (%d bytes), waiting for response...\n", (
int)bytes);
253 requestPtr->waitForBytesWritten();
263 printf(
"Response received! (%d ms)\n", time.elapsed());
265 if(
response.info().objDetected_.size())
267 QList<int> ids =
response.info().objDetected_.uniqueKeys();
268 for(
int i=0; i<ids.size(); ++i)
270 int count =
response.info().objDetected_.count(ids[i]);
273 printf(
"Object %d detected.\n", ids[i]);
277 printf(
"Object %d detected %d times.\n", ids[i], count);
283 printf(
"No objects detected.\n");
286 if(!jsonPath.isEmpty())
289 printf(
"JSON written to \"%s\"\n", jsonPath.toStdString().c_str());
294 printf(
"Failed to receive a response...\n");