Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

loading a model not working, crashing

Could someone help me. I am trying to load back a model i trained and saved, but i am doing something wrong somewhere. The original loading code i got from :https://books.google.co.uk/books?id=UjWoIFHcr58C&pg=PT446&lpg=PT446&dq=how+do+i+use+model-%3Eload+facerecognizer&source=bl&ots=S9i2zxkw6w&sig=LoQ6IdwdH0C6_07h1sCSFXJs8Jg&hl=en&sa=X&ved=0ahUKEwjAwZuGy9fMAhXmCcAKHVf_Boo4ChDoAQgbMAA#v=onepage&q&f=false

But i made check_labels in a vector so it compares more easily to images. I put some debug statements in various place to track down the problem. I could really use some help. I think this is the offending piece:

 if(images.size()>0) // Not sure if i can check this if we havent loaded images
        face_resized=images[images.size()-1]; 
    else face_resized=check_labels[check_labels.size()-1]; // Is this correct?
    cout << "After" << endl;
        cv::resize(face, face_resized, Size(im_width, im_height), 1.0, 1.0, INTER_CUBIC);

Code start:

#include "opencv2/core/core.hpp"
#include "opencv2/contrib/contrib.hpp"
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include "opencv2/objdetect/objdetect.hpp"

#include <iostream>
#include <fstream>
#include <sstream>

using namespace cv;
using namespace std;

double min_face_size=50;
double max_face_size=2000;

int im_width,im_height;

string saveModelPath="face-rec-model.txt";

static void read_csv(const string& filename, vector<Mat>& images, vector<int>& labels, std::map<int, string>& labelsInfo, 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,info;
while (getline(file, line)) {
    stringstream liness(line);
path.clear();classlabel.clear();info.clear();
    getline(liness, path, separator);
    getline(liness, classlabel,separator);
getline(liness, info, separator);
    if(!path.empty() && !classlabel.empty()) {
int label=atoi(classlabel.c_str());
if(!info.empty())
    labelsInfo.insert(std::make_pair(label,info));
        images.push_back(imread(path, 0));
        labels.push_back(atoi(classlabel.c_str()));
    }
}

}

 int main(int argc, const char *argv[]) {
// Check for valid command line arguments, print usage
// if no arguments were given.
if (argc != 4) {
    cout << "usage: " << argv[0] << " </path/to/haar_cascade> </path/to/csv.ext> </path/to/device id>" << endl;
    cout << "\t </path/to/haar_cascade> -- Path to the Haar Cascade for face detection." << endl;
    cout << "\t </path/to/csv.ext> -- Path to the CSV file with the face database." << endl;
    cout << "\t <device id> -- The webcam device id to grab frames from." << endl;
    exit(1);
}
// Get the path to your CSV:
string fn_haar = string(argv[1]);
string fn_csv = string(argv[2]);
int deviceId = atoi(argv[3]);
// These vectors hold the images and corresponding labels:
vector<Mat> images;
vector<int> labels;
std::map<int,string> labelsInfo;

// Here we will check if a pre-existing model exists so we dont have to train
string facerecAlgorithm = "FaceRecognizer.Fisherfaces"; // Update for other models
Ptr<FaceRecognizer>model=Algorithm::create<FaceRecognizer>(facerecAlgorithm);
vector<Mat> check_labels;
try {
cout << "Checking for previous trained model [" << saveModelPath << "]" << endl;
model->load(saveModelPath);
check_labels=model->get<Mat>("labels");
} catch(cv::Exception &e) {}
if(check_labels[0].rows<=0) {
cerr << saveModelPath << " does not exist" << endl;

// Read in the data (fails if no valid input filename is given, but you'll get an error message):
try {
    read_csv(fn_csv, images, labels,labelsInfo);
} catch (cv::Exception& e) {
    cerr << "Error opening file \"" << fn_csv << "\". Reason: " << e.msg << endl;
    // nothing more we can do
    exit(1);
}
// Get the height from the first image. We'll need this
// later in code to reshape the images to their original
// size AND we need to reshape incoming faces to this size:
im_width = images[0].cols;
im_height = images[0].rows;

//Test
int testLabel=labels[labels.size()-1];
images.pop_back();
labels.pop_back();

// Create a FaceRecognizer and train it on the given images:
// We can also use createEigenFaceRecognizer, etc.
Ptr<FaceRecognizer> model = createFisherFaceRecognizer(10,500.0);
//Ptr<FaceRecognizer> model = createEigenFaceRecognizer();
//Ptr<FaceRecognizer> model = createLBPHFaceRecognizer();
model->setLabelsInfo(labelsInfo);
cout << "Training.." << endl;
model->train(images, labels);

cout << "Saving the trained model to " << saveModelPath << endl;
model->save(saveModelPath);
} // End of training based on if saveModelPath exists
else {    
im_width = check_labels[0].cols;
im_height = check_labels[0].rows;

} 
// That's it for learning the Face Recognition model. You now
// need to create the classifier for the task of Face Detection.
// We are going to use the haar cascade you have specified in the
// command line arguments:
//
CascadeClassifier haar_cascade;
haar_cascade.load(fn_haar);
// Get a handle to the Video device:
VideoCapture cap(deviceId);
// Check if we can use this device at all:
if(!cap.isOpened()) {
    cerr << "Capture Device ID " << deviceId << "cannot be opened." << endl;
    return -1;
}
// Holds the current frame from the Video device:
Mat frame;
for(;;) {
    cap >> frame;
    // Clone the current frame:
    Mat original = frame.clone();
    // Convert the current frame to grayscale:
    Mat gray;
    cvtColor(original, gray, CV_BGR2GRAY);
    // Find the faces in the frame:
    vector< Rect_<int> > faces;
    haar_cascade.detectMultiScale(gray, faces,1.2,4,0|CV_HAAR_SCALE_IMAGE, Size(min_face_size,min_face_size),Size(max_face_size,max_face_size));
    // At this point you have the position of the faces in
    // faces. Now we'll get the faces, make a prediction and
    // annotate it in the video. Cool or what?
    for(int i = 0; i < faces.size(); i++) {
        // Process face by face:
        Rect face_i = faces[i];
        // Crop the face from the image. So simple with OpenCV C++:
        Mat face = gray(face_i);
        // Resizing the face is necessary for Eigenfaces and Fisherfaces. You can easily
        // verify this, by reading through the face recognition tutorial coming with OpenCV.
        // Resizing IS NOT NEEDED for Local Binary Patterns Histograms, so preparing the
        // input data really depends on the algorithm used.
        //
        // I strongly encourage you to play around with the algorithms. See which work best
        // in your scenario, LBPH should always be a contender for robust face recognition.
        //
        // Since I am showing the Fisherfaces algorithm here, I also show how to resize the
        // face you have just found:
    cout << "Arrived before resize" << endl;

    // 1. Is im_width/height assignment correct with non-vector check_labels
    // 2. Should we make check_labels a Vector
    // 3. We probably need to declare face_resized out of this loop 
    Mat face_resized;
    cout << "Before" << endl;
    if(images.size()>0)
        face_resized=images[images.size()-1]; 
    else face_resized=check_labels[check_labels.size()-1];
    cout << "After" << endl;
        cv::resize(face, face_resized, Size(im_width, im_height), 1.0, 1.0, INTER_CUBIC);
    cout << "Arrived after resize" << endl;
    //model->setLabelsInfo(labelsInfo);
        // Now perform the prediction, see how easy that is:
        int prediction = model->predict(face_resized);
    cout << "Arrived after prediction" << endl;
    double confidence = 0.0;
    model->predict(face_resized,prediction,confidence);
    cout << "Arrived after prediction with confidence" << endl;

    // And finally write all we've found out to the original image!
        // First of all draw a green rectangle around the detected face:
        rectangle(original, face_i, CV_RGB(0, 255,0), 1);

    // System stats

    // Clear our variables in case we get nothing 
    string box_text1 = "Unknown";
        string box_text2 = "Unknown";
        string box_text3 = "Unknown";
        string box_text4 = "Unknown";
        String box_text5 = "Unknown";
        String box_text6 = "Unknown";
        String box_text7 = "Unknown";
    String box_text8 = "Unknown";

    // Once we implement continuous collection by checking the difference 
    // between pictures we can track both known and unknown entities
    string box_text0 = format("%d known and 0 unknown entities in database",labels.size()); 
    //if((prediction==testLabel) && !model->getLabelInfo(prediction).empty())
    if(!model->getLabelInfo(prediction).empty()) {
        box_text1 = format("Prediction = %d, Name = %s, Confidence: %f", prediction,model->getLabelInfo(prediction).c_str(),confidence);
    box_text2 = "Status: Alive";
    box_text3 = "BPM: 64";
    box_text4 = "test";
    box_text5 = "Gender: Male";
    box_text6 = "test";
    box_text7 = "test";
    box_text8 = "test";
    }

        // Calculate the position for annotated text (make sure we don't
        // put illegal values in there):
        int pos_x = std::max(face_i.tl().x, 0); // Used to be x-10
        int pos_y = std::max(face_i.tl().y, 0); // USed to be y-10
        // And now put it into the image:
    putText(original, box_text0, Point(10,10),FONT_HERSHEY_PLAIN,1.0,CV_RGB(0,255,0),2.0);
        putText(original, box_text1, Point(pos_x, pos_y), FONT_HERSHEY_PLAIN, 1.0, CV_RGB(0,255,0), 2.0);
    putText(original, box_text2, Point(pos_x,pos_y+10),FONT_HERSHEY_PLAIN,1.0,CV_RGB(0,255,0),1.0);
    putText(original, box_text3, Point(pos_x,pos_y+20),FONT_HERSHEY_PLAIN,1.0,CV_RGB(0,255,0),1.0);
    putText(original, box_text4, Point(pos_x,pos_y+30),FONT_HERSHEY_PLAIN,1.0,CV_RGB(0,255,0),1.0);
    putText(original, box_text5, Point(pos_x,pos_y+40),FONT_HERSHEY_PLAIN,1.0,CV_RGB(0,255,0),1.0);
    putText(original, box_text6, Point(pos_x,pos_y+50),FONT_HERSHEY_PLAIN,1.0,CV_RGB(0,255,0),1.0);
    putText(original, box_text7, Point(pos_x,pos_y+60),FONT_HERSHEY_PLAIN,1.0,CV_RGB(0,255,0),1.0);
    }
cout << "Before imshow" << endl;
    // Show the result:
    imshow("face_recognizer", original);
    // And display it:
    char key = (char) waitKey(20);
cout << "End of loop" << endl;
    // Exit this loop on escape:
    if(key == 27)
        break;
}
return 0;

}