使用Python3识别并显示图片中的所有人脸,代码如下:#-*-coding:utf-8-*-#识别图片中的所有人脸并显示#filename:find_faces_in_picture.pyfromPILimportImageimportface_recognition#Transfer将jpg文件加载到numpy数组中image=face_recognition.load_image_file("linuxidc.com.jpg")#使用默认的HOG模型找出图片中的所有人脸#这个方法还是比较准确的,但是还是不如CNN模型,因为没有AccelerateusingGPU#Seealso:find_faces_in_picture_cnn.pyface_locations=face_recognition.face_locations(image)#UseCNN模型#face_locations=face_recognition.face_locations(image,number_of_times_to_upsample=0,model="cnn")#Print:IfoundfromthepictureHowmanyfacesprint("Ifound{}face(s)inthisphotograph.".format(len(face_locations)))#循环查找所有的人脸forface_locationinface_locations:#打印每个人脸的位置信息top,对吧,左下=face_locationprint("AfaceislocatedatpixellocationTop:{},Left:{},Bottom:{},Right:{}".format(top,left,bottom,right))#指定人脸的位置信息,然后显示人脸图片face_image=image[top:bottom,left:right]pil_image=Image.fromarray(face_image)pil_image.show()#或者执行python文件$python3www.linuxidc.com.py从图片中识别出10张人脸并显示出来。伊丰d10face(s)inthisphotograph.afaceislocatedatpixellocationtop:445,左:1867,底部:534,右:1957AfaceIsLocateCateTatpixellocationtop:544,左:643,底部:619,右:619,右:718afaceislocatecatiSlocateCateCateCateCateCateCateCateTaTpixellotittop:478,左:16478,左:16478,左:1647,左504,左:126,下:594,右:215人脸位于像素位置上:536,左:395,下:611,右:469人脸位于像素位置上:544,左:1042,下:619,右:1116人脸位于像素位置上:553,左:818,下:627,右:892AfaceislocatedatpixellocationTop:511,Left:1431,Bottom:586,Right:1506AfaceislocatedatpixellocationTop:564,Left:1227,Bottom:626,Right:1289AfaceislocatedatpixellocationTop:965,Left:498,Bottom:1017,Right:550如下图
