|
代码可以运行,但是卡成ppt,不知是硬件问题还是代码问题,求指点
代码如下:
#include <iostream>
#include <opencv2/opencv.hpp>
#include <opencv2/dnn.hpp>
#include <Windows.h>
using namespace cv;
using namespace std;
using namespace cv::dnn;
int main()
{
//导入模型&初始化
string dir = "D:\\VS\\opencv\\sources\\samples\\dnn\\face_detector\\";
Net net = readNetFromTensorflow(dir + "opencv_face_detector_uint8.pb", dir + "opencv_face_detector.pbtxt");
VideoCapture cap("C:\\Users\\qiuzh\\Desktop\\a.mp4");
Mat image;
for(size_t i = 0; i < cap.get(CAP_PROP_FRAME_COUNT); i++)
{
cap >> image;
resize(image, image, Size(image.cols * 0.5, image.rows * 0.5));
//人脸检测
Mat blob = blobFromImage(image, 1.0, Size(300, 300), Scalar(104, 177, 123));
net.setInput(blob);
Mat prob = net.forward();
Mat result(prob.size[2], prob.size[3], CV_32F, prob.ptr<float>());
for (int i = 0; i < result.rows; i++)
{
if (result.at<float>(i, 2) > 0.5)
{
//获取人脸坐标
int x1 = static_cast<int>(result.at<float>(i, 3) * image.cols);
int y1 = static_cast<int>(result.at<float>(i, 4) * image.rows);
int x2 = static_cast<int>(result.at<float>(i, 5) * image.cols);
int y2 = static_cast<int>(result.at<float>(i, 6) * image.rows);
//画出矩形框
rectangle(image, Rect(x1, y1, x2 - x1, y2 - y1), Scalar(100, 100, 200), 4);
}
}
imshow("人脸检测", image);
waitKey(1);
}
waitKey();
return 0;
} |
上一篇: 23课作业下一篇: 第38课代码求组
|