Fogeaters, Light The World.

24

2016-Mar

[OpenCV] 3.x - 캠 사용 & 얼굴 인식

작성자: title: MoonBlonix IP ADRESS: *.148.87.98 조회 수: 1683

출처 :: http://thinkpiece.tistory.com/244

OpenCV에서 제공하는 함수들 call만 하면 되니까 쉽고 간단하다.


/*
*   @file ocv_cam_face.cc
*   @brief Face detection using OpenCV with Webcam
*   @author http://thinkpiece.tistory.com
*/

#include "opencv/cv.h"
#include "opencv/highgui.h"
#include <iostream>

int main(int argc, char *argv[])
{

    // -------------------------------------------------------------------------
    // webcam routine
    cv::VideoCapture capture(0);

    if( !capture.isOpened() ) {
        std::cerr << "Could not open camera" << std::endl;
        return 0;
    }

    // create a window
    cv::namedWindow("webcam",1);

    // -------------------------------------------------------------------------
    // face detection configuration
    cv::CascadeClassifier face_classifier;
    face_classifier.load("/usr/local/Cellar/opencv/2.4.3/share/OpenCV/haarcascades/haarcascade_frontalface_default.xml");

    while (true) {
        bool frame_valid = true;

        cv::Mat frame_original;
        cv::Mat frame;

        try {
            capture >> frame_original; // get a new frame from webcam
            cv::resize(frame_original,frame,cv::Size(frame_original.cols/2,
                frame_original.rows/2),0,0,CV_INTER_NN); // downsample 1/2x
        } catch(cv::Exception& e) {
            std::cerr << "Exception occurred. Ignoring frame... " << e.err
                      << std::endl;
            frame_valid = false;
        }

        if (frame_valid) {
            try {
                // convert captured frame to gray scale & equalize
                cv::Mat grayframe;
                cv::cvtColor(frame, grayframe, CV_BGR2GRAY);
                cv::equalizeHist(grayframe,grayframe);

                // -------------------------------------------------------------
                // face detection routine

                // a vector array to store the face found
                std::vector<cv::Rect> faces;

                face_classifier.detectMultiScale(grayframe, faces, 
                    1.1, // increase search scale by 10% each pass
                    3,   // merge groups of three detections
                    CV_HAAR_FIND_BIGGEST_OBJECT|CV_HAAR_SCALE_IMAGE,
                    cv::Size(30,30));

                // -------------------------------------------------------------
                // draw the results
                for(int i=0; i<faces.size(); i++) {
                    cv::Point lb(faces[i].x + faces[i].width, 
                                 faces[i].y + faces[i].height);
                    cv::Point tr(faces[i].x, faces[i].y);

                    cv::rectangle(frame, lb, tr, cv::Scalar(0,255,0), 3, 4, 0); 
                }

                // print the output
                cv::imshow("webcam", frame);

            } catch(cv::Exception& e) {
                std::cerr << "Exception occurred. Ignoring frame... " << e.err
                          << std::endl;
            }
        }
        if (cv::waitKey(30) >= 0) break;
    }

    // VideoCapture automatically deallocate camera object
    return 0;
}


OpenCV에서 여러가지 템플릿을 미리 제공하고 있기 때문에 사용자가 직접 training하지 않더라도 사용할 수 있다. 나는 haarcascade_frontalface_default.xml을 사용해보았는데 잘된다. 정면 얼굴로는 이외에도 alt, alt2, tree 등등의 이름이 붙어있는 몇가지 버젼이 더 제공된다.

profile
List of Articles
번호 제목 글쓴이 날짜 조회 수
공지 [Web] 클라우드 IDE + 2 title: MoonBlonix 2017-06-25 15131
72 [VS] 다중 프로젝트 dll로 연결하기 title: MoonBlonix 2016-11-13 1627
71 [VS] DllMain 에서 DLL 호출하기 title: MoonBlonix 2016-11-13 1593
70 [Win API] 유니코드, 멀티바이트, TCHAR 문자열함수 title: MoonBlonix 2016-11-08 1459
69 [AVR] LED 입출력을 제어해보자 - 1 file title: Zwei츠바이 2016-09-16 1603
68 [알고리즘] 문자열 검색 title: MoonBlonix 2016-04-17 1702
67 [AVR] ATTiny13A 그리고 ADC에 대해 + 2 title: MoonBlonix 2016-04-01 1547
66 [OpenCV] 32bit(x86) 빌드 및 초기설정(GPU, TBB, IPP 등) title: MoonBlonix 2016-03-29 1545
65 [OpenCV] Mat 픽셀 접근방법 title: MoonBlonix 2016-03-26 1678
64 [OpenCV] Mat 구조를 Tesseract 에서 쓸 수 있게 title: MoonBlonix 2016-03-24 1455
63 [영상처리] Bitmap 구조 분석 title: MoonBlonix 2016-03-24 1706
» [OpenCV] 3.x - 캠 사용 & 얼굴 인식 title: MoonBlonix 2016-03-24 1683
61 [tesseract] (3.0.4) vs2013으로 빌드하기 title: MoonBlonix 2016-03-23 1607
60 [OpenCV] 3.1 설치하기 file title: MoonBlonix 2016-03-17 1427
59 AVR / ARM / DSP 비교 file + 2 title: MoonBlonix 2016-03-12 1738
58 (작성중)[OpenCV/ARM/DSP] 임베디드 환경에서의 OpenCV 사용 title: MoonBlonix 2016-03-12 1605
57 [OpenCV] 예제 코드 모음 file title: MoonBlonix 2016-03-12 1793
56 [OpenCV] 마커 추출 file title: MoonBlonix 2016-03-12 1407
55 [OpenCV] 영상 이진화 & 레이블링(Blob Labeling) file + 3 title: MoonBlonix 2016-03-12 1537
54 [OpenCV] 문자 인식 file + 1 title: MoonBlonix 2016-03-12 1468
53 [AVR] ATTiny13A 에 대한 숨겨진 사실들 title: MoonBlonix 2016-03-10 1630