In[5]:forface_locationinface_locations:
# 对人脸位置进行循环
top,right,bottom,left=face_location
# 为每个人脸画四边形的四个位置,可以看出分别是四边形的上,
# 右,下,左
rr,cc=draw.polygon_perimeter([top,top,bottom,bott
om],[left,right,right,left])
# 用polygon_perimeter绘制不填充的多边形
draw.set_color(img,[rr,cc],[255,0,0])
# 设置颜色为红色
('F:',img)
# 保存
原始的facedetect.jpg的图像如图8-5所示。
图8-5
而result.jpg的图像如图8-6所示。
图8-6
从示例可以看到,检测完毕的图片已经圈出了检测到的人脸。
下面使用face_recognition进行人脸识别,具体过程如下。
①准备好已经标注姓名或者ID的人脸照片;
②利用face_recognition将其编码;
③读入待识别的照片并将其编码;
④调用函数进行识别,结果通过True和False来给出。
In[1]:importface_recognition
#引入模块
In[2]:binface=face_recognition.load_image_file("")
#读入已知姓名或者ID的图片,这里已经知道该照片中的人是Bin
In[3]:toberecognized=face_recognition.load_image_
file("")
#读入待识别的照片
In[4]:binencoding=face_recognition.face_encodings(binface)[0]
#对已知ID的照片进行编码
In[5]:toberecognizedencoding=face_recognition.face_encodin
gs(toberecognized)[0]
#对待识别的照片进行编码
In[6]:knownfaces=[binencoding]
#将已知姓名或ID的照片编为一组。这个例子里已知照片只有一张
In[7]:results=face_recognition.compare_faces(knownfaces,
toberecognizedencoding)
#使用该函数给出结果
In[8]:results
