一、环境搭建

1.系统环境

Ubuntu 17.04

Python 2.7.14

pycharm 开发工具

2.开发环境,安装各种系统包

人脸检测基于dlib,dlib依赖Boost和cmake

$ sudo apt-get install build-essential cmake

$ sudo apt-get install libgtk-3-dev

$ sudo apt-get install libboost-all-dev

其他重要的包

$ pip install numpy

$ pip install scipy

$ pip install opencv-python

$ pip install dlib

安装 face_recognition

# 安装 face_recognition

$ pip install face_recognition

# 安装face_recognition过程中会自动安装 numpy、scipy 等

二、使用教程

1、facial_features文件夹

此demo主要展示了识别指定图片中人脸的特征数据,下面就是人脸的八个特征,我们就是要获取特征数据

'chin',

'left_eyebrow',

'right_eyebrow',

'nose_bridge',

'nose_tip',

'left_eye',

'right_eye',

'top_lip',

'bottom_lip'

运行结果:

自动识别图片中的人脸,并且识别它的特征

原图:

特征数据,数据就是运行出来的矩阵,也就是一个二维数组

代码:

# -*- coding: utf-8 -*-

# 自动识别人脸特征

# filename : find_facial_features_in_picture.py

# 导入pil模块 ,可用命令安装 apt-get install python-Imaging

from PIL import Image, ImageDraw

# 导入face_recogntion模块,可用命令安装 pip install face_recognition

import face_recognition

# 将jpg文件加载到numpy 数组中

image = face_recognition.load_image_file("chenduling.jpg")

#查找图像中所有面部的所有面部特征

face_landmarks_list = face_recognition.face_landmarks(image)

print("I found {} face(s) in this photograph.".format(len(face_landmarks_list)))

for face_landmarks in face_landmarks_list:

#打印此图像中每个面部特征的位置

facial_features = [

'chin',

'left_eyebrow',

'right_eyebrow',

'nose_bridge',

'nose_tip',

'left_eye',

'right_eye',

'top_lip',

'bottom_lip'

]

for facial_feature in facial_features:

print("The {} in this face has the following points: {}".format(facial_feature, face_landmarks[facial_feature]))

#让我们在图像中描绘出每个人脸特征!

pil_image = Image.fromarray(image)

d = ImageDraw.Draw(pil_image)

for facial_feature in facial_features:

d.line(face_landmarks[facial_feature], width=5)

pil_image.show()

2、find_face文件夹

不仅能识别出来所有的人脸,而且可以将其截图挨个显示出来,打印在前台窗口

原始的图片

识别的图片

代码:

# -*- coding: utf-8 -*-

# 识别图片中的所有人脸并显示出来

# filename : find_faces_in_picture.py

# 导入pil模块 ,可用命令安装 apt-get install python-Imaging

from PIL import Image

# 导入face_recogntion模块,可用命令安装 pip install face_recognition

import face_recognition

# 将jpg文件加载到numpy 数组中

image = face_recognition.load_image_file("yiqi.jpg")

# 使用默认的给予HOG模型查找图像中所有人脸

# 这个方法已经相当准确了,但还是不如CNN模型那么准确,因为没有使用GPU加速

# 另请参见: find_faces_in_picture_cnn.py

face_locations = face_recognition.face_locations(image)

# 使用CNN模型

# face_locations = face_recognition.face_locations(image, number_of_times_to_upsample=0, model="cnn")

# 打印:我从图片中找到了 多少 张人脸

print("I found {} face(s) in this photograph.".format(len(face_locations)))

# 循环找到的所有人脸

for face_location in face_locations:

# 打印每张脸的位置信息

top, right, bottom, left = face_location

print("A face is located at pixel location Top: {}, Left: {}, Bottom: {}, Right: {}".format(top, left, bottom, right))

# 指定人脸的位置信息,然后显示人脸图片

face_image = image[top:bottom, left:right]

pil_image = Image.fromarray(face_image)

pil_image.show()

#### 3、know_face文件夹

通过设定的人脸图片识别未知图片中的人脸

# -*- coding: utf-8 -*-

# 识别人脸鉴定是哪个人

# 导入face_recogntion模块,可用命令安装 pip install face_recognition

import face_recognition

#将jpg文件加载到numpy数组中

chen_image = face_recognition.load_image_file("chenduling.jpg")

#要识别的图片

unknown_image = face_recognition.load_image_file("sunyizheng.jpg")

#获取每个图像文件中每个面部的面部编码

#由于每个图像中可能有多个面,所以返回一个编码列表。

#但是由于我知道每个图像只有一个脸,我只关心每个图像中的第一个编码,所以我取索引0。

chen_face_encoding = face_recognition.face_encodings(chen_image)[0]

print("chen_face_encoding:{}".format(chen_face_encoding))

unknown_face_encoding = face_recognition.face_encodings(unknown_image)[0]

print("unknown_face_encoding :{}".format(unknown_face_encoding))

known_faces = [

chen_face_encoding

]

#结果是True/false的数组,未知面孔known_faces阵列中的任何人相匹配的结果

results = face_recognition.compare_faces(known_faces, unknown_face_encoding)

print("result :{}".format(results))

print("这个未知面孔是 陈都灵 吗? {}".format(results[0]))

print("这个未知面孔是 我们从未见过的新面孔吗? {}".format(not True in results))

4、video文件夹

通过调用电脑摄像头动态获取视频内的人脸,将其和我们指定的图片集进行匹配,可以告知我们视频内的人脸是否是我们设定好的

实现:

代码:

# -*- coding: utf-8 -*-

# 摄像头头像识别

import face_recognition

import cv2

video_capture = cv2.VideoCapture(0)

# 本地图像

chenduling_image = face_recognition.load_image_file("chenduling.jpg")

chenduling_face_encoding = face_recognition.face_encodings(chenduling_image)[0]

# 本地图像二

sunyizheng_image = face_recognition.load_image_file("sunyizheng.jpg")

sunyizheng_face_encoding = face_recognition.face_encodings(sunyizheng_image)[0]

# 本地图片三

zhangzetian_image = face_recognition.load_image_file("zhangzetian.jpg")

zhangzetian_face_encoding = face_recognition.face_encodings(zhangzetian_image)[0]

# Create arrays of known face encodings and their names

# 脸部特征数据的集合

known_face_encodings = [

chenduling_face_encoding,

sunyizheng_face_encoding,

zhangzetian_face_encoding

]

# 人物名称的集合

known_face_names = [

"michong",

"sunyizheng",

"chenduling"

]

face_locations = []

face_encodings = []

face_names = []

process_this_frame = True

while True:

# 读取摄像头画面

ret, frame = video_capture.read()

# 改变摄像头图像的大小,图像小,所做的计算就少

small_frame = cv2.resize(frame, (0, 0), fx=0.25, fy=0.25)

# opencv的图像是BGR格式的,而我们需要是的RGB格式的,因此需要进行一个转换。

rgb_small_frame = small_frame[:, :, ::-1]

# Only process every other frame of video to save time

if process_this_frame:

# 根据encoding来判断是不是同一个人,是就输出true,不是为flase

face_locations = face_recognition.face_locations(rgb_small_frame)

face_encodings = face_recognition.face_encodings(rgb_small_frame, face_locations)

face_names = []

for face_encoding in face_encodings:

# 默认为unknown

matches = face_recognition.compare_faces(known_face_encodings, face_encoding)

name = "Unknown"

# if match[0]:

# name = "michong"

# If a match was found in known_face_encodings, just use the first one.

if True in matches:

first_match_index = matches.index(True)

name = known_face_names[first_match_index]

face_names.append(name)

process_this_frame = not process_this_frame

# 将捕捉到的人脸显示出来

for (top, right, bottom, left), name in zip(face_locations, face_names):

# Scale back up face locations since the frame we detected in was scaled to 1/4 size

top *= 4

right *= 4

bottom *= 4

left *= 4

# 矩形框

cv2.rectangle(frame, (left, top), (right, bottom), (0, 0, 255), 2)

#加上标签

cv2.rectangle(frame, (left, bottom - 35), (right, bottom), (0, 0, 255), cv2.FILLED)

font = cv2.FONT_HERSHEY_DUPLEX

cv2.putText(frame, name, (left + 6, bottom - 6), font, 1.0, (255, 255, 255), 1)

# Display

cv2.imshow('monitor', frame)

# 按Q退出

if cv2.waitKey(1) & 0xFF == ord('q'):

break

video_capture.release()

cv2.destroyAllWindows()

5、boss文件夹

人脸识别用哪种python库_python的face_recognition人脸识别库的使用相关推荐

  1. python人脸_Python 使用 face_recognition 人脸识别

    Python 使用 face_recognition 人脸识别 人脸识别 face_recognition 是世界上最简单的人脸识别库. 使用 dlib 最先进的人脸识别功能构建建立深度学习,该模型准 ...

  2. opencv python考勤_Python+Opencv+Tkinter指纹识别与人脸识别的门禁兼考勤(二)

    一. 门禁考勤系统硬件设计 1 .硬件总体结构 PC端的intel处理器作为硬件平台的核心,是衡量系统能否达到标准的主要标志.本文结合考勤系统,采用intel i5处理器的PC与51单片机共同构建了门 ...

  3. python人脸识别实验报告总结_Python 使用 face_recognition 人脸识别

    Python 使用 face_recognition 人脸识别 人脸识别 face_recognition 是世界上最简单的人脸识别库. 使用 dlib 最先进的人脸识别功能构建建立深度学习,该模型准 ...

  4. python pyquery库_python解析HTML之:PyQuery库的介绍与使用

    前言 Python关于爬虫的库挺多的,也各有所长.了解前端的也都知道, jQuery 能够通过选择器精确定位 DOM 树中的目标并进行操作,所以我想如果能用 jQuery 去爬网页那就 cool 了. ...

  5. python升级第三方库_python一键升级所有第三方库

    import pip from subprocess import call for dist in pip.get_installed_distributions(): call("pip ...

  6. python beautifulsoup库_Python爬虫系列:BeautifulSoup库详解

    点击上方蓝字关注"程序员Bob"呀~ 每个人的生命都是通向自我的征途,是对一条道路的尝试,是一条小径的悄然召唤.人们从来都无法以绝对的自我之相存在,每一个人都在努力变成绝对自我,有 ...

  7. 移动端python开发_python前端之移动端库、框架及自动化和优化

    目的:学习移动端场景下的js事件:制作移动端特效常用的js库:介绍移动端常用开发框架Bootstrap:介绍动态样式语言less.sass.stylus的基本使用. 移动端js事件 移动端的操作方式和 ...

  8. 电容屏物体识别_一种基于触摸屏触摸点的物体识别方法与流程

    本发明涉及触摸屏触摸点物体识别技术领域,具体为一种基于触摸屏触摸点的物体识别方法. 背景技术: 多触点触摸屏支持多个触点同时输入,通过触摸屏的点的特征,进行物体识别是一个成熟的技术,以下简称物体识别为 ...

  9. 基于linux火焰识别算法,一种基于深度学习模型的火焰识别方法与流程

    本发明属于通信领域,具体涉及一种基于深度学习模型的火焰识别方法. 背景技术: 随着我国工业化与城镇水平的不断提高,现代设施大型公共建筑朝着空间大.进深广功能复杂的多元化方向发展,这对于防烟火朝着空间大 ...

最新文章

  1. 学web前端需要了解哪些常识
  2. 在查找预编译头时遇到意外的文件结尾。是否忘记了向源中添加“#include “pch.h“”?
  3. Struts2对象属性驱动
  4. 通用用户权限管理系统组件V3.8功能改进说明 - 行政审批流程组件的改进
  5. python 笔记 size-constrained-clustering (对类别大小做限制的聚类问题)
  6. dp 1.4协议_浅析关于HDMI接口与DP接口
  7. springboot 优雅停机_SpringBoot 优雅停止服务的几种方法 第309篇
  8. 例子---PHP与Form表单终结篇
  9. 关于tomcat的思考
  10. 不要在循环,条件或嵌套函数中调用 Hook
  11. iPad上浏览超大图像,kakadu和Jpeg2000在iOS上的例程
  12. 抠图 php中文网,ps cs3怎么抠图
  13. DOE全因子实验设计报告
  14. 《朱生豪情书全集》【手稿珍藏本】 梦中不识路,何以慰相思
  15. 基于SpringBoot+Bootstrap【爱码个人博客系统】附源码
  16. LVGL (8) 绘制流程
  17. 九连环解法java版
  18. 有限元与深度学习结合求解泊松方程-Petrov
  19. 《基于海思35xx nnie引擎进行经典目标检测算法模型推理》视频课程介绍
  20. C语言在屏幕上输出玫瑰花图片

热门文章

  1. 的计算机课小说,6 计算机课
  2. 考研政治——马克思原理唯物论之意识观
  3. Java基础 DAY01
  4. 茂林位置服务器,合肥北斗gps卫星定位系统-量身定制「茂林GPS运营中心」
  5. Android LCD(一):LCD基本原理篇(一/四)
  6. ajax后台如何把对象转为json_Ajax向前后台传递json和转换
  7. 乌尔维·阿西莫夫和 .art的不解情缘
  8. c语言循环移位寄存器,[转载]关于移位寄存器74HC164的使用
  9. Linux群组与文件权限
  10. arduino控制声音传感器