当前位置: 首页 > 科技观察

教你用一行命令实现人脸识别

时间:2023-03-20 13:32:06 科技观察

环境要求Ubuntu17.10Python2.7.14环境搭建1.安装Ubuntu17.10>安装步骤在这里。2、安装Python2.7.14(Ubuntu17.10默认Python版本为2.7.14)3、安装git、cmake、python-pip#installgit$sudoapt-getinstall-ygit#installcmake$sudoapt-getinstall-ycmake#installpython-pip$sudoapt-getinstall-ypython-pip4。安装编译dlibInstallface_recognition在此之前,需要先安装编译dlib。#编译dlib前安装boost$sudoapt-getinstalllibboost-all-dev#开始编译dlib#克隆dlib源码$gitclonehttps://github.com/davisking/dlib.git$cddlib$mkdirbuild$cdbuild$cmake..-DDLIB_USE_CUDA=0-DUSE_AVX_INSTRUCTIONS=1$cmake--构建。(注意中间有空格)$cd..$pythonsetup.pyinstall--yesUSE_AVX_INSTRUCTIONS--noDLIB_USE_CUDA5.安装face_recognition#安装face_recognition$pipinstallface_recognition#安装face_recognition会自动安装numpy,scipy等环境都搭建好了,在终端输入face_recognition命令,查看人脸识别是否成功。示例1(一行命令实现人脸识别):1.首先你需要提供一个文件夹,里面放着所有你想让系统识别的人的图片。每个人都有一张图片,图片以人名命名:known_people文件夹里有babe、成龙、容祖儿的照片2、接下来需要准备另一个文件夹,里面有你要识别的图片:theunknown_pic文件夹下面是需要识别的图片,其中韩红是机器无法识别的3.然后可以运行face_recognition命令,将刚才准备的两个文件夹作为参数传入,命令会返回所有的图片需要被识别的人:识别成功!!!示例2(识别图片中的所有人脸并显示):#filename:find_faces_in_picture.py#-*-coding:utf-8-*-#导入pil模块,使用命令-getinstallpython-ImagingfromPILimportImage#Import安装aptface_recognition模块,可以安装pipinstallface_recognitionimportface_recognition#loadthejpgfileintothenumpyarrayimage=face_recognition.load_image_file("/opt/face/unknown_pic/all_star.jpg")#使用默认的HOG模型查找图像中人中的Allfaces#这个方法还是比较准确的,但是还是不如CNN模型准确,因为没有使用GPU加速(image,number_of_times_to_upsample=0,model="cnn")#print:howmanyfacesIfoundinthepicture打印("Ifound{}face(s)inthisphotograph.".format(len(face_locations)))#loopAllfacesfoundforface_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文件$pythonfind_faces_in_picture.py从图片中识别出7张人脸并作为示例展示三(人脸特征自动识别):#filename:find_facial_features_in_picture.py#-*-coding:utf-8-*-#导入pil模块,可以使用命令安装apt-getinstallpython-ImagingfromPILimportImage,ImageDraw#Importtheface_recognition模块,可以使用命令安装pipinstallface_recognitionimportface_recognition#加载jpg文件到numpy数组image=face_recognition.load_image_file("biden.jpg")#找出图片中所有人脸的所有面部特征face_landmarks_list=face_recognition.face_landmarks(image)print("Ifound{}face(s)inthisphotograph.".format(len(face_landmarks_list)))forface_landmarksinface_landmarks_list:#打印这张图片中每个面部特征的位置facial_features=['chin','left_eyebrow','right_eyebrow','nose_bridge','nose_tip','left_eye','right_eye','top_lip','bottom_lip']forfacial_featureinfacial_features:print("The{}inthisfacehasthefollowingpoints:{}".format(facial_feature,face_landmarks[facial_feature]))#让我们描述图像中的每个面部特征!pil_image=Image.fromarray(image)d=ImageDraw.Draw(pil_image)forfacial_featureinfacial_features:d.line(face_landmarks[facial_feature],width=5)pil_image.show()自动识别人脸特征Who):#filename:recognize_faces_in_pictures.py#-*-conding:utf-8-*-#导入face_recognition模块,可以使用命令安装pipinstallface_recognitionimportface_recognition#加载jpg文件到numpy数组babe_image=face_recognition.load_image_file("/opt/face/known_people/babe.jpeg")Rong_zhu_er_image=face_recognition.load_image_file("/opt/face/known_people/Rongzhuer.jpg")unknown_image=face_recognition.load_image_file("/opt/face/unknown_pic/babe2.jpg")#获取每个人脸的每个人脸编码每个图像文件#由于每个图像中可能有多个人脸,这将返回一个编码列表#但是因为我知道每个图像只有一张脸,所以我只关心每个图像编码中的第一个,所以我取索引0.babe_face_encoding=face_recognition.face_encodings(babe_image)[0]Rong_zhu_er_face_encoding=face_recognition.face_encodings(Rong_zhu_er_image)[0]unknown_face_encoding=face_recognition.face_encodings(unknown_image)[0]known_faces=[babe_face_encoding,Rong_zhu_er_face_encoding]#结果是True/false的数组,未知匹配已知面孔数组中任何人的面孔results=face_recognition.compare_faces(known_faces,unknown_face_encoding)print("这是未知面孔宝贝吗?{}".format(results[0]))print("未知面孔是容祖儿?{}".format(results[1]))print("这张未知人脸是我们没见过的新人脸吗?{}".format(notTrueinresults))显示结果如例5所示(识别人脸特征和beauty):#filename:digital_makeup.py#-*-coding:utf-8-*-#导入pil模块,可以使用命令安装apt-getinstallpython-ImagingfromPILimportImage,ImageDraw#导入face_recognition模块,可以使用安装pipinstallface_recognitionimportface_recognition的命令#将jpg文件加载到一个numpy数组中ks_list:pil_image=Image.fromarray(image)d=ImageDraw.Draw(pil_image,'RGBA')#让眉毛成为噩梦d.polygon(face_landmarks['left_eyebrow'],fill=(68,54,39,128))d.多边形(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)#glossylipsd.polygon(face_landmarks['top_lip'],fill=(150,0,0,128))d.多边形(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)#shineeyesd.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()前后美女比较