> step1:脚本下载,两个文件,下载链接在百度网盘
链接:
提取码:uboo
图片: 脚本如下
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");
路径如下:(可以自己修改)
#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;
}
本文发布于:2024-02-01 19:39:51,感谢您对本站的认可!
本文链接:https://www.4u4v.net/it/170678759038987.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
留言与评论(共有 0 条评论) |