C++ OpenCV相片视频人脸识别统计人数
如需远程调试,可加QQ905733049由专业技术人员远程协助!
运行代码如下:
#include <iostream>
#include<opencv2/objdetect/objdetect.hpp>
#include<opencv2/highgui/highgui.hpp>
using namespace cv;
using namespace std;
CascadeClassifier faceCascade;
int num;
int main()
{
faceCascade.load("haarcascade_frontalface_alt2.xml");
Mat img = imread("a2.jpg");
Mat imgGray;
vector<Rect> faces;
if (img.empty())
{
return 1;
}
if (img.channels() == 3)
{
cvtColor(img, imgGray, CV_RGB2GRAY);
}
else
{
imgGray = img;
}
faceCascade.detectMultiScale(faces, 1.2, 6, 0, Size(0, 0));
if (faces.size() > 0)
{
for (int i = 0; i < faces.size(); i++)
{
rectangle(Point(faces[i].x, faces[i].y), Point(faces[i].x + faces[i].width, faces[i].y + faces[i].height), Scalar(0, 255, 0), 3, 8); // 框出人脸
num++;
}
}
string font = "Current number:";
string font1 = "pcs";
putText(img, font + to_string(num - 1) + font1, Point(30, 50), FONT_HERSHEY_TRIPLEX, 1.3, Scalar(26, 28, 124), 4);
imshow("FacesOfPrettyGirl", img);
waitKey(0);
return 0;
}
运行结果: