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:
matlabbe
2014-08-02 06:09:27 +00:00
parent b28ca440e8
commit 167aeee4bc
18 changed files with 359 additions and 250 deletions
+23 -23
View File
@@ -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");
+11 -9
View File
@@ -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_;
};
+29 -26
View File
@@ -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();
}