最快人脸检测:

最新版本支持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

发布了2718 篇原创文章 · 获赞 1004 · 访问量 536万+

猜你喜欢

转载自blog.csdn.net/jacke121/article/details/104705460