블로그 이미지
Kanais
Researcher & Developer 퍼즐을 완성하려면 퍼즐 조각들을 하나 둘씩 맞춰나가야 한다. 인생의 퍼즐 조각들을 하나 둘씩 맞춰나가다 보면 인생이란 퍼즐도 완성되는 날이 오려나...?

calendar

1 2 3 4 5 6
7 8 9 10 11 12 13
14 15 16 17 18 19 20
21 22 23 24 25 26 27
28 29 30

Notice

04-29 03:12

Recent Post

Recent Comment

Recent Trackback

Archive

2015. 4. 20. 15:50 Computer Vision/Face Detection



개발환경


사용툴       : Visual Studio 2013

라이브러리 : openCV library 2.4.10

프로젝트    : Visual C++ console application

개발날짜    : 2015-04-08



참고 : stackoverflow -OpenCV saving video to file

        Mat 접근방법 - REAL STORY



Skin color detection 하고 난 후  후처리 작업으로  Skin color 영역을 제외한 영역은 검은색으로 만들었다.

그리고 Haar 분류기를 사용하여 얼굴 인식을 해봤다.



void FaceDetection::Combine_FaceDetection(const char* filename)

{

    int cnt = 0;

    //create the cascade classifier object used for the face detection

    CascadeClassifier face_cascade;

    //use the haarcascade_frontalface_alt.xml library

    face_cascade.load("haarcascade_frontalface_alt.xml");

    //face_cascade.load("lbpcascade_frontalface.xml");

 

    CvCapture* capture;

 

    capture = cvCaptureFromFile(filename);

 

    assert(capture != NULL);

 

    IplImage* bgr_frame;// = cvQueryFrame(capture);

 

    CvSize size = cvSize((int)cvGetCaptureProperty(capture, CV_CAP_PROP_FRAME_WIDTH), 

                         (int)cvGetCaptureProperty(capture, CV_CAP_PROP_FRAME_HEIGHT));

 

    cvNamedWindow("CV_Window", 0);

    cvResizeWindow("CV_Window", 2048, 1024);

 

    CvVideoWriter *writer = cvCreateVideoWriter("combine.avi", CV_FOURCC('D', 'I', 'V', 'X'), 15, size);

 

    while ((bgr_frame = cvQueryFrame(capture)) != NULL)

    {

       ////////////////////////////////////////////////////////////////////////////////////////////////////

       // face detection and process

       ////////////////////////////////////////////////////////////////////////////////////////////////////

       cnt++;

 

       Mat gray;

       Mat frame;

       Mat imgHSV;

       Mat imgThresholded;

       IplImage* covert_img;

       frame = cvarrToMat(bgr_frame);

       cvtColor(frame, gray, CV_BGR2GRAY);

 

       equalizeHist(gray, gray);

 

       cvtColor(frame, imgHSV, COLOR_BGR2HSV); //Convert the captured frame from BGR to HSV

 

       inRange(imgHSV, Scalar(5, 61, 167), Scalar(111, 164, 255), imgThresholded); //Threshold the image


       covert_img = Covert_Frame(bgr_frame, imgThresholded);

 

       std::vector<Rect> faces;

       face_cascade.detectMultiScale(covert_img, faces, 1.1, 2, CV_HAAR_SCALE_IMAGE | CV_HAAR_DO_CANNY_PRUNING, Size(10, 10), Size(100, 100));

              

       //draw a rectangle for all found faces in the vector array on the original image.

       for (int i = 0; i < faces.size(); i++)

       {

           // face Mosaic process on the original image

           Mosaic_process(bgr_frame, faces[i]);

 

           Point pt1(faces[i].x + faces[i].width, faces[i].y + faces[i].height);

           Point pt2(faces[i].x, faces[i].y);

 

           rectangle(frame, pt1, pt2, cvScalar(0, 255, 0, 0), 2, 8, 0);

           printf("face detection! num : %d - %d\n", faces.size(), i);

       }

       //////////////////////////////////////////////////////////////////////////////////////////////////////////////

       cvWriteFrame(writer, covert_img);

       cvShowImage("CV_Window", covert_img);

       char c = cvWaitKey(1);

       if (c == 27) break;

       printf("A number of frame : %d\n", cnt);

    }

 

    cvReleaseVideoWriter(&writer);

    cvReleaseCapture(&capture);

    cvDestroyWindow("CV_Window");

}

 

IplImage* FaceDetection::Covert_Frame(IplImage* _origin, Mat skincolor)

{

    IplImage* origin = cvCloneImage(_origin);

    uchar* skindata = 0;

    int index = 0;

    unsigned char pixel_val = 0;

 

    for (int i = 0; i < origin->height; i++){

       skindata = skincolor.ptr<uchar>(i);    

       for (int j = 0; j < origin->width; j++){

           if (skindata[j] == 0) {

               index = (j * 3) + (i * origin->widthStep);

               origin->imageData[index] = pixel_val;

               origin->imageData[index + 1] = pixel_val;

               origin->imageData[index + 2] = pixel_val;

           }

       }

    }

    return origin;

}




결과화면


생각보다 결과가 좋게 나오질 않았다.

Haar Cascade Classifier 를 사용하여 Face Detection 하는 시간을 줄여보려고 했건만...

Face Detection 이 더 안된다... 비슷하기만 해도 상관없을텐데..

그래도 분류시간은 확실히 줄었다. 7초에서 5초정도로.. (debug 모드에서..)



posted by Kanais