人工智能之基于face_recognition的人脸检测与识别

不久乘高铁出行,看见高铁火车站已经实现了“刷脸进站”,而且效率很高,很感兴趣,今天抽时间研究一下,其实没那么复杂。

我基本上是基于https://github.com/ageitgey/face_recognition上的资料和源码做一些尝试和试验。

首先,需要配置我们的python环境,我悬着的python27(比较稳定),具体过程不多说了。

然后,需要安装这次的主角face_recognition库,这个的安装花了我不少时间,需要注意一下几点(按照本人的环境):

  1,首先,安装visual studio 2015,因为vs2015默认只安装c#相关组件,所以需要安装c++相关组件。

    ps:vs2015安装c++相关组件的方法:在vs2015中新建c++项目,出现下面场景

    

    选择第二项,确定后就会自动安装。

    为什么需要安装c++,因为安装face_recognition时会先安装dlib,dlib是基于c++的一个库。

  2,安装cmake(一个跨平台编译工具),然后需要将cmake的安装路径加入到系统环境变量path中去。

最后,就可以直接在dos中执行安装命令了(需要切换到python目录下的Script目录下):pip install  face_recognition,命令会自动帮你安装好需要的dlib库。 

到此为止,我们完成了face_recognition安装工作。

 

---------------------------------------------------------------分割线----------------------------------------------------------------------------------

下面给出几个实例来逐步了解“人脸识别”:

1.一行代码实现“人脸识别”

 

在Python目录中新建两个文件夹:分别表示“已知姓名的人”和“未知姓名的人”,图片以额、人名命名,如下:

 

 接下来,我们通过“认识的人”来识别“不认识的人”:

结果表明:1.jpg不认识,3.jpg是obama,unkown.jpg中有两个人,一个是obama,另一个不认识

结果还挺准确的!很给力!!

 

2.识别图片中所有的人脸,并显示出来

import Image
import face_recognition
image = face_recognition.load_image_file('F:/Python27/Scripts/all.jpg')
face_locations = face_recognition.face_locations(image)#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_locationprint('A face is located at pixel location Top:{},Left:{},Bottom:{},Right:{}'.format(top,right,bottom,left))face_image = image[top:bottom,left:right]pil_image=Image.fromarray(face_image)pil_image.show()
View Code

避坑指南:import Image需要先安装PIL库,在pycharm中安装的时候会报错(因为pil没有64位的版本),这时我们安装Pillow-PIL就好了。

我们的all.jpg如下:

 

 执行以下,看看结果:

没有错,总共12个人脸都被识别出来了!!!

 

3.给照片“美颜”

face_recognition可以识别人像的下巴,眼睛,鼻子,嘴唇,眼球等区域,包含以下这些个特征:

  facial_features = [ 'chin', 'left_eyebrow', 'right_eyebrow', 'nose_bridge', 'nose_tip', 'left_eye', 'right_eye', 'top_lip', 'bottom_lip' ]

       利用这些特征属性,可以轻松的给人像“美颜”

from PIL import Image, ImageDraw
face_recognition
import face_recognitionimage = face_recognition.load_image_file("F:/Python27/Scripts/known_people/obama.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()
View Code

执行下看看结果:

有点辣眼睛!!!!

 

4.利用笔记本摄像头识别人像

回到前面说的高铁站的“刷脸”,其实就是基于摄像头的“人像识别”。

这里要调用电脑的摄像头,而且涉及一些计算机视觉系统的计算,所以我们要先安装opencv库,

安装方法:

pip install --upgrade setuptools
pip install numpy Matplotlib
pip install opencv-python

 ps:如果报错:EnvironmentError: [Errno 13] Permission denied: 在install后加上--user即可

         小技巧:可以在python命令行中用 import site; site.getsitepackages()来确定当前的python环境的site-packages目录的位置

目的:这里我们需要用摄像头识别自己,那么首先需要有一张自己的照片,我将我的照片命名为mike.jpg,然后使用摄像头来识别我自己。

 看看代码:

import face_recognition
import cv2# This is a demo of running face recognition on live video from your webcam. It's a little more complicated than the
# other example, but it includes some basic performance tweaks to make things run a lot faster:
#   1. Process each video frame at 1/4 resolution (though still display it at full resolution)
#   2. Only detect faces in every other frame of video.# PLEASE NOTE: This example requires OpenCV (the `cv2` library) to be installed only to read from your webcam.
# OpenCV is *not* required to use the face_recognition library. It's only required if you want to run this
# specific demo. If you have trouble installing it, try any of the other demos that don't require it instead.# Get a reference to webcam #0 (the default one)
video_capture = cv2.VideoCapture(0)# Load a sample picture and learn how to recognize it.
obama_image = face_recognition.load_image_file("F:/Python27/Scripts/known_people/obama.jpg")
obama_face_encoding = face_recognition.face_encodings(obama_image)[0]# Load a second sample picture and learn how to recognize it.
biden_image = face_recognition.load_image_file("F:/Python27/Scripts/known_people/mike.jpg")
biden_face_encoding = face_recognition.face_encodings(biden_image)[0]# Create arrays of known face encodings and their names
known_face_encodings = [obama_face_encoding,biden_face_encoding
]
known_face_names = ["Barack Obama","mike"
]# Initialize some variables
face_locations = []
face_encodings = []
face_names = []
process_this_frame = Truewhile True:# Grab a single frame of videoret, frame = video_capture.read()# Resize frame of video to 1/4 size for faster face recognition processingsmall_frame = cv2.resize(frame, (0, 0), fx=0.25, fy=0.25)# Convert the image from BGR color (which OpenCV uses) to RGB color (which face_recognition uses)rgb_small_frame = small_frame[:, :, ::-1]# Only process every other frame of video to save timeif process_this_frame:# Find all the faces and face encodings in the current frame of videoface_locations = face_recognition.face_locations(rgb_small_frame)face_encodings = face_recognition.face_encodings(rgb_small_frame, face_locations)face_names = []for face_encoding in face_encodings:# See if the face is a match for the known face(s)matches = face_recognition.compare_faces(known_face_encodings, face_encoding)name = "Unknown"# If a match was found in known_face_encodings, just use the first one.if True in matches:first_match_index = matches.index(True)name = known_face_names[first_match_index]face_names.append(name)process_this_frame = not process_this_frame# Display the resultsfor (top, right, bottom, left), name in zip(face_locations, face_names):# Scale back up face locations since the frame we detected in was scaled to 1/4 sizetop *= 4right *= 4bottom *= 4left *= 4# Draw a box around the facecv2.rectangle(frame, (left, top), (right, bottom), (0, 0, 255), 2)# Draw a label with a name below the facecv2.rectangle(frame, (left, bottom - 35), (right, bottom), (0, 0, 255), cv2.FILLED)font = cv2.FONT_HERSHEY_DUPLEXcv2.putText(frame, name, (left + 6, bottom - 6), font, 1.0, (255, 255, 255), 1)# Display the resulting imagecv2.imshow('Video', frame)# Hit 'q' on the keyboard to quit!if cv2.waitKey(1) & 0xFF == ord('q'):break# Release handle to the webcam
video_capture.release()
cv2.destroyAllWindows()
View Code

只想看看结果:

 

看来,我被识别成功了。看起来有点小激动呢。

 

 

 

通过上面四个小例子基本了解face_recognition的用法,这只是小试牛刀,具体在现实中的应用要复杂很多,

我们需要大量的人脸数据,会涉及到机器学习和数学算法等等,而且根据应用场景的不同也会出现很多不同的要求。

这里只是一起学习分享,期待后续关于"人工智能"的内容。

 

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/254578.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

iOS 升级https的方案选择

我的选择是将UIWebView统一替换为WKWebView WKWebView AFN SDWebImage https的支持之前的博客都有涉及转载于:https://www.cnblogs.com/Jusive/p/6867531.html

预处理指令(C#)

目录预处理指令简介#define、#undef#if、#elif、#else、#endif#warning、#error#region、#endregion#line、#line default#pragma预处理指令简介 微软对预处理指令解释链接 https://docs.microsoft.com/zh-cn/dotnet/csharp/language-reference/preprocessor-directives/index…

NSWindowController的初始化创建代码

-(PRAboutWindowController*)aboutCtrl{ if(_aboutCtrl nil){ _aboutCtrl [[PRAboutWindowController alloc]initWithWindowNibName:"PRAboutWindowController"]; } return _aboutCtrl ; } 转载于:https://www.cnblogs.com/PJXWang/p/5816675.html

对CMMI3的学习和思考

原文出处: http://tech.it168.com/m/2007-08-02/200708020957750.shtml本文请勿转载。近来笔者所在公司正在为过CMMI3做各种准备,对公司的员工进行了一些相关的培训,作为项目管理人员的我,在学习CMMI3的过程中,也有了自…

Python3抓取糗百、不得姐

​点击关注 异步图书,置顶公众号 每天与你分享 IT好书 技术干货 职场知识 重要提示1:本文所列程序均基于Python3.6,低于Python3.6的Python版本可能无法运行.重要提示2:因所抓取的网站可能随时更改展示内容,因此程序也需及时跟进.重要提示3:本程序仅供学习,不能拿去做…

halcon边缘检测的方法及各种方法的适用范围

目录一、边缘提取二、BLOB分析检测三、赃物检测一、边缘提取 1、设置ROI兴趣区域 2、快速二值化,并连接相邻区域。 这样做的目的是进一步减少目标区域,通过二值化将目标区域大概轮廓提取出来 3、提取最接近目标区域的轮廓 常用函数有boundary&#xff0…

Oracle优化-表设计

前言  绝大多数的Oracle数据库性能问题都是由于数据库设计不合理造成的,只有少部分问题根植于Database Buffer、Share Pool、Redo Log Buffer等内存模块配置不合理,I/O争用,CPU争用等DBA职责范围上。所以除非是面对一个业已完成不可变更的系…

Win10远程桌面 出现 身份验证错误,要求的函数不受支持,这可能是由于CredSSP加密Oracle修正 解决方法...

升级至win10 最新版本10.0.17134,远程桌面连接Window Server时报错信息如下: 出现身份验证错误,要求的函数不正确,这可能是由于CredSSP加密Oracle修正。 解决方法: 运行 gpedit.msc 本地组策略: 计算机配置…

CMM2

原文出处:http://hi.baidu.com/seaweaver/blog/item/e80e7af427f674d9f2d3854a.html CMM2的六个KPA 1、需求管理 (RM,Requirement Management) 2、软件项目计划 (SPP,Software Project Planning&#…

查看linux系统核数

查看linux系统核数: grep ^processor /proc/cpuinfo | wc -l转载于:https://www.cnblogs.com/myyan/p/5822368.html

Rsyslog 日志相关内容

[rootserver vusers_home]# rpm -ql rsyslog|more ###.so结尾为模块,模块有分im为输入模块,om 为输出模块/etc/logrotate.d/syslog/etc/pki/rsyslog/etc/rc.d/init.d/rsyslog/etc/rsyslog.conf/etc/rsyslog.d/etc/sysconfig/rsyslog/lib64/rsyslog…

MFC导出对话框类DLL的实现

1.新建基于对话框的应用程序 2.新建MFC DLL工程 3.选择MFC DLL 4.选择扩展Dll选项(重要!!!) 5.为Dll工程添加一个MFC类,基类为CDialogEx 6.Dll新建的MFC 类中添加resource.h防止编译出错…

中国如何引进CMM评估,促进软件产业发展

北京软件行业协会 (本文转载自软件工程专家网www.21cmm.com) 一、CMM的含义及作用   CMM(软件能力成熟度模型:Capability Maturity Model For Software)是由美国卡内基梅 隆大学的软件工程研究所(SEI&a…

关于游戏平衡性——王者荣耀英雄伤害数值参考

收集王者荣耀各个英雄的装备对技能增加的百分比,这样的主要目的为保证游戏的平衡性。对于技能主要包括:血量、物理攻击、法术攻击、物理穿透、法术穿透、暴击等。关于各个装备,已经列成一张excel表格,在这里不再详细描述表格。在这…

Swift-setValuesForKeysWithDictionary

重写 setValuesForKeysWithDictionary 那么字典中可以有的字段在类中没有对应属性 class Person : NSObject {var age :Int 0 // 重写 setValuesForKeysWithDictionary 那么字典中可以有的字段在类中没有对应属性override func setValuesForKeysWithDictionary(keyedValues…

hdu 1269 迷宫城堡(trajan判环)

题目链接&#xff1a;http://acm.hdu.edu.cn/showproblem.php?pid1269 题意&#xff1a;略 题解&#xff1a;trajan模版直接求强连通分量。 #include <iostream> #include <cstring> #include <cstdio> using namespace std; const int N 1e4 10; const i…

Arithmetic图像处理halcon算子持续更新

目录abs_diff_imageabs_imageacos_imageadd_imageasin_imageatan2_imageatan_imagecos_imagediv_imageexp_imagegamma_imageinvert_imagelog_imagemax_imagemin_imagemult_imagepow_imagescale_imagesin_imagesqrt_imagesub_imagetan_imageabs_diff_image 功能&#xff1a;计算…

身于“乱世”,我们程序员应该如何打算?

今天看了这篇文章&#xff0c; 发现自己也有点生处乱世&#xff0c;不平之感&#xff0c;但是文章的朴实却让我有了一个良好的反省&#xff0c;特此转载 分类&#xff1a; 项目管理 2011-09-04 00:58 770人阅读 评论(12) 收藏 举报 不仅要低头拉车&#xff0c;还要抬头看路。…

Activity的启动流程

Activity的启动流程 努力工作 自己平时工作接触的frameworks代码比较多&#xff0c;但真正理解的很有限&#xff0c;一直在努力分析。。我主要还是用补丁的形式来看 core/java/android/app/Activity.java | 6 core/java/android/app/ActivityManagerNative.jav…

es6--箭头函数

基本用法 ES6允许使用“箭头”&#xff08;>&#xff09;定义函数。 var f v > v; 上面的箭头函数等同于&#xff1a; var f function(v) {return v; }; 如果箭头函数不需要参数或需要多个参数&#xff0c;就使用一个圆括号代表参数部分。 var f () > 5; // 等同于…