最快人脸检测:

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

 

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

 


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

 

这个好像没有关键点:

https://github.com/ShiqiYu/libfacedetection

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

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

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

评论(0

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

全部回复

上滑加载中

设置昵称

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

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

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