added Camera_queueSize for TCP images buffering

ImagesTcpServer can read from a path of images


git-svn-id: http://find-object.googlecode.com/svn/trunk/find_object@354 620bd6b2-0a58-f614-fd9a-1bd335dccda9
This commit is contained in:
matlabbe 2014-07-25 19:31:34 +00:00
parent e8f0016a55
commit 81c3bcb71c
7 changed files with 65 additions and 15 deletions

View File

@ -13,11 +13,12 @@
#include "Settings.h" #include "Settings.h"
#include "QtOpenCV.h" #include "QtOpenCV.h"
ImagesTcpServer::ImagesTcpServer(float hz, quint16 port, QObject * parent) : ImagesTcpServer::ImagesTcpServer(float hz, const QString & path, QObject * parent) :
QTcpServer(parent) QTcpServer(parent)
{ {
// Set camera parameters // Set camera parameters
Settings::setCamera_4imageRate(hz); Settings::setCamera_4imageRate(hz);
Settings::setCamera_5mediaPath(path);
connect(this, SIGNAL(newConnection()), this, SLOT(addClient())); connect(this, SIGNAL(newConnection()), this, SLOT(addClient()));
connect(&camera_, SIGNAL(imageReceived(const cv::Mat &)), this, SLOT(publishImage(const cv::Mat &))); connect(&camera_, SIGNAL(imageReceived(const cv::Mat &)), this, SLOT(publishImage(const cv::Mat &)));

View File

@ -16,7 +16,7 @@ class ImagesTcpServer : public QTcpServer
Q_OBJECT Q_OBJECT
public: public:
ImagesTcpServer(float hz = 10.0f, quint16 port = 0, QObject * parent = 0); ImagesTcpServer(float hz = 10.0f, const QString & path = "", QObject * parent = 0);
QHostAddress getHostAddress() const; QHostAddress getHostAddress() const;
quint16 getPort() const; quint16 getPort() const;

View File

@ -14,7 +14,8 @@ void showUsage()
printf("imagesTcpServer [options]\n" printf("imagesTcpServer [options]\n"
" Options:\n" " Options:\n"
" -hz #.# Image rate (default 10 Hz).\n" " -hz #.# Image rate (default 10 Hz).\n"
" -p # Set manually a port.\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");
exit(-1); exit(-1);
} }
@ -23,6 +24,7 @@ int main(int argc, char * argv[])
QString ipAddress; QString ipAddress;
float hz = 10.0f; float hz = 10.0f;
quint16 port = 0; quint16 port = 0;
QString path;
for(int i=1; i<argc; ++i) for(int i=1; i<argc; ++i)
{ {
@ -63,14 +65,32 @@ int main(int argc, char * argv[])
} }
continue; continue;
} }
if(strcmp(argv[i], "-path") == 0)
{
++i;
if(i < argc)
{
path = argv[i];
}
else
{
showUsage();
}
continue;
}
printf("Unrecognized option: %s\n", argv[i]); printf("Unrecognized option: %s\n", argv[i]);
showUsage(); showUsage();
} }
if(!path.isEmpty())
{
printf("Using images from path \"%s\"\n", path.toStdString().c_str());
}
QCoreApplication app(argc, argv); QCoreApplication app(argc, argv);
ImagesTcpServer server(hz, port); ImagesTcpServer server(hz, path);
if (!server.listen(QHostAddress::Any, port)) if (!server.listen(QHostAddress::Any, port))
{ {

View File

@ -21,8 +21,19 @@ CameraTcpClient::CameraTcpClient(QObject *parent) :
cv::Mat CameraTcpClient::getImage() cv::Mat CameraTcpClient::getImage()
{ {
cv::Mat img = image_; cv::Mat img;
image_ = cv::Mat(); if(images_.size())
{
// if queue changed after tcp connection ended with images still in the buffer
int queue = Settings::getCamera_9queueSize();
while(queue > 0 && images_.size() > queue)
{
images_.pop_front();
}
img = images_.front();
images_.pop_front();
}
return img; return img;
} }
@ -48,7 +59,12 @@ void CameraTcpClient::readReceivedData()
std::vector<unsigned char> buf(blockSize_); std::vector<unsigned char> buf(blockSize_);
in.readRawData((char*)buf.data(), blockSize_); in.readRawData((char*)buf.data(), blockSize_);
image_ = cv::imdecode(buf, cv::IMREAD_UNCHANGED); images_.push_back(cv::imdecode(buf, cv::IMREAD_UNCHANGED));
int queue = Settings::getCamera_9queueSize();
while(queue > 0 && images_.size() > queue)
{
images_.pop_front();
}
blockSize_ = 0; blockSize_ = 0;
} }
@ -161,16 +177,24 @@ void Camera::takeImage()
else else
{ {
img = cameraTcpClient_.getImage(); img = cameraTcpClient_.getImage();
if(cameraTcpClient_.imagesBuffered() > 0 && Settings::getCamera_9queueSize() == 0)
{
printf("[Warning] %d images buffered so far...\n", cameraTcpClient_.imagesBuffered());
}
while(img.empty() && cameraTcpClient_.waitForReadyRead()) while(img.empty() && cameraTcpClient_.waitForReadyRead())
{ {
img = cameraTcpClient_.getImage(); img = cameraTcpClient_.getImage();
} }
if(!cameraTcpClient_.waitForConnected()) if(img.empty())
{ {
printf("Connection is lost, try reconnecting to server (%s:%d)...\n", if(!cameraTcpClient_.waitForConnected())
Settings::getCamera_7IP().toStdString().c_str(), {
Settings::getCamera_8port()); printf("Connection is lost, trying to reconnect to server (%s:%d)... (at the rate of the camera: %d ms)\n",
cameraTcpClient_.connectToHost(Settings::getCamera_7IP(), Settings::getCamera_8port()); Settings::getCamera_7IP().toStdString().c_str(),
Settings::getCamera_8port(),
cameraTimer_.interval());
cameraTcpClient_.connectToHost(Settings::getCamera_7IP(), Settings::getCamera_8port());
}
} }
} }

View File

@ -17,7 +17,7 @@ class CameraTcpClient : public QTcpSocket
public: public:
CameraTcpClient(QObject * parent = 0); CameraTcpClient(QObject * parent = 0);
cv::Mat getImage(); cv::Mat getImage();
bool isConnected() const {return connected_;} int imagesBuffered() const {return images_.size();}
private Q_SLOTS: private Q_SLOTS:
void readReceivedData(); void readReceivedData();
@ -26,8 +26,7 @@ private Q_SLOTS:
private: private:
quint64 blockSize_; quint64 blockSize_;
cv::Mat image_; QVector<cv::Mat> images_;
bool connected_;
}; };
class Camera : public QObject { class Camera : public QObject {

View File

@ -1047,6 +1047,11 @@ void MainWindow::startProcessing()
QMessageBox::critical(this, tr("Camera error"), tr("Camera initialization failed! (with device %1)").arg(Settings::getCamera_1deviceId())); QMessageBox::critical(this, tr("Camera error"), tr("Camera initialization failed! (with device %1)").arg(Settings::getCamera_1deviceId()));
} }
} }
else if(Settings::getCamera_6useTcpCamera())
{
printf("%s\n", tr("Camera initialization failed! (with server %1:%2) Trying again in 1 second...").arg(Settings::getCamera_7IP()).arg(Settings::getCamera_8port()).toStdString().c_str());
QTimer::singleShot(1000, this, SLOT(startProcessing()));
}
else else
{ {
printf("[ERROR] Camera initialization failed! (with device %d)\n", Settings::getCamera_1deviceId()); printf("[ERROR] Camera initialization failed! (with device %d)\n", Settings::getCamera_1deviceId());

View File

@ -65,6 +65,7 @@ class Settings
PARAMETER(Camera, 6useTcpCamera, bool, false, "Use TCP/IP input camera."); PARAMETER(Camera, 6useTcpCamera, bool, false, "Use TCP/IP input camera.");
PARAMETER(Camera, 7IP, QString, "127.0.0.1", "The images server's IP to connect when useTcpCamera is checked."); PARAMETER(Camera, 7IP, QString, "127.0.0.1", "The images server's IP to connect when useTcpCamera is checked.");
PARAMETER(Camera, 8port, int, 5000, "The images server's port to connect when useTcpCamera is checked."); PARAMETER(Camera, 8port, int, 5000, "The images server's port to connect when useTcpCamera is checked.");
PARAMETER(Camera, 9queueSize, int, 1, "Maximum images buffered from TCP. If 0, all images are buffered.");
//List format : [Index:item0;item1;item3;...] //List format : [Index:item0;item1;item3;...]
PARAMETER(Feature2D, 1Detector, QString, "7:Dense;Fast;GFTT;MSER;ORB;SIFT;Star;SURF;BRISK" , "Keypoint detector."); PARAMETER(Feature2D, 1Detector, QString, "7:Dense;Fast;GFTT;MSER;ORB;SIFT;Star;SURF;BRISK" , "Keypoint detector.");