libfacedetection的使用

libfacedetection的使用

github地址
该项目已于2021年9月20日更新为3.0
现在网上大部分仍是过去版本,不便于使用,因此记录一下

libfacedetection的编译

在该版本下不再需要那些链接库之类的东西,作者已经在不断优化,我们只需要编译项目得到facedetection_export.h这个文件即可。

使用cmake进行编译

我在这里使用了cmake的gui进行编译
在这里插入图片描述
选择自己合适的文件夹即可

在文件编译完成后即可得到facedetection_export.h这个头文件

在这里插入图片描述

demo的使用

添加项目

在VS中随意新建一个项目
将libfacedetection-master中的.cpp文件和.h文件添加到项目中
还有facedetection_export.h这个头文件
项目结构大体如下
在这里插入图片描述

使用demo

作者已经为我们提供了demo在example文件夹下
只需要将其添加到项目中即可
由于在使用过程中可能需要用户自定义一些图片等内容,因此将部分参数进行修改(在测试中我所使用的是camera的demo,由于我太菜,不知道main的argc该怎么使用,直接在代码中将cap.open(0),打开电脑的摄像头)

#include <stdio.h>
#include <opencv2/opencv.hpp>
#include "facedetectcnn.h"

//define the buffer size. Do not change the size!
#define DETECT_BUFFER_SIZE 0x20000

using namespace cv;

int main() //原代码 int main(int argc, char* argv[])
{
    /*
    if (argc != 2)
    {
        printf("Usage: %s <camera index>\n", argv[0]);
        return -1;
    }
    */

    int* pResults = NULL;
    //pBuffer is used in the detection functions.
    //If you call functions in multiple threads, please create one buffer for each thread!
    unsigned char* pBuffer = (unsigned char*)malloc(DETECT_BUFFER_SIZE);
    if (!pBuffer)
    {
        fprintf(stderr, "Can not alloc buffer.\n");
        return -1;
    }


    VideoCapture cap;
    Mat im;

   // 原代码形式
   /*
	if( isdigit(argv[1][0]))
    {
        cap.open(argv[1][0]-'0');
        if(! cap.isOpened())
        {
            cerr << "Cannot open the camera." << endl;
            return 0;
        }
	*/
   // 改了一下 直接用电脑自带的摄像头
   cap.open(0);
   if (!cap.isOpened())
   {
        cerr << "Cannot open the camera." << endl;
        return 0;
   }

    if (cap.isOpened())
    {
        while (true)
        {
            cap >> im;
            //cout << "Image size: " << im.rows << "X" << im.cols << endl;
            Mat image = im.clone();

            ///
            // CNN face detection 
            // Best detection rate
            //
            //!!! The input image must be a BGR one (three-channel) instead of RGB
            //!!! DO NOT RELEASE pResults !!!
            TickMeter cvtm;
            cvtm.start();

            pResults = facedetect_cnn(pBuffer, (unsigned char*)(image.ptr(0)), image.cols, image.rows, (int)image.step);

            cvtm.stop();
            printf("time = %gms\n", cvtm.getTimeMilli());

            printf("%d faces detected.\n", (pResults ? *pResults : 0));
            Mat result_image = image.clone();
            //print the detection results
            for (int i = 0; i < (pResults ? *pResults : 0); i++)
            {
                short* p = ((short*)(pResults + 1)) + 142 * i;
                int confidence = p[0];
                int x = p[1];
                int y = p[2];
                int w = p[3];
                int h = p[4];

                //show the score of the face. Its range is [0-100]
                char sScore[256];
                snprintf(sScore, 256, "%d", confidence);
                cv::putText(result_image, sScore, cv::Point(x, y - 3), cv::FONT_HERSHEY_SIMPLEX, 0.5, cv::Scalar(0, 255, 0), 1);

                //draw face rectangle
                rectangle(result_image, Rect(x, y, w, h), Scalar(0, 255, 0), 2);
                //draw five face landmarks in different colors
                cv::circle(result_image, cv::Point(p[5], p[5 + 1]), 1, cv::Scalar(255, 0, 0), 2);
                cv::circle(result_image, cv::Point(p[5 + 2], p[5 + 3]), 1, cv::Scalar(0, 0, 255), 2);
                cv::circle(result_image, cv::Point(p[5 + 4], p[5 + 5]), 1, cv::Scalar(0, 255, 0), 2);
                cv::circle(result_image, cv::Point(p[5 + 6], p[5 + 7]), 1, cv::Scalar(255, 0, 255), 2);
                cv::circle(result_image, cv::Point(p[5 + 8], p[5 + 9]), 1, cv::Scalar(0, 255, 255), 2);

                //print the result
                printf("face %d: confidence=%d, [%d, %d, %d, %d] (%d,%d) (%d,%d) (%d,%d) (%d,%d) (%d,%d)\n",
                    i, confidence, x, y, w, h,
                    p[5], p[6], p[7], p[8], p[9], p[10], p[11], p[12], p[13], p[14]);

            }
            imshow("result", result_image);

            if ((cv::waitKey(2) & 0xFF) == 'q')
                break;
        }
    }




    //release the buffer
    free(pBuffer);

    return 0;
}

运行即可

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值