本文主要是介绍利用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实现人脸识别初体验的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!