妙招:使用Python实现图片在人脸识别并显示
使用Python3实现识别图片中的所有人脸并显示出来,代码如下:
#-*-coding:utf-8-*-#识别图片中的所有人脸并显示出来#filename:find_faces_in_picture.pyfromPILimportImageimportface_recognition#将jpg文件加载到numpy数组中image=face_recognition.load_image_file("linuxidc.com.jpg")#使用默认的给予HOG模型查找图像中所有人脸#这个方法已经相当准确了,但还是不如CNN模型那么准确,因为没有使用GPU加速#另请参见:find_faces_in_picture_cnn.pyface_locations=face_recognition.face_locations(image)#使用CNN模型#face_locations=face_recognition.face_locations(image,number_of_times_to_upsample=0,model="cnn")#打印:我从图片中找到了多少张人脸print("Ifound{}face(s)inthisphotograph.".format(len(face_locations)))#循环找到的所有人脸forface_locationinface_locations:#打印每张脸的位置信息top,right,bottom,left=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张人脸,并显示出来。
Ifound10face(s)inthisphotograph.AfaceislocatedatpixellocationTop:445,Left:1867,Bottom:534,Right:1957AfaceislocatedatpixellocationTop:544,Left:643,Bottom:619,Right:718AfaceislocatedatpixellocationTop:478,Left:1647,Bottom:553,Right:1722AfaceislocatedatpixellocationTop:504,Left:126,Bottom:594,Right:215AfaceislocatedatpixellocationTop:536,Left:395,Bottom:611,Right:469AfaceislocatedatpixellocationTop:544,Left:1042,Bottom:619,Right:1116AfaceislocatedatpixellocationTop:553,Left:818,Bottom:627,Right:892AfaceislocatedatpixellocationTop:511,Left:1431,Bottom:586,Right:1506AfaceislocatedatpixellocationTop:564,Left:1227,Bottom:626,Right:1289AfaceislocatedatpixellocationTop:965,Left:498,Bottom:1017,Right:550
如下图:
原文来自:https://www.linuxprobe.com/python-find-faces-picture.html
声明:本站所有文章资源内容,如无特殊说明或标注,均为采集网络资源。如若本站内容侵犯了原著者的合法权益,可联系本站删除。