loading a model not working, crashing

asked 2016-05-14 16:19:11 -0600

atv gravatar image

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 ...
(more)
edit retag flag offensive close merge delete