第一篇:编译Opencv并用于人脸检测

step 1:下载Visual Studio

个人电脑安装的Visual Studio 2019 community版本,下面以VS2019为例说明
VS官方下载地址
下载完visual studio installer后进行安装就行,详细网上一大堆教程

step 2:下载cmake

由于选用的VS2019,需要版本较高的cmake,我选用的是cmake3.15.0
cmake下载地址
由于个人电脑是64位的,选用x64版本

step 3:下载opencv source code

opencv github地址:https://github.com/opencv/opencv
注意这里选择branch里面的4.1.0分支,然后点击download下载
在这里插入图片描述
若是需要opencv的一些扩展功能,需要下载opencv_contrib源码
下载地址为:https://github.com/opencv/opencv_contrib
下载完毕两个压缩包后解压

step 4:进行编译

打开cmake的gui,分别设置cmakelists路径和build路径
在这里插入图片描述
点击configure,cmake会探测出当前用的VS版本为2019,其余默认(x64平台)
然后进行第一次Configure。

其中在第一次configure中会从网上下载:ippicv_2019_lnx_intel64_general_20180723.tgz
此包下载速度过慢,不想等的可以离线下载到本地,然后修改cmake修改配置

第一次configure完后有很多标红的地方,进行第二次configure,一般需要补充三个地方:(需要配置GPU加上的需要设置cuda相关)
(1)API库路径
(2)
扩展库路径
(3)
opencv_world

再次点击Configure进行编译,这次红色消失,配置成功
配置成功
然后按generate就会生成opencv的VS2019工程
生成
生成的结果
利用VS2019打开Opencv解决方案
打开结果
然后直接批量进行编译,点击生成
批量编译选项
大约等待20分钟编译完成
编译通过
对编译好的lib库和dll进行查看
编译结果
在测试前需要将编译好的dll添加到系统环境变量,然后需要重启动VS2019才能使得设置生效
将opencv_world.dll所在路径添加到环境变量

step 5:进行接口测试

我这里直接选取稍微复杂一点的人脸检测任务进行测试
libfacedetection的github地址为:
https://github.com/ShiqiYu/libfacedetection

下载下来后同样通过cmake进行solution的生成
配置
打开生成的sln,并同样选择批生成
进行demo的编译
生成成功
接下来查看demo的源代码

#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 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 !!!
	pResults = facedetect_cnn(pBuffer, (unsigned char*)(image.ptr(0)), image.cols, image.rows, (int)image.step);

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

		printf("face_rect=[%d, %d, %d, %d], confidence=%d, angle=%d\n", x,y,w,h,confidence, angle);
		rectangle(result_cnn, Rect(x, y, w, h), Scalar(0, 255, 0), 2);
	}
	imshow("result_cnn", result_cnn);

	waitKey();

    //release the buffer
    free(pBuffer);

	return 0;
}

此代码的含义是利用opencv库读入一幅图像,并且利用cnn来进行人脸检测

将ftd_demo设为启动项,然后输入需要测试的图片路径
调试设置
打个断点然后按F5进入调试
调试

这里推荐用Image watch来对图片进行观察:
Image watch可以在扩展中直接下载
在这里插入图片描述

image watch 进行debug

然后一步一步调试,由于是debug模式,而且opencv是源码编译,可以直接跳入opencv的所有接口查看所有实现细节
这里跳入了imread函数查看实现细节

最终结果图

检测到人脸
检测出的人脸

至此opencv源码编译、image watch调试、利用量化后的cnn模型进行人脸检测全部实现完毕,并且可以随时跳入opencv接口进行细节的查看

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值