얼굴 인식 코드
OpenCV를 이용한 얼굴 인식 코드
웹캠을 사용해서 실시간으로 얼굴을 인식해 준다.
#include <opencv2/opencv.hpp>
#include <iostream>
using namespace std;
using namespace cv;
int main() {
CascadeClassifier face_classifier;
Mat frame, grayframe;
vector<Rect> faces;
VideoCapture cap(0);
if (!cap.isOpened()) {
cout << "Could not open camera" << endl;
return -1;
}
face_classifier.load("haarcascade_frontalface_alt.xml");
while (true) {
cap >> frame;
cvtColor(frame, grayframe, COLOR_BGR2GRAY);
face_classifier.detectMultiScale(
grayframe, faces, 1.1, 3, 0, Size(30, 30)
);
for (int i = 0; i < faces.size(); i++) {
Point lb(faces[i].x + faces[i].width, faces[i].y + faces[i].height);
Point tr(faces[i].x, faces[i].y);
rectangle(frame, lb, tr, Scalar(0, 255, 0), 3, 4, 0);
}
imshow("Face Detection", frame);
if (waitKey(33) == 27) break; // ESC 키로 종료
}
return 0;
}