最快人脸检测:

举报
风吹稻花香 发表于 2021/06/04 23:09:18 2021/06/04
4.7k+ 0 0
【摘要】   最新版本支持pytorch。现已开源!支持最小检测人脸10x10大小   OpenCV DNN可以直接调用训练好的caffe模型文件,实现实时人脸检测,演示代码如下: 1#include <opencv2/opencv.hpp> 2#include <opencv2/dnn.hpp> 3#include <iost...

最新版本支持pytorch。现已开源!支持最小检测人脸10x10大小


      OpenCV DNN可以直接调用训练好的caffe模型文件,实现实时人脸检测,演示代码如下:
      1#include <opencv2/opencv.hpp>
      2#include <opencv2/dnn.hpp>
      3#include <iostream>
      4
      5using namespace cv;
      6using namespace cv::dnn;
      7using namespace std;
      8
      9const size_t inWidth = 300;
      10const size_t inHeight = 300;
      11const double inScaleFactor = 0.007843;
      12const Scalar meanVal(104.0, 117, 123.0);
      13const float confidence = 0.5;
      14
      15int main(int argc, char** argv) {
      16 string model = "D:/projects/models/yufacedetectnet-open-v2.caffemodel";
      17 string config = "D:/projects/models/yufacedetectnet-open-v2.prototxt";
      18 // read network
      19 Net net = readNetFromCaffe(config, model);
      20 VideoCapture cap(0);
      21 Mat frame;
      22 while (true) {
      23 bool ret = cap.read(frame);
      24 if (!ret) {
      25 break;
      26 }
      27 int64 start = getTickCount();
      28 Mat input_data = blobFromImage(frame, inScaleFactor, Size(320, 240), meanVal, false, false);
      29 net.setInput(input_data);
      30
      31 // 人脸检测
      32 Mat detection = net.forward();
      33 Mat detectionMat(detection.size[2], detection.size[3], CV_32F, detection.ptr<float>());
      34
      35 // 推断时间
      36 vector<double> layersTimings;
      37 double freq = getTickFrequency() / 1000;
      38 double time = net.getPerfProfile(layersTimings) / freq;
      39
      40 ostringstream ss;
      41 for (int i = 0; i < detectionMat.rows; i++)
      42 {
      43 // 置信度 0~1之间
      44 float score = detectionMat.at<float>(i, 2);
      45 if (score > confidence)
      46 {
      47 int xLeftBottom = static_cast<int>(detectionMat.at<float>(i, 3) * frame.cols);
      48 int yLeftBottom = static_cast<int>(detectionMat.at<float>(i, 4) * frame.rows);
      49 int xRightTop = static_cast<int>(detectionMat.at<float>(i, 5) * frame.cols);
      50 int yRightTop = static_cast<int>(detectionMat.at<float>(i, 6) * frame.rows);
      51
      52 Rect object((int)xLeftBottom, (int)yLeftBottom,
      53 (int)(xRightTop - xLeftBottom),
      54 (int)(yRightTop - yLeftBottom));
      55
      56 rectangle(frame, object, Scalar(0, 255, 0));
      57
      58 ss << score;
      59 String conf(ss.str());
      60 String label = "Face: " + conf;
      61 int baseLine = 0;
      62 Size labelSize = getTextSize(label, FONT_HERSHEY_SIMPLEX, 0.5, 1, &baseLine);
      63 rectangle(frame, Rect(Point(xLeftBottom, yLeftBottom - labelSize.height),
      64 Size(labelSize.width, labelSize.height + baseLine)),
      65 Scalar(255, 255, 255), FILLED);
      66 putText(frame, label, Point(xLeftBottom, yLeftBottom),
      67 FONT_HERSHEY_SIMPLEX, 0.5, Scalar(0, 0, 0));
      68 }
      69 }
      70 float fps = getTickFrequency() / (getTickCount() - start);
      71 ss.str("");
      72 ss << "FPS: " << fps << " ; inference time: " << time << " ms";
      73 putText(frame, ss.str(), Point(20, 20), 0, 0.75, Scalar(0, 0, 255), 2, 8);
      74 imshow("dnn_face_detection", frame);
      75 if (waitKey(1) >= 0) break;
      76 }
      77 waitKey(0);
      78 return 0;
      79}
  
 

这个好像没有关键点:

https://github.com/ShiqiYu/libfacedetection

文章来源: blog.csdn.net,作者:网奇,版权归原作者所有,如需转载,请联系作者。

原文链接:blog.csdn.net/jacke121/article/details/104705460

【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

作者其他文章

评论(0

抱歉,系统识别当前为高风险访问,暂不支持该操作

    全部回复

    上滑加载中

    设置昵称

    在此一键设置昵称,即可参与社区互动!

    *长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

    *长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。