利用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

相关文章

SpringBoot3实现Gzip压缩优化的技术指南

《SpringBoot3实现Gzip压缩优化的技术指南》随着Web应用的用户量和数据量增加,网络带宽和页面加载速度逐渐成为瓶颈,为了减少数据传输量,提高用户体验,我们可以使用Gzip压缩HTTP响应,... 目录1、简述2、配置2.1 添加依赖2.2 配置 Gzip 压缩3、服务端应用4、前端应用4.1 N

SpringBoot实现数据库读写分离的3种方法小结

《SpringBoot实现数据库读写分离的3种方法小结》为了提高系统的读写性能和可用性,读写分离是一种经典的数据库架构模式,在SpringBoot应用中,有多种方式可以实现数据库读写分离,本文将介绍三... 目录一、数据库读写分离概述二、方案一:基于AbstractRoutingDataSource实现动态

Python FastAPI+Celery+RabbitMQ实现分布式图片水印处理系统

《PythonFastAPI+Celery+RabbitMQ实现分布式图片水印处理系统》这篇文章主要为大家详细介绍了PythonFastAPI如何结合Celery以及RabbitMQ实现简单的分布式... 实现思路FastAPI 服务器Celery 任务队列RabbitMQ 作为消息代理定时任务处理完整

Java枚举类实现Key-Value映射的多种实现方式

《Java枚举类实现Key-Value映射的多种实现方式》在Java开发中,枚举(Enum)是一种特殊的类,本文将详细介绍Java枚举类实现key-value映射的多种方式,有需要的小伙伴可以根据需要... 目录前言一、基础实现方式1.1 为枚举添加属性和构造方法二、http://www.cppcns.co

使用Python实现快速搭建本地HTTP服务器

《使用Python实现快速搭建本地HTTP服务器》:本文主要介绍如何使用Python快速搭建本地HTTP服务器,轻松实现一键HTTP文件共享,同时结合二维码技术,让访问更简单,感兴趣的小伙伴可以了... 目录1. 概述2. 快速搭建 HTTP 文件共享服务2.1 核心思路2.2 代码实现2.3 代码解读3.

MySQL双主搭建+keepalived高可用的实现

《MySQL双主搭建+keepalived高可用的实现》本文主要介绍了MySQL双主搭建+keepalived高可用的实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,... 目录一、测试环境准备二、主从搭建1.创建复制用户2.创建复制关系3.开启复制,确认复制是否成功4.同

Java实现文件图片的预览和下载功能

《Java实现文件图片的预览和下载功能》这篇文章主要为大家详细介绍了如何使用Java实现文件图片的预览和下载功能,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... Java实现文件(图片)的预览和下载 @ApiOperation("访问文件") @GetMapping("

使用Sentinel自定义返回和实现区分来源方式

《使用Sentinel自定义返回和实现区分来源方式》:本文主要介绍使用Sentinel自定义返回和实现区分来源方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录Sentinel自定义返回和实现区分来源1. 自定义错误返回2. 实现区分来源总结Sentinel自定

Java实现时间与字符串互相转换详解

《Java实现时间与字符串互相转换详解》这篇文章主要为大家详细介绍了Java中实现时间与字符串互相转换的相关方法,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... 目录一、日期格式化为字符串(一)使用预定义格式(二)自定义格式二、字符串解析为日期(一)解析ISO格式字符串(二)解析自定义

opencv图像处理之指纹验证的实现

《opencv图像处理之指纹验证的实现》本文主要介绍了opencv图像处理之指纹验证的实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学... 目录一、简介二、具体案例实现1. 图像显示函数2. 指纹验证函数3. 主函数4、运行结果三、总结一、