Mercurial > hg > nsaunier > traffic-intelligence
comparison c/InputFrameListModule.cpp @ 401:b829ebdc18e6
simplified input of directories of video frames (simply use the video filename parameter to point at the directory)
| author | Nicolas Saunier <nicolas.saunier@polymtl.ca> |
|---|---|
| date | Mon, 29 Jul 2013 18:58:05 -0400 |
| parents | 7ef1071e3cc3 |
| children |
comparison
equal
deleted
inserted
replaced
| 400:7ef1071e3cc3 | 401:b829ebdc18e6 |
|---|---|
| 2 #include "utils.hpp" | 2 #include "utils.hpp" |
| 3 | 3 |
| 4 #include <fstream> | 4 #include <fstream> |
| 5 #include <ostream> | 5 #include <ostream> |
| 6 #include <iostream> | 6 #include <iostream> |
| 7 #include <algorithm> | |
| 8 | |
| 9 //#include <boost/algorithm/string.hpp> | |
| 10 #include <boost/filesystem.hpp> | |
| 7 | 11 |
| 8 #include "opencv2/core/core.hpp" | 12 #include "opencv2/core/core.hpp" |
| 9 #include "opencv2/highgui/highgui.hpp" | 13 #include "opencv2/highgui/highgui.hpp" |
| 10 | 14 |
| 11 InputFrameListModule::InputFrameListModule(const std::string& basePath, const std::string& pictureList) | 15 namespace fs = boost::filesystem; |
| 12 : mCurrentIdx(0), mInit(false), mBasePath(basePath+"/") { | |
| 13 loadFileList(pictureList); | |
| 14 } | |
| 15 InputFrameListModule::~InputFrameListModule() | |
| 16 { | |
| 17 | 16 |
| 17 InputFrameListModule::InputFrameListModule(const std::string& _dirname) | |
| 18 : mCurrentIdx(0), mInit(false), dirname(_dirname){ | |
| 19 loadImageList(); | |
| 18 } | 20 } |
| 19 | 21 |
| 22 InputFrameListModule::~InputFrameListModule(void) { } | |
| 20 | 23 |
| 24 | |
| 25 void InputFrameListModule::setFrameNumber(const unsigned int& frameNumber) { | |
| 26 if (frameNumber < filenames.size()) | |
| 27 mCurrentIdx = frameNumber; | |
| 28 else | |
| 29 mCurrentIdx = filenames.size()-1; | |
| 30 } | |
| 21 | 31 |
| 22 bool InputFrameListModule::getNextFrame(cv::Mat& mat) | 32 bool InputFrameListModule::getNextFrame(cv::Mat& mat) |
| 23 { | 33 { |
| 24 bool success = false; | 34 bool success = false; |
| 25 if(mCurrentIdx < mFileList.size()) | 35 if(mCurrentIdx < filenames.size()) { |
| 26 { | 36 mat = cv::imread(dirname+filenames[mCurrentIdx++]); |
| 27 const std::string& fileName = mBasePath+mFileList[mCurrentIdx++]; | |
| 28 mCurrentFrame = cv::imread(fileName); | |
| 29 | 37 |
| 30 if(!mCurrentFrame.empty()) | 38 if(!mat.empty()) |
| 31 success = true; | 39 success = true; |
| 32 mat = mCurrentFrame; | |
| 33 } | 40 } |
| 34 | |
| 35 | 41 |
| 36 return success; | 42 return success; |
| 37 } | 43 } |
| 38 | 44 |
| 39 unsigned int InputFrameListModule::getNbFrames() | 45 unsigned int InputFrameListModule::getNbFrames(void) { |
| 40 { | 46 return filenames.size(); |
| 41 return mFileList.size(); | |
| 42 } | 47 } |
| 43 | 48 |
| 44 void InputFrameListModule::loadFileList(const std::string& path) | 49 void InputFrameListModule::loadImageList(void) { |
| 45 { | 50 for (fs::directory_iterator iter(dirname); iter!=fs::directory_iterator(); iter++) |
| 46 std::ifstream inputFile((mBasePath+path).c_str()); | 51 filenames.push_back(iter->path().filename().string()); |
| 47 ::openCheck(inputFile, mBasePath+path, "InputFrameListModule::loadFileList"); | 52 |
| 48 std::string str; | 53 sort(filenames.begin(), filenames.end()); |
| 49 while( !inputFile.eof() ) | 54 |
| 50 { | 55 if(!filenames.empty()) { |
| 51 std::getline(inputFile, str); | 56 std::cout << dirname+filenames[0] << std::endl; |
| 52 if (str.empty()) | 57 cv::Mat tmpImg = cv::imread(dirname+filenames[0]); |
| 53 break; | 58 mSize = cv::Size(tmpImg.cols, tmpImg.rows); |
| 54 if (str.at(0) == '#' ) | 59 mInit = true; |
| 55 continue; /* comment */ | 60 } |
| 56 mFileList.push_back(str); | |
| 57 } | |
| 58 | |
| 59 if(!mFileList.empty()) | |
| 60 { | |
| 61 cv::Mat tmpImg = cv::imread(mBasePath+mFileList[0]); | |
| 62 mSize = cv::Size(tmpImg.cols, tmpImg.rows); | |
| 63 mInit = true; | |
| 64 } | |
| 65 } | 61 } |
