Ask Your Question

Face recognition Opencv confidence problem

asked 2016-12-18 18:29:58 -0600

Lafi gravatar image

i'm working on face recognition Project and i have followed the tutorial in the opencv website and i have use the same dataset , but when i test my program i have strange result (the confidence value) it gives me a large value((confidence = 621034879.) also it give me negative confidence when testing a sample from the same dataset . why the Confidence have such large value and have a negative value??? here is my code : `

include "opencv2/core/core.hpp"

#include "opencv2/contrib/contrib.hpp"
    #include "opencv2/highgui/highgui.hpp"
    #include <iostream>
#include <fstream>
#include <sstream>
using namespace cv;
using namespace std;
static void read_csv(const string& filename, vector<Mat>& images, vector<int>& labels, char separator = ';') {
std::ifstream file(filename.c_str(), ifstream::in);
if (!file) {
    string error_message = "No valid input file was given, please check the given filename.";
    CV_Error(CV_StsBadArg, error_message);
string line, path, classlabel;
while (getline(file, line)) {
    stringstream liness(line);
    getline(liness, path, separator);
    getline(liness, classlabel);
    if (!path.empty() && !classlabel.empty()) {
        images.push_back(imread(path, 0));

int main(int argc, const char *argv[]) {
// Check for valid command line arguments, print usage
// if no arguments were given.

// Get the path to your CSV.
string fn_csv = "C:/Users/ASUS/Documents/Visual Studio 2013/Projects/Car_Detection/a.txt";
// These vectors hold the images and corresponding labels.
vector<Mat> images;
vector<int> labels;
// Read in the data. This can fail if no valid
// input filename is given.
try {
    read_csv(fn_csv, images, labels);
catch (cv::Exception& e) {
    cerr << "Error opening file \"" << fn_csv << "\". Reason: " << e.msg << endl;
    // nothing more we can do
// Quit if there are not enough images for this demo.
if (images.size() <= 1) {
    string error_message = "This demo needs at least 2 images to work. Please add more images to your data set!";
    CV_Error(CV_StsError, error_message);
// Get the height from the first image. We'll need this
// later in code to reshape the images to their original
// size:
int height = images[0].rows;

Mat testSample = images[images.size() - 1];
int testLabel = labels[labels.size() - 10];


Ptr<FaceRecognizer> model = createEigenFaceRecognizer();
model->train(images, labels);
// The following line predicts the label of a given
// test image:
int predictedLabel = model->predict(testSample);

double confidence = 0.0;
model->predict(testSample, predictedLabel, confidence);
string result_message = format("Label = %d / confidence = %d.", predictedLabel, confidence);
cout << result_message << endl;

return 0;
edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted

answered 2016-12-18 20:00:10 -0600

berak gravatar image

confidence is a double, so use:

format("Label = %d / confidence = %f.", predictedLabel, confidence)

in your format string.

edit flag offensive delete link more


Thank you, you save may day,

Lafi gravatar imageLafi ( 2016-12-18 20:40:24 -0600 )edit

Question Tools

1 follower


Asked: 2016-12-18 18:29:58 -0600

Seen: 470 times

Last updated: Dec 18 '16