Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

oh, you can just imread() an image from disk, and use that for your prediction.

the demo code from like line 100 down(after the training) would look similar to: ( i removed phillip's nice comments for brevity ;[ )

// Load the test frame:
Mat frame = imread("myface.jpg",0); // the 0 is for: load as grayscale, saves a conversion

vector< Rect_<int> > faces;
haar_cascade.detectMultiScale(gray, faces);
for(int i = 0; i < faces.size(); i++) {
        // crop to found rect:
        Rect face_i = faces[i];
        Mat face = gray(face_i);
        Mat face_resized;
        cv::resize(face, face_resized, Size(im_width, im_height), 1.0, 1.0, INTER_CUBIC);

        // predict:
        int prediction = model->predict(face_resized);

        // visualize:
        rectangle(original, face_i, CV_RGB(0, 255,0), 1);
        string box_text = format("Prediction = %d", prediction);
        int pos_x = std::max(face_i.tl().x - 10, 0);
        int pos_y = std::max(face_i.tl().y - 10, 0);
        putText(original, box_text, Point(pos_x, pos_y), FONT_HERSHEY_PLAIN, 1.0, CV_RGB(0,255,0), 2.0);
}

imshow("face_recognizer", original);
waitKey(0); // wait forever

oh, you can just imread() an image from disk, and use that for your prediction.

the demo code from like line 100 down(after the training) would look similar to: ( i removed phillip's nice comments for brevity ;[ )

// Load the test frame:
Mat frame gray = imread("myface.jpg",0); // the 0 is for: load as grayscale, saves a conversion

vector< Rect_<int> > faces;
haar_cascade.detectMultiScale(gray, faces);
for(int i = 0; i < faces.size(); i++) {
        // crop to found rect:
        Rect face_i = faces[i];
        Mat face = gray(face_i);
        Mat face_resized;
        cv::resize(face, face_resized, Size(im_width, im_height), 1.0, 1.0, INTER_CUBIC);

        // predict:
        int prediction = model->predict(face_resized);

        // visualize:
        rectangle(original, face_i, CV_RGB(0, 255,0), 1);
        string box_text = format("Prediction = %d", prediction);
        int pos_x = std::max(face_i.tl().x - 10, 0);
        int pos_y = std::max(face_i.tl().y - 10, 0);
        putText(original, box_text, Point(pos_x, pos_y), FONT_HERSHEY_PLAIN, 1.0, CV_RGB(0,255,0), 2.0);
}

imshow("face_recognizer", original);
waitKey(0); // wait forever