文章基于face_recognition+OpenCV(大佬们真的是厉害,膜拜)

总结一下经验,以及遇到的坑

参考:

https://github.com/ageitgey/face_recognition/blob/master/README_Simplified_Chinese.md

https://github.com/ageitgey/face_recognition/blob/master/examples/facerec_from_webcam_faster.py

https://gist.github.com/ageitgey/629d75c1baac34dfa5ca2a1928a7aeaf

Face Recognition

在Git hub上看到一个强大的开源项目:face_recognition,具体内容大家可以参考这里:

https://github.com/ageitgey/face_recognition/blob/master/README_Simplified_Chinese.md

环境配置:

官方推荐是

  • Python 3.3+ or Python 2.7
  • macOS or Linux
  • Windows并不是我们官方支持的,但也许也能用

1. 安装face_recognition

在安装face_recognition之前先要安装dlib和相关Python依赖,安装dlib之前又要安装cmake(有点绕。。。)

cmake安装:

      先去官网下载自己对应的版本:Download | CMake

下载完后安装,之后打开软件,在工具栏找到 Tools > How to install for command line use

软件会向你展示几种安装方法,我选的是:

sudo "/Applications/CMake.app/Contents/bin/cmake-gui" --install

在终端里输入该命令就ok了;

 dlib安装:(参考https://gist.github.com/ageitgey/629d75c1baac34dfa5ca2a1928a7aeaf)

git clone https://github.com/davisking/dlib.git

依次输入下面代码

cd dlib
mkdir build
cd build
cmake ..
cmake --build .

安装完成以后,就可以进行face_recognition的安装了

pip3 install face_recognition

2. 验证安装是否成功

这里我们用一段代码来测试,新建一个文件夹img,在img下新建两个文件夹,分别命名为know和unknow

然后去搜集一些面部素材,这里我用的是Taylor Swfit和Tim Cook的,将收集到的素材放入know文件,并命名为对应的名字(为了简明易懂),konw文件下的文件是用来告诉机器这是谁,是让机器用来学习的。

unknow文件下放一些测试图片,这个文件下电脑不知道这是谁,需要电脑去判断

将文件放到根目录,在终端输入:

face_recognition img/know img/unknow

会输出类似内容

img/unknow/anne.jpeg,unknown_person
img/unknow/TS.jpeg,taylorSwift
img/unknow/xlz.jpeg,unknown_person
img/unknow/TM.jpg,timCook
img/unknow/Zuckerberg.jpeg,unknown_person

unknow_person代表没识别出的人,也就是在know文件中没有的人

有名字的是识别出来的,是与know文件里相对应的人

这样就表示我们安装成功了,下面进行实时的人脸识别。

3. 实时人脸检测

代码源自:https://github.com/ageitgey/face_recognition/blob/master/examples/facerec_from_webcam_faster.py

安装cv2,也就是opencv

pip install opencv-python

安装numpy库

pip install numpy

然后新建文件: face_recog.py

基本上就是复制这里的代码

import face_recognition
import cv2
import numpy as np# 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.
timCook_image = face_recognition.load_image_file("face_cv2/img/know/timCook.jpg")
timCook_face_encoding = face_recognition.face_encodings(timCook_image)[0]# Load a second sample picture and learn how to recognize it.
taylor_image = face_recognition.load_image_file("face_cv2/img/know/taylorSwift.jpeg")
taylor_face_encoding = face_recognition.face_encodings(taylor_image)[0]# Create arrays of known face encodings and their names
known_face_encodings = [timCook_face_encoding,taylor_face_encoding
]
known_face_names = ["TimCook","TaylorSwift"
]# 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]# Or instead, use the known face with the smallest distance to the new faceface_distances = face_recognition.face_distance(known_face_encodings, face_encoding)best_match_index = np.argmin(face_distances)if matches[best_match_index]:name = known_face_names[best_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()

在终端运行命令:(在调用摄像头这一部分,这里我出了点状况,在sublime text3中执行程序,会崩溃,但是在终端执行就没有问题,可能是我配置的有问题)

python3 face_recog.py

如果成功启动程序,可以用手机搜几张Taylor或者Tim的图片对着摄像头测试。

(图片来源:https://github.com/ageitgey/face_recognition/blob/master/README_Simplified_Chinese.md)

或者直接在know中存入你自己的照片,并修改这这一部分代码:

video_capture = cv2.VideoCapture(0)# 加载第一个样本照片
timCook_image = face_recognition.load_image_file("face_cv2/img/know/timCook.jpg")
timCook_face_encoding = face_recognition.face_encodings(timCook_image)[0]# 加载第二个样本照片
taylor_image = face_recognition.load_image_file("face_cv2/img/know/taylorSwift.jpeg")
taylor_face_encoding = face_recognition.face_encodings(taylor_image)[0]# 加载你自己的照片
your_image = face_recognition.load_image_file("face_cv2/img/know/your.jpg")
your_face_encoding = face_recognition.face_encodings(your_image)[0]# Create arrays of known face encodings and their names
known_face_encodings = [timCook_face_encoding,taylor_face_encoding,your_face_encoding
]
known_face_names = ["TimCook","TaylorSwift","yourName"
]

退出程序按“q”建

有讲的不明白的地方,或者出错的地方,大家可以私信我或者留言评论,一起学习一起进步。

  文章若有侵权,请及时联系我删除更改。

30分钟做一个人脸识别案例相关推荐

  1. C# 30分钟完成百度人脸识别——进阶篇(文末附源码)

    距离上次入门篇时隔两个月才出这进阶篇,小编惭愧,对不住关注我的卡哇伊的小伙伴们,为此小编用这篇博来谢罪. 前面的准备工作我就不说了,注册百度账号api,创建web网站项目,引入动态链接库引入. 不了解 ...

  2. 用Python做一个人脸识别系统,简单操作又实用~

    导语 今天给大家介绍一个非常简洁的人脸识别系统: 人脸识别,是基于人的脸部特征信息进行身份识别的一种生物识别技术.而通过我们Python编程,几行代码就可以实现人脸识别,这主要得益于face_reco ...

  3. 做一个人脸识别登录功能

    前天晚上散步到一个大学公寓门口,发现公寓的大门口都安装了人脸识别的门禁,就有种强烈的欲望 想要学习一下 哈哈,刚好也在做项目就想运用到这个技术-于是便有了开端. 视觉智能--人脸识别 基于阿里云的视觉 ...

  4. [分享] 30分钟做一个二维码名片应用,有源码!

    2019独角兽企业重金招聘Python工程师标准>>> 前言 30分钟带你用Wex5做一个微信公众号上使用的二维码名片,相应技术点有详细讲解,高清有码!(点击下载全部源码) 二维码现 ...

  5. 做一个人脸识别相关的毕业设计

    本文旨在简单聊一下做一个与人脸识别相关的本科毕业设计,希望不是挖坑文. 背景:普通本科毕业设计大多是构建网站(购物.管理系统.论坛),相对来说老师看的也审美疲劳,当然如果买毕设的话价格会相对便宜. 本 ...

  6. 使用OpenCV做一个人脸识别(Java)

    前言 当前很多博客实现人脸识别的大部分都是调用云厂家的接口,如百度,阿里云.以及我们乐橙开放平台也支持人脸识别等人工智能服务.这些都比较简单,会接开放平台,走接口请求基本上都掌握了.缺点就是有限制,收 ...

  7. (详细教程)opencv+pycharm+笔记本摄像头 做一个人脸识别

    目录 一.安装opencv-python 二.准备分类器 三.代码讲解 四.运行结果 附录: 总结 一.安装opencv-python 这里推荐使用pycharm直接安装opencv-python 打 ...

  8. OpenCV中视频操作及人脸识别案例

    目录 OpenCV中视频操作及人脸识别案例 视频操作 视频读写 从文件中读取视频并播放 保存视频 小结 视频追踪 meanshift Camshift 算法总结 小结 案例:人脸案例 人脸识别基础 实 ...

  9. 玩了一个人脸识别登录

    最近温差大,请别感冒 前言 这篇文章就没有目录了,直接从头正序开始即可. 因为突然接到了一个需求,一个xx局,内部使用的移动端项目(是我们开发的),需要添加一个人脸识别登录的需求. 内部员工使用的识别 ...

最新文章

  1. python函数参数*args和**args
  2. 线上环境HBASE-1.2.0出现oldWALs无法自动回收情况;
  3. 【Linux】一步一步学Linux——dhclient命令(156)
  4. Python Numpy 笔记
  5. 一文看懂高可用:异地多活
  6. 知识点整理-mysql怎么查看优化器优化后的sql
  7. Hive Udf Rank
  8. Aspose.Excel模板输出中名称管理器的使用
  9. Python之print语句
  10. 机器学习之线性回归 Linear Regression(二)Python实现
  11. 免上传音乐外链(QQ音乐)
  12. Unexpected error while obtaining screenshot from device: EOF
  13. SpringBoot + Spring Cloud +Vue 管理系统前端搭建(二、visual studio code开发前端项目
  14. 2014美国大学计算机专业排名,2014年美国大学计算机专业研究生排名
  15. /、/*、/**的区别
  16. 基于android的即时通讯APP 聊天APP
  17. android系统user/userdebug版本设置selinux到SELINUX_PERMISSIVE模式
  18. 使用高德地图2D/3D SDK添加海量描点Marker以及视图中显示所有描点、我的定位添加呼吸动画
  19. 科技论文翻译,俄语文档的语法有何特点
  20. 结合opencv学习DIP

热门文章

  1. Mac下127个常用软件
  2. 犀牛书第七版学习笔记:数据类型与结构-布尔值
  3. JOOQ之Tuple
  4. OAI L3与L2接口分析
  5. vue-qrcode-reader 实现直接扫码和相册扫码
  6. 软考备考-系统构架师-23-系统架构师考试经验总结
  7. STM32单片机-加密烧录Hex
  8. Spark 2.3.0 用户自定义聚合函数UserDefinedAggregateFunction和Aggregator
  9. Unity3D基础:1、窗口界面
  10. cvpr 2019 image caption