基于C++的Opencv人脸实时检测

基于C++的Opencv人脸检测

前言

这里我使用的Opencv4.5版本,其中是要使用连个文件:1、opencv_face_detector_uint8;2、opencv_face_detector.pbtxt,通过这个链接下载,免费不用币:https://download.csdn.net/download/Galen_xia/22003259

代码实例

void face_detector_demo() {
	std::string root_dir = "D:/opencv-4.5.1-vc14_vc15/opencv/sources/samples/dnn/face_detector/";
	dnn::Net net = dnn::readNetFromTensorflow(root_dir + "opencv_face_detector_uint8.pb",root_dir+"opencv_face_detector.pbtxt");
	VideoCapture capture("D:/opencv/1.mp4");
	Mat frame;
	while (true) {
		capture.read(frame);
		if (frame.empty()) {
			break;
		}
		Mat blob = dnn::blobFromImage(frame, 1.0, Size(300, 300), Scalar(104, 177, 123), false, false);
		net.setInput(blob);				//NCHW
		Mat probs = net.forward();		// 
		Mat res(Size(probs.size[2], probs.size[3]), CV_32F, probs.ptr<float>());
		// 解析结果
		for (int i = 0; i < res.rows;i++) {
			float confidence = res.at<float>(i, 2);
			if (confidence > 0.5) {
				int x1 = static_cast<int>(res.at<float>(i, 3)*frame.cols);
				int y1 = static_cast<int>(res.at<float>(i, 4)*frame.rows);
				int x2 = static_cast<int>(res.at<float>(i, 5)*frame.cols);
				int y2 = static_cast<int>(res.at<float>(i, 6)*frame.rows);
				Rect box(x1, y1, x2 - x1, y2 - y1);
				rectangle(frame, box, Scalar(0, 255, 0), 2, 8, 0);
			}
		}
		Mat dst;
		imshow("人脸检测演示:", frame);
		int c = waitKey(10);
		if (c == 27) {
			break;
		}
	}
}

效果展示

在这里插入图片描述

  • 3
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值