face_recognition 基础接口
face_recognition使用世界上最简单的人脸识别库,在Python或命令行中识别和操作人脸。
使用dlib最先进的人脸识别技术构建而成,并具有深度学习功能。 该模型在Labeled Faces in the Wild基准中的准确率为99.38%。
查找图片中的面孔
# 导入face_recognition模块
import face_recognition # 查找图片中的面孔
# 将jpg文件添加到numpy数组中
image = face_recognition.load_image_file("1.jpg")
# 查找图片中人脸(上下左右)的位置,图像中可能有多个人脸
# face_locations可能的值类似为 [(135,536,198,474),()]
face_locations = face_recognition.face_locations(image) print(face_locations)
查找和操作图片中的面部特征
# 导入face_recognition模块
import face_recognition # 查找图片中人脸的所有面部特征(眉毛,眼睛,鼻子,上下嘴唇,面部轮廓)
# 将jpg文件添加到numpy数组中
image = face_recognition.load_image_file("1.jpg")
face_landmarks_list = face_recognition.face_landmarks(image) print(face_landmarks_list)
/usr/bin/python3.6 /home/wjw/PycharmProjects/face/find_nose.py [{'chin': [(280, 439), (282, 493), (283, 547), (290, 603), (308, 654), (340, 698), (380, 733), (427, 760), (485, 770), (544, 766), (592, 738), (634, 704), (668, 661), (689, 613), (701, 563), (712, 514), (722, 466)], 'left_eyebrow': [(327, 373), (354, 340), (395, 323), (442, 324), (487, 337)], 'right_eyebrow': [(560, 344), (603, 340), (647, 348), (682, 372), (698, 410)], 'nose_bridge': [(519, 410), (517, 444), (515, 477), (513, 512)], 'nose_tip': [(461, 548), (485, 554), (508, 561), (532, 558), (555, 556)], 'left_eye': [(372, 424), (399, 420), (426, 420), (451, 429), (424, 433), (397, 432)], 'right_eye': [(577, 440), (605, 437), (631, 442), (655, 451), (628, 454), (601, 449)], 'top_lip': [(415, 617), (452, 600), (484, 593), (506, 600), (525, 598), (551, 610), (579, 634), (566, 630), (524, 620), (504, 619), (482, 616), (428, 616)], 'bottom_lip': [(579, 634), (546, 636), (518, 636), (498, 635), (475, 632), (447, 626), (415, 617), (428, 616), (479, 605), (500, 610), (520, 610), (566, 630)]}] Process finished with exit code 0
美图
寻找面部特征对于许多重要的东西非常有用,比如美图。
from PIL import Image, ImageDraw
import face_recognition # 将图片文件添加到numpy数组中
image = face_recognition.load_image_file("1.jpg") # 查找图像中的所有面部特征
face_landmarks_list = face_recognition.face_landmarks(image) for face_landmarks in face_landmarks_list:
pil_image = Image.fromarray(image)
d = ImageDraw.Draw(pil_image, 'RGBA') # 美化眉毛
d.polygon(face_landmarks['left_eyebrow'], fill=(68, 54, 39, 128))
d.polygon(face_landmarks['right_eyebrow'], fill=(68, 54, 39, 128))
d.line(face_landmarks['left_eyebrow'], fill=(68, 54, 39, 150), width=5)
d.line(face_landmarks['right_eyebrow'], fill=(68, 54, 39, 150), width=5) # 嘴唇光泽
d.polygon(face_landmarks['top_lip'], fill=(150, 0, 0, 128))
d.polygon(face_landmarks['bottom_lip'], fill=(150, 0, 0, 128))
d.line(face_landmarks['top_lip'], fill=(150, 0, 0, 64), width=8)
d.line(face_landmarks['bottom_lip'], fill=(150, 0, 0, 64), width=8) # 闪耀的眼睛
d.polygon(face_landmarks['left_eye'], fill=(255, 255, 255, 30))
d.polygon(face_landmarks['right_eye'], fill=(255, 255, 255, 30)) # 涂一些眼线
d.line(face_landmarks['left_eye'] + [face_landmarks['left_eye'][0]], fill=(0, 0, 0, 110), width=6)
d.line(face_landmarks['right_eye'] + [face_landmarks['right_eye'][0]], fill=(0, 0, 0, 110), width=6) # 显示图片
pil_image.show()
(丑了哈?没关系,技术重要!!)
识别图片中的面孔
# 导入face_recognition模块
import face_recognition # 识别图像中出现的人脸
# 获取每个图像文件中每个面部的面部编码
known_image = face_recognition.load_image_file("zhangjie.jpg")
unknown_image = face_recognition.load_image_file("uknow.jpg") # 由于每个图像中可能有多个人脸,所以返回一个编码列表。
# 但是事先知道每个图像只有一个人脸,每个图像中的第一个编码,取索引0。
biden_encoding = face_recognition.face_encodings(known_image)[0]
unknown_encoding = face_recognition.face_encodings(unknown_image)[0] # 获取比较结果
result = face_recognition.compare_faces([biden_encoding],unknown_encoding) print(result)