【人脸检测】libfacedetection demo

本文介绍了如何在WindowsVisualStudio2022环境中,通过NuGet安装OpenCV4.2库,配置CMake并集成libfacedetection进行人脸检测,展示了使用示例代码以及AVX2和OpenMP在高性能计算中的应用。
摘要由CSDN通过智能技术生成

人脸检测

平台: win vs2022

依赖 opencv [libfacedetection](GitHub - ShiqiYu/libfacedetection: An open source library for face detection in images. The face detection speed can reach 1000FPS.)

安装visual studio 2022和CMake

略,请自行百度。

使用nuget 安装opencv 4.2

创建一个空白项目,简单配置。

image-20240427150628774

image-20240427150746350

通过nuget 安装opencv 4.2

点击项目=》管理NuGet 程序包,在《浏览》界面,输入 opencv4.2,点击安装。

image-20240427150913123

image-20240427151030746

正在尝试收集与目标为“native,Version=v0.0”的项目“face-demo”有关的包“opencv4.2.2020.5.26”的依赖项信息
收集依赖项信息花费时间 42 毫秒
正在尝试解析程序包“opencv4.2.2020.5.26”的依赖项,DependencyBehavior 为“Lowest”
解析依赖项信息花费时间 0 毫秒
正在解析操作以安装程序包“opencv4.2.2020.5.26”
已解析操作以安装程序包“opencv4.2.2020.5.26”
正在将程序包“opencv4.2.2020.5.26”添加到文件夹“D:\code\face-demo\packages”
已将程序包“opencv4.2.2020.5.26”添加到文件夹“D:\code\face-demo\packages”
已将程序包“opencv4.2.2020.5.26”添加到“packages.config”
已将“opencv4.2 2020.5.26”成功安装到 face-demo
执行 nuget 操作花费时间 2.43 秒
已用时间: 00:00:02.9518685
========== 已完成 ==========


跑个opencv demo 试试

code

展示一个图片

#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <iostream>

int main() {
    // 读取图像文件
    cv::Mat image = cv::imread("example.jpg");

    // 检查图像是否成功加载
    if (image.empty()) {
        std::cerr << "Error: Unable to load image" << std::endl;
        return -1;
    }

    // 在窗口中显示图像
    cv::imshow("Image", image);

    // 等待用户按下任意键
    cv::waitKey(0);

    return 0;
}

执行结果

执行结果如下。

image-20240427151810871

libfacedetection

跨平台库,没有什么依赖,只要有C++编译器就可以。

win cmake 编译

生产静态库 文件,待会添加到我们的项目中。

首先,下载源码,

源码链接:GitHub - ShiqiYu/libfacedetection: An open source library for face detection in images. The face detection speed can reach 1000FPS.

cmake 配置

image-20240427152431254

cmake
扩展
  1. AVX2(Advanced Vector Extensions 2)是英特尔和 AMD 处理器架构中的指令集扩展,用于优化向量化计算。它引入了一组新的指令,使得处理器可以更有效地执行并行计算,特别是对于涉及大量数据的科学计算、图形处理和媒体处理等应用。

  2. OpenMP(Open Multi-Processing)是一种并行编程的标准,旨在简化多线程编程,并允许程序员利用多核处理器和共享内存体系结构来提高应用程序的性能。

    OpenMP 提供了一组指令和库函数,使得程序员可以通过在代码中添加一些简单的指令或注释来实现并行化。这些指令允许程序员标识出可以并行执行的代码段,并指定线程数、数据共享方式、任务分配策略等相关参数。

    使用 OpenMP,程序员可以轻松地将串行代码转换为并行代码,而无需深入了解线程管理和同步机制的复杂细节。这使得并行编程变得更加容易,同时可以在多核处理器上实现更高的性能。

    OpenMP 支持多种编程语言,包括 C、C++ 和 Fortran。它已成为高性能计算和科学计算领域中最流行的并行编程模型之一,被广泛应用于各种领域,包括仿真、数据分析、图像处理等。

生成静态库

如果你非得动态库,可以修改CMakeLists.txt

打开cmake 生成的项目文件,点击生成解决方案,就会在你设置的build目录看到facedetection.lib

头文件
facedetection_export.h

facedetectcnn.h

配置face-demo项目

首先,新建lib 和include 存放静态库文件和前面提到的头文件。

image-20240427154601054

接着, 右键项目,点击属性,打开C/C++ , 常规,附加包含目录,添加 新建的include 目录

image-20240427154931183

然后, 打开 链接器,输入,附加依赖项,添加lib/facedetection.lib

image-20240427155119623

代码

直接copy libfacedetection example 源文件。

/*
By downloading, copying, installing or using the software you agree to this license.
If you do not agree to this license, do not download, install,
copy or use the software.


                  License Agreement For libfacedetection
                     (3-clause BSD License)

Copyright (c) 2018-2020, Shiqi Yu, all rights reserved.
shiqi.yu@gmail.com

Redistribution and use in source and binary forms, with or without modification,
are permitted provided that the following conditions are met:

  * Redistributions of source code must retain the above copyright notice,
    this list of conditions and the following disclaimer.

  * Redistributions in binary form must reproduce the above copyright notice,
    this list of conditions and the following disclaimer in the documentation
    and/or other materials provided with the distribution.

  * Neither the names of the copyright holders nor the names of the contributors
    may be used to endorse or promote products derived from this software
    without specific prior written permission.

This software is provided by the copyright holders and contributors "as is" and
any express or implied warranties, including, but not limited to, the implied
warranties of merchantability and fitness for a particular purpose are disclaimed.
In no event shall copyright holders or contributors be liable for any direct,
indirect, incidental, special, exemplary, or consequential damages
(including, but not limited to, procurement of substitute goods or services;
loss of use, data, or profits; or business interruption) however caused
and on any theory of liability, whether in contract, strict liability,
or tort (including negligence or otherwise) arising in any way out of
the use of this software, even if advised of the possibility of such damage.
*/

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

//define the buffer size. Do not change the size!
//0x9000 = 1024 * (16 * 2 + 4), detect 1024 face at most
#define DETECT_BUFFER_SIZE 0x9000
using namespace cv;
using namespace std;

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

	//load an image and convert it to gray (single-channel)
	Mat image = imread(argv[1]); 
	if(image.empty())
	{
		fprintf(stderr, "Can not load the image file %s.\n", argv[1]);
		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;
    }
	

	///
	// 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)) + 16*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);

	waitKey();

    //release the buffer
    free(pBuffer);

	return 0;
}

注意,这个demo 运行需要命令行参数

右键项目,点击属性,调试,命令参数,输入“example.jpg”

image-20240427155649996

点击运行,大功告成

执行结果,如图

image-20240427155830780

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值