人脸识别之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案例

发布了23 篇原创文章 · 获赞 0 · 访问量 672

猜你喜欢

转载自blog.csdn.net/forever_008/article/details/103639592
今日推荐