1. iorodeo
  2. bias

Commits

iorodeo  committed d289188

Added videoWriter base class and integrated it into image logger.

  • Participants
  • Parent commits 3209f1f
  • Branches default

Comments (0)

Files changed (8)

File src/facade/basic_types.hpp

View file
         ERROR_DC1394_START_CAPTURE,
         ERROR_DC1394_GRAB_IMAGE,
 
+        // Video Writer Errors
+        ERROR_VIDEO_WRITER_ADD_FRAME,
+
         NUMBER_OF_ERROR,
     }; 
 
         TRIGGER_EXTERNAL
     };
 
+    enum VideoFileFormat
+    {
+        VIDEOFILE_FORMAT_AVI,
+        VIDEOFILE_FORMAT_FMF,
+        VIDEOFILE_FORMAT_UFMF
+    };
+
+    enum ImageRotationType
+    { 
+        IMAGE_ROTATION_0=0,
+        IMAGE_ROTATION_90=90,
+        IMAGE_ROTATION_180=180,
+        IMAGE_ROTATION_270=270
+    };
+
 
 } // namespace bias
 

File src/gui/CMakeLists.txt

View file
     image_grabber.hpp
     image_logger.hpp
     image_dispatcher.hpp
+    video_writer.hpp
     fps_estimator.hpp
     )
 
     image_grabber.cpp
     image_logger.cpp
     image_dispatcher.cpp
+    video_writer.cpp
     fps_estimator.cpp
     )
 

File src/gui/camera_window.cpp

View file
 #include "image_grabber.hpp"
 #include "image_dispatcher.hpp"
 #include "image_logger.hpp"
+#include "video_writer.hpp"
 #include <cstdlib>
 #include <cmath>
 #include <QtGui>
         msgText += "\n\n";
         msgText += errorMsg;
         QMessageBox::critical(this, msgTitle, msgText);
+        stopImageCapture();
     }
 
 
     }
 
 
+    void CameraWindow::imageLoggingError(unsigned int errorId, QString errorMsg)
+    {
+        QString msgTitle("Image Logging Error");
+        QString msgText("image logging has failed\n\nError ID: ");
+        msgText += QString::number(errorId);
+        msgText += "\n\n";
+        msgText += errorMsg;
+        QMessageBox::critical(this, msgTitle, msgText);
+        stopImageCapture();
+    }
+
+
     void CameraWindow::actionFileLoadConfigTriggered()
     {
         QString msgTitle("Development");
         cameraPtr_ = std::make_shared<Lockable<Camera>>(guid);
 
         threadPoolPtr_ = new QThreadPool(this);
+        threadPoolPtr_ -> setMaxThreadCount(MAX_THREAD_COUNT);
         newImageQueuePtr_ = std::make_shared<LockableQueue<StampedImage>>();
         logImageQueuePtr_ = std::make_shared<LockableQueue<StampedImage>>();
 
 
         startButtonPtr_ -> setEnabled(false);
         connectButtonPtr_ -> setEnabled(true);
-
     }
 
     void CameraWindow::setupImageLabels()
 
         if (logging_)
         {
+            // Wrap this with method
+            // Get video file name w/ extension based on current file format
+            // --------------------------------------------------------------------
             QString fileExtension = VIDEOFILE_EXTENSION_MAP[videoFileFormat_];
-            QString fileNameWithExt = currentVideoFileName_ + "." + fileExtension;
-            QFileInfo videoFileInfo(currentVideoFileDir_, fileNameWithExt);
-            QString fileFullPath = videoFileInfo.absoluteFilePath();
-            imageLoggerPtr_ = new ImageLogger(fileFullPath, logImageQueuePtr_);
+            QString fileName = currentVideoFileName_;
+            if (!fileExtension.isEmpty())
+            {
+                fileName +=  "." + fileExtension;
+            }
+            
+            QFileInfo videoFileInfo(currentVideoFileDir_, fileName);
+            QString fileNameFullPath = videoFileInfo.absoluteFilePath();
+            // --------------------------------------------------------------------
+
+            // Create video writer
+            std::shared_ptr<VideoWriter> vidWriterPtr = std::make_shared<VideoWriter>();
+            vidWriterPtr -> setFileName(fileNameFullPath);
+
+            imageLoggerPtr_ = new ImageLogger(vidWriterPtr, logImageQueuePtr_);
         }
 
         connect(
                 SLOT(stopImageCaptureError(unsigned int, QString))
                );
 
+        connect(
+                imageLoggerPtr_,
+                SIGNAL(imageLoggingError(unsigned int, QString)),
+                this,
+                SLOT(imageLoggingError(unsigned int, QString))
+               );
+
         threadPoolPtr_ -> start(imageGrabberPtr_);
         threadPoolPtr_ -> start(imageDispatcherPtr_);
         if (logging_)
             imageLoggerPtr_ -> releaseLock();
         }
 
+        threadPoolPtr_ -> waitForDone();
+
+        newImageQueuePtr_ -> acquireLock();
         newImageQueuePtr_ -> clear();
+        newImageQueuePtr_ -> releaseLock();
+
+        logImageQueuePtr_ -> acquireLock();
         logImageQueuePtr_ -> clear();
+        logImageQueuePtr_ -> releaseLock();
 
         startButtonPtr_ -> setText(QString("Start"));
         connectButtonPtr_ -> setEnabled(true);

File src/gui/camera_window.hpp

View file
     // BIAS forward declarations
     class ImageGrabber;
     class ImageDispatcher;
-    class ImageLogger;
-    struct StampedImage;
+    class ImageLogger; struct StampedImage;
     template <class T> class Lockable;
     template <class T> class LockableQueue;
 
-    enum ImageRotationType
-    { 
-        IMAGE_ROTATION_0=0,
-        IMAGE_ROTATION_90=90,
-        IMAGE_ROTATION_180=180,
-        IMAGE_ROTATION_270=270
-    };
-
-    enum VideoFileFormat
-    {
-        VIDEOFILE_FORMAT_AVI,
-        VIDEOFILE_FORMAT_FMF,
-        VIDEOFILE_FORMAT_UFMF
-    };
-
-
     class CameraWindow : public QMainWindow, private Ui::CameraWindow
     {
         Q_OBJECT
 
+        static const unsigned int MAX_THREAD_COUNT=10;
+
         public:
 
             CameraWindow(Guid cameraGuid, QWidget *parent=0);
             // Button callbacks
             void connectButtonClicked();
             void startButtonClicked();
+
+            // Errors
             void startImageCaptureError(unsigned int errorId, QString errorMsg);
             void stopImageCaptureError(unsigned int errorId, QString errorMsg);
+            void imageLoggingError(unsigned int errorId, QString errorMsg);
 
             // Display Timer update
             void updateDisplayOnTimer();
             void updateCameraPropertiesMenu();
             void updateCameraTriggerMenu();
 
-           
             void updatePreviewImageLabel();
             void updateCameraInfoMessage();
 
                     bool addFrameCount=true
                     );
             void resizeAllImageLabels();
-
             void updateHistogramPixmap(cv::Mat hist);
 
             void deleteMenuActions(QMenu *menuPtr, QActionGroup *actionGroupPtr=NULL);
 
             cv::Mat calcHistogram(cv::Mat mat);
 
-
     }; // class CameraWindow
 
 

File src/gui/image_logger.cpp

View file
 #include "image_logger.hpp"
+#include "basic_types.hpp"
 #include "exception.hpp"
 #include "stamped_image.hpp"
+#include "video_writer.hpp"
 #include <queue>
 #include <iostream>
 #include <opencv2/core/core.hpp>
-#include <opencv2/highgui/highgui.hpp>
 
 namespace bias
 {
     ImageLogger::ImageLogger(QObject *parent) : QObject(parent) 
     {
-        logFileName_ = QString("no_log_file_set");
         ready_ = false;
         stopped_ = true;
         frameCount_ = 0;
     }
 
     ImageLogger::ImageLogger (
-            QString logFileName,
+            std::shared_ptr<VideoWriter> videoWriterPtr,
             std::shared_ptr<LockableQueue<StampedImage>> logImageQueuePtr, 
             QObject *parent
             ) : QObject(parent)
     {
-        initialize(logFileName, logImageQueuePtr);
+        initialize(videoWriterPtr, logImageQueuePtr);
     }
 
     void ImageLogger::initialize( 
-            QString logFileName,
+            std::shared_ptr<VideoWriter> videoWriterPtr,
             std::shared_ptr<LockableQueue<StampedImage>> logImageQueuePtr 
             ) 
     {
-        logFileName_ = logFileName;
+        videoWriterPtr_ = videoWriterPtr;
         logImageQueuePtr_ = logImageQueuePtr;
         ready_ = true;
         stopped_ = true;
 
     void ImageLogger::run()
     {
-        StampedImage newStampImage;
+        StampedImage newStampedImage;
         unsigned int errorId = 0;
         bool haveNewImage = false;
         bool isFirst = true;
         bool error = false;
         bool done = false;
-        cv::VideoWriter videoWriter;
 
         if (!ready_) { return; }
 
             logImageQueuePtr_ -> acquireLock();
             if (!logImageQueuePtr_ -> empty())
             {
-                newStampImage = logImageQueuePtr_ -> front();
+                newStampedImage = logImageQueuePtr_ -> front();
                 logImageQueuePtr_ -> pop();
                 haveNewImage = true;
-                std::cout << "log   queue size = " << logImageQueuePtr_ -> size() << std::endl;
             }
+            std::cout << "log   queue size = " << logImageQueuePtr_ -> size() << std::endl;
             logImageQueuePtr_ -> releaseLock();
 
             if (haveNewImage)
             {
                 frameCount_++;
-                if (isFirst)
+
+                try 
                 {
-                    // On first frame get image size and open file.
-                    cv::Size frameSize = newStampImage.image.size();
-                    std::cout << "logFileName: " << logFileName_.toStdString() << std::endl;
-                    videoWriter.open(
-                            logFileName_.toStdString(),
-                            CV_FOURCC('D','I','V', 'X'),
-                            30.0,
-                            frameSize,false
-                            ); 
-                    isFirst = false;
+                    videoWriterPtr_ -> addFrame(newStampedImage);
                 }
-                if (videoWriter.isOpened())
+                catch (RuntimeError &runtimeError)
                 {
-                    if (frameCount_%5 == 0) 
-                    {
-                        std::cout << "writing image " << frameCount_ << std::endl << std::flush;
-                        videoWriter << newStampImage.image;
-                    }
-                }
-                else
-                {
-                    std::cout << "video file not open" << std::endl << std::flush;
+                    acquireLock();
+                    stopped_ = true;
+                    releaseLock();
+
+                    unsigned int errorId = ERROR_VIDEO_WRITER_ADD_FRAME;
+                    QString errorMsg("videoWriter addFrame failed");
+                    emit imageLoggingError(errorId, errorMsg);
                 }
             }
 
             done = stopped_;
             releaseLock();
         }
-    }
+
+        videoWriterPtr_ -> stop();
+
+    } // while (!done)
 
 } // namespace bias
 
+
+// cv videoWriter example
+// ----------------------------------------------------
+//#include <opencv2/highgui/highgui.hpp>
+//cv::VideoWriter videoWriter;
+//cv::Size frameSize = newStampedImage.image.size();
+//videoWriter.open( logFileName_.toStdString(), CV_FOURCC('D','I','V', 'X'), 30.0, frameSize, false );
+//videoWriter << newStampedImage.image;

File src/gui/image_logger.hpp

View file
 
 class QString;
 
+
 namespace bias
 {
 
+    class VideoWriter;
+
     struct StampedImage;
 
     class ImageLogger : public QObject, public QRunnable, public Lockable<Empty>
             ImageLogger(QObject *parent=0);
 
             ImageLogger(
-                    QString logFileName,
+                    std::shared_ptr<VideoWriter> videoWriterPtr,
                     std::shared_ptr<LockableQueue<StampedImage>> logImageQueuePtr, 
                     QObject *parent=0
                     );
 
             void initialize(
-                    QString logFileName,
+                    std::shared_ptr<VideoWriter> videoWriterPtr,
                     std::shared_ptr<LockableQueue<StampedImage>> logImageQueuePtr 
                     );
 
             void stop();
 
         signals:
-            void loggingError(unsigned int errorId, QString errorMsg);
+            void imageLoggingError(unsigned int errorId, QString errorMsg);
 
         private:
             bool ready_;
             bool stopped_;
             unsigned long frameCount_;
 
-            QString logFileName_;
+            std::shared_ptr<VideoWriter> videoWriterPtr_;
             std::shared_ptr<LockableQueue<StampedImage>> logImageQueuePtr_;
 
             void run();

File src/gui/video_writer.cpp

View file
+#include "video_writer.hpp"
+#include "Stamped_image.hpp"
+#include <iostream>
+
+namespace bias
+{
+    VideoWriter::VideoWriter() 
+    {
+        fileName_ = QString("filename_not_set");
+        size_ = cv::Size(0,0);
+        frameCount_ = 0;
+    }
+
+
+    VideoWriter::VideoWriter(QString fileName)
+    {
+        setFileName(fileName);
+        size_ = cv::Size(0,0);
+        frameCount_ = 0;
+    }
+
+
+    void VideoWriter::setFileName(QString fileName)
+    {
+        fileName_ = fileName;
+    }
+
+
+    void VideoWriter::setSize(cv::Size size)
+    {
+        size_ = size;
+    }
+
+
+    void VideoWriter::addFrame(StampedImage stampedImg)
+    {
+        frameCount_++;
+        std::cout << __PRETTY_FUNCTION__;
+        std::cout << ", added frame: " << frameCount_;
+        std::cout << ", w/ timeStamp: " << stampedImg.timeStamp << std::endl;
+    }
+
+
+    void VideoWriter::start()
+    {
+        frameCount_ = 0;
+        std::cout << __PRETTY_FUNCTION__;
+        std::cout << ", start, width = " << size_.width; 
+        std::cout << ", height = " << size_.height << std::endl;
+    }
+
+
+    void VideoWriter::start(cv::Size size)
+    {
+        setSize(size);
+        start();
+    }
+
+
+    void VideoWriter::start(QString fileName, cv::Size size)
+    {
+        setFileName(fileName);
+        setSize(size);
+        start();
+    }
+
+
+    void VideoWriter::stop()
+    {
+        std::cout << __PRETTY_FUNCTION__;
+        std::cout << ", stop, frame count = "<< frameCount_  << std::endl;
+    }
+
+
+} // namespace bias

File src/gui/video_writer.hpp

View file
+#ifndef BIAS_VIDEO_WRITER_HPP
+#define BIAS_VIDEO_WRITER_HPP
+#include <QString>
+#include <opencv2/core/core.hpp>
+//#include <opencv2/core/types_c.h>
+
+namespace bias
+{
+    class StampedImage;
+
+    class VideoWriter
+    {
+        public:
+
+            VideoWriter(); 
+            explicit VideoWriter(QString fileName);
+            virtual void setFileName(QString fileName);
+            virtual void setSize(cv::Size size);
+            virtual void addFrame(StampedImage stampedImg);
+            virtual void start();
+            virtual void start(cv::Size size);
+            virtual void start(QString fileName, cv::Size size);
+            virtual void stop();
+
+        private:
+
+            cv::Size size_;
+            QString fileName_;
+            unsigned long frameCount_;
+    };
+
+} // namespace bias
+
+#endif // #ifndef BIAS_VIDEO_WRITER_HPP