Commit cd345ea8 authored by berak's avatar berak

datasets: remove face dependancy

parent ab63d35d
set(the_description "datasets framework") set(the_description "datasets framework")
ocv_define_module(datasets opencv_core opencv_face opencv_ml opencv_flann opencv_text WRAP python) ocv_define_module(datasets opencv_core opencv_ml opencv_flann opencv_text WRAP python)
ocv_warnings_disable(CMAKE_CXX_FLAGS /wd4267) # flann, Win64 ocv_warnings_disable(CMAKE_CXX_FLAGS /wd4267) # flann, Win64
...@@ -41,38 +41,17 @@ ...@@ -41,38 +41,17 @@
#include "opencv2/core.hpp" #include "opencv2/core.hpp"
#include "opencv2/imgcodecs.hpp" #include "opencv2/imgcodecs.hpp"
#include "opencv2/face.hpp"
#include "opencv2/datasets/fr_lfw.hpp" #include "opencv2/datasets/fr_lfw.hpp"
#include <iostream> #include <iostream>
#include <cstdio> #include <cstdio>
#include <string> #include <string>
#include <vector> #include <vector>
#include <map>
using namespace std; using namespace std;
using namespace cv; using namespace cv;
using namespace cv::datasets; using namespace cv::datasets;
using namespace cv::face;
map<string, int> people;
int getLabel(const string &imagePath);
int getLabel(const string &imagePath)
{
size_t pos = imagePath.find('/');
string curr = imagePath.substr(0, pos);
map<string, int>::iterator it = people.find(curr);
if (people.end() == it)
{
people.insert(make_pair(curr, (int)people.size()));
it = people.find(curr);
}
return (*it).second;
}
int main(int argc, const char *argv[]) int main(int argc, const char *argv[])
{ {
...@@ -90,9 +69,8 @@ int main(int argc, const char *argv[]) ...@@ -90,9 +69,8 @@ int main(int argc, const char *argv[])
} }
string trainMethod(parser.get<string>("train")); string trainMethod(parser.get<string>("train"));
// These vectors hold the images and corresponding labels. // our trained threshold for "same":
vector<Mat> images; double threshold = 0;
vector<int> labels;
// load dataset // load dataset
Ptr<FR_lfw> dataset = FR_lfw::create(); Ptr<FR_lfw> dataset = FR_lfw::create();
...@@ -106,33 +84,26 @@ int main(int argc, const char *argv[]) ...@@ -106,33 +84,26 @@ int main(int argc, const char *argv[])
printf("train size: %u\n", (numSplits-1) * (unsigned int)dataset->getTest().size()); printf("train size: %u\n", (numSplits-1) * (unsigned int)dataset->getTest().size());
printf("test size: %u\n", (unsigned int)dataset->getTest().size()); printf("test size: %u\n", (unsigned int)dataset->getTest().size());
// 2200 pairsDevTrain, first split: correct: 373, from: 600 -> 62.1667%
Ptr<FaceRecognizer> model = createLBPHFaceRecognizer();
// 2200 pairsDevTrain, first split: correct: correct: 369, from: 600 -> 61.5%
//Ptr<FaceRecognizer> model = createEigenFaceRecognizer();
// 2200 pairsDevTrain, first split: correct: 372, from: 600 -> 62%
//Ptr<FaceRecognizer> model = createFisherFaceRecognizer();
if (trainMethod == "dev") // train on personsDevTrain.txt if (trainMethod == "dev") // train on personsDevTrain.txt
{ {
// collect average same-distances:
double avg = 0;
int count = 0;
for (unsigned int i=0; i<dataset->getTrain().size(); ++i) for (unsigned int i=0; i<dataset->getTrain().size(); ++i)
{ {
FR_lfwObj *example = static_cast<FR_lfwObj *>(dataset->getTrain()[i].get()); FR_lfwObj *example = static_cast<FR_lfwObj *>(dataset->getTrain()[i].get());
int currNum = getLabel(example->image1); Mat a = imread(path+example->image1, IMREAD_GRAYSCALE);
Mat img = imread(path+example->image1, IMREAD_GRAYSCALE); Mat b = imread(path+example->image2, IMREAD_GRAYSCALE);
images.push_back(img); double dist = norm(a,b);
labels.push_back(currNum); if (example->same)
{
currNum = getLabel(example->image2); avg += dist;
img = imread(path+example->image2, IMREAD_GRAYSCALE); count ++;
images.push_back(img); }
labels.push_back(currNum);
} }
model->train(images, labels); threshold = avg / count;
//string saveModelPath = "face-rec-model.txt";
//cout << "Saving the trained model to " << saveModelPath << endl;
//model->save(saveModelPath);
} }
vector<double> p; vector<double> p;
...@@ -140,8 +111,8 @@ int main(int argc, const char *argv[]) ...@@ -140,8 +111,8 @@ int main(int argc, const char *argv[])
{ {
if (trainMethod == "split") // train on the remaining 9 splits from pairs.txt if (trainMethod == "split") // train on the remaining 9 splits from pairs.txt
{ {
images.clear(); double avg = 0;
labels.clear(); int count = 0;
for (unsigned int j2=0; j2<numSplits; ++j2) for (unsigned int j2=0; j2<numSplits; ++j2)
{ {
if (j==j2) continue; // skip test split for training if (j==j2) continue; // skip test split for training
...@@ -150,19 +121,17 @@ int main(int argc, const char *argv[]) ...@@ -150,19 +121,17 @@ int main(int argc, const char *argv[])
for (unsigned int i=0; i<curr.size(); ++i) for (unsigned int i=0; i<curr.size(); ++i)
{ {
FR_lfwObj *example = static_cast<FR_lfwObj *>(curr[i].get()); FR_lfwObj *example = static_cast<FR_lfwObj *>(curr[i].get());
Mat a = imread(path+example->image1, IMREAD_GRAYSCALE);
int currNum = getLabel(example->image1); Mat b = imread(path+example->image2, IMREAD_GRAYSCALE);
Mat img = imread(path+example->image1, IMREAD_GRAYSCALE); double dist = norm(a,b);
images.push_back(img); if (example->same)
labels.push_back(currNum); {
avg += dist;
currNum = getLabel(example->image2); count ++;
img = imread(path+example->image2, IMREAD_GRAYSCALE); }
images.push_back(img);
labels.push_back(currNum);
} }
} }
model->train(images, labels); threshold = avg / count;
} }
unsigned int incorrect = 0, correct = 0; unsigned int incorrect = 0, correct = 0;
...@@ -171,26 +140,18 @@ int main(int argc, const char *argv[]) ...@@ -171,26 +140,18 @@ int main(int argc, const char *argv[])
{ {
FR_lfwObj *example = static_cast<FR_lfwObj *>(curr[i].get()); FR_lfwObj *example = static_cast<FR_lfwObj *>(curr[i].get());
//int currNum = getLabel(example->image1); Mat a = imread(path+example->image1, IMREAD_GRAYSCALE);
Mat img = imread(path+example->image1, IMREAD_GRAYSCALE); Mat b = imread(path+example->image2, IMREAD_GRAYSCALE);
int predictedLabel1 = model->predict(img); bool same = (norm(a,b) <= threshold);
if (same == example->same)
//currNum = getLabel(example->image2);
img = imread(path+example->image2, IMREAD_GRAYSCALE);
int predictedLabel2 = model->predict(img);
if ((predictedLabel1 == predictedLabel2 && example->same) ||
(predictedLabel1 != predictedLabel2 && !example->same))
{
correct++; correct++;
} else else
{
incorrect++; incorrect++;
}
} }
p.push_back(1.0*correct/(correct+incorrect)); p.push_back(1.0*correct/(correct+incorrect));
printf("correct: %u, from: %u -> %f\n", correct, correct+incorrect, p.back()); printf("correct: %u, from: %u -> %f\n", correct, correct+incorrect, p.back());
} }
double mu = 0.0; double mu = 0.0;
for (vector<double>::iterator it=p.begin(); it!=p.end(); ++it) for (vector<double>::iterator it=p.begin(); it!=p.end(); ++it)
{ {
......
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