블로그 이미지
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-28 13:51

Recent Post

Recent Comment

Recent Trackback

Archive

2015. 4. 15. 17:30 Computer Vision/Face Detection



개발환경


사용툴       : Visual Studio 2013

라이브러리 : openCV library 2.4.10

프로젝트    : Visual C++ console application

개발날짜    : 2015-04-15


출처 및 참고 : MARE's Computer Vision Study




void FaceDetection::Usinggpu_FaceDetection(const char* filename)

{ 

        //load xml file 

        string trainface = "haarcascade_frontalface_alt.xml";

        //declaration 

        CascadeClassifier ada_cpu;

        gpu::CascadeClassifier_GPU ada_gpu;

        CvCapture* capture;

        capture = cvCaptureFromFile(filename);


        assert(capture != NULL);

 

        if (!(ada_cpu.load(trainface)))

        {

               printf(" cpu ada xml load fail! \n");

               return;

        }

        if (!(ada_gpu.load(trainface)))

        {

               printf(" gpu ada xml load fail! \n");

               return;

        }

        IplImage* bgr_frame;

 

        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);

 

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

        {

           Mat gray;

           Mat frame;

           frame = cvarrToMat(bgr_frame);

           cvtColor(frame, gray, CV_BGR2GRAY);

 

           equalizeHist(gray, gray);

 

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

           //cpu case face detection code 

           vector< Rect > faces;

           ada_cpu.detectMultiScale(gray, faces);

           if (faces.size() >= 1)

           {

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

               {

                 rectangle(frame, faces[ji], CV_RGB(0, 0, 255), CV_FILLED, 4);

               }

           }

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

           //gpu case face detection code 

           

           gpu::GpuMat faceBuf_gpu;

           gpu::GpuMat GpuImg;

           GpuImg.upload(gray);

          int detectionNumber = ada_gpu.detectMultiScale(GpuImg, faceBuf_gpu);

           Mat faces_downloaded;

           if (detectionNumber >= 1)

           {

          faceBuf_gpu.colRange(0, detectionNumber).download(faces_downloaded);

           Rect* faces = faces_downloaded.ptr< Rect>();

 

           for (int ji = 0; ji < detectionNumber; ++ji)

           {

               rectangle(frame, Point(faces[ji].x, faces[ji].y), Point(faces[ji].x + faces[ji].width, 

                         faces[ji].y + faces[ji].height), CV_RGB(255, 0, 0), 2);

           }

        }

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

           //result display

           imshow("CV_Window", frame);

           waitKey(1);

        }

        cvReleaseImage(&bgr_frame);

        //cvReleaseVideoWriter(&writer);

        cvReleaseCapture(&capture);

        cvDestroyWindow("CV_Window");

}



결과화면

얼굴인식 안된 부분은 개인 프라이버시를 위해 별도로 모자이크 처리함.



posted by Kanais