python人脸识别需要的库_基于python的face_recognition库的人脸识别

安装配置

要安装face_recognition库,首先需要安装dlib,直接安装时会在安装dlib时出错,可能报错也可能会卡在那不动。因为pip在编译dlib时会出错,所以我们需要手动编译dlib再进行安装。

1、

pip install dlib

一般来说可能会报错,所以可以下载 来进行安装(cmd进入whl所在文件夹后使用pip安装)

2、安装完dlib之后即可安装face_recognition库

pip install face_recognition

调用一下库,检查是否成功导入。

3、安装opencv库

pip install opencv-python

编写人脸识别程序

代码如下

# -*- coding: UTF-8 -*-

import face_recognition

import cv2

import os

# 这是一个超级简单(但很慢)的例子,在你的网络摄像头上实时运行人脸识别

# PLEASE NOTE: This example requires OpenCV (the `cv2` library) to be installed only to read from your webcam.

# 请注意:这个例子需要安装OpenCV

# 具体的演示。如果你安装它有困难,试试其他不需要它的演示。

# 得到一个参考的摄像头# 0(默认)

video_capture = cv2.VideoCapture(0)

# 加载示例图片并学习如何识别它。

path ="images"#在同级目录下的images文件中放需要被识别出的人物图

total_image=[]

total_image_name=[]

total_face_encoding=[]

for fn in os.listdir(path): #fn 表示的是文件名

total_face_encoding.append(face_recognition.face_encodings(face_recognition.load_image_file(path+"/"+fn))[0])

fn=fn[:(len(fn)-4)]#截取图片名(这里应该把images文件中的图片名命名为为人物名)

total_image_name.append(fn)#图片名字列表

while True:

# 抓取一帧视频

ret, frame = video_capture.read()

# 发现在视频帧所有的脸和face_enqcodings

face_locations = face_recognition.face_locations(frame)

face_encodings = face_recognition.face_encodings(frame, face_locations)

# 在这个视频帧中循环遍历每个人脸

for (top, right, bottom, left), face_encoding in zip(face_locations, face_encodings):

# 看看面部是否与已知人脸相匹配。

for i,v in enumerate(total_face_encoding):

match = face_recognition.compare_faces([v], face_encoding,tolerance=0.5)

name = "Unknown"

if match[0]:

name = total_image_name[i]

break

# 画出一个框,框住脸

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)

# 显示结果图像

cv2.imshow('Video', frame)

# 按q退出

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

break

# 释放摄像头中的流

video_capture.release()

cv2.destroyAllWindows()

其次还需要在同级目录下新建一个images文件夹来进行摄像头的人脸图片对比。

代码原理如下:

1.遍历images文件中的图片

2.提取特征脸

3.摄像头每帧提取图片,提取特诊脸

4.遍历特征列表,找出符合特征脸

5.输出名字

最后,运行程序之后按q退出程序。

原文链接:https://blog.csdn.net/duanyijiangzhi/article/details/105084963

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值