linux下c 和dlib实现人脸识别,人脸识别(dlib版)-1 dlib 安装及基础使用

Dlib 是一个 C++ 工具库,包含机器学习算法,图像处理,网络及一些工具类库。在工业界,学术界都得到广泛使用。接下来的几篇文章中,我将会分享 dlib 库在人脸识别中的应用。这篇文章,将介绍dlib库的安装及基础使用。

安装

推荐使用编译源码的方式安装dlib库。我们使用 dlib-19.16 版本进行说明,首先下载源码.

curl https://github.com/davisking/dlib/archive/v19.16.zip -o dlib-19.16.zip

unzip dlib-19.16.zip

我们将通过使用 python 使用 dlib 库,安装之前,电脑上应安装 python。

在编译之前,然后确保电脑系统上,有 CMake 和 C++ 编译环境。

Linux 操作系统下,可通过安装包管理工具 apt-get或者yum安装对应的 cmake 及 g++。e.g.

sudo apt-get install cmake

sudo apt-get install gcc g++

Mac OS X 上,可以通过 brew 安装 cmake 及 g++.

Windows 操作系统,到 CMake 官网下载页下载对应的安装包,如 Win 64 版本的安装包。C++ 编译环境,建议安装 Visual Studio 2017。

下载源依赖之后,进入 dlib 源码目录,进行编译.

cd dlib-19.16

python3 setup.py install

安装完成后,验证是否安装成功

python3 -c "import dlib"

如果没有报错,则说明安装成功。

基础使用

接下来,我们将介绍 dlib 在图像处理上的基础使用。

目标检测

dlib 中的函数 find_candidate_object_locations() 能够识别指定的图片,并把可能存在目标的对象找出来。实现方法基于 Koen van de Sande 的论文 Segmentation as Selective Search for Object Recognition by Koen E. A. van de Sande, et al.。这个方法可以快速找到候选目标的区域,我们可以使用这些区域进行后续操作。

import dlib

image_file = 'bbt1.jpg'

img = dlib.load_rgb_image(image_file)

# Locations of candidate objects will be saved into rects

rects = []

dlib.find_candidate_object_locations(img, rects, min_size=500)

print("number of rectangles found {}".format(len(rects)))

for k, d in enumerate(rects):

print("Detection {}: Left: {} Top: {} Right: {} Bottom: {}".format(

k, d.left(), d.top(), d.right(), d.bottom()))

代码中, 函数 load_rgb_image(...) 接受一个文件名称,然后返回 numpy 数组对象,这个作为 find_candidate_object_locations(...) 函数的输入。函数 find_candidate_object_locations(...) 第二个参数 rects 为列表,保存找到候选推向所在的区域,第三个参数 min_size 表示找到的区域大小不应该小于指定的像素值。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
实现人脸识别,需要使用OpenCV和Dlib这两个库。以下是使用C++实现人脸识别的基本步骤: 1. 安装OpenCV和Dlib库 首先需要安装OpenCV和Dlib库,并将其包含到C++项目中。可以使用以下命令在Ubuntu上安装这两个库: ``` sudo apt-get install libopencv-dev sudo apt-get install libdlib-dev ``` 2. 加载人脸识别模型 使用Dlib库提供的人脸检测器和68个关键点检测器,需要加载人脸识别模型。可使用以下代码: ``` #include <dlib/opencv.h> #include <dlib/image_processing/frontal_face_detector.h> #include <dlib/image_processing.h> using namespace dlib; frontal_face_detector detector = get_frontal_face_detector(); shape_predictor sp; deserialize("shape_predictor_68_face_landmarks.dat") >> sp; ``` 3. 加载人脸数据库 将需要识别的人脸图片保存到人脸数据库中。可使用以下代码加载人脸数据库: ``` std::vector<matrix<rgb_pixel>> faces; std::vector<std::string> labels; // Load faces from a directory path load_image_dataset(faces, labels, "faces"); ``` 4. 人脸检测和关键点检测 使用Dlib库提供的人脸检测器和68个关键点检测器,对待识别的人脸图像进行处理,提取人脸特征。可使用以下代码: ``` // Load the input image cv::Mat inputImg = cv::imread("face.jpg"); // Convert the input image to Dlib's format cv_image<rgb_pixel> dlibImg(inputImg); // Detect faces in the image std::vector<rectangle> dets = detector(dlibImg); // Find the pose of each face std::vector<full_object_detection> shapes; for (unsigned long j = 0; j < dets.size(); ++j) { full_object_detection shape = sp(dlibImg, dets[j]); shapes.push_back(shape); } ``` 5. 人脸识别 将待识别的人脸特征与人脸数据库中的特征进行比对,找到最相似的人脸。可使用以下代码: ``` // Compute the face descriptor for each face std::vector<matrix<float,0,1>> faceDescriptors; for (unsigned long i = 0; i < shapes.size(); ++i) { matrix<rgb_pixel> faceChip; extract_image_chip(dlibImg, get_face_chip_details(shapes[i],150,0.25), faceChip); faceDescriptors.push_back(net(faceChip)); } // Find the closest match in the database std::vector<double> distances; std::string bestLabel; double bestDistance = 1.0; for (unsigned long i = 0; i < faces.size(); ++i) { double distance = length(faceDescriptors[0] - faceDescriptors[i]); if (distance < bestDistance) { bestDistance = distance; bestLabel = labels[i]; } } ``` 以上是使用C++实现人脸识别的基本步骤。可以根据实际需求对代码进行修改和优化。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值