#include <opencv2/core/core.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/opencv.hpp>
#include <vector>
#include <cstdio>
using namespace std;
using namespace cv;
CascadeClassifier cascade;
String haarfile = "D:/OpenCv/opencv/build/etc/haarcascades/haarcascade_frontalface_alt.xml";
int main()
{
if (!cascade.load(haarfile))
{
printf("coulud not load image...\n");
return -1;
}
Mat frame, grayImage, dstImage;
VideoCapture video(0);
if (!video.isOpened())
{
cout << "摄像头打开失败";
return -1;
}
while (1)
{
video >> frame;
dstImage = frame.clone();
cvtColor(frame, grayImage, COLOR_BGR2GRAY);
Scalar colors[] =
{
// 红橙黄绿青蓝紫
CV_RGB(255, 0, 0),
CV_RGB(255, 97, 0),
CV_RGB(255, 255, 0),
CV_RGB(0, 255, 0),
CV_RGB(0, 255, 255),
CV_RGB(0, 0, 255),
CV_RGB(160, 32, 240)
};
vector<Rect> rect;
cascade.detectMultiScale(grayImage, rect, 1.1, 3, 0); // 分类器对象调用
// 标记--在脸部画圆
for (int i = 0; i < rect.size(); i++)
{
Point center;
int radius;
center.x = cvRound((rect[i].x + rect[i].width * 0.5));
center.y = cvRound((rect[i].y + rect[i].height * 0.5));
radius = cvRound((rect[i].width + rect[i].height) * 0.25);
circle(dstImage, center, radius, colors[i % 7], 2);
}
imshow("【人脸识别detectMultiScale】", dstImage);
waitKey(10);
}
return 0;
}
调用摄像头对人脸进行识别
最新推荐文章于 2023-08-26 15:29:37 发布