Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

FaceRecognizer is there any way to use prediction with string format (instead of int).

Hello eveyone,

I would like to use receive string instead of int from prediction function.

my .csv file looks like that:

path/to/username1/username1-01.png;username1

path/to/username1/username1-02.png;username1

path/to/username2/username2-01.png;username2

path/to/username2/username2-02.png;username2

and so on

And I would like to get recognized label, for example username2 as variable, so I could write it to a text file. There is my code below, and I cannot use string format at the very bottom (string prediction = model->predict(face_resized);)

Can anyone help me with that?

> #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;
> 
> static void read_csv(const string&
> filename, vector<Mat>& images,
> vector<string>& 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));
>             labels.push_back((classlabel.c_str()));
>         }
>     } }
> 
> int main(int argc, const char *argv[])
> {     remove( "path_to_user.txt" );
> //change PATH!!!
>     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]);
>     string deviceId = string(argv[3]);
>     // These vectors hold the images and corresponding labels:
>     vector<Mat> images;
>     vector<string> labels;
>     // 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);
>     } 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:
>     int im_width = images[0].cols;
>     int im_height = images[0].rows;
>     // Create a FaceRecognizer and train it on the given images:
>     Ptr<FaceRecognizer> model = createFisherFaceRecognizer();
>     model->train(images, labels);
>     // 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);
>         // 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:
>             Mat face_resized;
>             cv::resize(face, face_resized, Size(im_width,
> im_height), 1.0, 1.0, INTER_CUBIC);
>             // Now perform the prediction, see how easy that is:
>             string prediction = model->predict(face_resized);
>         }
>     }
>     return 0; }

Cheers!