diff --git a/imagesTcpServer/ImagesTcpServer.cpp b/imagesTcpServer/ImagesTcpServer.cpp index e8c05557..70884e4d 100644 --- a/imagesTcpServer/ImagesTcpServer.cpp +++ b/imagesTcpServer/ImagesTcpServer.cpp @@ -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 &))); diff --git a/imagesTcpServer/ImagesTcpServer.h b/imagesTcpServer/ImagesTcpServer.h index 647c7142..e0c0a246 100644 --- a/imagesTcpServer/ImagesTcpServer.h +++ b/imagesTcpServer/ImagesTcpServer.h @@ -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; diff --git a/imagesTcpServer/main.cpp b/imagesTcpServer/main.cpp index 8951fcaa..72fb81ce 100644 --- a/imagesTcpServer/main.cpp +++ b/imagesTcpServer/main.cpp @@ -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 0 && images_.size() > queue) + { + images_.pop_front(); + } + + img = images_.front(); + images_.pop_front(); + } return img; } @@ -48,7 +59,12 @@ void CameraTcpClient::readReceivedData() std::vector 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,16 +177,24 @@ 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(!cameraTcpClient_.waitForConnected()) + if(img.empty()) { - printf("Connection is lost, try reconnecting to server (%s:%d)...\n", - Settings::getCamera_7IP().toStdString().c_str(), - Settings::getCamera_8port()); - cameraTcpClient_.connectToHost(Settings::getCamera_7IP(), Settings::getCamera_8port()); + if(!cameraTcpClient_.waitForConnected()) + { + 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(), + cameraTimer_.interval()); + cameraTcpClient_.connectToHost(Settings::getCamera_7IP(), Settings::getCamera_8port()); + } } } diff --git a/src/Camera.h b/src/Camera.h index b99347ad..b4f9e44e 100644 --- a/src/Camera.h +++ b/src/Camera.h @@ -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 images_; }; class Camera : public QObject { diff --git a/src/MainWindow.cpp b/src/MainWindow.cpp index 95980127..4a05e04f 100644 --- a/src/MainWindow.cpp +++ b/src/MainWindow.cpp @@ -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()); diff --git a/src/Settings.h b/src/Settings.h index 51aeccd0..143b264c 100644 --- a/src/Settings.h +++ b/src/Settings.h @@ -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.");