Commits

iorodeo  committed 5f0609c

Wrote bmp video writer class.

  • Participants
  • Parent commits d289188

Comments (0)

Files changed (10)

File src/facade/basic_types.hpp

 
         // Video Writer Errors
         ERROR_VIDEO_WRITER_ADD_FRAME,
+        ERROR_VIDEO_WRITER_INITIALIZE,
 
+        // Image Logger Errors
+        ERROR_IMAGE_LOGGER_MAX_QUEUE_SIZE,
+
+        
         NUMBER_OF_ERROR,
     }; 
 
 
     enum VideoFileFormat
     {
+        VIDEOFILE_FORMAT_BMP,
         VIDEOFILE_FORMAT_AVI,
         VIDEOFILE_FORMAT_FMF,
         VIDEOFILE_FORMAT_UFMF

File src/gui/CMakeLists.txt

     image_logger.hpp
     image_dispatcher.hpp
     video_writer.hpp
+    video_writer_bmp.hpp
     fps_estimator.hpp
     )
 
     image_logger.cpp
     image_dispatcher.cpp
     video_writer.cpp
+    video_writer_bmp.cpp
     fps_estimator.cpp
     )
 

File src/gui/camera_window.cpp

 #include "image_dispatcher.hpp"
 #include "image_logger.hpp"
 #include "video_writer.hpp"
+#include "video_writer_bmp.hpp"
 #include <cstdlib>
 #include <cmath>
 #include <QtGui>
     const double DEFAULT_IMAGE_DISPLAY_FREQ = 10.0;  
     const QSize PREVIEW_DUMMY_IMAGE_SIZE = QSize(320,256);
     const QSize DEFAULT_HISTOGRAM_IMAGE_SIZE = QSize(256,204);
-    const QString DEFAULT_VIDEO_FILE_NAME = QString("bias_video");
+    const QString DEFAULT_VIDEOFILE_NAME = QString("bias_video");
     QMap<VideoFileFormat, QString> createExtensionMap()
     {
         QMap<VideoFileFormat, QString> map;
+        map.insert(VIDEOFILE_FORMAT_BMP,  QString("bmp"));
         map.insert(VIDEOFILE_FORMAT_AVI,  QString("avi"));
         map.insert(VIDEOFILE_FORMAT_FMF,  QString("fmf"));
         map.insert(VIDEOFILE_FORMAT_UFMF, QString("ufmf"));
         msgText += "\n\n";
         msgText += errorMsg;
         QMessageBox::critical(this, msgTitle, msgText);
-        stopImageCapture();
     }
 
 
 
     void CameraWindow::imageLoggingError(unsigned int errorId, QString errorMsg)
     {
+        stopImageCapture();
         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();
     }
 
 
         {
             if (currentVideoFileName_.isEmpty())
             {
-                videoFileName = DEFAULT_VIDEO_FILE_NAME;
+                videoFileName = DEFAULT_VIDEOFILE_NAME;
             }
             else
             {
         // Get Format string
         QPointer<QAction> actionPtr = qobject_cast<QAction *>(sender());
         videoFileFormat_ = actionToVideoFileFormatMap_[actionPtr]; 
-
-        std::cout << "video file format: ";
-        switch (videoFileFormat_)
-        {
-            case VIDEOFILE_FORMAT_AVI:
-                std::cout << "avi";
-                break;
-            case VIDEOFILE_FORMAT_FMF:
-                std::cout << "fmf";
-                break;
-            case VIDEOFILE_FORMAT_UFMF:
-                std::cout << "ufmf";
-                break;
-            default:
-                std::cout << "unknown";
-                break;
-        }
-        std::cout << std::endl;
+        //std::cout << "video file format: "; 
+        //std::cout << VIDEOFILE_EXTENSION_MAP[videoFileFormat_].toStdString();
+        //std::cout << std::endl;
     }
 
 
 
         setDefaultVideoFileDir();
         currentVideoFileDir_ = defaultVideoFileDir_;
-        currentVideoFileName_ = DEFAULT_VIDEO_FILE_NAME;
+        currentVideoFileName_ = DEFAULT_VIDEOFILE_NAME;
 
         setupCameraMenu();
         setupLoggingMenu();
                );
 
         connect(
+                actionLoggingFormatBMPPtr_,
+                SIGNAL(triggered()),
+                this,
+                SLOT(actionLoggingFormatTriggered())
+               );
+
+        connect(
                 actionLoggingFormatAVIPtr_,
                 SIGNAL(triggered()),
                 this,
     void CameraWindow::setupLoggingMenu()
     {
         loggingFormatActionGroupPtr_ = new QActionGroup(menuLoggingFormatPtr_);
+        loggingFormatActionGroupPtr_ -> addAction(actionLoggingFormatBMPPtr_);
         loggingFormatActionGroupPtr_ -> addAction(actionLoggingFormatAVIPtr_);
         loggingFormatActionGroupPtr_ -> addAction(actionLoggingFormatFMFPtr_);
         loggingFormatActionGroupPtr_ -> addAction(actionLoggingFormatUFMFPtr_);
 
+        actionToVideoFileFormatMap_[actionLoggingFormatBMPPtr_] = VIDEOFILE_FORMAT_BMP;
         actionToVideoFileFormatMap_[actionLoggingFormatAVIPtr_] = VIDEOFILE_FORMAT_AVI;
         actionToVideoFileFormatMap_[actionLoggingFormatFMFPtr_] = VIDEOFILE_FORMAT_FMF;
         actionToVideoFileFormatMap_[actionLoggingFormatUFMFPtr_] = VIDEOFILE_FORMAT_UFMF;
                 logImageQueuePtr_
                 );
 
-        if (logging_)
-        {
-            // Wrap this with method
-            // Get video file name w/ extension based on current file format
-            // --------------------------------------------------------------------
-            QString fileExtension = VIDEOFILE_EXTENSION_MAP[videoFileFormat_];
-            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(
                 imageGrabberPtr_, 
                 SIGNAL(startCaptureError(unsigned int, QString)),
                 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_)
         {
+            // Create video writer based on video file format type
+            std::shared_ptr<VideoWriter> videoWriterPtr; 
+
+            switch (videoFileFormat_)
+            {
+                case VIDEOFILE_FORMAT_BMP:
+                    videoWriterPtr = std::make_shared<VideoWriter_bmp>();
+                    break;
+
+                default:
+                    videoWriterPtr = std::make_shared<VideoWriter>();
+                    break;
+
+            }
+
+            // Set output file
+            QString videoFileFullPath = getVideoFileFullPath();
+            videoWriterPtr -> setFileName(videoFileFullPath);
+
+            imageLoggerPtr_ = new ImageLogger(videoWriterPtr, logImageQueuePtr_);
+            connect(
+                    imageLoggerPtr_,
+                    SIGNAL(imageLoggingError(unsigned int, QString)),
+                    this,
+                    SLOT(imageLoggingError(unsigned int, QString))
+                   );
+
             threadPoolPtr_ -> start(imageLoggerPtr_);
         }
 
         captureTimeLabelPtr_ -> setText(stampString);
     }
 
+    QString CameraWindow::getVideoFileFullPath()
+    {
+        QString fileExtension = VIDEOFILE_EXTENSION_MAP[videoFileFormat_];
+        QString fileName = currentVideoFileName_;
+        if (!fileExtension.isEmpty())
+        {
+            fileName +=  "." + fileExtension;
+        }
+        QFileInfo videoFileInfo(currentVideoFileDir_, fileName);
+        QString videoFileFullPath = videoFileInfo.absoluteFilePath();
+        return videoFileFullPath;
+    }
+
     cv::Mat CameraWindow::calcHistogram(cv::Mat mat)
     {
         // -----------------------------------------------------------------------------

File src/gui/camera_window.hpp

             void setCameraInfoMessage(QString vendorName, QString modelName);
             void setMenuChildrenEnabled(QWidget *parentWidgetPtr, bool value);
             void setCaptureTimeLabel(double timeStamp);
+            QString getVideoFileFullPath();
 
             cv::Mat calcHistogram(cv::Mat mat);
 

File src/gui/camera_window.ui

      <property name="title">
       <string>Format</string>
      </property>
+     <addaction name="actionLoggingFormatBMPPtr_"/>
      <addaction name="actionLoggingFormatAVIPtr_"/>
      <addaction name="actionLoggingFormatFMFPtr_"/>
      <addaction name="actionLoggingFormatUFMFPtr_"/>
     <string>45</string>
    </property>
   </action>
+  <action name="actionLoggingFormatBMPPtr_">
+   <property name="checkable">
+    <bool>true</bool>
+   </property>
+   <property name="text">
+    <string>bmp</string>
+   </property>
+  </action>
  </widget>
  <resources/>
  <connections/>

File src/gui/image_logger.cpp

 
 namespace bias
 {
+    const unsigned int MAX_LOG_QUEUE_SIZE = 500;
+
     ImageLogger::ImageLogger(QObject *parent) : QObject(parent) 
     {
         ready_ = false;
     void ImageLogger::run()
     {
         StampedImage newStampedImage;
-        unsigned int errorId = 0;
         bool haveNewImage = false;
-        bool isFirst = true;
-        bool error = false;
         bool done = false;
+        unsigned int logQueueSize;
 
         if (!ready_) { return; }
 
                 logImageQueuePtr_ -> pop();
                 haveNewImage = true;
             }
-            std::cout << "log   queue size = " << logImageQueuePtr_ -> size() << std::endl;
+            logQueueSize =  logImageQueuePtr_ -> size();
             logImageQueuePtr_ -> releaseLock();
 
+            std::cout << "queue size: " << logQueueSize << std::endl;
+
+            if (logQueueSize > MAX_LOG_QUEUE_SIZE)
+            {
+                unsigned int errorId = ERROR_IMAGE_LOGGER_MAX_QUEUE_SIZE;
+                QString errorMsg("logger image queue has exceeded the maximum allowed size");
+                emit imageLoggingError(errorId, errorMsg);
+            }
+
+
             if (haveNewImage)
             {
                 frameCount_++;
                 }
                 catch (RuntimeError &runtimeError)
                 {
-                    acquireLock();
-                    stopped_ = true;
-                    releaseLock();
-
-                    unsigned int errorId = ERROR_VIDEO_WRITER_ADD_FRAME;
-                    QString errorMsg("videoWriter addFrame failed");
+                    unsigned int errorId = runtimeError.id();;
+                    QString errorMsg = QString::fromStdString(runtimeError.what());
                     emit imageLoggingError(errorId, errorMsg);
                 }
             }
             releaseLock();
         }
 
-        videoWriterPtr_ -> stop();
-
     } // while (!done)
 
 } // namespace bias

File src/gui/video_writer.cpp

         frameCount_ = 0;
     }
 
+    VideoWriter::~VideoWriter() {}
+
 
     void VideoWriter::setFileName(QString fileName)
     {
 
     void VideoWriter::addFrame(StampedImage stampedImg)
     {
-        frameCount_++;
         std::cout << __PRETTY_FUNCTION__;
         std::cout << ", added frame: " << frameCount_;
         std::cout << ", w/ timeStamp: " << stampedImg.timeStamp << std::endl;
+        frameCount_++;
     }
 
 
-    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

 #ifndef BIAS_VIDEO_WRITER_HPP
 #define BIAS_VIDEO_WRITER_HPP
+#include "stamped_image.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 ~VideoWriter();
             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:
+        protected:
 
             cv::Size size_;
             QString fileName_;

File src/gui/video_writer_bmp.cpp

+#include "video_writer_bmp.hpp"
+#include "basic_types.hpp"
+#include "exception.hpp"
+#include <iostream>
+#include <QFileInfo>
+#include <stdexcept>
+#include <opencv2/highgui/highgui.hpp>
+
+namespace bias
+{
+    const QString IMAGE_FILE_BASE = QString("image_");
+    const QString IMAGE_FILE_EXT = QString(".bmp");
+
+    VideoWriter_bmp::VideoWriter_bmp() : VideoWriter() 
+    {
+        isFirst_ = true;
+    }
+
+    VideoWriter_bmp::VideoWriter_bmp(QString fileName) : VideoWriter(fileName) 
+    {
+        isFirst_ = true;
+    }
+
+    VideoWriter_bmp::~VideoWriter_bmp() {}
+
+    void VideoWriter_bmp::addFrame(StampedImage stampedImg)
+    {
+        if (isFirst_)
+        {
+            initialize();
+            isFirst_= false;
+        }
+
+        QString imageFileName = IMAGE_FILE_BASE;  
+        imageFileName += QString::number(frameCount_);
+        imageFileName += IMAGE_FILE_EXT;
+        QFileInfo imageFileInfo(logDir_,imageFileName);
+        QString fullPathName = imageFileInfo.absoluteFilePath();
+        if (frameCount_%2==0) 
+        {
+            try
+            {
+                cv::imwrite(fullPathName.toStdString(), stampedImg.image);
+            }
+            catch (std::runtime_error &err)
+            {
+                unsigned int errorId = ERROR_VIDEO_WRITER_ADD_FRAME;
+                std::string errorMsg("adding frame failed - "); 
+                errorMsg += err.what();
+                throw RuntimeError(errorId, errorMsg);
+            }
+        }
+        frameCount_++;
+    }
+
+    void VideoWriter_bmp::initialize()
+    {
+        QFileInfo fileInfo(fileName_);
+        baseName_ = fileInfo.baseName();
+        baseDir_ = fileInfo.dir();
+
+        if (!baseDir_.exists())
+        {
+            unsigned int errorId = ERROR_VIDEO_WRITER_INITIALIZE;
+            std::string errorMsg("base log directory, "); 
+            errorMsg += baseDir_.absolutePath().toStdString();
+            errorMsg += std::string("does not exist");
+            throw RuntimeError(errorId, errorMsg);
+        }
+
+        logDir_ = QDir(baseDir_.absolutePath() + "/" + baseName_);
+        if (logDir_.exists()) 
+        {
+            // Log directory exists - increment baseName until we find
+            // one which doesn't a bit kludgey, but easy.
+            QString baseNameTemp;
+
+            unsigned int cnt = 1;
+            while (logDir_.exists())
+            {
+                baseNameTemp =  baseName_ + "_" + QString::number(cnt);
+                logDir_ = QDir(baseDir_.absolutePath() + "/" + baseNameTemp);
+                cnt++;
+            }
+
+            baseName_ = baseNameTemp;
+        }
+
+        if (!baseDir_.mkdir(baseName_))
+        {
+            unsigned int errorId = ERROR_VIDEO_WRITER_INITIALIZE;
+            std::string errorMsg("unable to create log directory, "); 
+            errorMsg += baseDir_.absolutePath().toStdString();
+            throw RuntimeError(errorId, errorMsg);
+        }
+
+    }
+
+} // namespace bias

File src/gui/video_writer_bmp.hpp

+#ifndef BIAS_VIDEO_WRITER_BMP_HPP
+#define BIAS_VIDEO_WRITER_BMP_HPP
+#include "video_writer.hpp"
+#include <QDir>
+#include <QString>
+#include <vector>
+
+namespace bias
+{
+
+    class VideoWriter_bmp : public VideoWriter
+    {
+
+        public:
+
+            VideoWriter_bmp();
+            explicit VideoWriter_bmp(QString fileName);
+            virtual ~VideoWriter_bmp();
+            virtual void addFrame(StampedImage stampedImg);
+
+        private:
+            void initialize();
+            bool isFirst_;
+            QDir baseDir_;
+            QDir logDir_;
+            QString baseName_;
+    };
+   
+}
+
+#endif // #ifndef BIAS_VIDEO_WRITER_PPM_HPP