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:
parent
e8f0016a55
commit
81c3bcb71c
@ -13,11 +13,12 @@
|
||||
#include "Settings.h"
|
||||
#include "QtOpenCV.h"
|
||||
|
||||
ImagesTcpServer::ImagesTcpServer(float hz, quint16 port, QObject * parent) :
|
||||
ImagesTcpServer::ImagesTcpServer(float hz, const QString & path, QObject * parent) :
|
||||
QTcpServer(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 &)));
|
||||
|
||||
@ -16,7 +16,7 @@ class ImagesTcpServer : public QTcpServer
|
||||
Q_OBJECT
|
||||
|
||||
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;
|
||||
quint16 getPort() const;
|
||||
|
||||
@ -14,7 +14,8 @@ void showUsage()
|
||||
printf("imagesTcpServer [options]\n"
|
||||
" Options:\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);
|
||||
}
|
||||
|
||||
@ -23,6 +24,7 @@ int main(int argc, char * argv[])
|
||||
QString ipAddress;
|
||||
float hz = 10.0f;
|
||||
quint16 port = 0;
|
||||
QString path;
|
||||
|
||||
for(int i=1; i<argc; ++i)
|
||||
{
|
||||
@ -63,14 +65,32 @@ int main(int argc, char * argv[])
|
||||
}
|
||||
continue;
|
||||
}
|
||||
if(strcmp(argv[i], "-path") == 0)
|
||||
{
|
||||
++i;
|
||||
if(i < argc)
|
||||
{
|
||||
path = argv[i];
|
||||
}
|
||||
else
|
||||
{
|
||||
showUsage();
|
||||
}
|
||||
continue;
|
||||
}
|
||||
|
||||
printf("Unrecognized option: %s\n", argv[i]);
|
||||
showUsage();
|
||||
}
|
||||
|
||||
if(!path.isEmpty())
|
||||
{
|
||||
printf("Using images from path \"%s\"\n", path.toStdString().c_str());
|
||||
}
|
||||
|
||||
QCoreApplication app(argc, argv);
|
||||
|
||||
ImagesTcpServer server(hz, port);
|
||||
ImagesTcpServer server(hz, path);
|
||||
|
||||
if (!server.listen(QHostAddress::Any, port))
|
||||
{
|
||||
|
||||
@ -21,8 +21,19 @@ CameraTcpClient::CameraTcpClient(QObject *parent) :
|
||||
|
||||
cv::Mat CameraTcpClient::getImage()
|
||||
{
|
||||
cv::Mat img = image_;
|
||||
image_ = cv::Mat();
|
||||
cv::Mat img;
|
||||
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;
|
||||
}
|
||||
|
||||
@ -48,7 +59,12 @@ void CameraTcpClient::readReceivedData()
|
||||
|
||||
std::vector<unsigned char> buf(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;
|
||||
}
|
||||
|
||||
@ -161,18 +177,26 @@ void Camera::takeImage()
|
||||
else
|
||||
{
|
||||
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())
|
||||
{
|
||||
img = cameraTcpClient_.getImage();
|
||||
}
|
||||
if(img.empty())
|
||||
{
|
||||
if(!cameraTcpClient_.waitForConnected())
|
||||
{
|
||||
printf("Connection is lost, try reconnecting to server (%s:%d)...\n",
|
||||
printf("Connection is lost, trying to reconnect to server (%s:%d)... (at the rate of the camera: %d ms)\n",
|
||||
Settings::getCamera_7IP().toStdString().c_str(),
|
||||
Settings::getCamera_8port());
|
||||
Settings::getCamera_8port(),
|
||||
cameraTimer_.interval());
|
||||
cameraTcpClient_.connectToHost(Settings::getCamera_7IP(), Settings::getCamera_8port());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if(img.empty())
|
||||
{
|
||||
|
||||
@ -17,7 +17,7 @@ class CameraTcpClient : public QTcpSocket
|
||||
public:
|
||||
CameraTcpClient(QObject * parent = 0);
|
||||
cv::Mat getImage();
|
||||
bool isConnected() const {return connected_;}
|
||||
int imagesBuffered() const {return images_.size();}
|
||||
|
||||
private Q_SLOTS:
|
||||
void readReceivedData();
|
||||
@ -26,8 +26,7 @@ private Q_SLOTS:
|
||||
|
||||
private:
|
||||
quint64 blockSize_;
|
||||
cv::Mat image_;
|
||||
bool connected_;
|
||||
QVector<cv::Mat> images_;
|
||||
};
|
||||
|
||||
class Camera : public QObject {
|
||||
|
||||
@ -1047,6 +1047,11 @@ void MainWindow::startProcessing()
|
||||
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
|
||||
{
|
||||
printf("[ERROR] Camera initialization failed! (with device %d)\n", Settings::getCamera_1deviceId());
|
||||
|
||||
@ -65,6 +65,7 @@ class Settings
|
||||
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, 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;...]
|
||||
PARAMETER(Feature2D, 1Detector, QString, "7:Dense;Fast;GFTT;MSER;ORB;SIFT;Star;SURF;BRISK" , "Keypoint detector.");
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user