文章目录

  • `dlib`概述
  • 人脸检测
    • CPU版本人脸检测算法
      • 检测步骤
      • 示例代码:
    • CUDA版本人脸检测算法
      • 检测步骤
      • 示例代码
    • 类定义与接口源码
      • 人脸检测中用到的重要的类(概述)
      • `fhog_object_detector`类接口定义
      • `rectangle`类接口定义
      • `cnn_face_detection_model_v1`类定义
      • `mmod_rectangle` `mmod_rectangles` `mmod_rectangless`

dlib概述

Dlib是一个包含机器学习算法的C++开源工具包。Dlib可以帮助您创建很多复杂的机器学习方面的软件来帮助解决实际问题。目前Dlib已经被广泛的用在行业和学术领域,包括机器人,嵌入式设备,移动电话和大型高性能计算环境.

人脸检测

CPU版本人脸检测算法

检测步骤

  1. 获取hog detector
  2. 传入image,数据格式为numpy.ndarray
  3. (可选项) 获取分数值和检测列表
  4. 获取人脸坐标

示例代码:

import dlibfrom cv2 import cv2# step 1. create an object detector based on hog
detector = dlib.get_frontal_face_detector()  # _dlib_pybind11.fhog_object_detector# step 2. read an image using dlib or cv2
# note that the difference between the image data formated as numpy.ndarray read by dlib and cv2 is that dlib read it channels as *R G B* order while cv2 read as *B G R*,so you should do one more step to convert the image if using cv2
image_path = "sample.jpg"
img = dlib.load_rgb_image(image_path)
# img = cv2.imread(image_path)
# img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)# step 3. using the numpy.ndarray image data as input to detect the front face on the image
# The 1 in the second argument indicates that we should upsample the image 1 time.
# This will make everything bigger and allow us to detect more faces.
detections = detector(img, 1) # List[_dlib_pybind11.rectangle]# step 3.1 (Optional) if you want to get more detail information,using function run() instead
# detections, scores, idx = detector.run(img, 1, 0.5) # List[_dlib_pybind11.rectangle] List[int] List[int]# step 4. get point coordinates from the detection results
# let's just fetch one instead all of the in a loop
detection = detections[0]
left,top,right,bottom = detection .left(),detection .top(),detection .right(),detection .bottom()# step x : now you can do whatever you want since you've already got what you want.

CUDA版本人脸检测算法

检测步骤

  1. 下载模型文件
  2. 加载模型文件,生成卷积神经网络人脸检测对象
  3. 传入image,数据格式为numpy.ndarray
  4. (可选项) 获取分数值和检测列表
  5. 获取人脸坐标

示例代码

import dlib
# step 1. make sure you have downloaded the correct model file
face_detector_model_path = '../models/mmod_human_face_detector.dat'# step 2. load this model and create a cnn face detector
face_detector = dlib.cnn_face_detection_model_v1(face_detector_model_path)  # dlib.cnn_face_detection_model_v1# step 3. read an image using dlib or cv2
# note that the difference between the image data formated as numpy.ndarray read by dlib and cv2 is that dlib read it channels as *R G B* order while cv2 read as *B G R*,so you should do one more step to convert the image if using cv2
image_path = "sample.jpg"
img = dlib.load_rgb_image(image_path)
# img = cv2.imread(image_path)
# img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)# step 4. predict and detect
detections = face_detector(img, 1)  # dlib.mmod_rectangles# step 5. get just one of the rectangle instead all of them ,the type is mmod_rectangle
detection = detections[0]  # dlib.mmod_rectangle
# the mmod_rectangle contains two parts : confidence and rect
print(detection.confidence, detection.rect)# step 6.get face coordinates for just one as sample
left,top,right,bottom = detection.rect.left(),detection.rect.top(),detection.rect.right(),detection.rect.bottom()# step x. do anything you would like to

类定义与接口源码

人脸检测中用到的重要的类(概述)

  1. dlib.fhog_object_detector : hog模型的人脸检测对象,常用方法: __call()__run()
  2. dlib.rectangle:人脸检测结果,用于表示人脸的矩形区域,常用方法:left() right()``top()``bottom()
  3. dlib.cnn_face_detection_model_v1:卷积神经网络模型的人脸检测对象,常用方法: __call()__
  4. dlib.mmod_rectangle:人脸检测结果,包含了表示人脸的巨型区域以及检测置信度,成员包含: rectconfidence,其中,rectdlib.rectangle类型,confidencefloat类型
  5. dlib.mmod_rectangles:包含多个 dlib.mmod_rectangle对象

类定义参考链接:Python API 链接

fhog_object_detector类接口定义

fhog_object_detector类在源码中为C++类,这里使用伪代码编译观察其接口与调用方法

class dlib.fhog_object_detector():"""This object represents a sliding window histogram-of-oriented-gradients based object detector."""def __call__(self: dlib.fhog_object_detector, image: array, upsample_num_times: int = 0L) -> dlib.rectangles:"""requiresimage is a numpy ndarray containing either an 8bit grayscale or RGB image.upsample_num_times >= 0ensuresThis function runs the object detector on the input image and returns a list of detections.Upsamples the image upsample_num_times before running the basic detector."""def __init__(self: dlib.fhog_object_detector, arg0: unicode) -> None:'''Loads an object detector from a file that contains the output of the train_simple_object_detector() routine or a serialized C++ object of type object_detector<scan_fhog_pyramid<pyramid_down<6>>>.detection_window_heightdetection_window_widthnum_detectors'''passdef run(self: dlib.fhog_object_detector, image: array, upsample_num_times: int = 0L,adjust_threshold: float = 0.0) -> tuple:"""requiresimage is a numpy ndarray containing either an 8bit grayscale or RGB image.upsample_num_times >= 0ensuresThis function runs the object detector on the input image and returns a tuple of (list of detections, list of scores, list of weight_indices).Upsamples the image upsample_num_times before running the basic detector."""passdef run_multiple(detectors: list, image: array, upsample_num_times: int = 0L, adjust_threshold: float = 0.0)->tuple:"""requiresdetectors is a list of detectors.image is a numpy ndarray containing either an 8bit grayscale or RGB image.upsample_num_times >= 0ensuresThis function runs the list of object detectors at once on the input image and returns a tuple of (list of detections, list of scores, list of weight_indices).Upsamples the image upsample_num_times before running the basic detector."""passdef save(self: dlib.fhog_object_detector, detector_output_filename: unicode)->None:'''Save a simple_object_detector to the provided path.'''pass

rectangle类接口定义

class dlib.rectangleThis object represents a rectangular area of an image.__init__(*args, **kwargs)Overloaded function.__init__(self: dlib.rectangle, left: int, top: int, right: int, bottom: int) -> None__init__(self: dlib.rectangle, rect: dlib::drectangle) -> None__init__(self: dlib.rectangle, rect: dlib.rectangle) -> None__init__(self: dlib.rectangle) -> Nonearea(self: dlib.rectangle) → intbl_corner(self: dlib.rectangle) → dlib.pointReturns the bottom left corner of the rectangle.bottom(self: dlib.rectangle) → intbr_corner(self: dlib.rectangle) → dlib.pointReturns the bottom right corner of the rectangle.center(self: dlib.rectangle) → dlib.pointcontains(*args, **kwargs)Overloaded function.contains(self: dlib.rectangle, point: dlib.point) -> boolcontains(self: dlib.rectangle, point: dlib.dpoint) -> boolcontains(self: dlib.rectangle, x: int, y: int) -> boolcontains(self: dlib.rectangle, rectangle: dlib.rectangle) -> booldcenter(self: dlib.rectangle) → dlib.pointheight(self: dlib.rectangle) → intintersect(self: dlib.rectangle, rectangle: dlib.rectangle) → dlib.rectangleis_empty(self: dlib.rectangle) → boolleft(self: dlib.rectangle) → intright(self: dlib.rectangle) → inttl_corner(self: dlib.rectangle) → dlib.pointReturns the top left corner of the rectangle.top(self: dlib.rectangle) → inttr_corner(self: dlib.rectangle) → dlib.pointReturns the top right corner of the rectangle.width(self: dlib.rectangle) → int

cnn_face_detection_model_v1类定义

class dlib.cnn_face_detection_model_v1This object detects human faces in an image. The constructor loads the face detection model from a file. You can download a pre-trained model from http://dlib.net/files/mmod_human_face_detector.dat.bz2.__call__(*args, **kwargs)Overloaded function.__call__(self: dlib.cnn_face_detection_model_v1, imgs: list, upsample_num_times: int=0L, batch_size: int=128L) -> std::vector<std::vector<dlib::mmod_rect, std::allocator<dlib::mmod_rect> >, std::allocator<std::vector<dlib::mmod_rect, std::allocator<dlib::mmod_rect> > > >takes a list of images as input returning a 2d list of mmod rectangles__call__(self: dlib.cnn_face_detection_model_v1, img: array, upsample_num_times: int=0L) -> std::vector<dlib::mmod_rect, std::allocator<dlib::mmod_rect> >Find faces in an image using a deep learning model.Upsamples the image upsample_num_times before running the face detector.__init__(self: dlib.cnn_face_detection_model_v1, filename: unicode) → Non

mmod_rectangle mmod_rectangles mmod_rectangless

class dlib.mmod_rectangleWrapper around a rectangle object and a detection confidence score.__init__x.__init__(...) initializes x; see help(type(x)) for signatureconfidencerect
class dlib.mmod_rectanglesAn array of mmod rectangle objects.__init__(*args, **kwargs)Overloaded function.__init__(self: dlib.mmod_rectangles) -> None__init__(self: dlib.mmod_rectangles, arg0: dlib.mmod_rectangles) -> NoneCopy constructor__init__(self: dlib.mmod_rectangles, arg0: iterable) -> Noneappend(self: dlib.mmod_rectangles, x: dlib.mmod_rectangle) → NoneAdd an item to the end of the listcount(self: dlib.mmod_rectangles, x: dlib.mmod_rectangle) → intReturn the number of times x appears in the listextend(*args, **kwargs)Overloaded function.extend(self: dlib.mmod_rectangles, L: dlib.mmod_rectangles) -> NoneExtend the list by appending all the items in the given listextend(self: dlib.mmod_rectangles, arg0: list) -> Noneinsert(self: dlib.mmod_rectangles, i: int, x: dlib.mmod_rectangle) → NoneInsert an item at a given position.pop(*args, **kwargs)Overloaded function.pop(self: dlib.mmod_rectangles) -> dlib.mmod_rectangleRemove and return the last itempop(self: dlib.mmod_rectangles, i: int) -> dlib.mmod_rectangleRemove and return the item at index iremove(self: dlib.mmod_rectangles, x: dlib.mmod_rectangle) → NoneRemove the first item from the list whose value is x. It is an error if there is no such item.
class dlib.mmod_rectanglessA 2D array of mmod rectangle objects.__init__(*args, **kwargs)Overloaded function.__init__(self: dlib.mmod_rectangless) -> None__init__(self: dlib.mmod_rectangless, arg0: dlib.mmod_rectangless) -> NoneCopy constructor__init__(self: dlib.mmod_rectangless, arg0: iterable) -> Noneappend(self: dlib.mmod_rectangless, x: dlib.mmod_rectangles) → NoneAdd an item to the end of the listcount(self: dlib.mmod_rectangless, x: dlib.mmod_rectangles) → intReturn the number of times x appears in the listextend(*args, **kwargs)Overloaded function.extend(self: dlib.mmod_rectangless, L: dlib.mmod_rectangless) -> NoneExtend the list by appending all the items in the given listextend(self: dlib.mmod_rectangless, arg0: list) -> Noneinsert(self: dlib.mmod_rectangless, i: int, x: dlib.mmod_rectangles) → NoneInsert an item at a given position.pop(*args, **kwargs)Overloaded function.pop(self: dlib.mmod_rectangless) -> dlib.mmod_rectanglesRemove and return the last itempop(self: dlib.mmod_rectangless, i: int) -> dlib.mmod_rectanglesRemove and return the item at index iremove(self: dlib.mmod_rectangless, x: dlib.mmod_rectangles) → NoneRemove the first item from the list whose value is x. It is an error if there is no such item.

dlib实现人脸检测方法相关推荐

  1. OpenCV-Python实战(14)——人脸检测详解(仅需6行代码学会4种人脸检测方法)

    OpenCV-Python实战(14)--人脸检测详解(仅需6行代码学会4种人脸检测方法) 0. 前言 1. 人脸处理简介 2. 安装人脸处理相关库 2.1 安装 dlib 2.2 安装 face_r ...

  2. 人脸检测方法研究记录

    --------------------- 本文来自 HamTam12 的CSDN 博客 ,全文地址请点击:https://blog.csdn.net/sinat_14916279/article/d ...

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

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

  4. dlib做人脸检测判断有没有检测到人脸

    dlib做人脸检测判断有没有检测到人脸 文章目录: 一.人脸检测 二.判断有没有检测到人脸 其实我用dlib 检测人脸只是用来做个触发条件,但是我不知道怎么判断有没有检测到人,因为从返回值只知道是一个 ...

  5. python+opencv+dlib实现人脸检测与表情识别

    python+opencv+dlib实现人脸检测与表情识别 一,dlib简单介绍:Dlib包含广泛的机器学习算法.所有的设计都是高度模块化的,快速执行,并且通过一个干净而现代的C ++ API,使用起 ...

  6. matlab中facedetector,Matlab人脸检测方法(Face Parts Detection)详解

    今天同学让我帮忙制作一个人脸表情识别的样本库,其中主要是对人脸进行裁剪,这里用到了一个相对较新的Matlab人脸检测方法Face Parts Detection,网上百度了一下发现关于Matlab人脸 ...

  7. 关于人脸检测方法的比较

    通过阅读文献总结了一下不同人脸检测方法的优缺点. 首先人脸检测方法大致分为三类吧, 1)基于特征的人脸检测 整体轮廓法 肤色检测法 器官分布法 2)模板匹配法的人脸检测 镶嵌图法(又称马赛克法) 预定 ...

  8. dlib实现人脸识别方法

    文章目录 概述 方法实现 实现步骤 示例代码 重载方法 概述 此示例演示如何使用dlib作为人脸识别工具,dlib提供一个方法可将人脸图片数据映射到128维度的空间向量,如果两张图片来源于同一个人,那 ...

  9. 基于dlib进行人脸检测

    1.dlib的基本概念 1. Dlib是一个深度学习开源工具,基于C++开发,也支持Python开发接口. 2. 由于Dlib对于人脸特征提取支持很好,有很多训练好的人脸特征提取模型供开发者使用,所以 ...

最新文章

  1. 程序设置横屏后,锁屏时会被销毁一遍,解锁时又重新加载onCreat的问题解决
  2. Linux 创建桌面应用程序图标 (Ubuntu 18.04 16.04、Linux Mint、Deepin、等均适用 )
  3. 点击回应、关闭确认以及另一种获取设备环境句柄的方法
  4. 【三次优化】剑指 Offer 35. 复杂链表的复制
  5. HDU 1248 寒冰王座(完全背包问题另类解法)
  6. Linux内核调试的方式以及工具集锦
  7. java struts2 上传图片_Java框架Struts2实现图片上传功能
  8. SAP License:为什么要划分物料组
  9. Intel 加速分布式计算系统的三个神器
  10. python基础之变量类型和数据运算
  11. mysql 里的1对n虚线_mysql workbench EER图,里面的实线以及虚线的关系
  12. Linux终端配色和Xshell命令行配色
  13. 网络安全概论 第四章 电子邮件安全加密(PGP)
  14. 重新启动计算机以关闭用户账户控制,win10如何彻底关闭用户帐户控制?
  15. 2020-2021追番报告
  16. Android系统增加字体库及修改系统默认字体
  17. 冥土追魂(暴力枚举)
  18. java应用程序接口批量访问_spring中使用mybatis实现批量插入的示例代码
  19. 元认知能力-认知的理解
  20. Flink Table和SQL的表和视图、Connectors和timestamp数据类型

热门文章

  1. 二维码在微信中无法保存的解决办法
  2. 从理论到实战|深度学习项目从训练到部署全流程技术
  3. 记2018湖南CCF-CCSP
  4. 分享35套非常华丽的免费 PSD 网页按钮素材!
  5. Linux基础命令收集
  6. 地理信息培训考试(all)20+min90+
  7. [No00003E]26个字母暗藏的单词秘密
  8. 功课数学分析(一) 第一讲
  9. 详解(Spring Ioc)本质 DI
  10. epic转移游戏_游戏圈刷屏!惊呼为“业界革命”的虚幻引擎5,是个啥?