//視頻人臉檢測
#include "pch.h"
#include<opencv2/objdetect/objdetect.hpp>
#include<opencv2/highgui/highgui.hpp>
#include<opencv2/imgproc/imgproc.hpp>
#include <iostream>
using namespace cv;
using namespace std;
CascadeClassifier faceCascade;
int main()
{
//bool a = faceCascade.load("C:\\myProjects\\faceDetector\\faceDetector\\Face\\haarcascade_frontalface_alt2.xml");
//bool a = faceCascade.load("C:\\myProjects\\faceDetector\\faceDetector\\Face\\haarcascade_eye.xml");
bool a = faceCascade.load("C:\\myProjects\\faceDetector\\faceDetector\\Face\\haarcascade_eye_tree_eyeglasses.xml");
VideoCapture capture;
// 打開攝像頭
capture.open(0);
// capture.open("video.avi"); // 打開視頻
if (!capture.isOpened())
{
cout << "open camera failed. " << endl;
return -1;
}
Mat img, imgGray;
vector<Rect> faces;
while (1)
{
// 讀取圖像至img
capture >> img;
if (img.empty())
{
continue;
}
if (img.channels() == 3)
{
cvtColor(img, imgGray, CV_RGB2GRAY);
}
else
{
imgGray = img;
}
// 檢測人臉
faceCascade.detectMultiScale(imgGray, faces, 1.2, 6, 0, Size(0, 0));
if (faces.size() > 0)
{
for (int i = 0; i < faces.size(); i++)
{
rectangle(img, Point(faces[i].x, faces[i].y), Point(faces[i].x + faces[i].width, faces[i].y + faces[i].height), Scalar(0, 255, 0), 1, 8);
}
}
// 顯示
imshow("CamerFace", img);
// delay ms 等待按鍵退出
if (waitKey(1) > 0)
{
break;
}
}
return 0;
}
寫給自己的文章,做個記錄。
參考博客:https://blog.csdn.net/qq_30155503/article/details/79475461