基于face_recognition库的摄像头实时人脸识别测试
前言
介绍一个基于python的开源人脸识别库,且其离线识别率高达99.38%,
github上的网址:github链接
该库可以通过python或者命令行即可实现人脸识别的功能。使用dlib深度学习人脸识别技术构建,在户外脸部检测数据库基准(Labeled Faces in the Wild)上的准确率为99.38%。
在github上有相关的链接和API文档
安装配置
安装配置很简单,按照github上的说明一步一步来就可以了。
根据你的python版本输入指令:
sudo pip install face_recognition
或者
sudo pip3 install face_recognition
正常来说,安装过程中会出错,会在安装dlib时出错,可能报错也可能会卡在那不动。因为pip在编译dlib时会出错,所以我们需要手动编译dlib再进行安装。
1、先下载下来dlib的源码。
git clone https://github.com/davisking/dlib.git
2、编译dlib。
cd dlib
mkdir build
cd build
cmake .. -DDLIB_USE_CUDA=0 -DUSE_AVX_INSTRUCTIONS=1
cd ..
sudo python setup.py install
注意:这个安装步骤是默认认为没有GPU的,所以不支持cuda。
在自己手动编译了dlib后,我们可以在python中import dlib了。
之后再重新安装,就可以配置成功了。
根据你的python版本输入指令:
sudo pip install face_recognition
或者
sudo pip3 install face_recognition
安装成功之后,我们可以在python中正常import face_recognition了。
摄像头实时识别
# -*- coding: utf-8 -*-
import face_recognition
import cv2video_capture = cv2.VideoCapture(1)obama_img = face_recognition.load_image_file("obama.jpg")
obama_face_encoding = face_recognition.face_encodings(obama_img)[0]face_locations = []
face_encodings = []
face_names = []
process_this_frame = Truewhile True:ret, frame = video_capture.read()small_frame = cv2.resize(frame, (0, 0), fx=0.25, fy=0.25)if process_this_frame:face_locations = face_recognition.face_locations(small_frame)face_encodings = face_recognition.face_encodings(small_frame, face_locations)face_names = []for face_encoding in face_encodings:match = face_recognition.compare_faces([obama_face_encoding], face_encoding)if match[0]:name = "Barack"else:name = "unknown"face_names.append(name)process_this_frame = not process_this_framefor (top, right, bottom, left), name in zip(face_locations, face_names):top *= 4right *= 4bottom *= 4left *= 4cv2.rectangle(frame, (left, top), (right, bottom), (0, 0, 255), 2)cv2.rectangle(frame, (left, bottom - 35), (right, bottom), (0, 0, 255), 2)font = cv2.FONT_HERSHEY_DUPLEXcv2.putText(frame, name, (left+6, bottom-6), font, 1.0, (255, 255, 255), 1)cv2.imshow('Video', frame)if cv2.waitKey(1) & 0xFF == ord('q'):breakvideo_capture.release()
cv2.destroyAllWindows()
识别结果:
我直接在手机上百度了几张图试试,程序识别出了奥巴马。
识别多人的人脸识别测试
代码:
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("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("zhangchi.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","zhang chi"
]# 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), 2)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()
运行结果如图:
开心,毕竟修补了很多坑,有空再来细看程序.
基于face_recognition库的摄像头实时人脸识别测试相关推荐
- Java + opencv 实现人脸识别,图片人脸识别、视频人脸识别、摄像头实时人脸识别
搭建环境 opencv官网下载windows安装包 https://opencv.org/releases/ 选择最新版4.1.1 下载完成后是一个opencv-4.1.1-vc14_vc15.exe ...
- python+ opencv实现摄像头实时人脸识别并实现汉字标框
opencv的puttxt()函数不能汉字输出,这也是困惑好多人都问题,经过几天的查资料,改代码终于成功实现opencv汉字输出. 第一种方法是 是通过写一段代码,能够转码,封装一下再调用,从而实现汉 ...
- 基于FaceNet的实时人脸识别训练
FaceNet人脸特征提取 FaceNet是一种用于提取人脸图像特征的深度神经网络.它由谷歌研究人员 Schroff 等人提出. 论文地址:https://arxiv.org/abs/1503.038 ...
- python人脸识别库_基于facenet的实时人脸识别系统
facenet_facerecognition opencv+mtcnn+facenet+python+tensorflow 实现实时人脸识别 Abstract:本文记录了在学习深度学习过程中,使用o ...
- Keras之CNN:基于Keras利用cv2建立训练存储卷积神经网络模型(2+1)并调用摄像头进行实时人脸识别
Keras之CNN:基于Keras利用cv2建立训练存储卷积神经网络模型(2+1)并调用摄像头进行实时人脸识别 目录 输出结果 设计思路 核心代码 输出结果 设计思路 核心代码 # -*- codin ...
- 基于python,虹软sdk3.0实现的实时人脸识别
前言: 虹软sdk3.0是目前用过的最方便,效果最好的且免费的离线人脸识别SDK. 提供的编程语音没有python,有大佬用c++代码接口转成python调用的, 我在此基础上完善了一些功能,能够实现 ...
- 基于python调用摄像头进行人脸识别,支持多张人脸同时识别
介绍 调用摄像头进行人脸识别, 支持多张人脸同时识别; 摄像头人脸录入 请不要离摄像头过近, 人脸超出摄像头范围时会有 "OUT OF RANGE" 提醒 提取特征建立人脸数据库 ...
- 基于视频的实时人脸识别(含代码)
文章目录 介绍 思路介绍 运行环境介绍 模型介绍 人脸关键点预测器 人脸识别模型 效果展示 识别过程 代码 建立本地人脸库 人脸识别 介绍 思路介绍 无论是基于视频或者调用摄像头来完成人脸识别,其实是 ...
- 基于MTCNN和FaceNet的实时人脸检测识别系统
文章目录 模型介绍 MTCNN FaceNet 基于MTCNN和FaceNet的实时人脸检测识别系统 在LFW数据集上测试 参考文献 GitHub项目地址:https://github.com/Har ...
最新文章
- CCIE PASSED
- python3项目-Python3基础教程(十九)—— 项目结构
- scrapy实现post请求与请求传参
- 最简单的制作从USB启动的系统的方法
- 《京东618实践:一元抢宝系统的数据库架构优化》阅读笔记
- 【报错笔记】pom.xml第一行报错,显示红色叉号
- Program terminated with signal 11, Segmentation fault.
- 同学你好,听说你想学习做硬件?
- 《天天数学》连载18:一月十八日
- 代码里经常看见idle,是什么意思
- Windows切换窗口
- 【Python】列表 - 集大成篇
- win10打开蓝牙_Win10系统中蓝牙鼠标可以配对却无法使用应该如何解决?
- 微信营销辅助工具能够帮助我们解锁哪些新功能?
- Ajax提交与submit提交对比
- [webView stopLoading]; 和 [webView release];
- Google Earth Engine(GEE)——随机森林分类法绘制了2000年、2010年和2020年圭亚那的红树林APP详细代码
- 51单片机入学第一课———点亮自己的LED灯珠
- 什么是VPS(Virtual Private Server 虚拟专用服务器)技术?
- 【前端】JavaScript详细教程(一)