MasteringOpenCV / code

Code for the book "Mastering OpenCV with Practical Computer Vision Projects" by Packt Publishing 2012.
Other
2.71k stars 1.64k forks source link

Chapter 8: loading files #67

Closed orgicus closed 8 years ago

orgicus commented 8 years ago

Hi,

I am going through the "Finishing touches: Saving and loading files" part of Chapter 8. It's a very well explained and easy to follow chapter overall, however I'm a bit stuck on this part:

You may also want to save the array of preprocessed faces and labels, if you will want to add more data to the training set later.

That is exactly what I'm trying.

I've added the .yml save/load as suggested:

if (keypress == 's'){
            cout << "saving model to disk" << endl;
            model->save("faceRecModel.yml");
        }
        if (keypress == 'l'){
            cout << "loading faceRecModel.yml" << endl;
            string facerecAlgorithm = "FaceRecognizer.Fisherfaces";
            model = Algorithm::create<FaceRecognizer>(facerecAlgorithm);
            Mat labels;
            try {

                model->load("faceRecModel.yml");
                labels = model->get<Mat>("labels");

                cout << "loaded faceRecModel.yml -> " << labels.rows << "labels read" << endl;

            } catch (cv::Exception &e) {

                cout << &e << endl;

                if (labels.rows <= 0) {
                    cerr << "ERROR: Couldn't load trained data from [faceRecModel.yml]!" << endl;
                    exit(1);
                }
            }
        }

I can see the model is loaded, although it has one label when I've trained two faces. The main issue is I can't easily go to training mode when restarting the program and loading the .yml model file. I would need to save the previous training session (preprocessed faces and labels) to continue.

Any tips/hints that will help implement this would be very helpful.

Thank you, George

Off topic: I could use a hand with Chapter 4 as well.

shervinemami commented 8 years ago

Yes it sounds like you need to save all the preprocessed faces & labels to disk, then reload them. You probably need to do it by saving each image to a PGM or PNG file using a filename scheme you come up with, and perhaps save the labels either as text or YML file, and save some info such as the number of faces and labels, etc.

orgicus commented 8 years ago

Thank you very much for the suggestions. I will start implementing. I may get back in case I get stuck but so far opencv docs make sense

shervinemami commented 8 years ago

No worries, closing this bug for now but you can re-open it if needed.