Opencv(C++)版,人脸检测

阅读: 评论:0

Opencv(C++)版,人脸检测

Opencv(C++)版,人脸检测

opencv调用官方训练好的模型进行人脸检测

> step1:脚本下载,两个文件,下载链接在百度网盘
链接:
提取码:uboo

图片: 脚本如下

> step2:编辑脚本路径

string root_dir = "D:\Downloads\opencv\sources\samples\dnn\face_detector\";dnn::Net net = dnn::readNetFromTensorflow(root_dir + "opencv_face_detector_uint8.pb", root_dir + "opencv_face_detector.pbtxt");

路径如下:(可以自己修改)

> step2:编写代码

#include<iostream>
#include<opencv2/dnn.hpp>
#include <fstream>
#include <opencv2/core/core.hpp> 
using namespace cv::dnn;
using namespace std;
using namespace cv;
int main()
{string root_dir = "D:\Downloads\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(0);              // 打开摄像头检测VideoCapture capture("D:\Downloads\face.mp4");Mat frame;char str1[10];string ch = "%";while (true){ad(frame);if (pty()){break;}Mat blob = dnn::blobFromImage(frame, 1.0, Size(300, 300), Scalar(104, 177, 123), false, false);net.setInput(blob);Mat probs = net.forward();Mat detectionMat(probs.size[2], probs.size[3], CV_32F, probs.ptr<float>());// 解析结果for (int i = 0; i < ws; i++){float confidence = detectionMat.at<float>(i, 2);if (confidence > 0.5){int x1 = static_cast<int>(detectionMat.at<float>(i, 3) * ls);int y1 = static_cast<int>(detectionMat.at<float>(i, 4) * ws);int x2 = static_cast<int>(detectionMat.at<float>(i, 5) * ls);int y2 = static_cast<int>(detectionMat.at<float>(i, 6) * ws);Rect box(x1, y1, x2 - x1, y2 - y1);rectangle(frame, box, Scalar(0, 255, 0), 2, 8, 0);sprintf_s(str1, "%.2lf", confidence * 100);putText(frame, str1 + ch, Point(x1, y1), FONT_HERSHEY_SIMPLEX, 1.0, Scalar(0, 255,0), 2, 8);}}imshow("人脸检测演示", frame);int c = waitKey(1);if (c == 27){break;}}destroyAllWindows();return 0;
}

直接运行,看结果

注意注意注意:::::必须给C++配置opencv模块,才能运行此代码

本文发布于:2024-02-01 19:39:51,感谢您对本站的认可!

本文链接:https://www.4u4v.net/it/170678759038987.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

标签:Opencv
留言与评论(共有 0 条评论)
   
验证码:

Copyright ©2019-2022 Comsenz Inc.Powered by ©

网站地图1 网站地图2 网站地图3 网站地图4 网站地图5 网站地图6 网站地图7 网站地图8 网站地图9 网站地图10 网站地图11 网站地图12 网站地图13 网站地图14 网站地图15 网站地图16 网站地图17 网站地图18 网站地图19 网站地图20 网站地图21 网站地图22/a> 网站地图23