Python3利⽤face_recognition实现⼈脸识别的⽅法
前⾔
之前实践了下face++在线⼈脸识别版本,这回做⼀下离线版本。github 上⾯有关于face_recognition的相关资料,本⼈只是做个搬运⼯,对其中的⼀些内容进⾏搬运,对其中⼀些例⼦进⾏实现。
官⽅描述:
face_recognition是⼀个强⼤、简单、易上⼿的⼈脸识别开源项⽬,并且配备了完整的开发⽂档和应⽤案例,特别是兼容树莓派系统。本项⽬是世界上最简洁的⼈脸识别库,你可以使⽤Python和命令⾏⼯具提取、识别、操作⼈脸。本项⽬的⼈脸识别是基于业内领先的C++开源库 dlib中的深度学习模型,⽤Labeled Faces in the Wild⼈脸数据集进⾏测试,有⾼达99.38%的准确率。但对⼩孩和亚洲⼈脸的识别准确率尚待提升。
(关于兼容树莓派,以后有板⼦了再做⼀下)
下⾯两个链接划重点
环境配置
ubuntu16.04(其他环境的安装可以参考第⼀个链接,官⽅有说明)
pycharm(可忽略,怎么舒服怎么来)
python3
opencv(我的是4.1.2,三点⼏的版本应该也⼀样)
实际上只需要安装face_recognition,当然,没有opencv的也需要安装⼀下opencv
pip3 install face_recognition
图⽚准备
由于需要做⼀些图⽚的⽐对,因此需要准备⼀些图⽚,本⽂图⽚取⾃以下链接
接下来开始操作
官⽅还有提供命令⾏的操作(这个没去做),本⽂不做这个,我们只要是要在python中⽤face_recognition,因此定位到这⼀块。
这个api⽂档地址就是上⾯的第⼆个链接。进去之后可以看到:
part1.识别图⽚中的⼈是谁
代码ubuntu怎么安装python
# part1
# 识别图⽚中的⼈是谁
import face_recognition
known_image = face_recognition.load_image_file("lyf1.jpg")
unknown_image = face_recognition.load_image_file("lyf2.jpg")
lyf_encoding = face_recognition.face_encodings(known_image)[0]
unknown_encoding = face_recognition.face_encodings(unknown_image)[0]
results = face_recognitionpare_faces([lyf_encoding], unknown_encoding)
# A list of True/False values indicating which known_face_encodings match the face encoding to check
print(type(results))
print(results)
if results[0] == True:
print("yes")
else:
print("no")
结果
<class 'list'>
[True]
yes
part2.从图⽚中到⼈脸
代码
# part2
# 从图⽚中到⼈脸(定位⼈脸位置)
import face_recognition
import cv2
image = face_recognition.load_image_file("lyf1.jpg")
face_locations_useCNN = face_recognition.face_locations(image,model='cnn')
# model – Which face detection model to use. “hog” is less accurate but faster on CPUs.
# “cnn” is a more accurate deep-learning model which is GPU/CUDA accelerated (if available). The default is “hog”.
face_locations_noCNN=face_recognition.face_locations(image)
# A list of tuples of found face locations in css (top, right, bottom, left) order
# 因为返回值的顺序是这样⼦的,因此在后⾯的for循环⾥⾯赋值要注意按这个顺序来
print("face_location_useCNN:")
print(face_locations_useCNN)
face_num1=len(face_locations_useCNN)
print(face_num1) # The number of faces
print("face_location_noCNN:")
print(face_locations_noCNN)
face_num2=len(face_locations_noCNN)
print(face_num2) # The number of faces
# 到这⾥为⽌,可以观察两种情况的坐标和⼈脸数,⼀般来说,坐标会不⼀样,但是检测出来的⼈脸数应该是⼀样的# 也就是说face_num1 = face_num2; face_locations_useCNN 和 face_locations_noCNN 不⼀样
org = cv2.imread("lyf1.jpg")
img = cv2.imread("lyf1.jpg")
cv2.imshow("lyf1.jpg",img) # 原始图⽚
# Go to get the data and draw the rectangle
# use CNN
for i in range(0,face_num1):
top = face_locations_useCNN[i][0]
right = face_locations_useCNN[i][1]
bottom = face_locations_useCNN[i][2]
left = face_locations_useCNN[i][3]
start = (left, top)
end = (right, bottom)
color = (0,255,255)
thickness = 2
# Show the result
cv2.imshow("useCNN",img)
# for face_location in face_locations_noCNN:
#
# # Print the location of each face in this image
# top, right, bottom, left = face_location
# # 等价于下⾯的这种写法
for i in range(0,face_num2):
top = face_locations_noCNN[i][0]
right = face_locations_noCNN[i][1]
bottom = face_locations_noCNN[i][2]
left = face_locations_noCNN[i][3]
start = (left, top)
end = (right, bottom)
color = (0,255,255)
thickness = 2
cv2.imshow("no cnn ",org)
cv2.waitKey(0)
cv2.destroyAllWindows()
结果
face_location_useCNN:
[(223, 470, 427, 266)]
1
face_location_noCNN:
[(242, 489, 464, 266)]
1
图⽚效果⼤致是这样
part3.到⼈脸并将其裁剪打印出来(使⽤cnn定位⼈脸)
代码
# part3
# 到⼈脸并将其裁剪打印出来(使⽤cnn定位⼈脸)
from PIL import Image
import face_recognition
# Load the jpg file into a numpy array
image = face_recognition.load_image_file("lyf1.jpg")
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)) face_image = image[top:bottom, left:right]
pil_image = Image.fromarray(face_image)
pil_image.show()
结果
I found 1 face(s) in this photograph.
A face is located at pixel location Top: 205, Left: 276, Bottom: 440, Right: 512
图⽚效果⼤致是这样
part4.识别单张图⽚中⼈脸的关键点
代码
# part4 识别单张图⽚中⼈脸的关键点
from PIL import Image, ImageDraw
import face_recognition
# Load the jpg file into a numpy array
image = face_recognition.load_image_file("lyf1.jpg")
# Find all facial features in all the faces in the image
face_landmarks_list = face_recognition.face_landmarks(image)
# print(face_landmarks_list)
print("I found {} face(s) in this photograph.".format(len(face_landmarks_list)))
# Create a PIL imagedraw object so we can draw on the picture
pil_image = Image.fromarray(image)
d = ImageDraw.Draw(pil_image)
for face_landmarks in face_landmarks_list:
# Print the location of each facial feature in this image
for facial_feature in face_landmarks.keys():
print("The {} in this face has the following points: {}".format(facial_feature, face_landmarks[facial_feature]))
# Let's trace out each facial feature in the image with a line!
for facial_feature in face_landmarks.keys():
d.line(face_landmarks[facial_feature], width=5)
# Show the picture
pil_image.show()
结果
I found 1 face(s) in this photograph.
The left_eyebrow in this face has the following points: [(305, 285), (321, 276), (340, 277), (360, 281), (377, 288)] The right_eye in this face has the following points: [(422, 313), (432, 303), (446, 302), (459, 305), (449, 312), (435, 314)]
The nose_bridge in this face has the following points: [(394, 309), (394, 331), (395, 354), (396, 375)]
The right_eyebrow in this face has the following points: [(407, 287), (424, 278), (442, 273), (461, 272), (478, 279)] The bottom_lip in this face has the following points: [(429, 409), (419, 421), (408, 428), (398, 430), (389, 429), (377, 424), (364, 412), (370, 413), (389, 414), (398, 415), (407, 413), (423, 411)]
The chin in this face has the following points: [(289, 295), (291, 323), (296, 351), (303, 378), (315, 403), (332, 428), (353, 448), (376, 464), (400, 467), (422, 461), (441, 444), (459, 425), (473, 403), (484, 377), (490, 351), (493, 323), (493, 296)]
The top_lip in this face has the following points: [(364, 412), (377, 407), (389, 403), (397, 406), (406, 402), (417, 405), (429, 409), (423, 411), (406, 412), (397, 414), (389, 413), (370, 413)]
The left_eye in this face has the following points: [(327, 308), (339, 304), (353, 306), (364, 314), (352, 317), (338,
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论