Sunday, June 2, 2013

OpenCV ARM - Face Detect


Here's another application that uses opencv libraries in the Debian disto. It performs face detection using Haar cascade.


The code below continuously captures frames with a USB web-camera. (In this demo, I displayed a sample picture on a separate laptop but this will also work for actual faces). Using the input frames and a loaded Haar classifier cascade, vector of rectangles containing the detected faces is returned to the user. A bounding box is drawn to each face to show successful detection.

face_detect.py
import cv2.cv as cv

HAAR_CASCADE = "/usr/local/share/OpenCV/haarcascades/haarcascade_frontalface_default.xml"

cv.NamedWindow( "mycamera" )
capture = cv.CreateCameraCapture(0)
storage = cv.CreateMemStorage()
cascade = cv.Load(HAAR_CASCADE)

while True:
    image = cv.QueryFrame(capture)
    faces = cv.HaarDetectObjects(image, cascade, storage, 1.1, 3, cv.CV_HAAR_DO_CANNY_PRUNING, (100,100))
    for((x,y,w,h),i) in faces:
        cv.Rectangle(image, (x,y), (x+w, y+h), (0,255,0), 3)
    cv.ShowImage( "mycamera", image )
    if cv.WaitKey(5) > 0:
        break

cv.DestroyWindow( "mycamera" )

Below is the C/C++ equivalent of the above python script. (The code appears lengthy due to the comments and cleanup routines.)

face_detect.cpp
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/objdetect/objdetect.hpp>
using namespace cv;

#define HAAR_CASCADE "/usr/local/share/OpenCV/haarcascades/haarcascade_frontalface_default.xml"

int main(int argc, const char ** argv)
{
    cvNamedWindow( "mycamera" /*, CV_WINDOW_AUTOSIZE*/); // create camera window, 1=CV_WINDOW_AUTOSIZE(default,the user cannot resize the window)
    CvCapture *capture = cvCreateCameraCapture(0); // start capturing frames from camera (i.e. /dev/video0 device)
    CvMemStorage *storage = cvCreateMemStorage(); // create new memory storage
    CvHaarClassifierCascade *cascade = (CvHaarClassifierCascade *)cvLoad(HAAR_CASCADE); // load frontal face detector

    while (1){
        IplImage *image = cvQueryFrame(capture); // grab and retrieve frame
        CvSeq *faces = cvHaarDetectObjects(image, cascade, storage, 1.1, 3, CV_HAAR_DO_CANNY_PRUNING, cvSize(100,100)); // find rectangular regions that contain faces
        for(int i=0; i < faces->total; i++){
            CvRect *r = (CvRect *)cvGetSeqElem(faces, i); // get bounding rectangle of the detected face
            cvRectangle(image, cvPoint(r->x, r->y), cvPoint(r->x + r->width, r->y + r->height), cvScalar(0,255,0), 3); // draw a green rectangle
        }
        cvShowImage( "mycamera", image ); // display image within window
        if (cvWaitKey(5) > 0) // press any key to exit
            break;
    }
    // cleanups
    cvReleaseHaarClassifierCascade( &cascade ); // release cascade
    cvReleaseMemStorage( &storage ); // release memory storage
    cvReleaseCapture( &capture ); // stop capturing/reading
    cvDestroyWindow( "mycamera" ); // end camera window
    return 0;
}

3 comments:

  1. Hello!

    My name is Maria Marilyn from www.techbugs.net and I'm sending this message to send an invite for a guest posting oportunity.

    This will greatly benefit your blog in terms of promotion and search engine optimization (SEO). The link juice will even be more valuable since you are posting in a site with related theme (tech). The article to be posted will be linked to a profile wherewith you can include your tech blog's URL or any other web address you'd like to include as well as a few details about yourself.

    If you're interested, please read the guest post guidelines through this link - http://techbugs.net/write-for-us and PM me on Facebook - http://www.facebook.com/techbugs along with your article (pasted on the message or through a Word document attachment) plus a short bio for your profile.

    Thank you very much and looking forward to hearing from you soon!

    Sincerely,
    Maria Marilyn

    ReplyDelete
  2. containing the detected faces is returned to the user. A bounding box is drawn to each face to show WOW Gold
    Gold Kaufen für World of Warcraft

    ReplyDelete
  3. Hi 'yus,

    How many fps do you get?

    Regards

    ReplyDelete