Computer >> 컴퓨터 >  >> 프로그램 작성 >> C++

C++를 사용하여 OpenCV에서 실시간으로 사람의 얼굴을 감지하는 방법은 무엇입니까?

<시간/>

실시간으로 얼굴을 감지하는 것은 정지 사진에서 얼굴을 감지하는 것과 유사합니다. 유일한 차이점은 실시간 얼굴 감지이며 컴퓨터의 비디오 스트림을 가져와야 합니다. 이 프로그램에서는 'VideoCapture()' 함수를 사용했습니다. 이 기능은 다른 카메라에서 비디오를 캡처하고 할당된 매트릭스에 프레임을 임시로 저장합니다. 여기에서 이 기능은 기본 카메라에서 비디오를 캡처하고 프레임을 '실시간' 매트릭스에 임시로 저장합니다.

다음 프로그램은 실시간으로 사람의 얼굴을 감지합니다 -

예시

#include<iostream>
#include<opencv2/highgui/highgui.hpp>
#include<opencv2/imgproc/imgproc.hpp>
//This header includes definition of 'rectangle()' function//
#include<opencv2/objdetect/objdetect.hpp>
//This header includes the definition of Cascade Classifier//
#include<string>
using namespace std;
using namespace cv;
int main(int argc, char** argv) {
   Mat video_stream;//Declaring a matrix hold frames from video stream//
   VideoCapture real_time(0);//capturing video from default webcam//
   namedWindow("Face Detection");//Declaring an window to show the result//
   string trained_classifier_location = "C:/opencv/sources/data/haarcascades/haarcascade_frontalface_alt.xml";//Defining the location our XML Trained Classifier in a string//
   CascadeClassifier faceDetector;//Declaring an object named 'face detector' of CascadeClassifier class//
   faceDetector.load(trained_classifier_location);//loading the XML trained classifier in the object//
   vector<Rect>faces;//Declaring a rectangular vector named faces//
   while (true) {
      faceDetector.detectMultiScale(video_stream, faces, 1.1, 4, CASCADE_SCALE_IMAGE, Size(30, 30));//Detecting the faces in 'image_with_humanfaces' matrix//
      real_time.read(video_stream);// reading frames from camera and loading them in 'video_stream' Matrix//
      for (int i = 0; i < faces.size(); i++){ //for locating the face
         Mat faceROI = video_stream(faces[i]);//Storing face in the matrix//
         int x = faces[i].x;//Getting the initial row value of face rectangle's starting point//
         int y = faces[i].y;//Getting the initial column value of face rectangle's starting point//
         int h = y + faces[i].height;//Calculating the height of the rectangle//
         int w = x + faces[i].width;//Calculating the width of the rectangle//
         rectangle(video_stream, Point(x, y), Point(w, h), Scalar(255, 0, 255), 2, 8, 0);//Drawing a rectangle using around the faces//
      }
      imshow("Face Detection", video_stream);
      //Showing the detected face//
      if (waitKey(10) == 27){ //wait time for each frame is 10 milliseconds//
         break;
      }
   }
   return 0;
}

출력

C++를 사용하여 OpenCV에서 실시간으로 사람의 얼굴을 감지하는 방법은 무엇입니까?