人脸识别之face_recognition学习笔记(二)

本文详细介绍如何在Python中使用face_recognition库进行人脸检测、关键点定位及人脸识别。通过实例代码,展示从加载图片到识别图片中人物的全过程。

前面一篇文章学习了怎样使用命令行实现人脸的识别和检测

四、在python中使用face_recognition

通过在python 中导入face_recognition模块,可以使用丰富的API

import face_recognition

(一)在图片中定位人脸的位置

例子),使用下面的代码:

import face_recognition

# Load the jpg file into a numpy array
image = face_recognition.load_image_file("my_picture.jpg")

# Find all the faces in the image using the default HOG-based model.
# This method is fairly accurate, but not as accurate as the CNN model and not GPU accelerated.
# See also: find_faces_in_picture_cnn.py
face_locations = face_recognition.face_locations(image)

# face_locations is now an array listing the co-ordinates of each face!

上面的代码,使用默认的HOG-based模型,也可以使用cnn(例子)来达到更加准确的检测:(这种方法需要GPU)

import face_recognition

image = face_recognition.load_image_file("my_picture.jpg")
face_locations = face_recognition.face_locations(image, model="cnn")

# face_locations is now an array listing the co-ordinates of each face!

当有很多图片,比如视频需要处理,又有GPU加速的时候,参考这个案例

(二)识别单一图片中人脸的关键点

使用如下代码:(例子

import face_recognition

image = face_recognition.load_image_file("my_picture.jpg")
face_landmarks_list = face_recognition.face_landmarks(image)

# face_landmarks_list is now an array with the locations of each facial feature in each face.
# face_landmarks_list[0]['left_eye'] would be the location and outline of the first person's left eye.

通过这个简单的API接口,就可以十分方便地实现面部特征的关键点的提取;

(三)识别图片中人物是谁

使用如下代码:

import face_recognition

picture_of_me = face_recognition.load_image_file("me.jpg")
my_face_encoding = face_recognition.face_encodings(picture_of_me)[0]

# my_face_encoding now contains a universal 'encoding' of my facial features that can be compared to any other picture of a face!

unknown_picture = face_recognition.load_image_file("unknown.jpg")
unknown_face_encoding = face_recognition.face_encodings(unknown_picture)[0]

# Now we can see the two face encodings are of the same person with `compare_faces`!

results = face_recognition.compare_faces([my_face_encoding], unknown_face_encoding)

if results[0] == True:
    print("It's a picture of me!")
else:
    print("It's not a picture of me!")

这里其实是生成一个list,这个list包含有所有已知名字的人脸的encoding,然后在确定名字的时候,将未知名字的图片与list中的每一项进行对比,产生的结果也是一个list,这个list包含每一个对比的结果

更多python案例

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值