简单机器学习人脸识别工具face-recognition python小试,一行代码实现人脸识别

摘要: 1行代码实现人脸识别,1. 首先你需要提供一个文件夹,里面是所有你希望系统认识的人的图片。其中每个人一张图片,图片以人的名字命名。2. 接下来,你需要准备另一个文件夹,里面是你要识别的图片。3. 然后你就可以运行face_recognition命令了,把刚刚准备的两个文件夹作为参数传入,命令就会返回需要识别的图片中都出现了谁,1行代码足以!!!

环境要求:

环境搭建:

1. 安装 Ubuntu17.10 > 安装步骤在这里

2. 安装 Python2.7.14 (Ubuntu17.10 默认Python版本为2.7.14)

3. 安装 git 、cmake 、 python-pip

# 安装 git
$ sudo apt-get install -y git
# 安装 cmake
$ sudo apt-get install -y cmake
# 安装 python-pip $ sudo apt-get install -y python-pip 

4. 安装编译dlib

安装face_recognition这个之前需要先安装编译dlib

# 编译dlib前先安装 boost
$ sudo apt-get install libboost-all-dev

# 开始编译dlib
# 克隆dlib源代码
$ git clone https://github.com/davisking/dlib.git
$ cd dlib $ mkdir build $ cd build $ cmake .. -DDLIB_USE_CUDA=0 -DUSE_AVX_INSTRUCTIONS=1 $ cmake --build .(注意中间有个空格) $ cd .. $ python setup.py install --yes USE_AVX_INSTRUCTIONS --no DLIB_USE_CUDA 

5. 安装 face_recognition

# 安装 face_recognition
$ pip install face_recognition
# 安装face_recognition过程中会自动安装 numpy、scipy 等

环境搭建完成后,在终端输入 face_recognition 命令查看是否成功

环境搭建完成后,在终端输入 face_recognition 命令查看是否成功

实现人脸识别:


示例一(1行代码实现人脸识别):

1. 首先你需要提供一个文件夹,里面是所有你希望系统认识的人的图片。其中每个人一张图片,图片以人的名字命名:

known_people文件夹下有babe、成龙、容祖儿的照片

known_people文件夹下有babe、成龙、容祖儿的照片

2. 接下来,你需要准备另一个文件夹,里面是你要识别的图片:

unknown_pic文件夹下是要识别的图片,其中韩红是机器不认识的

unknown_pic文件夹下是要识别的图片,其中韩红是机器不认识的

3. 然后你就可以运行face_recognition命令了,把刚刚准备的两个文件夹作为参数传入,命令就会返回需要识别的图片中都出现了谁:

识别成功!!!

识别成功!!!


示例二(识别图片中的所有人脸并显示出来):

# filename : find_faces_in_picture.py
# -*- coding: utf-8 -*-
# 导入pil模块 ,可用命令安装 apt-get install python-Imaging
from PIL import Image # 导入face_recogntion模块,可用命令安装 pip install face_recognition import face_recognition # 将jpg文件加载到numpy 数组中 image = face_recognition.load_image_file("/opt/face/unknown_pic/all_star.jpg") # 使用默认的给予HOG模型查找图像中所有人脸 # 这个方法已经相当准确了,但还是不如CNN模型那么准确,因为没有使用GPU加速 # 另请参见: find_faces_in_picture_cnn.py face_locations = face_recognition.face_locations(image) # 使用CNN模型 # face_locations = face_recognition.face_locations(image, number_of_times_to_upsample=0, model="cnn") # 打印:我从图片中找到了 多少 张人脸 print("I found {} face(s) in this photograph.".format(len(face_locations))) # 循环找到的所有人脸 for face_location in face_locations: # 打印每张脸的位置信息 top, right, bottom, left = face_location print("A face is located at pixel location 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() 

如下图为用于识别的图片

用于识别的图片

# 执行python文件
$ python find_faces_in_picture.py

从图片中识别出7张人脸,并显示出来,如下图

从图片中识别出7张人脸,并显示出来


示例三(自动识别人脸特征):

# filename : find_facial_features_in_picture.py
# -*- coding: utf-8 -*-
# 导入pil模块 ,可用命令安装 apt-get install python-Imaging
from PIL import Image, ImageDraw # 导入face_recogntion模块,可用命令安装 pip install face_recognition import face_recognition # 将jpg文件加载到numpy 数组中 image = face_recognition.load_image_file("biden.jpg") #查找图像中所有面部的所有面部特征 face_landmarks_list = face_recognition.face_landmarks(image) print("I found {} face(s) in this photograph.".format(len(face_landmarks_list))) for face_landmarks in face_landmarks_list: #打印此图像中每个面部特征的位置 facial_features = [ 'chin', 'left_eyebrow', 'right_eyebrow', 'nose_bridge', 'nose_tip', 'left_eye', 'right_eye', 'top_lip', 'bottom_lip' ] for facial_feature in facial_features: print("The {} in this face has the following points: {}".format(facial_feature, face_landmarks[facial_feature])) #让我们在图像中描绘出每个人脸特征! pil_image = Image.fromarray(image) d = ImageDraw.Draw(pil_image) for facial_feature in facial_features: d.line(face_landmarks[facial_feature], width=5) pil_image.show() 

自动识别出人脸特征(轮廓)

自动识别出人脸特征


示例四(识别人脸鉴定是哪个人):

# filename : recognize_faces_in_pictures.py
# -*- conding: utf-8 -*-
# 导入face_recogntion模块,可用命令安装 pip install face_recognition
import face_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/Rong zhu er.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的数组,未知面孔known_faces阵列中的任何人相匹配的结果 results = face_recognition.compare_faces(known_faces, unknown_face_encoding) print("这个未知面孔是 Babe 吗? {}".format(results[0])) print("这个未知面孔是 容祖儿 吗? {}".format(results[1])) print("这个未知面孔是 我们从未见过的新面孔吗? {}".format(not True in results)) 

显示结果下如图

显示结果如图


示例五(识别人脸特征并美颜):

# filename : digital_makeup.py
# -*- coding: utf-8 -*-
# 导入pil模块 ,可用命令安装 apt-get install python-Imaging
from PIL import Image, ImageDraw # 导入face_recogntion模块,可用命令安装 pip install face_recognition import face_recognition #将jpg文件加载到numpy数组中 image = face_recognition.load_image_file("biden.jpg") #查找图像中所有面部的所有面部特征 face_landmarks_list = face_recognition.face_landmarks(image) for face_landmarks in face_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]], 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() 

美颜前后对比如下图

美颜前后对比

转载于:https://www.cnblogs.com/pyxiaomangshe/p/7776866.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
----------------- # DFace • [![License](http://pic.dface.io/apache2.svg)](https://opensource.org/licenses/Apache-2.0) | **`Linux CPU`** | **`Linux GPU`** | **`Mac OS CPU`** | **`Windows CPU`** | |-----------------|---------------------|------------------|-------------------| | [![Build Status](http://pic.dface.io/pass.svg)](http://pic.dface.io/pass.svg) | [![Build Status](http://pic.dface.io/pass.svg)](http://pic.dface.io/pass.svg) | [![Build Status](http://pic.dface.io/pass.svg)](http://pic.dface.io/pass.svg) | [![Build Status](http://pic.dface.io/pass.svg)](http://pic.dface.io/pass.svg) | **基于多任务卷积网络(MTCNN)和Center-Loss的多人实时人脸检测和人脸识别系统。** [Github项目地址](https://github.com/kuaikuaikim/DFace) [Slack 聊天组](https://dfaceio.slack.com/) **DFace** 是个开源的深度学习人脸检测和人脸识别系统。所有功能都采用 **[pytorch](https://github.com/pytorch/pytorch)** 框架开发。pytorch是一个由facebook开发的深度学习框架,它包含了一些比较有趣的高级特性,例如自动求导,动态构图等。DFace天然的继承了这些优点,使得它的训练过程可以更加简单方便,并且实现代码可以更加清晰易懂。 DFace可以利用CUDA来支持GPU加速模式。我们建议尝试linux GPU这种模式,它几乎可以实现实时的效果。 所有的灵感都来源于学术界最近的一些研究成果,例如 [Joint Face Detection and Alignment using Multi-task Cascaded Convolutional Networks](https://arxiv.org/abs/1604.02878) 和 [FaceNet: A Unified Embedding for Face Recognition and Clustering](https://arxiv.org/abs/1503.03832) **MTCNN 结构**   ![mtcnn](http://affluent.oss-cn-hangzhou.aliyuncs.com/html/images/mtcnn_st.png) ** 如果你对DFace感兴趣并且想参与到这个项目中, 以下TODO是一些需要实现的功能,我定期会更新,它会实时展示一些需要开发的清单。提交你的fork request,我会用issues来跟踪和反馈所有的问题。也可以加DFace的官方Q群 681403076 也可以加本人微信 jinkuaikuai005 ** ### TODO(需要开发的功能) - 基于center loss 或者triplet loss原理开发人脸对比功能,模型采用ResNet inception v2. 该功能能够比较两张人脸图片的相似性。具体可以参考 [Paper](https://arxiv.org/abs/1503.03832)和[FaceNet](https://github.com/davidsandberg/facenet) - 反欺诈功能,根据光线,质地等人脸特性来防止照片攻击,视频攻击,回放攻击等。具体可参考LBP算法和SVM训练模型。 - 3D人脸反欺诈。 - mobile移植,根据ONNX标准把pytorch训练好的模型迁移到caffe2,一些numpy算法改用c++实现。 - Tensor RT移植,高并发。 - Docker支持,gpu版 ## 安装 DFace主要有两大模块,人脸检测和人脸识别。我会提供所有模型训练和运行的详细步骤。你首先需要构建一个pytorch和cv2的python环境,我推荐使用Anaconda来设置一个独立的虚拟环境。目前作者倾向于Linux Ubuntu安装环境。感谢山东一位网友提供windows DFace安装体验,windos安装教程具体 可参考他的[博客](http://www.alearner.top/index.php/2017/12/23/dface-pytorch-win64-gpu) ### 依赖 * cuda 8.0 * anaconda * pytorch * torchvision * cv2 * matplotlib ```shell git clone https://gitee.com/kuaikuaikim/dface.git ``` 在这里我提供了一个anaconda的环境依赖文件environment.yml (windows请用environment-win64.yml),它能方便你构建自己的虚拟环境。 ```shell cd dface conda env create -f environment.yml ``` 添加python搜索模块路径 ```shell export PYTHONPATH=$PYTHONPATH:{your local DFace root path} ``` ### 人脸识别和检测 如果你对mtcnn模型感兴趣,以下过程可能会帮助到你。 #### 训练mtcnn模型 MTCNN主要有三个网络,叫做**PNet**, **RNet** 和 **ONet**。因此我们的训练过程也需要分三步先后进行。为了更好的实现效果,当前被训练的网络都将依赖于上一个训练好的网络来生成数据。所有的人脸数据集都来自 **[WIDER FACE](http://mmlab.ie.cuhk.edu.hk/projects/WIDERFace/)** 和 **[CelebA](http://mmlab.ie.cuhk.edu.hk/projects/CelebA.html)**。WIDER FACE仅提供了大量的人脸边框定位数据,而CelebA包含了人脸关键点定位数据。以下训练除了 生成ONet的人脸关键点训练数据和标注文件 该步骤使用CelebA数据集,其他一律使用WIDER FACE。如果使用wider face的 wider_face_train.mat 注解文件需要转换成txt格式的,我这里用h5py写了个 [转换脚本](https://gitee.com/kuaikuaikim/dface/blob/master/dface/prepare_data/widerface_annotation_gen/transform.py). 这里我提供一个已经转换好的wider face注解文件 [anno_store/wider_origin_anno.txt](https://gitee.com/kuaikuaikim/dface/blob/master/anno_store/wider_origin_anno.txt), 以下训练过程参数名--anno_file默认就是使用该转换好的注解文件。 * 创建 dface 训练数据临时目录,对应于以下所有的参数名 --dface_traindata_store ```shell mkdir {your dface traindata folder} ``` * 生成PNet训练数据和标注文件 ```shell python dface/prepare_data/gen_Pnet_train_data.py --prefix_path {注解文件中图片的目录前缀,就是wider face图片所在目录} --dface_traindata_store {之前创建的dface训练数据临时目录} --anno_file {wider face 注解文件,可以不填,默认使用anno_store/wider_origin_anno.txt} ``` * 乱序合并标注文件 ```shell python dface/prepare_data/assemble_pnet_imglist.py ``` * 训练PNet模型 ```shell python dface/train_net/train_p_net.py ``` * 生成RNet训练数据和标注文件 ```shell python dface/prepare_data/gen_Rnet_train_data.py --prefix_path {注解文件中图片的目录前缀,就是wider face图片所在目录} --dface_traindata_store {之前创建的dface训练数据临时目录} --anno_file {wider face 注解文件,可以不填,默认使用anno_store/wider_origin_anno.txt} --pmodel_file {之前训练的Pnet模型文件} ``` * 乱序合并标注文件 ```shell python dface/prepare_data/assemble_rnet_imglist.py ``` * 训练RNet模型 ```shell python dface/train_net/train_r_net.py ``` * 生成ONet训练数据和标注文件 ```shell python dface/prepare_data/gen_Onet_train_data.py --prefix_path {注解文件中图片的目录前缀,就是wider face图片所在目录} --dface_traindata_store {之前创建的dface训练数据临时目录} --anno_file {wider face 注解文件,可以不填,默认使用anno_store/wider_origin_anno.txt} --pmodel_file {之前训练的Pnet模型文件} --rmodel_file {之前训练的Rnet模型文件} ``` * 生成ONet的人脸五官关键点训练数据和标注文件 ```shell python dface/prepare_data/gen_landmark_48.py ``` * 乱序合并标注文件(包括人脸五官关键点) ```shell python dface/prepare_data/assemble_onet_imglist.py ``` * 训练ONet模型 ```shell python dface/train_net/train_o_net.py ``` #### 测试人脸检测 ```shell python test_image.py ``` ### 人脸对比 @TODO 根据center loss实现人脸识别 ## 测试效果 ![mtcnn](http://affluent.oss-cn-hangzhou.aliyuncs.com/html/images/dface_demoall.PNG) ### QQ交流群(模型获取请加群) #### 681403076 ![](http://affluent.oss-cn-hangzhou.aliyuncs.com/html/images/dfaceqqsm.png) #### 本人微信 ##### jinkuaikuai005 ![](http://affluent.oss-cn-hangzhou.aliyuncs.com/html/images/perqr.jpg) ## License [Apache License 2.0](LICENSE)
好的,我知道你的问题了。关于Python实现面部特效,我可以向你介绍一下PythonFace_recognition,它可以实现人脸识别和面部特效。 Face_recognition是一个基于Python人脸识别库,它使用dlib库来实现人脸识别,可以检测和识别图像中的人脸,并提取面部特征,比如眼睛、鼻子、嘴巴等部位的位置和轮廓。 具体实现过程如下: 1. 首先需要安装Face_recognition库和dlib库,可以使用pip install face_recognition和pip install dlib命令来安装。 2. 导入Face_recognition库和Pillow库,使用load_image_file函数加载需要处理的图片。 3. 使用face_locations函数来获取图像中人脸的位置坐标,使用face_landmarks函数来获取面部特征的位置坐标。 4. 使用Pillow库的ImageDraw模块来绘制面部特征,比如眼睛、鼻子、嘴巴等部位的位置和轮廓。 下面是一个简单的示例代码实现在图片中绘制人脸位置和面部特征: ``` import face_recognition from PIL import Image, ImageDraw # 加载图片 image = face_recognition.load_image_file("test.jpg") # 获取人脸位置坐标 face_locations = face_recognition.face_locations(image) # 获取面部特征位置坐标 face_landmarks = face_recognition.face_landmarks(image) # 绘制人脸位置 for face_location in face_locations: top, right, bottom, left = face_location draw = ImageDraw.Draw(image) draw.rectangle(((left, top), (right, bottom)), outline=(255, 0, 0), width=2) # 绘制面部特征 for face_landmark in face_landmarks: for name, points in face_landmark.items(): draw = ImageDraw.Draw(image) draw.line(points, fill=(255, 255, 255), width=2) # 保存绘制后的图片 image.save("result.jpg") ``` 这样就可以实现简单的面部特效了。当然,Face_recognition库还有很多其他的功能和用法,你可以查看官方文档来了解更多。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值