基于python3、 face_recognition 实现人脸检测

face_recognition 是一个python的开源人脸识别库  号称是识别率百分之99  (虽然我没感觉到)网上资料非常多,而且用这个做实时性的人脸识别效率还可以(虽然初始化慢...)  

话不多说上代码


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

# 导入pil模块 ,可用命令安装 apt-get install python-Imaging
from PIL import Image
# 导入face_recogntion模块,可用命令安装 pip install face_recognition
import face_recognition
import cv2
import time
# 将jpg文件加载到numpy 数组中
t=time.time()
image = face_recognition.load_image_file("yiqi.jpg")
frame=cv2.imread("yiqi.jpg")
# 使用默认的给予HOG模型查找图像中所有人脸
# 这个方法已经相当准确了,但还是不如CNN模型那么准确,因为没有使用GPU加速
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))
# 指定人脸的位置信息,然后显示人脸图片
        cv2.rectangle(frame, (left, top), (right, bottom), (0, 0, 255), 2)
cv2.imshow('tuxiang',frame)
cv2.waitKey(1)  #刷新界面 不然只会呈现灰色
print('运行时间{}'.format(time.time()-t))
time.sleep(5)  #暂停五秒  展示图片

效果图

 

运行时间 0.36700010299682617s

安装face_recognition这个库还是有点麻烦的 推荐这个链接

https://blog.csdn.net/qq_15192373/article/details/78623741 可以参考这个 python3.6以上安装face_recogntion就会很简单

虽然安装可能会麻烦些,但是用起来确实很简单,人脸检测直接使用face_recognition.face_locations()方法就可以了。

使用到了opencv模块 主要是用来呈现图片以及对人脸标框(cv2.rectangle)

使用到了time库 主要是为了暂停看一下效果 和用来检测一下运行时间(time.time()函数)

如果想使用face_recognition 做人脸识别,可以看我博客 里面有。

如有问题,或有什么建议可加群:894243022或发邮箱[email protected] 

该文章有使用链接,如有侵权还请见谅。使用本文章或代码还请声明。

猜你喜欢

转载自blog.csdn.net/Nirvana_6174/article/details/83116431
今日推荐