1.安装

首先,必须提前安装cmake、numpy、dlib,其中,由于博主所用的python版本是3.6.4(为了防止不兼容,所以用之前的版本),只能安装19.7.0及之前版本的dlib,所以直接pip install dlib会报错,需要pip install dlib==19.7.0

安装完预备库之后就可以直接pip install face_recognition

2.应用

(1)提取人脸

import face_recognition
from PIL import Image
image = face_recognition.load_image_file("1.jpg")
face_locations = face_recognition.face_locations(image) # top, right, bottom, left
#以下展示提取的人脸
for face_location in face_locations:
# Print the location of each face in this image
top, right, bottom, left = face_location
# You can access the actual face itself like this:
face_image = image[top:bottom, left:right]
pil_image = Image.fromarray(face_image)
pil_image.show()

(2)查找面部特征轮廓线

import face_recognition
from PIL import Image,ImageDraw
image = face_recognition.load_image_file("1.jpg")
face_landmarks_list = face_recognition.face_landmarks(image)
#以下为展示轮廓线
pil_image = Image.fromarray(image)
d = ImageDraw.Draw(pil_image)
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:
d.line(face_landmarks[facial_feature], width=5)
del d
pil_image.show()

(3)比较人脸

import face_recognition
known_image = face_recognition.load_image_file("known_person.jpg")
unknown_image = face_recognition.load_image_file("unknown.jpg") biden_encoding = face_recognition.face_encodings(known_image)[0]
unknown_encoding = face_recognition.face_encodings(unknown_image)[0] results = face_recognition.compare_faces([biden_encoding], unknown_encoding)

(4)同时识别多张人脸

①使用pillow库

#使用pillow库
import face_recognition
from PIL import Image, ImageDraw # Load a second sample picture and learn how to recognize it.
first_image = face_recognition.load_image_file("3.jpg")
first_face_encoding = face_recognition.face_encodings(first_image)[0] second_image = face_recognition.load_image_file("5.jpg")
second_face_encoding = face_recognition.face_encodings(second_image)[0] # Create arrays of known face encodings and their names
known_face_encodings = [
first_face_encoding,
second_face_encoding
]
known_face_names = [
"first",
"second"
] # Load an image with an unknown face
unknown_image = face_recognition.load_image_file("1.jpg") # Find all the faces and face encodings in the unknown image
unknown_face_locations = face_recognition.face_locations(unknown_image)
unknown_face_encodings = face_recognition.face_encodings(unknown_image, unknown_face_locations)
pil_image = Image.fromarray(unknown_image)
# Create a Pillow ImageDraw Draw instance to draw with
draw = ImageDraw.Draw(pil_image) # Loop through each face found in the unknown image
for (top, right, bottom, left), unknown_face_encoding in zip(unknown_face_locations, unknown_face_encodings):
# See if the face is a match for the known face(s)
matches = face_recognition.compare_faces(known_face_encodings, unknown_face_encoding, tolerance=0.5)
name = "Unknown"
# 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] # Draw a box around the face using the Pillow module
draw.rectangle(((left, top), (right, bottom)), outline=(0, 0, 255)) # Draw a label with a name below the face
text_width, text_height = draw.textsize(name)
draw.rectangle(((left, bottom-text_height-10), (right, bottom)), fill=(0, 0, 255), outline=(0, 0, 255))
draw.text((left+6, bottom-text_height-3), name, fill=(255, 255, 255, 255)) # Remove the drawing library from memory as per the Pillow docs
del draw
# Display the resulting image
pil_image.show()
②使用opencv库 #使用opencv库
import face_recognition
import cv2 # 人物名称的集合
known_face_names = ["first","second"]
face_locations = []
face_encodings = []
demo_names = []
process_this_demo = True # 本地图像一
first_image = face_recognition.load_image_file("1.jpg")
first_encoding = face_recognition.face_encodings(first_image)[0]
# 本地图像二
second_image = face_recognition.load_image_file("5.jpg")
second_encoding = face_recognition.face_encodings(second_image)[0] known_face_encodings = [first_encoding,second_encoding] # demo
path = "7.jpg"
demo = cv2.imread(path)
demo_image = face_recognition.load_image_file(path)
demo_encodings = face_recognition.face_encodings(demo_image)
rgb_demo = demo[:, :, ::-1]
demo_face_locations = face_recognition.face_locations(rgb_demo) for demo_encoding in demo_encodings:
# 默认为unknown
matches = face_recognition.compare_faces(known_face_encodings, demo_encoding,tolerance=0.5)
name = "unknown"
if True in matches:
first_match_index = matches.index(True)
name = known_face_names[first_match_index]
demo_names.append(name) # 将捕捉到的人脸显示出来
for (top, right, bottom, left), name in zip(demo_face_locations, demo_names):
# Scale back up face locations since the demo we detected in was scaled to 1/4 size
# 矩形框
cv2.rectangle(demo, (left, top), (right, bottom), (0, 0, 255), thickness=1)
#加上标签
cv2.rectangle(demo, (left, bottom-15), (right, bottom), (0, 0, 255), cv2.FILLED)
font = cv2.FONT_HERSHEY_DUPLEX
cv2.putText(demo, name, (left+5,bottom-3), font, 0.5, (255, 255, 255), 1 )
# Display
cv2.imshow("CJK's practice", demo)
cv2.waitKey(0)
cv2.destroyAllWindows()

(5)摄像头实时辨别人脸

import face_recognition
import cv2,time video_capture = cv2.VideoCapture(0)
# 本地图像一
first_image = face_recognition.load_image_file("1.jpg")
first_face_encoding = face_recognition.face_encodings(first_image)[0] # 本地图像二
second_image = face_recognition.load_image_file("3.jpg")
second_face_encoding = face_recognition.face_encodings(second_image)[0] # 本地图片三
third_image = face_recognition.load_image_file("5.jpg")
third_face_encoding = face_recognition.face_encodings(third_image)[0] # Create arrays of known face encodings and their names
# 脸部特征数据的集合
known_face_encodings = [
first_face_encoding,
second_face_encoding,
third_face_encoding
]
# 人物名称的集合
known_face_names = [
"first",
"second",
"third"
]
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 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-15), (right, bottom), (0, 0, 255), cv2.FILLED)
font = cv2.FONT_HERSHEY_DUPLEX
cv2.putText(frame, name, (left+5, bottom-3), 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()

python face_recognition安装及各种应用的更多相关文章

  1. 手把手教你用1行代码实现人脸识别 --Python Face_recognition

    环境要求: Ubuntu17.10 Python 2.7.14 环境搭建: 1. 安装 Ubuntu17.10 > 安装步骤在这里 2. 安装 Python2.7.14 (Ubuntu17.10 ...

  2. Python的安装和详细配置

    Python是一种面向对象.解释型计算机程序设计语言.被认为是比较好的胶水语言.至于其他的,你可以去百度一下.本文仅介绍python的安装和配置,供刚入门的朋友快速搭建自己的学习和开发环境.本人欢迎大 ...

  3. python requests 安装

    在 windows 系统下,只需要输入命令 pip install requests ,即可安装. 在 linux 系统下,只需要输入命令 sudo  pip install requests ,即可 ...

  4. Python 的安装与配置(Windows)

    Python2.7安装配置 python的官网地址:https://www.python.org/ 我这里下载的是python2.7.12版本的 下载后点击安装文件,直接点击下一步知道finally完 ...

  5. 初学python之安装Jupyter notebook

    一开始安装python的时候,安装的是最新版的python3.6的最新版.而且怕出问题,选择的都是默认安装路径.以为这样总不会出什么问题.一开始确实这样,安装modgodb等一切顺利.然而在安装jup ...

  6. 转: python如何安装pip和easy_installer工具

    原文地址: http://blog.chinaunix.net/uid-12014716-id-3859827.html 1.在以下地址下载最新的PIP安装文件:http://pypi.python. ...

  7. CentOS 6.5升级Python和安装IPython

    <转自:http://www.noanylove.com/2014/10/centos-6-5-sheng-ji-python-he-an-zhuang-ipython/>自己常用.以做备 ...

  8. python Scrapy安装和介绍

    python Scrapy安装和介绍 Windows7下安装1.执行easy_install Scrapy Centos6.5下安装 1.库文件安装yum install libxslt-devel ...

  9. window下从python开始安装科学计算环境

    Numpy等Python科学计算包的安装与配置 参考: 1.下载并安装 http://www.jb51.net/article/61810.htm 1.安装easy_install,就是为了我们安装第 ...

随机推荐

  1. 一文带你认知定时消息发布RocketMQ

    摘要:DMS任意时间定时消息能力发布. DMS是华为云的分布式消息中间件服务.适用于解决分布式架构中的系统解耦.跨系统跨地域数据流通.分布式事务协调等难题,协助构建优雅的现代化应用架构,提供可兼容 K ...

  2. JavaScript 基础知识(一):对象以及原型

    前言 JavaScript 常被描述为一种基于原型的语言--每个对象拥有一个原型对象,对象以其原型为模板.从原型继承方法和属性.原型对象也可能拥有原型,并从中继承方法和属性,一层一层.以此类推.这种关 ...

  3. HDFS的读写流程——宏观与微观

    HDFS的读写流程--宏观与微观 HDFS:分布式文件系统,负责存放数据 分布式文件系统:就是将我们的数据放到多台电脑上存储. 写数据:就是将客户端上的数据上传到HDFS 宏观过程 客户端向HDFS发 ...

  4. ORA-01950: no privileges on tablespace 'USERS'-- 解决办法

    ORA-01950: no privileges on tablespace 'USERS'   原因: 在表空间 "USERS" 无权限 解决办法:   用户登录,查看当前用户所 ...

  5. 【NOI P模拟赛】寻找道路(bfs,最短路)

    题面 一道特殊的最短路题. 给一个 n n n 个点 m m m 条有向边的图,每条边上有数字 0 \tt0 0 或 1 \tt1 1 ,定义一个路径的长度为这个路径上依次经过的边上的数字拼在一起后在 ...

  6. 【Java】学习路径63-反射、类的加载-附思维导图(完结)

    这一章的知识在实际开发也没有那么重要,主要是了解即可,另外掌握如何使用反射机制. 类的使用: 在虚拟机中: 类的加载->类的连接->类的初始化 类的加载   只会加载需要用到的类,加载到内 ...

  7. 【Java】学习路径48-线程锁ReentrantLock

    与上一章学习的线程锁synchronized类似,都是为了解决线程安全的问题. 使用方法: 新建一个ReentrantLock对象.(如果使用Thread多线程,则需要声明static静态) 然后在需 ...

  8. Android Kotlin Annotation Processer

    Annotation Processer 注解处理器(Annotation Processer)是javac内置的注解处理工具,可以在编译时处理注解,让我们自己做相应的处理.比如生成重复度很高的代码, ...

  9. KingbaseES ALTER TABLE 中 USING 子句的用法

    using子句用于在修改表字段类型的时候,进行显示的转换类型. 1.建表 create table t(id integer); 2.插入数据 insert into t select generat ...

  10. KingbaseES R3 集群主备切换信号量(semctl)错误故障分析案例

    案例说明: 某项目KingbaseES R3 一主一备流复制集群在主备切换测试中出现故障,导致主备无法正常切换:由于bm要求,数据库相关日志无法从主机中获取,只能在现场进行分析:通过对比主备切换时的时 ...