摘要:1行代码实现人脸识别,1.首先你需要提供一个文件夹,里面放着所有你想让系统识别的人的照片。每个人都有一张照片,照片以人名命名。2、接下来你需要准备另外一个文件夹,里面有你要识别的图片。3、然后就可以运行face_recognition命令,将刚才准备的两个文件夹作为参数传入,命令会返回需要识别的图片中出现了谁。一行代码就够了!!!环境要求:Ubuntu17.10Python2.7.14环境搭建:1、安装Ubuntu17.10>安装步骤在这里2、安装Python2.7.14(Ubuntu17.10默认Python版本为2.7.14)3、安装git、cmake、python-pip#安装git$sudoapt-getinstall-ygit#安装cmake$sudoapt-getinstall-ycmake#安装python-pip$sudoapt-getinstall-ypython-pip4.安装face_recognition前先安装编译dlib需要先安装编译dlib#编译dlib前先安装boost$sudoapt-getinstalllibboost-all-dev#开始编译dlib#clonedlib源码$gitclonehttps://github.com/davisking/dlib.git$cddlib$mkdirbuild$cdbuild$cmake..-DDLIB_USE_CUDA=0-DUSE_AVX_INSTRUCTIONS=1$cmake--build。(注意中间有一个空格)$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、接下来需要准备另外一个文件夹,里面有你要识别的图片:unknown_pic文件夹下面是要识别的图片,其中韩红是机器无法识别的3.然后就可以运行face_recognition命令,将刚才准备的两个文件夹作为参数传入,命令会返回所有需要识别的图片Whowasidentified:识别成功!!!示例2(识别图片中的所有人脸并显示):#filename:find_faces_in_picture.py#-*-coding:utf-8-*-#导入pil模块,使用命令apt-getinstallpython-安装ImagingfromPILimportImage#导入face_recognition模块,可以安装pipinstallface_recognitionimportface_recognition#加载jpg文件到numpy数组image=face_recognition.load_image_file("/opt/face/unknown_pic/all_star.jpg")#使用默认HOG模型查找imagesAllfacesinthemiddle#这种方法已经相当准确了,但仍然不如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)))#循环遍历所有在face_locations中为face_location找到的面孔:#打印每个人脸的位置信息top,right,bottom,left=face_locationprint("一张人脸位于像素位置Top:{},Left:{},Bottom:{},Right:{}".format(top,left,bottom,right))#指定人脸的位置信息,然后显示人脸图像face_image=image[top:bottom,left:right]pil_image=Image.fromarray(face_image)pil_image.show()as下图识别图片#执行python文件$pythonfind_faces_in_picture.py从图片中识别出7张人脸并显示出来,如下例3(人脸特征自动识别):#filename:find_facial_features_in_picture.py#-*-coding:utf-8-*-#导入pil模块,可以使用命令安装apt-getinstallpython-ImagingfromPILimportImage,ImageDraw#导入face_recognition模块,可以使用命令安装pipinstallface_recognitionimportface_recognition#加载jpg文件到numpy数组image=face_recognition.load_image_file("biden.jpg")#找出图像中所有面孔的所有面部特征face_landmarks_list=face_recognition.face_landmarks(image)print("我在这张照片中找到了{}面孔。".format(len(face_landmarks_list)))对于face_landmarks_list中的face_landmarks:#打印每个面部特征在这张图片中的位置]forfacial_featureinfacial_features:print("这张人脸中的{}有以下几点:{}".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()自动识别面部特征(轮廓)例4(识别谁是人脸识别):#filename:recognize_faces_in_pictures.py#-*-conding:utf-8-*-#导入face_recognition模块,可以使用命令安装pipinstallface_recognitionimportface_recognition#加载jpg文件到numpy中arraybabe_image=face_recognition.load_image_file("/opt/face/known_people/babe.jpeg")容_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")#获取每个图像文件中每个人脸的人脸编码#因为每个图像可能有多个faces,所以返回一个编码列表#但是因为我知道每张图片只有一张脸,所以我只关心每张图片中的第一个编码,所以我取索引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的数组,未知与known_faces数组中任何人匹配的面孔results=face_recognition.compare_faces(known_faces,unknown_face_encoding)print("这是未知面孔宝贝吗?{}".format(results[0]))print("这张未知面孔是容祖儿?{}".format(results[1]))print("这张未知人脸是我们没见过的新人脸吗?{}".format(notTrueinresults))显示结果如下例五(识别五官美化):#filename:digital_makeup.py#-*-coding:utf-8-*-#导入pil模块,可以使用命令安装apt-getinstallpython-ImagingfromPILimportImage,ImageDraw#导入face_recognition模块,可以安装pipinstallface_recognitionimportface_recognition#将jpg文件加载到numpy数组中ion.face_landmarks(image)forface_landmarksinface_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]],填充=(0,0,0,110),宽度=6)d.line(face_landmarks['right_eye']+[face_landmarks['right_eye'][0]],fill=(0,0,0,110),宽度=6)pil_image.show()
