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 git-svn-id: http://find-object.googlecode.com/svn/trunk/find_object@364 620bd6b2-0a58-f614-fd9a-1bd335dccda9
This commit is contained in:
@@ -1,3 +1,4 @@
|
||||
ADD_SUBDIRECTORY( tcpObjectsClient )
|
||||
ADD_SUBDIRECTORY( tcpImagesServer )
|
||||
ADD_SUBDIRECTORY( tcpRequest )
|
||||
ADD_SUBDIRECTORY( similarity )
|
||||
@@ -15,17 +15,17 @@
|
||||
#include <QtGui/QTransform>
|
||||
|
||||
ImagesTcpServer::ImagesTcpServer(float hz, const QString & path, QObject * parent) :
|
||||
QTcpServer(parent)
|
||||
QTcpSocket(parent)
|
||||
{
|
||||
// Set camera parameters
|
||||
Settings::setCamera_4imageRate(hz);
|
||||
Settings::setCamera_5mediaPath(path);
|
||||
|
||||
connect(this, SIGNAL(newConnection()), this, SLOT(addClient()));
|
||||
connect(&camera_, SIGNAL(imageReceived(const cv::Mat &)), this, SLOT(publishImage(const cv::Mat &)));
|
||||
connect(this, SIGNAL(connected()), this, SLOT(startCamera()));
|
||||
}
|
||||
|
||||
QHostAddress ImagesTcpServer::getHostAddress() const
|
||||
QHostAddress ImagesTcpServer::getHostAddress()
|
||||
{
|
||||
QHostAddress hostAddress;
|
||||
|
||||
@@ -49,21 +49,21 @@ QHostAddress ImagesTcpServer::getHostAddress() const
|
||||
return hostAddress;
|
||||
}
|
||||
|
||||
quint16 ImagesTcpServer::getPort() const
|
||||
{
|
||||
return this->serverPort();
|
||||
}
|
||||
|
||||
void ImagesTcpServer::publishImage(const cv::Mat & image)
|
||||
{
|
||||
QList<QTcpSocket*> clients = this->findChildren<QTcpSocket*>();
|
||||
if(clients.size())
|
||||
if(image.empty())
|
||||
{
|
||||
std::vector<unsigned char> buf;
|
||||
cv::imencode(".png", image, buf);
|
||||
|
||||
for(QList<QTcpSocket*>::iterator iter = clients.begin(); iter!=clients.end(); ++iter)
|
||||
printf("No more images...\n");
|
||||
camera_.pause();
|
||||
Q_EMIT connectionLost();
|
||||
}
|
||||
else
|
||||
{
|
||||
if(this->waitForConnected())
|
||||
{
|
||||
std::vector<unsigned char> buf;
|
||||
cv::imencode(".png", image, buf);
|
||||
|
||||
QByteArray block;
|
||||
QDataStream out(&block, QIODevice::WriteOnly);
|
||||
out.setVersion(QDataStream::Qt_4_0);
|
||||
@@ -71,20 +71,20 @@ void ImagesTcpServer::publishImage(const cv::Mat & image)
|
||||
out.writeRawData((char*)buf.data(), (int)buf.size());
|
||||
out.device()->seek(0);
|
||||
out << (quint64)(block.size() - sizeof(quint64));
|
||||
(*iter)->write(block);
|
||||
this->write(block);
|
||||
|
||||
}
|
||||
else
|
||||
{
|
||||
printf("Lost connection...\n");
|
||||
camera_.pause();
|
||||
Q_EMIT connectionLost();
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
printf("Paused...\n");
|
||||
camera_.pause();
|
||||
}
|
||||
}
|
||||
|
||||
void ImagesTcpServer::addClient()
|
||||
void ImagesTcpServer::startCamera()
|
||||
{
|
||||
QTcpSocket * client = this->nextPendingConnection();
|
||||
connect(client, SIGNAL(disconnected()), client, SLOT(deleteLater()));
|
||||
if(!camera_.isRunning())
|
||||
{
|
||||
printf("Start...\n");
|
||||
|
||||
@@ -5,27 +5,29 @@
|
||||
* Author: mathieu
|
||||
*/
|
||||
|
||||
#ifndef TCPCLIENT_H_
|
||||
#define TCPCLIENT_H_
|
||||
#ifndef IMAGESTCPSERVER_H_
|
||||
#define IMAGESTCPSERVER_H_
|
||||
|
||||
#include "find_object/Camera.h"
|
||||
#include <QtNetwork/QTcpServer>
|
||||
#include <QtNetwork/QTcpSocket>
|
||||
|
||||
class ImagesTcpServer : public QTcpServer
|
||||
class ImagesTcpServer : public QTcpSocket
|
||||
{
|
||||
Q_OBJECT
|
||||
|
||||
public:
|
||||
static QHostAddress getHostAddress();
|
||||
|
||||
public:
|
||||
ImagesTcpServer(float hz = 10.0f, const QString & path = "", QObject * parent = 0);
|
||||
|
||||
QHostAddress getHostAddress() const;
|
||||
quint16 getPort() const;
|
||||
|
||||
|
||||
private Q_SLOTS:
|
||||
void addClient();
|
||||
void startCamera();
|
||||
void publishImage(const cv::Mat & image);
|
||||
|
||||
Q_SIGNALS:
|
||||
void connectionLost();
|
||||
|
||||
private:
|
||||
Camera camera_;
|
||||
};
|
||||
|
||||
@@ -11,11 +11,11 @@
|
||||
|
||||
void showUsage()
|
||||
{
|
||||
printf("imagesTcpServer [options]\n"
|
||||
printf("\ntcpImagesServer [options] port\n"
|
||||
" Options:\n"
|
||||
" -hz #.# Image rate (default 10 Hz).\n"
|
||||
" -p # Set manually a port to which the clients will connect.\n"
|
||||
" -path \"\" Set a path of a directory of images or a video file.\n");
|
||||
" --hz #.# Image rate (default 10 Hz).\n"
|
||||
" --host #.#.#.# Set host address.\n"
|
||||
" --path \"\" Set a path of a directory of images or a video file.\n");
|
||||
exit(-1);
|
||||
}
|
||||
|
||||
@@ -23,15 +23,19 @@ int main(int argc, char * argv[])
|
||||
{
|
||||
QString ipAddress;
|
||||
float hz = 10.0f;
|
||||
quint16 port = 0;
|
||||
QString path;
|
||||
|
||||
for(int i=1; i<argc; ++i)
|
||||
if(argc < 2)
|
||||
{
|
||||
if(strcmp(argv[i], "-hz") == 0)
|
||||
showUsage();
|
||||
}
|
||||
|
||||
for(int i=1; i<argc-1; ++i)
|
||||
{
|
||||
if(strcmp(argv[i], "-hz") == 0 || strcmp(argv[i], "--hz") == 0)
|
||||
{
|
||||
++i;
|
||||
if(i < argc)
|
||||
if(i < argc-1)
|
||||
{
|
||||
hz = std::atof(argv[i]);
|
||||
if(hz < 0.0f)
|
||||
@@ -46,18 +50,12 @@ int main(int argc, char * argv[])
|
||||
}
|
||||
continue;
|
||||
}
|
||||
if(strcmp(argv[i], "-p") == 0)
|
||||
if(strcmp(argv[i], "-host") == 0 || strcmp(argv[i], "--host") == 0)
|
||||
{
|
||||
++i;
|
||||
if(i < argc)
|
||||
if(i < argc-1)
|
||||
{
|
||||
int v = std::atoi(argv[i]);
|
||||
if(v < 0)
|
||||
{
|
||||
printf("[ERROR] Port not valid : %s\n", argv[i]);
|
||||
showUsage();
|
||||
}
|
||||
port = v;
|
||||
ipAddress = argv[i];
|
||||
}
|
||||
else
|
||||
{
|
||||
@@ -65,10 +63,10 @@ int main(int argc, char * argv[])
|
||||
}
|
||||
continue;
|
||||
}
|
||||
if(strcmp(argv[i], "-path") == 0)
|
||||
if(strcmp(argv[i], "-path") == 0 || strcmp(argv[i], "--path") == 0)
|
||||
{
|
||||
++i;
|
||||
if(i < argc)
|
||||
if(i < argc-1)
|
||||
{
|
||||
path = argv[i];
|
||||
}
|
||||
@@ -83,24 +81,29 @@ int main(int argc, char * argv[])
|
||||
showUsage();
|
||||
}
|
||||
|
||||
quint16 port = std::atoi(argv[argc-1]);
|
||||
|
||||
if(!path.isEmpty())
|
||||
{
|
||||
printf("Using images from path \"%s\"\n", path.toStdString().c_str());
|
||||
}
|
||||
|
||||
QCoreApplication app(argc, argv);
|
||||
|
||||
ImagesTcpServer server(hz, path);
|
||||
|
||||
if (!server.listen(QHostAddress::Any, port))
|
||||
QObject::connect(&server, SIGNAL(connectionLost()), &app, SLOT(quit()));
|
||||
|
||||
if(ipAddress.isEmpty())
|
||||
{
|
||||
printf("ERROR: Unable to start the TCP server: %s\n", server.errorString().toStdString().c_str());
|
||||
ipAddress = server.getHostAddress().toString();
|
||||
}
|
||||
server.connectToHost(ipAddress, port);
|
||||
|
||||
if(!server.waitForConnected())
|
||||
{
|
||||
printf("ERROR: Unable to connect to %s:%d\n", ipAddress.toStdString().c_str(), port);
|
||||
return -1;
|
||||
}
|
||||
|
||||
printf("Images server waiting on \"%s:%d\"...\n",
|
||||
server.getHostAddress().toString().toStdString().c_str(), server.getPort());
|
||||
|
||||
return app.exec();
|
||||
}
|
||||
|
||||
|
||||
@@ -12,15 +12,13 @@
|
||||
#include <QtCore/QPointF>
|
||||
#include <QtCore/QTime>
|
||||
|
||||
TcpClient::TcpClient(const QString & hostname, quint16 port, QObject *parent) :
|
||||
TcpClient::TcpClient(QObject *parent) :
|
||||
QTcpSocket(parent),
|
||||
blockSize_(0)
|
||||
{
|
||||
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()));
|
||||
|
||||
this->connectToHost(hostname, port);
|
||||
}
|
||||
|
||||
void TcpClient::readReceivedData()
|
||||
|
||||
@@ -14,7 +14,7 @@ class TcpClient : public QTcpSocket
|
||||
{
|
||||
Q_OBJECT;
|
||||
public:
|
||||
TcpClient(const QString & hostname, quint16 port, QObject * parent = 0);
|
||||
TcpClient(QObject * parent = 0);
|
||||
|
||||
private Q_SLOTS:
|
||||
void readReceivedData();
|
||||
|
||||
@@ -11,7 +11,7 @@
|
||||
|
||||
void showUsage()
|
||||
{
|
||||
printf("tcpClient [hostname] port\n");
|
||||
printf("\ntcpObjectsClient [hostname] port\n");
|
||||
exit(-1);
|
||||
}
|
||||
|
||||
@@ -37,30 +37,16 @@ int main(int argc, char * argv[])
|
||||
|
||||
if(ipAddress.isEmpty())
|
||||
{
|
||||
// find out which IP to connect to
|
||||
QList<QHostAddress> ipAddressesList = QNetworkInterface::allAddresses();
|
||||
// use the first non-localhost IPv4 address
|
||||
for (int i = 0; i < ipAddressesList.size(); ++i)
|
||||
{
|
||||
if (ipAddressesList.at(i) != QHostAddress::LocalHost &&
|
||||
ipAddressesList.at(i).toIPv4Address())
|
||||
{
|
||||
ipAddress = ipAddressesList.at(i).toString();
|
||||
break;
|
||||
}
|
||||
}
|
||||
// if we did not find one, use IPv4 localhost
|
||||
if (ipAddress.isEmpty())
|
||||
{
|
||||
ipAddress = QHostAddress(QHostAddress::LocalHost).toString();
|
||||
}
|
||||
ipAddress = QHostAddress(QHostAddress::LocalHost).toString();
|
||||
}
|
||||
|
||||
QCoreApplication app(argc, argv);
|
||||
|
||||
printf("Connecting to \"%s:%d\"...\n", ipAddress.toStdString().c_str(), port);
|
||||
|
||||
TcpClient client(ipAddress, port);
|
||||
TcpClient client;
|
||||
|
||||
client.connectToHost(ipAddress, port);
|
||||
|
||||
if(client.waitForConnected())
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user