Added "--tcp_threads" argument for multi-threaded detections (on multiple ports)

This commit is contained in:
matlabbe
2015-11-30 00:27:57 -05:00
parent 164da72169
commit cff1d4eac7
12 changed files with 304 additions and 90 deletions
-2
View File
@@ -4,9 +4,7 @@ SET(headers_ui
)
IF("${FINDOBJECT_QT_VERSION}" STREQUAL "4")
#This will generate moc_* for Qt
QT4_WRAP_CPP(moc_srcs ${headers_ui})
### Qt Gui stuff end###
ELSE()
QT5_WRAP_CPP(moc_srcs ${headers_ui})
ENDIF()
+88 -46
View File
@@ -30,14 +30,17 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#include <QtCore/QFile>
#include <QtCore/QTime>
#include <opencv2/opencv.hpp>
#include <find_object/TcpServer.h>
#include "TcpResponse.h"
#include "find_object/JsonWriter.h"
void showUsage()
{
printf("\ntcpRequest [options] --scene image.png --out # --in #\n"
"\ntcpRequest [options] --scene image.png --port #\n"
" \"out\" is the port to which the image is sent.\n"
" \"in\" is the port from which the detection is received.\n"
" \"port\" is the bidirectional port from which the image is sent AND the detection is received.\n"
" Options:\n"
" --host #.#.#.# Set host address.\n"
" --json \"path\" Path to an output JSON file.\n"
@@ -52,6 +55,7 @@ int main(int argc, char * argv[])
QString jsonPath;
quint16 portOut = 0;
quint16 portIn = 0;
quint16 bidrectionalPort = 0;
for(int i=1; i<argc; ++i)
{
@@ -111,6 +115,20 @@ int main(int argc, char * argv[])
}
continue;
}
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;
}
if(strcmp(argv[i], "--json") == 0 || strcmp(argv[i], "-json") == 0)
{
@@ -137,18 +155,25 @@ int main(int argc, char * argv[])
showUsage();
}
if(portOut == 0)
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)
{
printf("Argument --out should be set.\n");
showUsage();
}
else if(portIn == 0)
else if(portIn == 0 && bidrectionalPort == 0)
{
printf("Argument --in should be set.\n");
showUsage();
}
else if(scenePath.isEmpty())
{
printf("Argument --scene should be set.\n");
showUsage();
}
if(ipAddress.isEmpty())
@@ -169,22 +194,34 @@ int main(int argc, char * argv[])
QObject::connect(&response, SIGNAL(detectionReceived()), &app, SLOT(quit()));
QObject::connect(&response, SIGNAL(disconnected()), &app, SLOT(quit()));
QObject::connect(&request, SIGNAL(disconnected()), &app, SLOT(quit()));
QObject::connect(&response, SIGNAL(error(QAbstractSocket::SocketError)), &app, SLOT(quit()));
QObject::connect(&request, SIGNAL(error(QAbstractSocket::SocketError)), &app, SLOT(quit()));
request.connectToHost(ipAddress, portOut);
response.connectToHost(ipAddress, portIn);
if(!request.waitForConnected())
QTcpSocket * requestPtr = &request;
if(bidrectionalPort == 0)
{
printf("ERROR: Unable to connect to %s:%d\n", ipAddress.toStdString().c_str(), portOut);
return -1;
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);
if(!request.waitForConnected())
{
printf("ERROR: Unable to connect to %s:%d\n", ipAddress.toStdString().c_str(), portOut);
return -1;
}
}
else
{
printf("Using bidirectional port\n");
requestPtr = &response;
response.connectToHost(ipAddress, bidrectionalPort);
}
if(!response.waitForConnected())
{
printf("ERROR: Unable to connect to %s:%d\n", ipAddress.toStdString().c_str(), portIn);
printf("ERROR: Unable to connect to %s:%d\n", ipAddress.toStdString().c_str(), bidrectionalPort?bidrectionalPort:portIn);
return -1;
}
@@ -196,60 +233,65 @@ int main(int argc, char * argv[])
QDataStream out(&block, QIODevice::WriteOnly);
out.setVersion(QDataStream::Qt_4_0);
out << (quint64)0;
if(bidrectionalPort)
{
out << (quint32)find_object::TcpServer::kDetectObject;
}
out.writeRawData((char*)buf.data(), (int)buf.size());
out.device()->seek(0);
out << (quint64)(block.size() - sizeof(quint64));
if(request.waitForReadyRead())
qint64 bytes = requestPtr->write(block);
printf("Image published (%d bytes), waiting for response...\n", (int)bytes);
QTime time;
time.start();
// wait for response
if(bidrectionalPort)
{
requestPtr->waitForBytesWritten();
response.waitForReadyRead();
}
else
{
qint64 bytes = request.write(block);
printf("Image published (%d bytes), waiting for response...\n", (int)bytes);
QTime time;
time.start();
// wait for response
app.exec();
}
if(response.dataReceived())
if(response.dataReceived())
{
printf("Response received! (%d ms)\n", time.elapsed());
// print detected objects
if(response.info().objDetected_.size())
{
printf("Response received! (%d ms)\n", time.elapsed());
// print detected objects
if(response.info().objDetected_.size())
QList<int> ids = response.info().objDetected_.uniqueKeys();
for(int i=0; i<ids.size(); ++i)
{
QList<int> ids = response.info().objDetected_.uniqueKeys();
for(int i=0; i<ids.size(); ++i)
int count = response.info().objDetected_.count(ids[i]);
if(count == 1)
{
int count = response.info().objDetected_.count(ids[i]);
if(count == 1)
{
printf("Object %d detected.\n", ids[i]);
}
else
{
printf("Object %d detected %d times.\n", ids[i], count);
}
printf("Object %d detected.\n", ids[i]);
}
else
{
printf("Object %d detected %d times.\n", ids[i], count);
}
}
else
{
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());
}
}
else
{
printf("Failed to receive a response...\n");
return -1;
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());
}
}
else
{
printf("Server is busy...\n");
printf("Failed to receive a response...\n");
return -1;
}