From f04f19111152ac99eef3ca6026c38062d67b2b8f Mon Sep 17 00:00:00 2001 From: Dmitriy Anisimov Date: Sat, 15 Nov 2014 13:43:59 +0300 Subject: [PATCH] minor update of ar_hmdb code --- modules/datasets/doc/datasets/ar_hmdb.rst | 4 +- .../include/opencv2/datasets/ar_hmdb.hpp | 3 +- modules/datasets/samples/ar_hmdb.cpp | 13 +- .../datasets/samples/ar_hmdb_benchmark.cpp | 116 +++++++----------- modules/datasets/src/ar_hmdb.cpp | 61 +++++---- 5 files changed, 87 insertions(+), 110 deletions(-) diff --git a/modules/datasets/doc/datasets/ar_hmdb.rst b/modules/datasets/doc/datasets/ar_hmdb.rst index dd4e297f2..289125022 100644 --- a/modules/datasets/doc/datasets/ar_hmdb.rst +++ b/modules/datasets/doc/datasets/ar_hmdb.rst @@ -10,7 +10,7 @@ _`"HMDB: A Large Human Motion Database"`: http://serre-lab.clps.brown.edu/resour 1. From link above download dataset files: hmdb51_org.rar & test_train_splits.rar. - 2. Unpack them. + 2. Unpack them. Unpack all archives from directory: hmdb51_org/ and remove them. 3. To load data run: ./opencv/build/bin/example_datasets_ar_hmdb -p=/home/user/path_to_unpacked_folders/ @@ -25,7 +25,7 @@ To run this benchmark execute: ./opencv/build/bin/example_datasets_ar_hmdb_benchmark -p=/home/user/path_to_unpacked_folders/ -(precomputed features should be unpacked in the same folder: /home/user/path_to_unpacked_folders/hmdb51_org_stips/) +(precomputed features should be unpacked in the same folder: /home/user/path_to_unpacked_folders/hmdb51_org_stips/. Also unpack all archives from directory: hmdb51_org_stips/ and remove them.) **References:** diff --git a/modules/datasets/include/opencv2/datasets/ar_hmdb.hpp b/modules/datasets/include/opencv2/datasets/ar_hmdb.hpp index 9264503ef..cd2b278b7 100644 --- a/modules/datasets/include/opencv2/datasets/ar_hmdb.hpp +++ b/modules/datasets/include/opencv2/datasets/ar_hmdb.hpp @@ -56,8 +56,9 @@ namespace datasets struct AR_hmdbObj : public Object { + int id; std::string name; - std::vector videoNames; + std::string videoName; }; class CV_EXPORTS AR_hmdb : public Dataset diff --git a/modules/datasets/samples/ar_hmdb.cpp b/modules/datasets/samples/ar_hmdb.cpp index c4b2cd55c..993964e19 100644 --- a/modules/datasets/samples/ar_hmdb.cpp +++ b/modules/datasets/samples/ar_hmdb.cpp @@ -74,15 +74,14 @@ int main(int argc, char *argv[]) // And its size. int numSplits = dataset->getNumSplits(); printf("splits number: %u\n", numSplits); + printf("train 1 size: %u\n", (unsigned int)dataset->getTrain(1).size()); + printf("test 1 size: %u\n", (unsigned int)dataset->getTest(1).size()); AR_hmdbObj *example = static_cast(dataset->getTrain(1)[0].get()); - printf("name: %s\n", example->name.c_str()); - vector &videoNames = example->videoNames; - printf("size: %u\n", (unsigned int)videoNames.size()); - for (vector::iterator it=videoNames.begin(); it!=videoNames.end(); ++it) - { - printf("%s\n", (*it).c_str()); - } + printf("first image:\n"); + printf("action id: %u\n", example->id); + printf("action: %s\n", example->name.c_str()); + printf("file: %s\n", example->videoName.c_str()); return 0; } diff --git a/modules/datasets/samples/ar_hmdb_benchmark.cpp b/modules/datasets/samples/ar_hmdb_benchmark.cpp index 39ff07c31..f9412ee74 100644 --- a/modules/datasets/samples/ar_hmdb_benchmark.cpp +++ b/modules/datasets/samples/ar_hmdb_benchmark.cpp @@ -58,23 +58,6 @@ using namespace cv::datasets; using namespace cv::flann; using namespace cv::ml; -unsigned int getNumFiles(vector< Ptr > &curr); -unsigned int getNumFiles(vector< Ptr > &curr) -{ - unsigned int numFiles = 0; - for (unsigned int i=0; i(curr[i].get()); - vector &videoNames = example->videoNames; - for (vector::iterator it=videoNames.begin(); it!=videoNames.end(); ++it) - { - numFiles++; - } - } - - return numFiles; -} - void fillData(const string &path, vector< Ptr > &curr, Index &flann_index, Mat1f &data, Mat1i &labels); void fillData(const string &path, vector< Ptr > &curr, Index &flann_index, Mat1f &data, Mat1i &labels) { @@ -87,36 +70,31 @@ void fillData(const string &path, vector< Ptr > &curr, Index &flann_inde for (unsigned int i=0; i(curr[i].get()); - vector &videoNames = example->videoNames; - for (vector::iterator it=videoNames.begin(); it!=videoNames.end(); ++it) + string featuresFullPath = path + "hmdb51_org_stips/" + example->name + "/" + example->videoName + ".txt"; + + ifstream infile(featuresFullPath.c_str()); + string line; + // skip header + for (unsigned int j=0; j<3; ++j) { - string featuresFile = *it + ".txt"; - string featuresFullPath = path + "hmdb51_org_stips/" + example->name + "/" + featuresFile; - - ifstream infile(featuresFullPath.c_str()); - string line; - // skip header - for (unsigned int j=0; j<3; ++j) - { - getline(infile, line); - } - while (getline(infile, line)) - { - // 7 skip, hog+hof: 72+90 read - vector elems; - split(line, elems, '\t'); - - for (unsigned int j=0; j elems; + split(line, elems, '\t'); + + for (unsigned int j=0; jid; + numFiles++; } } @@ -148,43 +126,35 @@ int main(int argc, char *argv[]) vector res; for (int currSplit=0; currSplit > &curr = dataset->getTrain(currSplit); - unsigned int numTrainFiles = getNumFiles(curr); unsigned int numFeatures = 0; for (unsigned int i=0; i(curr[i].get()); - vector &videoNames = example->videoNames; - for (vector::iterator it=videoNames.begin(); it!=videoNames.end(); ++it) + string featuresFullPath = path + "hmdb51_org_stips/" + example->name + "/" + example->videoName + ".txt"; + ifstream infile(featuresFullPath.c_str()); + string line; + // skip header + for (unsigned int j=0; j<3; ++j) { - string featuresFile = *it + ".txt"; - string featuresFullPath = path + "hmdb51_org_stips/" + example->name + "/" + featuresFile; + getline(infile, line); + } + while (getline(infile, line)) + { + numFeatures++; + if (currSample < sampleNum) + { + // 7 skip, hog+hof: 72+90 read + vector elems; + split(line, elems, '\t'); - ifstream infile(featuresFullPath.c_str()); - string line; - // skip header - for (unsigned int j=0; j<3; ++j) - { - getline(infile, line); - } - while (getline(infile, line)) - { - numFeatures++; - if (currSample < sampleNum) + for (unsigned int j=0; j elems; - split(line, elems, '\t'); - - for (unsigned int j=0; jgetTest(currSplit); - unsigned int numTestFiles = getNumFiles(curr); + unsigned int numTestFiles = curr.size(); Mat1f testData(numTestFiles, resultClusters); Mat1i testLabels(numTestFiles, 1); // ground true @@ -262,7 +233,6 @@ int main(int argc, char *argv[]) double accuracy = 1.0*correct/numTestFiles; printf("correctly recognized actions: %f\n", accuracy); res.push_back(accuracy); - } double accuracy = 0.0; diff --git a/modules/datasets/src/ar_hmdb.cpp b/modules/datasets/src/ar_hmdb.cpp index 23ee4f088..43a0aadb3 100644 --- a/modules/datasets/src/ar_hmdb.cpp +++ b/modules/datasets/src/ar_hmdb.cpp @@ -42,6 +42,8 @@ #include "opencv2/datasets/ar_hmdb.hpp" #include "opencv2/datasets/util.hpp" +#include + namespace cv { namespace datasets @@ -63,26 +65,9 @@ private: void loadDataset(const string &path); - void loadAction(const string &fileName, vector &train_, vector &test_); + map actionsId; }; -void AR_hmdbImp::loadAction(const string &fileName, vector &train_, vector &test_) -{ - ifstream infile(fileName.c_str()); - string video, label; - while (infile >> video >> label) - { - if ("1"==label) - { - train_.push_back(video); - } else - if ("2"==label) - { - test_.push_back(video); - } - } -} - /*AR_hmdbImp::AR_hmdbImp(const string &path, int number) { loadDataset(path, number); @@ -120,18 +105,40 @@ void AR_hmdbImp::loadDatasetSplit(const string &path, int number) getDirList(pathDataset, fileNames); for (vector::iterator it=fileNames.begin(); it!=fileNames.end(); ++it) { - Ptr currTrain(new AR_hmdbObj); - Ptr currTest(new AR_hmdbObj); - currTrain->name = *it; - currTest->name = *it; - - train.back().push_back(currTrain); - test.back().push_back(currTest); + string &action = *it; + map::iterator itId = actionsId.find(action); + int id; + if (itId == actionsId.end()) + { + actionsId.insert(make_pair(action, actionsId.size())); + id = actionsId.size(); + } else + { + id = (*itId).second; + } char tmp[2]; sprintf(tmp, "%u", number+1); - string fileName(pathSplit + currTrain->name + "_test_split" + tmp + ".txt"); - loadAction(fileName, currTrain->videoNames, currTest->videoNames); + string fileName(pathSplit + action + "_test_split" + tmp + ".txt"); + + ifstream infile(fileName.c_str()); + string video, label; + while (infile >> video >> label) + { + Ptr curr(new AR_hmdbObj); + curr->id = id; + curr->name = action; + curr->videoName = video; + + if ("1"==label) + { + train.back().push_back(curr); + } else + if ("2"==label) + { + test.back().push_back(curr); + } + } } }