Ask Your Question
0

Using captured image instead of webcam

asked Feb 11 '13

James gravatar image

updated Feb 11 '13

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?

Preview: (hide)

1 answer

Sort by » oldest newest most voted
1

answered Feb 11 '13

berak gravatar image

updated Feb 11 '13

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(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
Preview: (hide)

Comments

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

James gravatar imageJames (Feb 11 '13)edit

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

berak gravatar imageberak (Feb 11 '13)edit

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

James gravatar imageJames (Feb 11 '13)edit

Question Tools

1 follower

Stats

Asked: Feb 11 '13

Seen: 366 times

Last updated: Feb 11 '13