pimmes111
pimmes111

Reputation: 200

OpenCV: Eigenfaces with CSV file

I'm struggling with this issue for quite some time now and maybe someone here might have a suggestion of what's going wrong. I'm trying to use the libfacerec to implement Eigenfaces in OpenCV from this site: https://github.com/bytefish/libfacerec I'm using OpenCV-2.3.1 with Visual Studio 2010

The sample code usess the orl_faces dataset from this site: http://www.cl.cam.ac.uk/research/dtg/attarchive/facedatabase.html and loads these images by using a csv file. In this file all paths to all 400 images (10 images of 40 different people) are listed and a label is attached to each persons. Both entries are seperated by a " ; ". I've added a few lines of this csv file below:

C:/Users/PIMMES/Documents/libraries/orl_faces/s1/1.pgm;0
C:/Users/PIMMES/Documents/libraries/orl_faces/s1/2.pgm;0
...
C:/Users/PIMMES/Documents/libraries/orl_faces/s2/1.pgm;1
C:/Users/PIMMES/Documents/libraries/orl_faces/s2/2.pgm;1
...
etc

I've added the piece of code below which should load the image data. This is exactly the same piece of code as listed in the main.cpp file in the /src folder from the libfacerec website:

void read_csv(const string& filename, vector<Mat>& images, vector<int>& labels, char separator = ';')
{
    std::ifstream file(filename.c_str(), ifstream::in);
    if (!file) throw std::exception();
    string line, path, classlabel;

    while (getline(file, line))
{
        stringstream liness(line);
        getline(liness, path, separator);
        getline(liness, classlabel);
        images.push_back(imread(path, 0));
        labels.push_back(atoi(classlabel.c_str()));
    }
}

int main(int argc, const char *argv[])
{
    // check for command line arguments
    if(argc != 2)
    {
        cout << "usage: " << argv[0] << " <csv.ext>" << endl;
        exit(1);
    }

    // path to your CSV
    string fn_csv = string(argv[1]);

    // images and corresponding labels
    vector<Mat> images;
    vector<int> labels;

    // read in the data
    try
    {
        read_csv(fn_csv, images, labels);
    }
    catch (exception& e)
    {
        cerr << "Error opening file \"" << fn_csv << "\"." << endl;
        exit(1);
    }

    // get width and height
    int width = images[0].cols;
    int height = images[0].rows;

    // get test instances
    Mat testSample = images[images.size() - 1];
    int testLabel = labels[labels.size() - 1];
    ...
    etc.
}

The whole project builds fine without any errors, but when I try to run a crash occurs. I went into Debug mode and figured that both vector< Mat > images and vector< int > labels (don't mind the spaces cause without them it doesn't display properly here) are still 0 which means no data is loaded. However when I print the variables height and width it show 140 for both (all images from orl_faces are 140x140 pixels)

So my question, what is going wrong? Why aren't both vectors filled while height and width are filled?

Edit: It seems that both vectors are filled correctly on my other pc (vector images [400], vector labels [400]. However the program still crashes and when running Debug I find this error:

Unhandled exception at 0x77c415de in Test.exe: 0xC0000005: Access violation writing location 0x00000000.

It is located in the mat.hpp file and when stepping through this file, a vector v shows these errors: [size] CXX0030: Error: expression cannot be evaluated [capacity CXX0030: Error: expression cannot be evaluated

Upvotes: 1

Views: 5230

Answers (1)

bytefish
bytefish

Reputation: 3797

I am pretty sure the problem is this.

You are linking against the libraries:

opencv_core231.lib
opencv_highgui231.lib
opencv_imgproc231.lib

And then you build with the Debug Configuration in Visual Studio. See the problem? If you want to do this switch to the opencv_core231d.lib libraries. BUT: The OpenCV2.3.1 superpack for some mysterious reasons doesn't come with the tbb_debug.dll, so the Debug build is going to fail. If you are using the superpack and want to use libfacerec, then activate the Release Build Configuration in Visual Studio, build & run and everything is going to work just fine.

I've written a tutorial on it, which should be easy to follow: http://www.bytefish.de/blog/opencv_visual_studio_and_libfacerec. Scroll to the very bottom to see the Eigenfaces in Windows. So you see, it actually works.

Upvotes: 1

Related Questions