Ask Your Question

Using captured image instead of webcam

asked 2013-02-11 05:08:03 -0500

James gravatar image

updated 2013-02-11 05:27:08 -0500

Hi there, I'm trying to use the Haarclassifier of Open CV to detect and then classify the face using one of the routine algorithms. I'm already doing some experiments and tests, as I was testing the code from Open CV tutorials from this address:

Face Recognition in Videos with OpenCV

I was wondering how I can change the input source of detection and classification from webcam device, to an already captured image, in another word I want to give the application path of the image and then have an output with a rectangle around the face and predicted label above it, How can I change the input source in the code?

edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted

answered 2013-02-11 05:32:53 -0500

berak gravatar image

updated 2013-02-11 06:26:57 -0500

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 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( - 10, 0);
        int pos_y = std::max( - 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
edit flag offensive delete link more


Thanks in advance, but how can I save the image in a path?

James gravatar imageJames ( 2013-02-11 05:40:49 -0500 )edit

imwrite("fuihwefihuiw.png",img); // did you mean 'just save it' ?

berak gravatar imageberak ( 2013-02-11 05:42:55 -0500 )edit

yes, that's it, thank you very much, the issue is solved now

James gravatar imageJames ( 2013-02-11 05:47:29 -0500 )edit
Login/Signup to Answer

Question Tools

1 follower


Asked: 2013-02-11 05:08:03 -0500

Seen: 204 times

Last updated: Feb 11 '13