opcv--c++项目(二)--基于网络摄像头的人脸识别
代码中的模型是训练好的:haarcascade_frontalface_default.xml#include <opencv2/imgcodecs.hpp>#include <opencv2/highgui.hpp>#include <opencv2/imgproc.hpp>#include <opencv2/objdetect.hpp>#incl
·
代码中的模型是训练好的:haarcascade_frontalface_default.xml
#include <opencv2/imgcodecs.hpp>
#include <opencv2/highgui.hpp>
#include <opencv2/imgproc.hpp>
#include <opencv2/objdetect.hpp>
#include <iostream>
using namespace cv;
using namespace std;
/////////////// 人脸检测 //////////////////////
void main() {
VideoCapture cap(0);//无外接摄像头用0
Mat img;
while (true) {
cap.read(img);
CascadeClassifier faceCascade;/*用于对象检测的级联分类器类*/
faceCascade.load("Resources/haarcascade_frontalface_default.xml");//从文件加载分类器(已经训练好的模型)
if (faceCascade.empty()) { cout << "XML file not loaded" << endl; }//判断文件是否调用,存在
//创建矩形向量
vector<Rect> faces;
//人脸级联点检测多尺度法
faceCascade.detectMultiScale(img, faces, 1.1, 10);//在输入图像中检测不同大小的对象。检测到的对象将以矩形列表的形式返回。img/*输入*/, faces/*输出*/, 1.1/*比例因子*/, 10/*最小邻居*/
for (int i = 0; i < faces.size(); i++) {
rectangle(img, faces[i].tl()/*左上角*/, faces[i].br()/*右下角*/, Scalar(255, 0, 255), 3);//绘制矩形
}
imshow("Image", img);
waitKey(1);
}
}

魔乐社区(Modelers.cn) 是一个中立、公益的人工智能社区,提供人工智能工具、模型、数据的托管、展示与应用协同服务,为人工智能开发及爱好者搭建开放的学习交流平台。社区通过理事会方式运作,由全产业链共同建设、共同运营、共同享有,推动国产AI生态繁荣发展。
更多推荐
所有评论(0)