利用dlib实现人脸识别初体验

2024-03-17 22:12

本文主要是介绍利用dlib实现人脸识别初体验,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

前面的文章中介绍了如何编译dlib静态库和动态库,那么下一步就是利用dlib完成一个最简单的识别图片中人脸的程序。该程序参考了一些网上最常用的例子。下面将测试的环境、代码、遇到的问题、解决方法一一列出,供大家参考,希望能给大家一些帮助。

测试平台:x86_64 + 银河麒麟(v10)

开发框架:Qt(5.15.2)

测试代码:

#include <iostream>
#include <QApplication>
#include "dlib/image_processing/frontal_face_detector.h"
#include "dlib/gui_widgets.h"
#include "dlib/image_io.h"


using namespace dlib;
using namespace std;


int main(int argc, char *argv[])
{
    auto detector = get_frontal_face_detector();
    auto path = "/home/consys/20240315_204947_3.jpg"; //分辨率1280*720
    image_window win;
    array2d<unsigned char> img;
    load_image(img, path);      //通过路径加载图像
    pyramid_up(img);
    std::vector<rectangle> dets = detector(img);    //存储人脸矩阵
    cout << "number of faces detected:" << dets.size() << endl;
    win.clear_overlay();        //清除缓存
    win.set_image(img);         //设置图像
    win.add_overlay(dets, rgb_pixel(255, 0, 0));    //添加一个overlay
    pause();
    return 0;
}

测试效果:

 利用dlib开发的第一个程序,遇到的问题,编写了上述代码后,直接运行遇到的问题:

1.提示找不到dlib相关的符号

/usr/local/include/dlib/test_for_odr_violations.h:24: error: undefined reference to 'USER_ERROR__inconsistent_build_configuration__see_dlib_faq_1_'
/usr/local/include/dlib/test_for_odr_violations.h:51: error: undefined reference to 'DLIB_VERSION_MISMATCH_CHECK__EXPECTED_VERSION_19_24_99'
../face_dlib/main.cpp:18: error: undefined reference to 'dlib::image_window::image_window()'
../face_dlib/main.cpp:26: error: undefined reference to 'dlib::image_window::clear_overlay()'
../face_dlib/main.cpp:18: error: undefined reference to 'dlib::image_window::~image_window()'
../face_dlib/main.cpp:18: error: undefined reference to 'dlib::image_window::~image_window()'
/usr/local/include/dlib/test_for_odr_violations.h:24: error: undefined reference to 'USER_ERROR__inconsistent_build_configuration__see_dlib_faq_1_'
/usr/local/include/dlib/test_for_odr_violations.h:51: error: undefined reference to 'DLIB_VERSION_MISMATCH_CHECK__EXPECTED_VERSION_19_24_99'
/usr/local/include/dlib/matrix/lapack/gesvd.h:38: error: undefined reference to 'dgesvd_'
/usr/local/include/dlib/matrix/matrix_blas_bindings.h:148: error: undefined reference to 'cblas_saxpy'
/usr/local/include/dlib/matrix/matrix_blas_bindings.h:177: error: undefined reference to 'cblas_sscal'
/usr/local/include/dlib/image_processing/frontal_face_detector.h:115: error: undefined reference to 'dlib::base64::base64()'
/usr/local/include/dlib/image_processing/frontal_face_detector.h:2358: error: undefined reference to 'dlib::base64::decode(std::istream&, std::ostream&) const'
/usr/local/include/dlib/image_processing/frontal_face_detector.h:115: error: undefined reference to 'dlib::base64::~base64()'
/usr/local/include/dlib/image_processing/frontal_face_detector.h:115: error: undefined reference to 'dlib::base64::~base64()'
/usr/local/include/dlib/gui_widgets/style.h:527: error: undefined reference to 'dlib::draw_sunken_rectangle(dlib::canvas const&, dlib::rectangle const&, unsigned char)'
/usr/local/include/dlib/compress_stream/compress_stream_kernel_1.h:180: error: undefined reference to 'dlib::entropy_decoder_kernel_2::entropy_decoder_kernel_2()'
/usr/local/include/dlib/compress_stream/compress_stream_kernel_1.h:181: error: undefined reference to 'dlib::entropy_decoder_kernel_2::set_stream(std::istream&)'
/usr/local/include/dlib/compress_stream/compress_stream_kernel_1.h:196: error: undefined reference to 'dlib::entropy_decoder_kernel_2::get_target(unsigned int)'
/usr/local/include/dlib/compress_stream/compress_stream_kernel_1.h:201: error: undefined reference to 'dlib::entropy_decoder_kernel_2::decode(unsigned int, unsigned int)'
/usr/local/include/dlib/compress_stream/compress_stream_kernel_1.h:180: error: undefined reference to 'dlib::entropy_decoder_kernel_2::~entropy_decoder_kernel_2()'
/usr/local/include/dlib/compress_stream/compress_stream_kernel_1.h:180: error: undefined reference to 'dlib::entropy_decoder_kernel_2::~entropy_decoder_kernel_2()'
/usr/local/include/dlib/gui_widgets/widgets.h:3980: error: undefined reference to 'dlib::image_window::add_overlay(std::vector<dlib::image_display::overlay_rect, std::allocator<dlib::image_display::overlay_rect> > const&)'
/usr/local/include/dlib/gui_widgets/widgets.h:3942: error: undefined reference to 'dlib::image_display::get_image_display_rect() const'
/usr/local/include/dlib/gui_widgets/widgets.h:3946: error: undefined reference to 'dlib::base_window::set_size(int, int)'
/usr/local/include/dlib/entropy_decoder_model/entropy_decoder_model_kernel_5.h:422: error: undefined reference to 'dlib::entropy_decoder_kernel_2::get_target(unsigned int)'
/usr/local/include/dlib/entropy_decoder_model/entropy_decoder_model_kernel_5.h:456: error: undefined reference to 'dlib::entropy_decoder_kernel_2::decode(unsigned int, unsigned int)'
/usr/local/include/dlib/entropy_decoder_model/entropy_decoder_model_kernel_5.h:503: error: undefined reference to 'dlib::entropy_decoder_kernel_2::decode(unsigned int, unsigned int)'
/usr/local/include/dlib/entropy_decoder_model/entropy_decoder_model_kernel_5.h:551: error: undefined reference to 'dlib::entropy_decoder_kernel_2::get_target(unsigned int)'
/usr/local/include/dlib/entropy_decoder_model/entropy_decoder_model_kernel_5.h:553: error: undefined reference to 'dlib::entropy_decoder_kernel_2::decode(unsigned int, unsigned int)'
/usr/local/include/dlib/image_loader/png_loader.h:173: error: undefined reference to 'dlib::png_loader::png_loader(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)'
/usr/local/include/dlib/image_loader/jpeg_loader.h:100: error: undefined reference to 'dlib::jpeg_loader::jpeg_loader(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)'
/usr/local/include/dlib/image_loader/webp_loader.h:107: error: undefined reference to 'dlib::webp_loader::webp_loader(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)'
/usr/local/include/dlib/gui_widgets/widgets.h:3277: error: undefined reference to 'dlib::scrollable_region::set_total_rect_size(unsigned long, unsigned long)'
/usr/local/include/dlib/gui_widgets/widgets.h:3279: error: undefined reference to 'dlib::scrollable_region::set_total_rect_size(unsigned long, unsigned long)'
/usr/local/include/dlib/gui_widgets/widgets.h:3283: error: undefined reference to 'dlib::base_window::invalidate_rectangle(dlib::rectangle const&)'
/usr/local/include/dlib/gui_widgets/widgets.h:3288: error: undefined reference to 'dlib::popup_menu_region::disable()'
/usr/local/include/dlib/image_loader/image_loader.h:550: error: undefined reference to 'dlib::entropy_decoder_kernel_2::entropy_decoder_kernel_2()'
/usr/local/include/dlib/image_loader/image_loader.h:551: error: undefined reference to 'dlib::entropy_decoder_kernel_2::set_stream(std::istream&)'
/usr/local/include/dlib/image_loader/image_loader.h:550: error: undefined reference to 'dlib::entropy_decoder_kernel_2::~entropy_decoder_kernel_2()'
/usr/local/include/dlib/image_loader/image_loader.h:736: error: undefined reference to 'dlib::entropy_decoder_kernel_2::entropy_decoder_kernel_2()'
/usr/local/include/dlib/image_loader/image_loader.h:737: error: undefined reference to 'dlib::entropy_decoder_kernel_2::set_stream(std::istream&)'
/usr/local/include/dlib/image_loader/image_loader.h:736: error: undefined reference to 'dlib::entropy_decoder_kernel_2::~entropy_decoder_kernel_2()'
/usr/local/include/dlib/image_loader/png_loader.h:123: error: undefined reference to 'dlib::png_loader::is_gray() const'
/usr/local/include/dlib/image_loader/png_loader.h:126: error: undefined reference to 'dlib::png_loader::is_graya() const'
/usr/local/include/dlib/image_loader/png_loader.h:129: error: undefined reference to 'dlib::png_loader::is_rgb() const'
/usr/local/include/dlib/image_loader/png_loader.h:132: error: undefined reference to 'dlib::png_loader::is_rgba() const'
/usr/local/include/dlib/image_loader/png_loader.h:123: error: undefined reference to 'dlib::png_loader::is_gray() const'
/usr/local/include/dlib/image_loader/png_loader.h:126: error: undefined reference to 'dlib::png_loader::is_graya() const'
/usr/local/include/dlib/image_loader/png_loader.h:129: error: undefined reference to 'dlib::png_loader::is_rgb() const'
/usr/local/include/dlib/image_loader/png_loader.h:132: error: undefined reference to 'dlib::png_loader::is_rgba() const'
/usr/local/include/dlib/image_loader/jpeg_loader.h:51: error: undefined reference to 'dlib::jpeg_loader::is_gray() const'
/usr/local/include/dlib/image_loader/jpeg_loader.h:56: error: undefined reference to 'dlib::jpeg_loader::is_rgba() const'
/usr/local/include/dlib/image_loader/webp_loader.h:73: error: undefined reference to 'dlib::webp_loader::read_rgba(unsigned char*, unsigned long, int) const'
/usr/local/include/dlib/entropy_decoder_model/entropy_decoder_model_kernel_5.h:422: error: undefined reference to 'dlib::entropy_decoder_kernel_2::get_target(unsigned int)'
/usr/local/include/dlib/test_for_odr_violations.h:24: error: undefined reference to 'USER_ERROR__inconsistent_build_configuration__see_dlib_faq_1_'
/usr/local/include/dlib/test_for_odr_violations.h:51: error: undefined reference to 'DLIB_VERSION_MISMATCH_CHECK__EXPECTED_VERSION_19_24_99'

 问题原因:找不到dlib相关的符号

解决方法:在pro文件中添加 LIBS += -L/usr/local/lib -ldlib

2.提示找不到dgesvd_ cblas_saxpy cblas_sscal

/usr/local/include/dlib/matrix/lapack/gesvd.h:38: error: undefined reference to 'dgesvd_'
/usr/local/include/dlib/matrix/matrix_blas_bindings.h:148: error: undefined reference to 'cblas_saxpy'
/usr/local/include/dlib/matrix/matrix_blas_bindings.h:177: error: undefined reference to 'cblas_sscal'

问题原因:dlib依赖lapack cblas,编译时需要显式的指明这两个所在的路径

解决方法:LIBS += -L/usr/lib/x86_64-linux-gnu -llapack -lcblas

3 我自己的环境有两个qt版本,一个是qt5.12.8,一个是qt5.15.2,5.15.2是自己安装的。5.12.8是安装系统自带的,libqtCore.so在/usr/lib/x86_64-linux-gnu下,新版本安装在/opt/Qt/5.15.2/gcc_64/lib目录下,我自己遇到的问题是LD_LIBRARY_PATH环境变量顺序有问题,原来的环境变量顺序是:/usr/lib/x86_64-linux-gnu:/opt/Qt/5.15.2/gcc_64/lib,这个顺序会造成先找到/usr/lib/x86_64-linux-gnu下的libqtcore.so,但是自己的工程配置的是5.15.2,这样会造成版本qt版本不对,找不到Qt_5.15宏,错误提示如下

/usr/lib/x86_64-linux-gnu/libQt5Core.so.5: version `Qt_5.15' not found (required by /home/consys/qt_project/build-face_dlib-Desktop_Qt_5_15_2_GCC_64bit-Debug/face_dlib)

解决方法:调整LD_LIBRARY_PATH的顺序即可。

/opt/Qt/5.15.2/gcc_64/lib:/usr/lib/x86_64-linux-gnu

待优化问题

1、图片中人脸识别时间特别长(20多秒),这明显不合理,需要回答几个问题,改代码通过什么方式进行人脸识别的?CNN?还是DNN?还是其它的技术?这个时间长度与图片分辨率是否有关系?将分辨率降为640*480后,时间明显缩短至10s组左右,但是还是时间太长,优化识别时间。

这篇关于利用dlib实现人脸识别初体验的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/820343

相关文章

使用Python实现IP地址和端口状态检测与监控

《使用Python实现IP地址和端口状态检测与监控》在网络运维和服务器管理中,IP地址和端口的可用性监控是保障业务连续性的基础需求,本文将带你用Python从零打造一个高可用IP监控系统,感兴趣的小伙... 目录概述:为什么需要IP监控系统使用步骤说明1. 环境准备2. 系统部署3. 核心功能配置系统效果展

Python实现微信自动锁定工具

《Python实现微信自动锁定工具》在数字化办公时代,微信已成为职场沟通的重要工具,但临时离开时忘记锁屏可能导致敏感信息泄露,下面我们就来看看如何使用Python打造一个微信自动锁定工具吧... 目录引言:当微信隐私遇到自动化守护效果展示核心功能全景图技术亮点深度解析1. 无操作检测引擎2. 微信路径智能获

Python中pywin32 常用窗口操作的实现

《Python中pywin32常用窗口操作的实现》本文主要介绍了Python中pywin32常用窗口操作的实现,pywin32主要的作用是供Python开发者快速调用WindowsAPI的一个... 目录获取窗口句柄获取最前端窗口句柄获取指定坐标处的窗口根据窗口的完整标题匹配获取句柄根据窗口的类别匹配获取句

在 Spring Boot 中实现异常处理最佳实践

《在SpringBoot中实现异常处理最佳实践》本文介绍如何在SpringBoot中实现异常处理,涵盖核心概念、实现方法、与先前查询的集成、性能分析、常见问题和最佳实践,感兴趣的朋友一起看看吧... 目录一、Spring Boot 异常处理的背景与核心概念1.1 为什么需要异常处理?1.2 Spring B

Python位移操作和位运算的实现示例

《Python位移操作和位运算的实现示例》本文主要介绍了Python位移操作和位运算的实现示例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一... 目录1. 位移操作1.1 左移操作 (<<)1.2 右移操作 (>>)注意事项:2. 位运算2.1

如何在 Spring Boot 中实现 FreeMarker 模板

《如何在SpringBoot中实现FreeMarker模板》FreeMarker是一种功能强大、轻量级的模板引擎,用于在Java应用中生成动态文本输出(如HTML、XML、邮件内容等),本文... 目录什么是 FreeMarker 模板?在 Spring Boot 中实现 FreeMarker 模板1. 环

Qt实现网络数据解析的方法总结

《Qt实现网络数据解析的方法总结》在Qt中解析网络数据通常涉及接收原始字节流,并将其转换为有意义的应用层数据,这篇文章为大家介绍了详细步骤和示例,感兴趣的小伙伴可以了解下... 目录1. 网络数据接收2. 缓冲区管理(处理粘包/拆包)3. 常见数据格式解析3.1 jsON解析3.2 XML解析3.3 自定义

SpringMVC 通过ajax 前后端数据交互的实现方法

《SpringMVC通过ajax前后端数据交互的实现方法》:本文主要介绍SpringMVC通过ajax前后端数据交互的实现方法,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价... 在前端的开发过程中,经常在html页面通过AJAX进行前后端数据的交互,SpringMVC的controll

Spring Security自定义身份认证的实现方法

《SpringSecurity自定义身份认证的实现方法》:本文主要介绍SpringSecurity自定义身份认证的实现方法,下面对SpringSecurity的这三种自定义身份认证进行详细讲解,... 目录1.内存身份认证(1)创建配置类(2)验证内存身份认证2.JDBC身份认证(1)数据准备 (2)配置依

利用python实现对excel文件进行加密

《利用python实现对excel文件进行加密》由于文件内容的私密性,需要对Excel文件进行加密,保护文件以免给第三方看到,本文将以Python语言为例,和大家讲讲如何对Excel文件进行加密,感兴... 目录前言方法一:使用pywin32库(仅限Windows)方法二:使用msoffcrypto-too