前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >CPU实时帧率超过100+,嵌入式端首选的人脸检测库推荐

CPU实时帧率超过100+,嵌入式端首选的人脸检测库推荐

作者头像
OpenCV学堂
发布2020-03-10 21:36:10
1.2K0
发布2020-03-10 21:36:10
举报

开源人脸库调用尝试

基于卷积神经网络的人脸检测库,实现了硬件指令加速,纯C++ SDK接口,不依赖第三方库与接口,可以独立使用,同时也提供了模型文件下载,之前的版本是caffe模型,最新版本支持pytorch。现已开源!支持最小检测人脸10x10大小

OpenCV DNN可以直接调用训练好的caffe模型文件,实现实时人脸检测,演示代码如下:

代码语言:javascript
复制
 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}

运行效果如下:

Github地址

代码语言:javascript
复制
https://github.com/ShiqiYu/libfacedetection
本文参与 腾讯云自媒体分享计划,分享自微信公众号。
原始发表:2020-03-06,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 OpenCV学堂 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
相关产品与服务
人脸识别
腾讯云神图·人脸识别(Face Recognition)基于腾讯优图强大的面部分析技术,提供包括人脸检测与分析、比对、搜索、验证、五官定位、活体检测等多种功能,为开发者和企业提供高性能高可用的人脸识别服务。 可应用于在线娱乐、在线身份认证等多种应用场景,充分满足各行业客户的人脸属性识别及用户身份确认等需求。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档