Commits

iorodeo committed 77b4cb0

Added avi video writer.

Comments (0)

Files changed (9)

src/gui/CMakeLists.txt

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

src/gui/camera_window.cpp

 #include "image_logger.hpp"
 #include "video_writer.hpp"
 #include "video_writer_bmp.hpp"
+#include "video_writer_avi.hpp"
 #include <cstdlib>
 #include <cmath>
 #include <QtGui>
     QMap<VideoFileFormat, QString> createExtensionMap()
     {
         QMap<VideoFileFormat, QString> map;
-        map.insert(VIDEOFILE_FORMAT_BMP,  QString("bmp"));
+        map.insert(VIDEOFILE_FORMAT_BMP,  QString(""));
         map.insert(VIDEOFILE_FORMAT_AVI,  QString("avi"));
         map.insert(VIDEOFILE_FORMAT_FMF,  QString("fmf"));
         map.insert(VIDEOFILE_FORMAT_UFMF, QString("ufmf"));
         {
             currentVideoFileDir_ = defaultVideoFileDir_;
         }
-        QString extString = VIDEOFILE_EXTENSION_MAP[videoFileFormat_];
-        QString saveFileWithExt = currentVideoFileName_ + "." + extString;
-        QFileInfo videoFileInfo(currentVideoFileDir_, saveFileWithExt);
+        QString videoFileFullPath = getVideoFileFullPath();
+        QFileInfo videoFileInfo(videoFileFullPath);
 
         // Query user for desired video filename and directory
         QString videoFileString = QFileDialog::getSaveFileName(
 
         tabWidgetPtr_ -> setCurrentWidget(previewTabPtr_);
 
-        cameraPtr_ -> acquireLock();
-        Guid cameraGuid = cameraPtr_ -> getGuid();
-        cameraPtr_ -> releaseLock();
-
         QString windowTitle("BIAS Camera Window, Guid: ");
-        windowTitle += QString::fromStdString(cameraGuid.toString());
+        windowTitle += QString::fromStdString(guid.toString());
         setWindowTitle(windowTitle);
 
         updateCameraInfoMessage();
                     videoWriterPtr = std::make_shared<VideoWriter_bmp>();
                     break;
 
+                case VIDEOFILE_FORMAT_AVI:  
+                    videoWriterPtr = std::make_shared<VideoWriter_avi>();
+                    break;
+
                 default:
                     videoWriterPtr = std::make_shared<VideoWriter>();
                     break;
-
             }
 
             // Set output file
-            QString videoFileFullPath = getVideoFileFullPath();
+            QString videoFileFullPath = getVideoFileFullPathWithGuid();
             videoWriterPtr -> setFileName(videoFileFullPath);
 
             imageLoggerPtr_ = new ImageLogger(videoWriterPtr, logImageQueuePtr_);
         return videoFileFullPath;
     }
 
+    QString CameraWindow::getVideoFileFullPathWithGuid()
+    {
+        cameraPtr_ -> acquireLock();
+        Guid cameraGuid = cameraPtr_ -> getGuid();
+        cameraPtr_ -> releaseLock();
+        
+        QString fileExtension = VIDEOFILE_EXTENSION_MAP[videoFileFormat_];
+        QString fileName = currentVideoFileName_;
+        fileName += "_guid_" + QString::fromStdString(cameraGuid.toString());
+        if (!fileExtension.isEmpty())
+        {
+            fileName +=  "." + fileExtension;
+        }
+        QFileInfo videoFileInfo(currentVideoFileDir_, fileName);
+        QString videoFileFullPath = videoFileInfo.absoluteFilePath();
+        return videoFileFullPath;
+    }
+
     cv::Mat CameraWindow::calcHistogram(cv::Mat mat)
     {
         // -----------------------------------------------------------------------------

src/gui/camera_window.hpp

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

src/gui/video_writer.cpp

         }
         std::cout << std::endl;
         frameCount_++;
+    } 
+    
+    QString VideoWriter::getFileName()
+    {
+        return fileName_;
     }
 
+    cv::Size VideoWriter::getSize()
+    {
+        return size_;
+    }
+
+    unsigned int VideoWriter::getFrameSkip()
+    {
+        return frameSkip_;
+    }
+
 
 } // namespace bias

src/gui/video_writer.hpp

 #include "stamped_image.hpp"
 #include <QString>
 #include <opencv2/core/core.hpp>
-//#include <opencv2/core/types_c.h>
 
 namespace bias
 {
             virtual void setSize(cv::Size size);
             virtual void setFrameSkip(unsigned int frameSkip);
             virtual void addFrame(StampedImage stampedImg);
+            virtual QString getFileName();
+            virtual cv::Size getSize();
+            virtual unsigned int getFrameSkip();
 
         protected:
 

src/gui/video_writer_avi.cpp

+#include "video_writer_avi.hpp"
+#include "basic_types.hpp"
+#include "exception.hpp"
+#include <QFileInfo>
+#include <QDir>
+#include <iostream>
+
+namespace bias
+{ 
+    // ----------------------------------------------------------
+    // Temporary ... need to estimate this before starting video
+    const double DEFAULT_AVI_FPS = 30.0; 
+    // ----------------------------------------------------------
+
+    const unsigned int VideoWriter_avi::DEFAULT_FRAME_SKIP = 5;
+
+    VideoWriter_avi::VideoWriter_avi() : VideoWriter() 
+    {
+        isFirst_ = true;
+        setFrameSkip(DEFAULT_FRAME_SKIP);
+    }
+
+    VideoWriter_avi::VideoWriter_avi(QString fileName) : VideoWriter(fileName)
+    {
+        isFirst_ = true;
+        setFrameSkip(DEFAULT_FRAME_SKIP);
+    }
+
+    VideoWriter_avi::~VideoWriter_avi() {};
+
+    void VideoWriter_avi::addFrame(StampedImage stampedImg)
+    {
+        if (isFirst_)
+        {
+            initialize(stampedImg);
+            isFirst_ = false;
+        }
+        if (frameCount_%frameSkip_==0)
+        {
+            videoWriter_ << stampedImg.image;
+        }
+        frameCount_++;
+    }
+
+    void VideoWriter_avi::initialize(StampedImage stampedImg)
+    {
+        QFileInfo fileInfo(fileName_);
+        QString incrFileName = fileName_;
+
+        if (fileInfo.exists())
+        {
+            QDir filePath = QDir(fileInfo.absolutePath());
+            QString baseName = fileInfo.baseName();
+            QString ext = fileInfo.suffix();
+
+            unsigned int cnt = 2;
+            while(fileInfo.exists())
+            {
+                QString ver = "_v" + QString::number(cnt);
+                fileInfo = QFileInfo(filePath, baseName + ver + "." + ext);
+                incrFileName = fileInfo.absoluteFilePath();
+                cnt++;
+            }
+        }
+
+        try
+        {
+            videoWriter_.open(
+                    incrFileName.toStdString(), 
+                    CV_FOURCC('D','I','V', 'X'), 
+                    DEFAULT_AVI_FPS, 
+                    stampedImg.image.size(), 
+                    false 
+                    );
+        }
+        catch (cv::Exception &e)
+        {
+            unsigned int errorId = ERROR_VIDEO_WRITER_INITIALIZE;
+            std::string errorMsg("video writer unable to open file:\n\n"); 
+            errorMsg += e.what();
+            throw RuntimeError(errorId, errorMsg); 
+        }
+
+        if (!videoWriter_.isOpened())
+        {
+            unsigned int errorId = ERROR_VIDEO_WRITER_INITIALIZE;
+            std::string errorMsg("video writer unable to open file:\n\n"); 
+            errorMsg += "no exception thrown, but file not opened??";
+            throw RuntimeError(errorId, errorMsg); 
+        }
+        setSize(stampedImg.image.size());
+    }
+
+} // namespace bias
+

src/gui/video_writer_avi.hpp

+#ifndef BIAS_VIDEO_WRITER_AVI_HPP
+#define BIAS_VIDEO_WRITER_AVI_HPP
+
+#include "video_writer.hpp"
+#include <opencv2/highgui/highgui.hpp>
+
+namespace bias
+{
+    class VideoWriter_avi : public VideoWriter
+    {
+        public:
+
+            VideoWriter_avi();
+            explicit VideoWriter_avi(QString fileName);
+            virtual ~VideoWriter_avi();
+            virtual void addFrame(StampedImage stampedImg);
+
+            static const unsigned int DEFAULT_FRAME_SKIP;
+
+        protected:
+
+            bool isFirst_;
+            cv::VideoWriter videoWriter_;
+            void initialize(StampedImage stampedImage);
+            
+    };
+   
+}
+
+#endif // #ifndef BIAS_VIDEO_WRITER_AVI_HPP
+

src/gui/video_writer_bmp.cpp

 
 namespace bias
 {
-    const QString IMAGE_FILE_BASE = QString("image_");
-    const QString IMAGE_FILE_EXT = QString(".bmp");
-    const unsigned int DEFAULT_FRAME_SKIP = 5;
+    const QString VideoWriter_bmp::IMAGE_FILE_BASE = QString("image_");
+    const QString VideoWriter_bmp::IMAGE_FILE_EXT = QString(".bmp");
+    const unsigned int VideoWriter_bmp::DEFAULT_FRAME_SKIP = 5;
 
     VideoWriter_bmp::VideoWriter_bmp() : VideoWriter() 
     {
         isFirst_ = true;
-        frameSkip_ = DEFAULT_FRAME_SKIP;
+        setFrameSkip(DEFAULT_FRAME_SKIP);
     }
 
     VideoWriter_bmp::VideoWriter_bmp(QString fileName) : VideoWriter(fileName) 
     {
         isFirst_ = true;
-        frameSkip_ = DEFAULT_FRAME_SKIP;
+        setFrameSkip(DEFAULT_FRAME_SKIP);
     }
 
     VideoWriter_bmp::~VideoWriter_bmp() {}
                 throw RuntimeError(errorId, errorMsg);
             }
         }
+
         frameCount_++;
     }
 
             // one which doesn't a bit kludgey, but easy.
             QString baseNameTemp;
 
-            unsigned int cnt = 1;
+            unsigned int cnt = 2;
             while (logDir_.exists())
             {
-                baseNameTemp =  baseName_ + "_" + QString::number(cnt);
+                baseNameTemp =  baseName_ + "_v" + QString::number(cnt);
                 logDir_ = QDir(baseDir_.absolutePath() + "/" + baseNameTemp);
                 cnt++;
             }

src/gui/video_writer_bmp.hpp

 
 namespace bias
 {
-
     class VideoWriter_bmp : public VideoWriter
     {
-
         public:
 
             VideoWriter_bmp();
             virtual ~VideoWriter_bmp();
             virtual void addFrame(StampedImage stampedImg);
 
-        private:
+            static const QString IMAGE_FILE_BASE;
+            static const QString IMAGE_FILE_EXT;
+            static const unsigned int DEFAULT_FRAME_SKIP;
+
+        protected:
             void initialize();
             bool isFirst_;
             QDir baseDir_;
    
 }
 
-#endif // #ifndef BIAS_VIDEO_WRITER_PPM_HPP
+#endif // #ifndef BIAS_VIDEO_WRITER_BMP_HPP