Python三十行代码实现简单人脸识别的示例代码
作者:徐先生没洗头 时间:2022-08-29 18:07:49
一、库介绍
opencv,face_recognition,numpy,以及dlib
注意:
安装opencv速度可能过慢,需要更换国内镜像源,参考:https://www.jb51.net/article/208359.htm
附带Python3.7,64位版本 dlib whl下载路径:dlib-19_jb51.rar
二、库安装
pip install opencv-python
pip install face_recognition
pip install numpy
dlib库需进入whl文件路径下安装
pip install dlib-19.17.99-cp37-cp37m-win_amd64.whl
三、face_recognition库简单介绍
face_recognition的load_image_file方法会加载图片,并返回一个ndarray类型的数据
face_path = "C://Users//25103//Desktop//Python人脸识别//face//徐先生.jpg"
image = face_recognition.load_image_file(face_path)
face_recognition的face_encoding方法,可从返回的ndarray类型数据中提取人脸特征,可同时提取多个特征,返回值为列表类型
face_encoding = face_recognition.face_encodings(image)[0]
face_recognition的face_location方法可以获取图片中所有人脸的位置,其返回值为一个列表
face_locations = face_recognition.face_locations(rgb_frame)
四、代码实现以及注释讲解
# coding = utf-8
import dlib
import cv2
import face_recognition
import os
# 创建视频对象
video_capture = cv2.VideoCapture(0)
# 加载需要识别的人脸图片(这张图片需要仅有一张脸)
# face_recognition的load_image_file方法会加载图片,并返回一个ndarray类型的数据
# ndarray类型就是NumPy的数组类型,其中的元素类型可以一致也可以不一致
face_path = "C://Users//25103//Desktop//Python人脸识别//face//徐先生.jpg"
image = face_recognition.load_image_file(face_path)
# face_recognition的face_encoding方法,可从返回的ndarray类型数据中提取人脸特征,可同时提取多个特征,返回值为列表类型
# 因为照片中只有一个人脸,所以我们取列表的第一个值
face_encoding = face_recognition.face_encodings(image)[0]
while True:
# 从视频对象中读取一帧照片
ret,frame = video_capture.read()
# 将照片缩小,加快处理速度,这里将其缩小为原图的1/4
# frame = cv2.rectangle(frame,(0,0),fx=0.25,fy=0.25)
# 因为cv2用的是BGR色彩,我们组要将其转化为RGB进行处理
rgb_frame = frame[:,:,::-1] # 列表转置操作
# face_recognition的face_location方法可以获取图片中所有人脸的位置,其返回值为一个列表
face_locations = face_recognition.face_locations(rgb_frame)
print("共从视频中找到了{}张人脸".format(len(face_locations)))
# 获取视频中所有人脸的特征
face_encodings = face_recognition.face_encodings(rgb_frame,face_locations)
for face in face_encodings:
# 比较两个特征值——encoding1与encoding2,匹配返回True,否则返回False。tolerance越低,顾名思义,容错率越低,返回值为列表类型
match = face_recognition.compare_faces([face_encoding],face,tolerance=0.4)
name = "不认识的人"
if match[0]:
# face为图片名称
name = os.path.basename(face_path[0:-4])
print("找到了{}".format(name))
来源:https://blog.csdn.net/X_xs_mxt/article/details/115130201
标签:Python,人脸识别
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
python pandas处理excel表格数据的常用方法总结
2023-06-02 19:49:47
![](https://img.aspxhome.com/file/2023/4/83674_0s.png)
Python使用pickle模块储存对象操作示例
2021-05-17 00:29:49
如何用ASP创建日志文件
2008-03-10 17:27:00
Python3中的json模块使用详解
2021-09-27 22:21:22
Python实现k-means算法
2023-07-01 22:06:53
![](https://img.aspxhome.com/file/2023/8/79588_0s.png)
oracle移植到mysql注意事项
2008-12-25 13:09:00
在SUSE10环境下安装和配置MySQL数据库
2008-12-17 15:03:00
docker-py 用Python调用Docker接口的方法
2023-04-07 03:15:46
![](https://img.aspxhome.com/file/2023/5/77775_0s.jpg)
python实现简单购物商城
2022-01-23 06:47:39
asp dictionary对象的用法
2011-04-14 11:09:00
windows下MySQL5.6版本安装及配置过程附有截图和详细说明
2024-01-13 18:58:56
![](https://img.aspxhome.com/file/2023/5/65285_0s.png)
php删除一个路径下的所有文件夹和文件的方法
2024-06-05 09:51:11
MySQL远程无法连接的一些常见原因总结
2024-01-24 21:24:21
![](https://img.aspxhome.com/file/2023/4/79144_0s.png)
JavaScript substr() 字符串截取函数使用详解
2024-06-05 09:55:13
Python简单实现阿拉伯数字和罗马数字的互相转换功能示例
2021-08-29 08:52:57
![](https://img.aspxhome.com/file/2023/4/79794_0s.png)
使用Python的toolz库开始函数式编程的方法
2022-10-18 10:29:55
搭建websocket消息推送服务,必须要考虑的几个问题
2023-11-28 12:23:45
![](https://img.aspxhome.com/file/2023/2/135192_0s.png)
AspJpeg 2.0组件使用教程(GIF篇)
2008-12-16 19:37:00
![](https://img.aspxhome.com/file/UploadPic/200812/16/aspjpeg-10-48.gif)
ADO.NET数据连接池剖析
2023-06-27 17:23:29
![](https://img.aspxhome.com/file/UploadPic/201211/30/20121130203620988s.png)
vue实现评论列表功能
2023-07-02 17:05:29
![](https://img.aspxhome.com/file/2023/3/139923_0s.png)