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

环境要求:Ubuntu17.10

Python 2.7.14

环境搭建:

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 命令查看是否成功

实现人脸识别:

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

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

2. 接下来,你需要准备另一个文件夹,里面是你要识别的图片:unknown_pic文件夹下是要识别的图片,其中韩红是机器不认识的

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

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

# filename : find_faces_in_picture.py# -*- coding: utf-8 -*-# 导入pil模块 ,可用命令安装 apt-get install python-Imagingfrom PIL import Image# 导入face_recogntion模块,可用命令安装 pip install face_recognitionimport 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.pyface_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张人脸,并显示出来,如下图

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

# filename : find_facial_features_in_picture.py# -*- coding: utf-8 -*-# 导入pil模块 ,可用命令安装 apt-get install python-Imagingfrom PIL import Image, ImageDraw# 导入face_recogntion模块,可用命令安装 pip install face_recognitionimport 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_recognitionimport 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-Imagingfrom PIL import Image, ImageDraw# 导入face_recogntion模块,可用命令安装 pip install face_recognitionimport 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()美颜前后对比如下图

python实现人脸识别代码_手把手教你用1行代码实现人脸识别——Python Face_recogni...相关推荐

  1. 人脸识别的python实现代码_手把手教你用1行代码实现人脸识别 --Python Face_recognition...

    # filename : digital_makeup.py # -*- coding: utf-8 -*- # 导入pil模块 ,可用命令安装 apt-get install python-Imag ...

  2. python安装好的界面_手把手教你配置最漂亮的PyCharm界面,Python程序员必备!

    高逼格超美的IDE界面,是每个程序员的梦想! 随着人工智能/机器学习的兴起,Python作为一门"漂亮的语言",再次获得广大程序员的关注.而JetBrains出品的PyCharm无 ...

  3. python处理时间序列非平稳_手把手教你用Python处理非平稳时间序列

    简介 预测一个家庭未来三个月的用电量,估计特定时期道路上的交通流量,预测一只股票在纽约证券交易所交易的价格--这些问题都有什么共同点? 它们都属于时间序列数据的范畴!如果没有"时间" ...

  4. 手把手教你用1行代码实现人脸识别 -- Python Face_recognition

    2019独角兽企业重金招聘Python工程师标准>>> 环境要求: Ubuntu17.10 Python 2.7.14 环境搭建: 1. 安装 Ubuntu17.10 > 安装 ...

  5. python数据预测代码_手把手教你用Python玩转时序数据,从采样、预测到聚类丨代码...

    原标题:手把手教你用Python玩转时序数据,从采样.预测到聚类丨代码 原作 Arnaud Zinflou 郭一璞 编译 时序数据,也就是时间序列的数据. 像股票价格.每日天气.体重变化这一类,都是时 ...

  6. python 时间序列prophet 模型分析_手把手教你用Prophet快速进行时间序列预测(附Prophet和R代码)...

    原标题:手把手教你用Prophet快速进行时间序列预测(附Prophet和R代码) 作者:ANKIT CHOUDHARY:翻译:王雨桐:校对:丁楠雅: 本文约3000字,建议阅读12分钟. 本文将通过 ...

  7. js如何运行python代码_手把手教你如何使用Python执行js代码

    前言 各位小伙伴,大家好,这次咱们来说一下关于爬虫方向的一个知识,Python如何执行js,快来看看吧!!! 为什么要引出Python执行js这个问题? 都说术业有专攻,每个语言也都有自己的长处和短处 ...

  8. python 合并excel 自动更新_手把手教你4种方法用Python批量实现多Excel多Sheet合并

    一.前言 大家好,我是崔艳飞.前两天给大家分享了Python自动化文章:手把手教你利用Python轻松拆分Excel为多个CSV文件,而后在Python进阶交流群里边有读者遇到一个问题,他有很多个Ex ...

  9. python爬取资料怎么样_手把手教你Python爬取新房数据

    原标题:手把手教你Python爬取新房数据 项目背景 新房数据,对于房地产置业者来说是买房的重要参考依据,对于房地产开发商来说,也是分析竞争对手项目的绝佳途径,对于房地产代理来说,是踩盘前的重要准备. ...

最新文章

  1. 明年,我要用 AI 给全村写对联
  2. python爬虫入门代码-如何开始写你的第一个爬虫脚本——简单爬虫入门!
  3. dubbo是长连接还是短连接_从快手短视频看,内容平台如何做好产品与用户的连接及运营实操...
  4. 安卓9.0添加服务修改SELinux
  5. 常用sql操作语句实战演示
  6. xposed学习四:总结
  7. 从零开始学架构2 - 高性能篇
  8. 力扣572. 另一棵树的子树(JavaScript)
  9. 无法启动 nexus 服务,错误1067:进程意外终止。java环境变量设置技巧。
  10. 12、设计模式-结构型模式-外观模式
  11. 如何使用Hasu USB to USB Controller Converter刷写tmk固件交换Caps和Ctrl
  12. 物流项目管理的团队建设 (zt)
  13. 出版一本书可以赚多少钱_今年出版5本书
  14. matlab 绘图与动画制作
  15. 大恒halcon 深度学习公开课
  16. word中编号样式库内找不到带圈编号的解决方案
  17. C语言牛客网(NowCoder)刷题篇
  18. [转] Carmack 谈 d3d 与 ogl,定位专业应用的OpenGL,专注娱乐应用的DirectX,未来:OpenGL、DirectX并行发展
  19. 完美的css背景图片全屏显示,能比例缩小,不留空白
  20. [012量化交易] python 最高价 最低价

热门文章

  1. 第二个作业:贝叶斯估计
  2. 如何在iPhone上关闭Siri App建议
  3. 甘肃文艺界专家学者集广智 献策促千年洮砚文化传承
  4. 【kali】Mestasploit——基本使用2、Exploit模块、playload、metepreter
  5. 在线数据处理与交易处理(EDI许可证)
  6. 物理机安装unRAID Pro 6.8.1开心版详细教程
  7. 使用ArcGIS API for Silverlight + Visifire绘制地图统计图
  8. opencv获取外接摄像头_opencv获取摄像头视频
  9. leanback 使用
  10. 一篇文章,掌握所有开源数据库的现状