From e37d64a3f32679beb21ea277434e0f2614176aed Mon Sep 17 00:00:00 2001 From: matlabbe Date: Sat, 2 Aug 2014 06:10:48 +0000 Subject: [PATCH] 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 --- tools/tcpRequest/CMakeLists.txt | 54 ++++++ tools/tcpRequest/TcpResponse.cpp | 96 +++++++++++ tools/tcpRequest/TcpResponse.h | 38 +++++ tools/tcpRequest/main.cpp | 278 +++++++++++++++++++++++++++++++ 4 files changed, 466 insertions(+) create mode 100644 tools/tcpRequest/CMakeLists.txt create mode 100644 tools/tcpRequest/TcpResponse.cpp create mode 100644 tools/tcpRequest/TcpResponse.h create mode 100644 tools/tcpRequest/main.cpp diff --git a/tools/tcpRequest/CMakeLists.txt b/tools/tcpRequest/CMakeLists.txt new file mode 100644 index 00000000..fe92405b --- /dev/null +++ b/tools/tcpRequest/CMakeLists.txt @@ -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) + diff --git a/tools/tcpRequest/TcpResponse.cpp b/tools/tcpRequest/TcpResponse.cpp new file mode 100644 index 00000000..06c5718f --- /dev/null +++ b/tools/tcpRequest/TcpResponse.cpp @@ -0,0 +1,96 @@ +/* + * TcpResponse.cpp + * + * Created on: 2014-05-05 + * Author: mathieu + */ + +#include "TcpResponse.h" + +#include +#include +#include +#include + +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 data; + in >> data; + + objectsDetected_.clear(); + for(int i=0; i(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"); +} + diff --git a/tools/tcpRequest/TcpResponse.h b/tools/tcpRequest/TcpResponse.h new file mode 100644 index 00000000..f60711f4 --- /dev/null +++ b/tools/tcpRequest/TcpResponse.h @@ -0,0 +1,38 @@ +/* + * TCPResponse.h + * + * Created on: 2014-05-05 + * Author: mathieu + */ + +#ifndef TCPRESPONSE_H_ +#define TCPRESPONSE_H_ + +#include +#include +#include +#include + +class TcpResponse : public QTcpSocket +{ + Q_OBJECT; +public: + TcpResponse(QObject * parent = 0); + const QMultiMap > & 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 > objectsDetected_; + bool dataReceived_; +}; + +#endif /* TCPCLIENT_H_ */ diff --git a/tools/tcpRequest/main.cpp b/tools/tcpRequest/main.cpp new file mode 100644 index 00000000..d72602a9 --- /dev/null +++ b/tools/tcpRequest/main.cpp @@ -0,0 +1,278 @@ +/* + * main.cpp + * + * Created on: 2014-05-05 + * Author: mathieu + */ + +#include +#include +#include +#include +#include +#include "TcpResponse.h" + +#ifdef WITH_JSONCPP +#include +#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 > & objectsDetected, const QString & path) +{ +#ifdef WITH_JSONCPP + if(!path.isEmpty()) + { + Json::Value root; + Json::Value detections; + + if(objectsDetected.size()) + { + for(QMultiMap >::const_iterator iter = objectsDetected.constBegin(); + iter!= objectsDetected.end();) + { + char index = 'a'; + QMultiMap >::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 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 ids = response.objectsDetected().uniqueKeys(); + for(int i=0; i