2014-08-02 06:10:48 +00:00
|
|
|
/*
|
2014-08-06 13:43:29 +00:00
|
|
|
Copyright (c) 2011-2014, Mathieu Labbe - IntRoLab - Universite de Sherbrooke
|
|
|
|
|
All rights reserved.
|
|
|
|
|
|
|
|
|
|
Redistribution and use in source and binary forms, with or without
|
|
|
|
|
modification, are permitted provided that the following conditions are met:
|
|
|
|
|
* Redistributions of source code must retain the above copyright
|
|
|
|
|
notice, this list of conditions and the following disclaimer.
|
|
|
|
|
* Redistributions in binary form must reproduce the above copyright
|
|
|
|
|
notice, this list of conditions and the following disclaimer in the
|
|
|
|
|
documentation and/or other materials provided with the distribution.
|
|
|
|
|
* Neither the name of the Universite de Sherbrooke nor the
|
|
|
|
|
names of its contributors may be used to endorse or promote products
|
|
|
|
|
derived from this software without specific prior written permission.
|
|
|
|
|
|
|
|
|
|
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
|
|
|
|
|
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
|
|
|
|
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
2014-08-11 15:49:53 +00:00
|
|
|
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY
|
2014-08-06 13:43:29 +00:00
|
|
|
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
|
|
|
|
|
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
|
|
|
|
|
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
|
|
|
|
|
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
|
|
|
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
|
|
|
|
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
|
*/
|
2014-08-02 06:10:48 +00:00
|
|
|
|
|
|
|
|
#include <QtNetwork/QNetworkInterface>
|
|
|
|
|
#include <QtCore/QCoreApplication>
|
|
|
|
|
#include <QtCore/QFile>
|
|
|
|
|
#include <QtCore/QTime>
|
|
|
|
|
#include <opencv2/opencv.hpp>
|
2015-11-30 00:27:57 -05:00
|
|
|
#include <find_object/TcpServer.h>
|
2014-08-02 06:10:48 +00:00
|
|
|
#include "TcpResponse.h"
|
2014-08-04 01:33:52 +00:00
|
|
|
#include "find_object/JsonWriter.h"
|
2014-08-02 06:10:48 +00:00
|
|
|
|
|
|
|
|
void showUsage()
|
|
|
|
|
{
|
|
|
|
|
printf("\ntcpRequest [options] --scene image.png --out # --in #\n"
|
2015-11-30 00:27:57 -05:00
|
|
|
"\ntcpRequest [options] --scene image.png --port #\n"
|
2014-08-02 06:10:48 +00:00
|
|
|
" \"out\" is the port to which the image is sent.\n"
|
|
|
|
|
" \"in\" is the port from which the detection is received.\n"
|
2015-11-30 00:27:57 -05:00
|
|
|
" \"port\" is the bidirectional port from which the image is sent AND the detection is received.\n"
|
2014-08-02 06:10:48 +00:00
|
|
|
" Options:\n"
|
2014-08-27 20:30:56 +00:00
|
|
|
" --host #.#.#.# Set host address.\n"
|
2014-08-28 17:54:29 +00:00
|
|
|
" --json \"path\" Path to an output JSON file.\n"
|
|
|
|
|
" --help Show this help.\n");
|
2014-08-04 01:33:52 +00:00
|
|
|
exit(-1);
|
2014-08-02 06:10:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int main(int argc, char * argv[])
|
|
|
|
|
{
|
|
|
|
|
QString ipAddress;
|
|
|
|
|
QString scenePath;
|
|
|
|
|
QString jsonPath;
|
|
|
|
|
quint16 portOut = 0;
|
|
|
|
|
quint16 portIn = 0;
|
2015-11-30 00:27:57 -05:00
|
|
|
quint16 bidrectionalPort = 0;
|
2014-08-02 06:10:48 +00:00
|
|
|
|
|
|
|
|
for(int i=1; i<argc; ++i)
|
|
|
|
|
{
|
|
|
|
|
if(strcmp(argv[i], "--host") == 0 || strcmp(argv[i], "-host") == 0)
|
|
|
|
|
{
|
|
|
|
|
++i;
|
|
|
|
|
if(i < argc)
|
|
|
|
|
{
|
|
|
|
|
ipAddress = argv[i];
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
printf("error parsing --host\n");
|
|
|
|
|
showUsage();
|
|
|
|
|
}
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
if(strcmp(argv[i], "--scene") == 0 || strcmp(argv[i], "-scene") == 0)
|
|
|
|
|
{
|
|
|
|
|
++i;
|
|
|
|
|
if(i < argc)
|
|
|
|
|
{
|
|
|
|
|
scenePath = argv[i];
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
printf("error parsing --scene\n");
|
|
|
|
|
showUsage();
|
|
|
|
|
}
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
if(strcmp(argv[i], "--out") == 0 || strcmp(argv[i], "-out") == 0)
|
|
|
|
|
{
|
|
|
|
|
++i;
|
|
|
|
|
if(i < argc)
|
|
|
|
|
{
|
|
|
|
|
portOut = std::atoi(argv[i]);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
printf("error parsing --out\n");
|
|
|
|
|
showUsage();
|
|
|
|
|
}
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
if(strcmp(argv[i], "--in") == 0 || strcmp(argv[i], "-in") == 0)
|
|
|
|
|
{
|
|
|
|
|
++i;
|
|
|
|
|
if(i < argc)
|
|
|
|
|
{
|
|
|
|
|
portIn = std::atoi(argv[i]);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
printf("error parsing --in\n");
|
|
|
|
|
showUsage();
|
|
|
|
|
}
|
|
|
|
|
continue;
|
|
|
|
|
}
|
2015-11-30 00:27:57 -05:00
|
|
|
if(strcmp(argv[i], "--port") == 0 || strcmp(argv[i], "-port") == 0)
|
|
|
|
|
{
|
|
|
|
|
++i;
|
|
|
|
|
if(i < argc)
|
|
|
|
|
{
|
|
|
|
|
bidrectionalPort = std::atoi(argv[i]);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
printf("error parsing --port\n");
|
|
|
|
|
showUsage();
|
|
|
|
|
}
|
|
|
|
|
continue;
|
|
|
|
|
}
|
2014-08-02 06:10:48 +00:00
|
|
|
|
2014-08-27 20:30:56 +00:00
|
|
|
if(strcmp(argv[i], "--json") == 0 || strcmp(argv[i], "-json") == 0)
|
2014-08-04 01:33:52 +00:00
|
|
|
{
|
2014-08-27 20:30:56 +00:00
|
|
|
++i;
|
|
|
|
|
if(i < argc)
|
2014-08-04 01:33:52 +00:00
|
|
|
{
|
2014-08-27 20:30:56 +00:00
|
|
|
jsonPath = argv[i];
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
printf("error parsing --json\n");
|
|
|
|
|
showUsage();
|
2014-08-04 01:33:52 +00:00
|
|
|
}
|
2014-08-27 20:30:56 +00:00
|
|
|
continue;
|
2014-08-04 01:33:52 +00:00
|
|
|
}
|
|
|
|
|
|
2014-08-28 17:54:29 +00:00
|
|
|
if(strcmp(argv[i], "-help") == 0 ||
|
|
|
|
|
strcmp(argv[i], "--help") == 0)
|
|
|
|
|
{
|
|
|
|
|
showUsage();
|
|
|
|
|
}
|
|
|
|
|
|
2014-08-02 06:10:48 +00:00
|
|
|
printf("Unrecognized option: %s\n", argv[i]);
|
|
|
|
|
showUsage();
|
|
|
|
|
}
|
|
|
|
|
|
2015-11-30 00:27:57 -05:00
|
|
|
if(bidrectionalPort == 0 && portOut == 0 && portIn == 0)
|
|
|
|
|
{
|
|
|
|
|
printf("Arguments --port or [--in and --out] should be set.\n");
|
|
|
|
|
showUsage();
|
|
|
|
|
}
|
|
|
|
|
else if(portOut == 0 && bidrectionalPort == 0)
|
2014-08-02 06:10:48 +00:00
|
|
|
{
|
|
|
|
|
printf("Argument --out should be set.\n");
|
|
|
|
|
showUsage();
|
|
|
|
|
}
|
2015-11-30 00:27:57 -05:00
|
|
|
else if(portIn == 0 && bidrectionalPort == 0)
|
2014-08-02 06:10:48 +00:00
|
|
|
{
|
|
|
|
|
printf("Argument --in should be set.\n");
|
2015-11-30 00:27:57 -05:00
|
|
|
showUsage();
|
2014-08-02 06:10:48 +00:00
|
|
|
}
|
|
|
|
|
else if(scenePath.isEmpty())
|
|
|
|
|
{
|
|
|
|
|
printf("Argument --scene should be set.\n");
|
2015-11-30 00:27:57 -05:00
|
|
|
showUsage();
|
2014-08-02 06:10:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if(ipAddress.isEmpty())
|
|
|
|
|
{
|
|
|
|
|
ipAddress = QHostAddress(QHostAddress::LocalHost).toString();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
cv::Mat image = cv::imread(scenePath.toStdString());
|
|
|
|
|
if(image.empty())
|
|
|
|
|
{
|
|
|
|
|
printf("Cannot read image from \"%s\".\n", scenePath.toStdString().c_str());
|
|
|
|
|
showUsage();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
QCoreApplication app(argc, argv);
|
|
|
|
|
QTcpSocket request;
|
|
|
|
|
TcpResponse response;
|
|
|
|
|
|
|
|
|
|
QObject::connect(&response, SIGNAL(detectionReceived()), &app, SLOT(quit()));
|
|
|
|
|
QObject::connect(&response, SIGNAL(disconnected()), &app, SLOT(quit()));
|
2014-08-06 03:00:17 +00:00
|
|
|
QObject::connect(&response, SIGNAL(error(QAbstractSocket::SocketError)), &app, SLOT(quit()));
|
2014-08-02 06:10:48 +00:00
|
|
|
|
2015-11-30 00:27:57 -05:00
|
|
|
QTcpSocket * requestPtr = &request;
|
|
|
|
|
if(bidrectionalPort == 0)
|
|
|
|
|
{
|
|
|
|
|
QObject::connect(&request, SIGNAL(disconnected()), &app, SLOT(quit()));
|
|
|
|
|
QObject::connect(&request, SIGNAL(error(QAbstractSocket::SocketError)), &app, SLOT(quit()));
|
|
|
|
|
|
|
|
|
|
request.connectToHost(ipAddress, portOut);
|
|
|
|
|
response.connectToHost(ipAddress, portIn);
|
2014-08-02 06:10:48 +00:00
|
|
|
|
2015-11-30 00:27:57 -05:00
|
|
|
if(!request.waitForConnected())
|
|
|
|
|
{
|
|
|
|
|
printf("ERROR: Unable to connect to %s:%d\n", ipAddress.toStdString().c_str(), portOut);
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else
|
2014-08-02 06:10:48 +00:00
|
|
|
{
|
2015-11-30 00:27:57 -05:00
|
|
|
printf("Using bidirectional port\n");
|
|
|
|
|
requestPtr = &response;
|
|
|
|
|
response.connectToHost(ipAddress, bidrectionalPort);
|
2014-08-02 06:10:48 +00:00
|
|
|
}
|
|
|
|
|
|
2015-11-30 00:27:57 -05:00
|
|
|
|
2014-08-02 06:10:48 +00:00
|
|
|
if(!response.waitForConnected())
|
|
|
|
|
{
|
2015-11-30 00:27:57 -05:00
|
|
|
printf("ERROR: Unable to connect to %s:%d\n", ipAddress.toStdString().c_str(), bidrectionalPort?bidrectionalPort:portIn);
|
2014-08-02 06:10:48 +00:00
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// publish image
|
|
|
|
|
std::vector<unsigned char> buf;
|
|
|
|
|
cv::imencode(".png", image, buf);
|
|
|
|
|
|
|
|
|
|
QByteArray block;
|
|
|
|
|
QDataStream out(&block, QIODevice::WriteOnly);
|
|
|
|
|
out.setVersion(QDataStream::Qt_4_0);
|
|
|
|
|
out << (quint64)0;
|
2015-11-30 00:27:57 -05:00
|
|
|
if(bidrectionalPort)
|
|
|
|
|
{
|
|
|
|
|
out << (quint32)find_object::TcpServer::kDetectObject;
|
|
|
|
|
}
|
2014-08-02 06:10:48 +00:00
|
|
|
out.writeRawData((char*)buf.data(), (int)buf.size());
|
|
|
|
|
out.device()->seek(0);
|
|
|
|
|
out << (quint64)(block.size() - sizeof(quint64));
|
|
|
|
|
|
2015-11-30 00:27:57 -05:00
|
|
|
qint64 bytes = requestPtr->write(block);
|
|
|
|
|
printf("Image published (%d bytes), waiting for response...\n", (int)bytes);
|
|
|
|
|
|
|
|
|
|
QTime time;
|
|
|
|
|
time.start();
|
2014-08-06 03:00:17 +00:00
|
|
|
|
2015-11-30 00:27:57 -05:00
|
|
|
// wait for response
|
|
|
|
|
if(bidrectionalPort)
|
|
|
|
|
{
|
|
|
|
|
requestPtr->waitForBytesWritten();
|
|
|
|
|
response.waitForReadyRead();
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2014-08-06 03:00:17 +00:00
|
|
|
app.exec();
|
2015-11-30 00:27:57 -05:00
|
|
|
}
|
2014-08-06 03:00:17 +00:00
|
|
|
|
2015-11-30 00:27:57 -05:00
|
|
|
if(response.dataReceived())
|
|
|
|
|
{
|
|
|
|
|
printf("Response received! (%d ms)\n", time.elapsed());
|
|
|
|
|
// print detected objects
|
|
|
|
|
if(response.info().objDetected_.size())
|
2014-08-02 06:10:48 +00:00
|
|
|
{
|
2015-11-30 00:27:57 -05:00
|
|
|
QList<int> ids = response.info().objDetected_.uniqueKeys();
|
|
|
|
|
for(int i=0; i<ids.size(); ++i)
|
2014-08-02 06:10:48 +00:00
|
|
|
{
|
2015-11-30 00:27:57 -05:00
|
|
|
int count = response.info().objDetected_.count(ids[i]);
|
|
|
|
|
if(count == 1)
|
2014-08-02 06:10:48 +00:00
|
|
|
{
|
2015-11-30 00:27:57 -05:00
|
|
|
printf("Object %d detected.\n", ids[i]);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
printf("Object %d detected %d times.\n", ids[i], count);
|
2014-08-02 06:10:48 +00:00
|
|
|
}
|
2014-08-06 03:00:17 +00:00
|
|
|
}
|
2014-08-02 06:10:48 +00:00
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2015-11-30 00:27:57 -05:00
|
|
|
printf("No objects detected.\n");
|
|
|
|
|
}
|
|
|
|
|
// write json
|
|
|
|
|
if(!jsonPath.isEmpty())
|
|
|
|
|
{
|
|
|
|
|
find_object::JsonWriter::write(response.info(), jsonPath);
|
|
|
|
|
printf("JSON written to \"%s\"\n", jsonPath.toStdString().c_str());
|
2014-08-02 06:10:48 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2015-11-30 00:27:57 -05:00
|
|
|
printf("Failed to receive a response...\n");
|
2014-08-06 03:00:17 +00:00
|
|
|
return -1;
|
2014-08-02 06:10:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|