Commit f04f1911 authored by Dmitriy Anisimov's avatar Dmitriy Anisimov

minor update of ar_hmdb code

parent 3929e238
......@@ -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:**
......
......@@ -56,8 +56,9 @@ namespace datasets
struct AR_hmdbObj : public Object
{
int id;
std::string name;
std::vector<std::string> videoNames;
std::string videoName;
};
class CV_EXPORTS AR_hmdb : public Dataset
......
......@@ -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<AR_hmdbObj *>(dataset->getTrain(1)[0].get());
printf("name: %s\n", example->name.c_str());
vector<string> &videoNames = example->videoNames;
printf("size: %u\n", (unsigned int)videoNames.size());
for (vector<string>::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;
}
......@@ -58,23 +58,6 @@ using namespace cv::datasets;
using namespace cv::flann;
using namespace cv::ml;
unsigned int getNumFiles(vector< Ptr<Object> > &curr);
unsigned int getNumFiles(vector< Ptr<Object> > &curr)
{
unsigned int numFiles = 0;
for (unsigned int i=0; i<curr.size(); ++i)
{
AR_hmdbObj *example = static_cast<AR_hmdbObj *>(curr[i].get());
vector<string> &videoNames = example->videoNames;
for (vector<string>::iterator it=videoNames.begin(); it!=videoNames.end(); ++it)
{
numFiles++;
}
}
return numFiles;
}
void fillData(const string &path, vector< Ptr<Object> > &curr, Index &flann_index, Mat1f &data, Mat1i &labels);
void fillData(const string &path, vector< Ptr<Object> > &curr, Index &flann_index, Mat1f &data, Mat1i &labels)
{
......@@ -87,36 +70,31 @@ void fillData(const string &path, vector< Ptr<Object> > &curr, Index &flann_inde
for (unsigned int i=0; i<curr.size(); ++i)
{
AR_hmdbObj *example = static_cast<AR_hmdbObj *>(curr[i].get());
vector<string> &videoNames = example->videoNames;
for (vector<string>::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))
{
// 7 skip, hog+hof: 72+90 read
vector<string> elems;
split(line, elems, '\t');
ifstream infile(featuresFullPath.c_str());
string line;
// skip header
for (unsigned int j=0; j<3; ++j)
for (unsigned int j=0; j<descriptorNum; ++j)
{
getline(infile, line);
sample(0, j) = (float)atof(elems[j+7].c_str());
}
while (getline(infile, line))
{
// 7 skip, hog+hof: 72+90 read
vector<string> elems;
split(line, elems, '\t');
for (unsigned int j=0; j<descriptorNum; ++j)
{
sample(0, j) = (float)atof(elems[j+7].c_str());
}
flann_index.knnSearch(sample, nresps, dists, 1, SearchParams());
data(numFiles, nresps(0, 0)) ++;
}
labels(numFiles, 0) = i;
numFiles++;
flann_index.knnSearch(sample, nresps, dists, 1, SearchParams());
data(numFiles, nresps(0, 0)) ++;
}
labels(numFiles, 0) = example->id;
numFiles++;
}
}
......@@ -148,43 +126,35 @@ int main(int argc, char *argv[])
vector<double> res;
for (int currSplit=0; currSplit<numSplits; ++currSplit)
{
Mat1f samples(sampleNum, descriptorNum);
unsigned int currSample = 0;
vector< Ptr<Object> > &curr = dataset->getTrain(currSplit);
unsigned int numTrainFiles = getNumFiles(curr);
unsigned int numFeatures = 0;
for (unsigned int i=0; i<curr.size(); ++i)
{
AR_hmdbObj *example = static_cast<AR_hmdbObj *>(curr[i].get());
vector<string> &videoNames = example->videoNames;
for (vector<string>::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))
getline(infile, line);
}
while (getline(infile, line))
{
numFeatures++;
if (currSample < sampleNum)
{
numFeatures++;
if (currSample < sampleNum)
{
// 7 skip, hog+hof: 72+90 read
vector<string> elems;
split(line, elems, '\t');
// 7 skip, hog+hof: 72+90 read
vector<string> elems;
split(line, elems, '\t');
for (unsigned int j=0; j<descriptorNum; ++j)
{
samples(currSample, j) = (float)atof(elems[j+7].c_str());
}
currSample++;
for (unsigned int j=0; j<descriptorNum; ++j)
{
samples(currSample, j) = (float)atof(elems[j+7].c_str());
}
currSample++;
}
}
}
......@@ -202,6 +172,7 @@ int main(int argc, char *argv[])
printf("resulted clusters number: %u\n", resultClusters);
unsigned int numTrainFiles = curr.size();
Mat1f trainData(numTrainFiles, resultClusters);
Mat1i trainLabels(numTrainFiles, 1);
......@@ -232,7 +203,7 @@ int main(int argc, char *argv[])
// prepare to predict
curr = dataset->getTest(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;
......
......@@ -42,6 +42,8 @@
#include "opencv2/datasets/ar_hmdb.hpp"
#include "opencv2/datasets/util.hpp"
#include <map>
namespace cv
{
namespace datasets
......@@ -63,26 +65,9 @@ private:
void loadDataset(const string &path);
void loadAction(const string &fileName, vector<string> &train_, vector<string> &test_);
map<string, int> actionsId;
};
void AR_hmdbImp::loadAction(const string &fileName, vector<string> &train_, vector<string> &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<string>::iterator it=fileNames.begin(); it!=fileNames.end(); ++it)
{
Ptr<AR_hmdbObj> currTrain(new AR_hmdbObj);
Ptr<AR_hmdbObj> currTest(new AR_hmdbObj);
currTrain->name = *it;
currTest->name = *it;
train.back().push_back(currTrain);
test.back().push_back(currTest);
string &action = *it;
map<string, int>::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<AR_hmdbObj> 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);
}
}
}
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment