공학왕이될거야

노트북 카메라 연동 얼굴 인식

전공/Opencv



#include <iostream>

#include <opencv2/opencv.hpp>


#include <stdlib.h>

#include <stdio.h>


using namespace std;

using namespace cv;



/* @ function main */

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

{


   // open the default camera

   cv::VideoCapture cap(0);


   // check if we succeeded

   if (!cap.isOpened()) {

      std::cerr << "Could not open camera" << std::endl;

      return -1;

   }


   // create a window

   cv::namedWindow("webcam", 1);


   // face detection configuration

   cv::CascadeClassifier face_classifier;


   face_classifier.load("C:/opencv/sources/data/haarcascades/haarcascade_frontalface_alt.xml");


   cv::Mat frame;


   while(1) {

      bool frame_valid = true;


      try {


         // get a new frame from webcam

         cap >> frame;


      }

      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) == 10) break;

   }


   // VideoCapture automatically deallocate camera object

   return 0;


}



'전공 > Opencv' 카테고리의 다른 글

Face-tracking Robot 졸업작품  (18) 2018.01.29
저장된 사진 띄우기  (0) 2017.07.24
ubuntu 16.04 opencv 3.2.0 설치  (2) 2017.07.21
Opencv 설치하기 / 우분투  (0) 2017.07.11
Opencv 설치하기  (1) 2017.07.07