Added TcpRequest tool (ask a running find_object to process an image and wait results over TCP)
Refactored CameraTcpClient to CameraTcpServer (to send images we connect to find_object, instead of connecting find_object to an image server), removed parameter CameraIP (missing files) git-svn-id: http://find-object.googlecode.com/svn/trunk/find_object@365 620bd6b2-0a58-f614-fd9a-1bd335dccda9
This commit is contained in:
parent
167aeee4bc
commit
e37d64a3f3
54
tools/tcpRequest/CMakeLists.txt
Normal file
54
tools/tcpRequest/CMakeLists.txt
Normal file
@ -0,0 +1,54 @@
|
||||
### Qt Gui stuff ###
|
||||
SET(headers_ui
|
||||
TcpResponse.h
|
||||
)
|
||||
#This will generate moc_* for Qt
|
||||
QT4_WRAP_CPP(moc_srcs ${headers_ui})
|
||||
### Qt Gui stuff end###
|
||||
|
||||
SET(SRC_FILES
|
||||
TcpResponse.cpp
|
||||
main.cpp
|
||||
${moc_srcs}
|
||||
)
|
||||
|
||||
SET(INCLUDE_DIRS
|
||||
${CMAKE_CURRENT_SOURCE_DIR}/../../include
|
||||
${CMAKE_CURRENT_SOURCE_DIR}
|
||||
${OpenCV_INCLUDE_DIRS}
|
||||
)
|
||||
|
||||
INCLUDE(${QT_USE_FILE})
|
||||
|
||||
SET(LIBRARIES
|
||||
${OpenCV_LIBS}
|
||||
${QT_LIBRARIES}
|
||||
)
|
||||
|
||||
IF(JSONCPP_FOUND)
|
||||
SET(INCLUDE_DIRS
|
||||
${INCLUDE_DIRS}
|
||||
${JSONCPP_INCLUDE_DIRS}
|
||||
)
|
||||
SET(LIBRARIES
|
||||
${LIBRARIES}
|
||||
${JSONCPP_LIBRARIES}
|
||||
)
|
||||
ADD_DEFINITIONS("-DWITH_JSONCPP")
|
||||
ENDIF(JSONCPP_FOUND)
|
||||
|
||||
# Make sure the compiler can find include files from our library.
|
||||
INCLUDE_DIRECTORIES(${INCLUDE_DIRS})
|
||||
|
||||
# Add binary called "example" that is built from the source file "main.cpp".
|
||||
# The extension is automatically found.
|
||||
ADD_EXECUTABLE(tcpRequest ${SRC_FILES})
|
||||
TARGET_LINK_LIBRARIES(tcpRequest find_object ${LIBRARIES})
|
||||
|
||||
SET_TARGET_PROPERTIES( tcpRequest
|
||||
PROPERTIES OUTPUT_NAME ${PROJECT_PREFIX}-tcpRequest)
|
||||
|
||||
INSTALL(TARGETS tcpRequest
|
||||
RUNTIME DESTINATION bin COMPONENT runtime
|
||||
BUNDLE DESTINATION "${CMAKE_BUNDLE_LOCATION}" COMPONENT runtime)
|
||||
|
||||
96
tools/tcpRequest/TcpResponse.cpp
Normal file
96
tools/tcpRequest/TcpResponse.cpp
Normal file
@ -0,0 +1,96 @@
|
||||
/*
|
||||
* TcpResponse.cpp
|
||||
*
|
||||
* Created on: 2014-05-05
|
||||
* Author: mathieu
|
||||
*/
|
||||
|
||||
#include "TcpResponse.h"
|
||||
|
||||
#include <opencv2/opencv.hpp>
|
||||
#include <QtGui/QTransform>
|
||||
#include <QtCore/QPointF>
|
||||
#include <QtCore/QTime>
|
||||
|
||||
TcpResponse::TcpResponse(QObject *parent) :
|
||||
QTcpSocket(parent),
|
||||
blockSize_(0),
|
||||
dataReceived_(false)
|
||||
{
|
||||
connect(this, SIGNAL(readyRead()), this, SLOT(readReceivedData()));
|
||||
connect(this, SIGNAL(error(QAbstractSocket::SocketError)), this, SLOT(displayError(QAbstractSocket::SocketError)));
|
||||
connect(this, SIGNAL(disconnected()), this, SLOT(connectionLost()));
|
||||
}
|
||||
|
||||
void TcpResponse::readReceivedData()
|
||||
{
|
||||
QDataStream in(this);
|
||||
in.setVersion(QDataStream::Qt_4_0);
|
||||
|
||||
if (blockSize_ == 0)
|
||||
{
|
||||
if (this->bytesAvailable() < (int)sizeof(quint16))
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
in >> blockSize_;
|
||||
}
|
||||
|
||||
if (this->bytesAvailable() < blockSize_)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
blockSize_ = 0;
|
||||
|
||||
QVector<float> data;
|
||||
in >> data;
|
||||
|
||||
objectsDetected_.clear();
|
||||
for(int i=0; i<data.size(); i+=12)
|
||||
{
|
||||
// get data
|
||||
int id = (int)data[i];
|
||||
float width = data[i+1];
|
||||
float height = data[i+2];
|
||||
|
||||
// Find corners Qt
|
||||
QTransform homography(data[i+3], data[i+4], data[i+5],
|
||||
data[i+6], data[i+7], data[i+8],
|
||||
data[i+9], data[i+10], data[i+11]);
|
||||
|
||||
objectsDetected_.insert(id, QPair<QRect, QTransform>(QRect(0,0,width, height), homography));
|
||||
}
|
||||
|
||||
dataReceived_ = true;
|
||||
Q_EMIT detectionReceived();
|
||||
}
|
||||
|
||||
void TcpResponse::displayError(QAbstractSocket::SocketError socketError)
|
||||
{
|
||||
switch (socketError)
|
||||
{
|
||||
case QAbstractSocket::RemoteHostClosedError:
|
||||
break;
|
||||
case QAbstractSocket::HostNotFoundError:
|
||||
printf("Tcp error: The host was not found. Please "
|
||||
"check the host name and port settings.\n");
|
||||
break;
|
||||
case QAbstractSocket::ConnectionRefusedError:
|
||||
printf("The connection was refused by the peer. "
|
||||
"Make sure Find-Object is running, "
|
||||
"and check that the host name and port "
|
||||
"settings are correct.\n");
|
||||
break;
|
||||
default:
|
||||
printf("The following error occurred: %s.\n", this->errorString().toStdString().c_str());
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
void TcpResponse::connectionLost()
|
||||
{
|
||||
printf("Connection lost!\n");
|
||||
}
|
||||
|
||||
38
tools/tcpRequest/TcpResponse.h
Normal file
38
tools/tcpRequest/TcpResponse.h
Normal file
@ -0,0 +1,38 @@
|
||||
/*
|
||||
* TCPResponse.h
|
||||
*
|
||||
* Created on: 2014-05-05
|
||||
* Author: mathieu
|
||||
*/
|
||||
|
||||
#ifndef TCPRESPONSE_H_
|
||||
#define TCPRESPONSE_H_
|
||||
|
||||
#include <QtNetwork/QTcpSocket>
|
||||
#include <QtCore/QMultiMap>
|
||||
#include <QtGui/QTransform>
|
||||
#include <QtCore/QRect>
|
||||
|
||||
class TcpResponse : public QTcpSocket
|
||||
{
|
||||
Q_OBJECT;
|
||||
public:
|
||||
TcpResponse(QObject * parent = 0);
|
||||
const QMultiMap<int, QPair<QRect, QTransform> > & objectsDetected() const {return objectsDetected_;}
|
||||
bool dataReceived() const {return dataReceived_;}
|
||||
|
||||
private Q_SLOTS:
|
||||
void readReceivedData();
|
||||
void displayError(QAbstractSocket::SocketError socketError);
|
||||
void connectionLost();
|
||||
|
||||
Q_SIGNALS:
|
||||
void detectionReceived();
|
||||
|
||||
private:
|
||||
quint16 blockSize_;
|
||||
QMultiMap<int, QPair<QRect, QTransform> > objectsDetected_;
|
||||
bool dataReceived_;
|
||||
};
|
||||
|
||||
#endif /* TCPCLIENT_H_ */
|
||||
278
tools/tcpRequest/main.cpp
Normal file
278
tools/tcpRequest/main.cpp
Normal file
@ -0,0 +1,278 @@
|
||||
/*
|
||||
* main.cpp
|
||||
*
|
||||
* Created on: 2014-05-05
|
||||
* Author: mathieu
|
||||
*/
|
||||
|
||||
#include <QtNetwork/QNetworkInterface>
|
||||
#include <QtCore/QCoreApplication>
|
||||
#include <QtCore/QFile>
|
||||
#include <QtCore/QTime>
|
||||
#include <opencv2/opencv.hpp>
|
||||
#include "TcpResponse.h"
|
||||
|
||||
#ifdef WITH_JSONCPP
|
||||
#include <jsoncpp/json/writer.h>
|
||||
#endif
|
||||
|
||||
void showUsage()
|
||||
{
|
||||
printf("\ntcpRequest [options] --scene image.png --out # --in #\n"
|
||||
" \"out\" is the port to which the image is sent.\n"
|
||||
" \"in\" is the port from which the detection is received.\n"
|
||||
" Options:\n"
|
||||
" --host #.#.#.# Set host address.\n"
|
||||
#ifdef WITH_JSONCPP
|
||||
" --json \"path\" Path to an output JSON file.\n"
|
||||
#endif
|
||||
);
|
||||
exit(-1);
|
||||
}
|
||||
|
||||
void writeJSON(const QMultiMap<int, QPair<QRect, QTransform> > & objectsDetected, const QString & path)
|
||||
{
|
||||
#ifdef WITH_JSONCPP
|
||||
if(!path.isEmpty())
|
||||
{
|
||||
Json::Value root;
|
||||
Json::Value detections;
|
||||
|
||||
if(objectsDetected.size())
|
||||
{
|
||||
for(QMultiMap<int,QPair<QRect,QTransform> >::const_iterator iter = objectsDetected.constBegin();
|
||||
iter!= objectsDetected.end();)
|
||||
{
|
||||
char index = 'a';
|
||||
QMultiMap<int,QPair<QRect,QTransform> >::const_iterator jter = iter;
|
||||
for(;jter != objectsDetected.constEnd() && jter.key() == iter.key(); ++jter)
|
||||
{
|
||||
QString name = QString("object_%1%2").arg(jter.key()).arg(objectsDetected.count(jter.key())>1?QString(index++):"");
|
||||
detections.append(name.toStdString());
|
||||
|
||||
Json::Value homography;
|
||||
homography.append(jter.value().second.m11());
|
||||
homography.append(jter.value().second.m12());
|
||||
homography.append(jter.value().second.m13());
|
||||
homography.append(jter.value().second.m21());
|
||||
homography.append(jter.value().second.m22());
|
||||
homography.append(jter.value().second.m23());
|
||||
homography.append(jter.value().second.m31()); // dx
|
||||
homography.append(jter.value().second.m32()); // dy
|
||||
homography.append(jter.value().second.m33());
|
||||
root[name.toStdString()]["width"] = jter.value().first.width();
|
||||
root[name.toStdString()]["height"] = jter.value().first.height();
|
||||
root[name.toStdString()]["homography"] = homography;
|
||||
}
|
||||
iter = jter;
|
||||
}
|
||||
}
|
||||
|
||||
root["objects"] = detections;
|
||||
|
||||
// write in a nice readible way
|
||||
Json::StyledWriter styledWriter;
|
||||
//std::cout << styledWriter.write(root);
|
||||
QFile file(path);
|
||||
file.open(QIODevice::WriteOnly | QIODevice::Text);
|
||||
QTextStream out(&file);
|
||||
out << styledWriter.write(root).c_str();
|
||||
file.close();
|
||||
printf("JSON written to \"%s\"\n", path.toStdString().c_str());
|
||||
}
|
||||
#else
|
||||
printf("Not built with JSON support!\n");
|
||||
#endif
|
||||
}
|
||||
|
||||
int main(int argc, char * argv[])
|
||||
{
|
||||
QString ipAddress;
|
||||
QString scenePath;
|
||||
QString jsonPath;
|
||||
quint16 portOut = 0;
|
||||
quint16 portIn = 0;
|
||||
|
||||
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], "--json") == 0 || strcmp(argv[i], "-json") == 0)
|
||||
{
|
||||
++i;
|
||||
if(i < argc)
|
||||
{
|
||||
jsonPath = argv[i];
|
||||
}
|
||||
else
|
||||
{
|
||||
printf("error parsing --json\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;
|
||||
}
|
||||
|
||||
printf("Unrecognized option: %s\n", argv[i]);
|
||||
showUsage();
|
||||
}
|
||||
|
||||
if(portOut == 0)
|
||||
{
|
||||
printf("Argument --out should be set.\n");
|
||||
showUsage();
|
||||
}
|
||||
else if(portIn == 0)
|
||||
{
|
||||
printf("Argument --in should be set.\n");
|
||||
}
|
||||
else if(scenePath.isEmpty())
|
||||
{
|
||||
printf("Argument --scene should be set.\n");
|
||||
}
|
||||
|
||||
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()));
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
if(!response.waitForConnected())
|
||||
{
|
||||
printf("ERROR: Unable to connect to %s:%d\n", ipAddress.toStdString().c_str(), portIn);
|
||||
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;
|
||||
out.writeRawData((char*)buf.data(), (int)buf.size());
|
||||
out.device()->seek(0);
|
||||
out << (quint64)(block.size() - sizeof(quint64));
|
||||
request.write(block);
|
||||
printf("Image published, waiting for response...\n");
|
||||
QTime time;
|
||||
time.start();
|
||||
|
||||
// wait for response
|
||||
app.exec();
|
||||
|
||||
if(response.dataReceived())
|
||||
{
|
||||
printf("Response received! (%d ms)\n", time.elapsed());
|
||||
// print detected objects
|
||||
if(response.objectsDetected().size())
|
||||
{
|
||||
QList<int> ids = response.objectsDetected().uniqueKeys();
|
||||
for(int i=0; i<ids.size(); ++i)
|
||||
{
|
||||
int count = response.objectsDetected().count(ids[i]);
|
||||
if(count == 1)
|
||||
{
|
||||
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())
|
||||
{
|
||||
writeJSON(response.objectsDetected(), jsonPath);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
printf("Failed to receive a response...\n");
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user