这个也是看了BILIBILI的嗨哥总结出来的一个方法

首先要准备的文件有三个

dlib_face_recognition_resnet_model_v1.dat

shape_predictor_68_face_landmarks.dat

这两个可以在http://dlib.net/files/这里下载

haarcascade_frontalface_alt2.xml

这个是OpenCV里带的分类器

为了识别到我们训练的人脸给自己建一个容器——也就是上图的文件夹lib

里面丢自己想要识别的人脸

然后这是我的CMakeList.TXT

cmake_minimum_required(VERSION 3.19.0)  PROJECT(dlib_facedetector2)   SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11 -O2 -DDLIB_JPEG_SUPPORT")IF(CMAKE_CXX_COMPILER_ID STREQUAL "Clang")SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Weverything")
ELSEIF(CMAKE_CXX_COMPILER_ID STREQUAL "GNU")SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -Wextra")
ENDIF()INCLUDE(/home/pi/dlib/dlib-19.22/dlib/cmake)
#INCLUDE OPENCV
FIND_PACKAGE(OpenCV REQUIRED)
INCLUDE_DIRECTORIES(${OpenCV_INCLUDE_DIRS})
message(STATUS "Opencv include dir found at ${OpenCV_INCLUDE_DIRS}")INCLUDE_DIRECTORIES(/home/pi/dlib/dlib-19.22) LINK_DIRECTORIES(/home/pi/dlib/dlib-19.22/dlib) ADD_EXECUTABLE(dlib_detector2  image2.cpp )TARGET_LINK_LIBRARIES(dlib_detector2 dlib ${OpenCV_LIBS})

然后是我的image2.cpp

#include <cstdio>
#include <vector>
#include <algorithm>
#include <cstdio>#include <iostream>
#include <fstream>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <algorithm>#include "opencv/cv.h"
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include "opencv2/videoio.hpp"
#include "opencv2/imgproc/imgproc.hpp"#include </home/pi/dlib/dlib-19.22/dlib/dnn.h>
#include </home/pi/dlib/dlib-19.22/dlib/gui_widgets.h>
#include </home/pi/dlib/dlib-19.22/dlib/clustering.h>
#include </home/pi/dlib/dlib-19.22/dlib/string.h>
#include </home/pi/dlib/dlib-19.22/dlib/image_io.h>
#include </home/pi/dlib/dlib-19.22/dlib/image_processing/frontal_face_detector.h>
#include </home/pi/dlib/dlib-19.22/dlib/opencv.h>
#include <opencv2/core/core.hpp>
#include <opencv2/freetype.hpp>
#include <dlib/image_processing/render_face_detections.h>
#include <dlib/image_processing.h>
#include <opencv/highgui.h>
#include <opencv2/opencv.hpp>using namespace dlib;
using namespace std;
using namespace cv;template <template <int, template<typename>class, int, typename> class block, int N, template<typename>class BN, typename SUBNET>
using residual = add_prev1<block<N, BN, 1, tag1<SUBNET>>>;template <template <int, template<typename>class, int, typename> class block, int N, template<typename>class BN, typename SUBNET>
using residual_down = add_prev2<avg_pool<2, 2, 2, 2, skip1<tag2<block<N, BN, 2, tag1<SUBNET>>>>>>;template <int N, template <typename> class BN, int stride, typename SUBNET>
using block = BN<con<N, 3, 3, 1, 1, relu<BN<con<N, 3, 3, stride, stride, SUBNET>>>>>;template <int N, typename SUBNET> using ares = relu<residual<block, N, affine, SUBNET>>;
template <int N, typename SUBNET> using ares_down = relu<residual_down<block, N, affine, SUBNET>>;template <typename SUBNET> using alevel0 = ares_down<256, SUBNET>;
template <typename SUBNET> using alevel1 = ares<256, ares<256, ares_down<256, SUBNET>>>;
template <typename SUBNET> using alevel2 = ares<128, ares<128, ares_down<128, SUBNET>>>;
template <typename SUBNET> using alevel3 = ares<64, ares<64, ares<64, ares_down<64, SUBNET>>>>;
template <typename SUBNET> using alevel4 = ares<32, ares<32, ares<32, SUBNET>>>;using anet_type = loss_metric<fc_no_bias<128, avg_pool_everything<alevel0<alevel1<alevel2<alevel3<alevel4<max_pool<3, 3, 2, 2, relu<affine<con<32, 7, 7, 2, 2,input_rgb_image_sized<150>>>>>>>>>>>>>;
cv::Mat MyResizeImage(cv::Mat pSrc, double dScale)
{cv::Size sSize = cv::Size(pSrc.cols*dScale, pSrc.rows*dScale);cv::Mat pDes = cv::Mat(sSize, CV_32S);resize(pSrc, pDes, sSize);return pDes;
int main()
{cv::Mat mimg;std::vector<matrix<float, 0, 1>> vec;        //定义一个向量组,用于存放每一个人脸的编码;float vec_error[30];int count_img = 0;                      //定义一个浮点型的数组,用于存放一个人脸编码与人脸库的每一个人脸编码的差值;std::vector<cv::String> fileNames,img_path;cv::glob("/home/pi/face_test/lib",img_path);fileNames = img_path;for (int i = 0; i < img_path.size(); i++){if((img_path[i].find(".jpg") != img_path[i].npos)||(img_path[i].find(".png") != img_path[i].npos)){size_t pos = img_path[i].find_last_of('/');size_t len = img_path[i].find_last_of('.');    fileNames[i] = img_path[i].substr(pos+1,len-pos-1);cout << "file name:" << fileNames[i] << endl;count_img++;}} cout << "The number of picture is:" << count_img << endl;//我们要做的第一件事是加载所有模型。首先,因为我们需要在图像中查找人脸我们需要人脸检测器:frontal_face_detector detector = get_frontal_face_detector();shape_predictor sp;deserialize("shape_predictor_68_face_landmarks.dat") >> sp;anet_type net;deserialize("dlib_face_recognition_resnet_model_v1.dat") >> net;matrix<rgb_pixel> img_obj;            //定义dlib型图片,彩色
//以下建立人脸编码库代码--------------------------------------------------------------for (int k = 0; k < count_img; k++)  //依次加载完图片库里的文件{string fileFullName = img_path[k];//图片地址+文件名load_image(img_obj, fileFullName);std::vector<dlib::rectangle> dets = detector(img_obj);  //检测人脸,位置大小信息存放到dets中if (dets.size()<1)cout << "There is no face" << endl;else if (dets.size()>1)cout << "There is to many face" << endl;else{std::vector<matrix<rgb_pixel>> faces;//定义存放截取人脸数据组auto shape = sp(img_obj, dets[0]);matrix<rgb_pixel> face_chip;extract_image_chip(img_obj, get_face_chip_details(shape, 150, 0.25), face_chip);//截取人脸部分,并将大小调为150*150faces.push_back(move(face_chip));std::vector<matrix<float, 0, 1>> face_descriptors = net(faces);//载入Resnet残差网络,返回128D人脸特征vec.push_back(face_descriptors[k]);                       //保存这一个人脸的特征向量到vec向量的对应位置cout << "The vector of picture " << img_path[k] << endl;//打印该人脸的标签和特征向量 }}
//以下是识别代码------------------------------------------------------------------------------Ptr<freetype::FreeType2> ft2;ft2=freetype::createFreeType2();ft2->loadFontData("/usr/share/fonts/truetype/arphic/uming.ttc",0);cv::VideoCapture cap(-1);if (!cap.isOpened()){cerr << "Unable to connect to camera" << endl;return 1;}while(waitKey(1) != 27) {cv::Mat mimg;if (!cap.read(mimg)){cap.set(CV_CAP_PROP_POS_FRAMES, 0);continue;}//cv::Mat mimg = MyResizeImage(mimg, 0.4);array2d<rgb_pixel> img_src;dlib::assign_image(img_src, dlib::cv_image<dlib::bgr_pixel>(mimg));//pyramid_up(img_src);std::vector<matrix<rgb_pixel>> faces_test;for (auto face_test : detector(img_src)){auto shape_test = sp(img_src, face_test);matrix<rgb_pixel> face_chip_test;extract_image_chip(img_src, get_face_chip_details(shape_test, 150, 0.25), face_chip_test);faces_test.push_back(move(face_chip_test));}std::vector<dlib::rectangle> dets_test = detector(img_src);std::vector<matrix<float, 0, 1>> face_test_descriptors = net(faces_test);cout<<"size:"<<face_test_descriptors.size()<<endl;for (size_t i = 0; i < face_test_descriptors.size(); ++i)                 //比对,识别{for (size_t j = 0; j < vec.size(); j++){vec_error[j] = (double)length(face_test_descriptors[i] - vec[j]);cout <<vec.size()<< ":pic_"<<j<<"->pic_"<<i<<" vec_error is:" << vec_error[j] << endl;std::string text = "不熟";if (vec_error[j] < 0.4){//多个人就找出一个阀值以下的text = fileNames[j];//得到文件名cout <<"找到:"<<fileNames[j]<<","<<text<<endl;} cv::Point origin;origin.x = dets_test[i].left();origin.y = dets_test[i].top();ft2->putText(mimg, text, origin, 40/*size*/,Scalar(255,0,0), -1, 8, true );}cv::rectangle(mimg, cv::Rect(dets_test[i].left(), dets_test[i].top(), dets_test[i].width(), dets_test[i].width()), cv::Scalar(0, 0, 255), 1, 1, 0);//画矩形框}#if 0dlib::cv_image<bgr_pixel> dimg(mimg);//转成dlib格式image_window win(dimg);win.wait_until_closed();#else    imshow("src", mimg);      #endif  }
}

其中有一点要注意的就是输出frame框的时候如果字体有问题的话会输出框框的,/usr/share/fonts/truetype/arphic/uming.ttc这个就是字体的位置。找字体地址可以用fc-list :lang=zh。 具体详情参考这个老哥https://blog.csdn.net/iteye_9818/article/details/82676982。

最后就是一如既往的

sudo cmake ..
sudo make
ls
有我们想要的小飞机的时候
就可以愉快得    ./dlib_detector2   了

但是我的帧率有一点点感人,希望有解决办法的老哥救救孩子

最后就是结果展示

参考资料:①https://blog.csdn.net/qq_42109746/article/details/88255741?utm_medium=distribute.pc_relevant.none-task-blog-2%7Edefault%7EBlogCommendFromMachineLearnPai2%7Edefault-4.base&depth_1-utm_source=distribute.pc_relevant.none-task-blog-2%7Edefault%7EBlogCommendFromMachineLearnPai2%7Edefault-4.base

②https://blog.csdn.net/u012819339/article/details/82698570

感谢感谢

在树莓派下使用dlib及resnet的人脸检测及识别相关推荐

  1. python摄像头跟随人脸_Python3利用Dlib实现摄像头实时人脸检测和平铺显示示例

    1. 引言 在某些场景下,我们不仅需要进行实时人脸检测追踪,还要进行再加工:这里进行摄像头实时人脸检测,并对于实时检测的人脸进行初步提取: 单个/多个人脸检测,并依次在摄像头窗口,实时平铺显示检测到的 ...

  2. python恶搞代码打开对方摄像头_Python 3 利用 Dlib 实现摄像头实时人脸检测和平铺显示...

    1. 引言 在某些场景下,我们不仅需要进行实时人脸检测追踪,还要进行再加工:这里进行摄像头实时人脸检测,并对于实时检测的人脸进行初步提取: 单个/多个人脸检测,并依次在摄像头窗口,实时平铺显示检测到的 ...

  3. python实现面部特效_Python 3 利用 Dlib 实现摄像头实时人脸检测和平铺显示

    1. 引言 在某些场景下,我们不仅需要进行实时人脸检测追踪,还要进行再加工:这里进行摄像头实时人脸检测,并对于实时检测的人脸进行初步提取: 单个/多个人脸检测,并依次在摄像头窗口,实时平铺显示检测到的 ...

  4. 人脸检测和识别(中文标记)完整项目源代码(基于深度学习+python3.6+dlib+PIL+CNN+(tensorflow、keras)10分钟实现 区分欢乐颂中人物详细图文教程和完整项目代码)

    转载请注明:https://blog.csdn.net/wyx100/article/details/80428424 效果展示 未完待续... 环境配置 win7sp1 python         ...

  5. 【dlib库】进行人脸检测+人脸关键点检测+人脸对齐

    原图像: 1. 人脸检测 import cv2 import dlib import matplotlib.pyplot as plt # 获取图片 my_img = cv2.imread('my_i ...

  6. Python 3 利用 Dlib 实现人脸检测和剪切

    0. 引言 利用 Python 开发,借助 Dlib 库进行人脸检测 / face detection 和剪切:   1. crop_faces_show.py : 将检测到的人脸剪切下来,依次排序平 ...

  7. OpenCV vs Dlib 人脸检测比较分析

    点击我爱计算机视觉标星,更快获取CVML新技术 人脸检测是计算机视觉最典型的应用之一,早期OpenCV的logo就是Haar人脸检测的示意图. 很多人的第一个OpenCV学习目标就是跑通Haar级联人 ...

  8. 【Dlib人脸识别】1. Dlib人脸检测的基本原理

    Dlib中,人脸识别的基本思路为: 计算已知图片中所有人脸对应的特征向量: 计算要识别的未知图片中所有人脸对应的特征向量: 计算人脸之间的欧式距离: 如果两张人脸之间的欧式距离小于设定的阈值,则认为是 ...

  9. 【Dlib】人脸检测、特征点检测、人脸对齐、人脸识别

    本文是利用dlib库,进行人脸检测.特征点检测.人脸对齐.所有前提是假设已经安装了dlib. 参考链接: 1.http://developer.51cto.com/art/201801/564529. ...

  10. 基于 dlib 的人脸检测(68关键点)

    提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档 文章目 目录 文章目录 前言 一.背景 (1)环境搭建 (2)下载开源数据集 二.具体实现 效果展示: 效果展示: 总结 前言 imu ...

最新文章

  1. TensorFlow(6)神经网络训练(DNN)
  2. mybatis基于注解的入门案例
  3. Python入门100题 | 第012题
  4. wxWidgets:wxRegEx类用法
  5. php yii2 finfo file,FileHelper:文件系统助手
  6. 关于虚拟机vmware三种网络模式
  7. php静态地图api,静态图API | 百度地图API SDK
  8. Lind.DDD.Manager里的3,7,15,31,63,127,255,511,1023,2047
  9. 如何加强测评机构自身的规范化管理, 不断提高测评的能力和水平
  10. 【三维路径规划】基于matlab蚁群算法无人机三维路径规划【含Matlab源码 1278期】
  11. SPSS因子分析案例
  12. 基于人体姿态识别算法的行人抬手分析
  13. Prometheus客户端docker监控cAdvisor
  14. 磁共振线圈分类_磁共振线圈的发展历程
  15. 青岛胶州职业教育中心计算机基础专业证,胶州市职业教育中心学校着眼胶州发展大局,精准培养人才...
  16. JDBC Driver介绍
  17. pyscripter与python的关系_详解python开发环境PyScripter中文乱码问题解决方案
  18. ISCSI linux/windows配置及使用
  19. C语言结构体详解(结构体定义,使用,结构体大小等)
  20. 用ajax接收后台数据里的具体数据,ajax动态接收后台向后台传输数据以及接收数据...

热门文章

  1. 趣头条面试题:ThreadLocal是什么?怎么用?为什么用它?有什么缺点
  2. 5s管理推进的三个阶段及三大实施原则
  3. SQLAlchemy session 使用问题
  4. 迅捷新版PDF转换器
  5. pandas入门(一):pandas的安装和创建
  6. MD5加密算法的原理
  7. Android studio上音频文件格式问题
  8. 从Android运行时出发,打造我们的脱壳神器 - zyq8709--dexhunter(二代抽取壳)
  9. php简单的注册登录页面模板,注册登录页面模板(示例代码)
  10. css内联样式!important