机器学习笔记 - 使用Face recognition、OpenCV、Python进行人脸识别
一、安装相关包
安装dlib、cmake、face_recognition。
face_recognition是世界上最简单的人脸识别库,使用 dlib 最先进的深度学习人脸识别技术构建。 该模型的准确率为 99.38%。
二、获取人脸的128位数组编码
使用基本演绎法这两个演员的脸测试,只用了这两张正脸进行识别,如果要识别准确,得准备多种角度的照片,才能比较精准。
三、参考代码
# 使用Face recognition获取人脸128位数组from imutils.video import VideoStream
from imutils import paths
import face_recognition
import argparse
import pickle
import cv2
import os
import imutilsdef train():# grab the paths to the input images in our datasetprint("[INFO] quantifying faces...")imagePaths = ['C:Users/xia/Desktop/Watson.png','C:/Users/xia/Desktop/Sherlock.png']# initialize the list of known encodings and known namesknownEncodings = []knownNames = []# loop over the image pathsfor (i, imagePath) in enumerate(imagePaths):# extract the person name from the image pathprint("[INFO] processing image {}/{}".format(i + 1, len(imagePaths)))name = 'Watson'if i==1:name = 'Sherlock'# load the input image and convert it from BGR (OpenCV ordering)# to dlib ordering (RGB)image = cv2.imread(imagePath)rgb = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)# detect the (x, y)-coordinates of the bounding boxes# corresponding to each face in the input imageboxes = face_recognition.face_locations(rgb, model='cnn')# compute the facial embedding for the faceencodings = face_recognition.face_encodings(rgb, boxes)# loop over the encodingsfor encoding in encodings:# add each encoding + name to our set of known names and# encodingsknownEncodings.append(encoding)knownNames.append(name)# dump the facial encodings + names to diskprint("[INFO] serializing encodings...")data = {"encodings": knownEncodings, "names": knownNames}f = open('C:/Users/xia/Desktop/encodings.pickle', "wb")f.write(pickle.dumps(data))f.close()
四、进行人脸识别测试
# 使用Face recognition获取人脸128位from imutils.video import VideoStream
from imutils import paths
import face_recognition
import argparse
import pickle
import cv2
import os
import imutils# 在图片上测试
def reimage():# load the known faces and embeddingsprint("[INFO] loading encodings...")data = pickle.loads(open('C:/Users/xia/Desktop/encodings.pickle', "rb").read())# load the input image and convert it from BGR to RGBimage = cv2.imread('C:/Users/xia/Desktop/v2.jpg')rgb = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)# detect the (x, y)-coordinates of the bounding boxes corresponding# to each face in the input image, then compute the facial embeddings# for each faceprint("[INFO] recognizing faces...")boxes = face_recognition.face_locations(rgb, model='cnn')encodings = face_recognition.face_encodings(rgb, boxes)# initialize the list of names for each face detectednames = []# loop over the facial embeddingsfor encoding in encodings:# attempt to match each face in the input image to our known# encodingsmatches = face_recognition.compare_faces(data["encodings"], encoding)name = "Unknown"# check to see if we have found a matchif True in matches:# find the indexes of all matched faces then initialize a# dictionary to count the total number of times each face# was matchedmatchedIdxs = [i for (i, b) in enumerate(matches) if b]counts = {}# loop over the matched indexes and maintain a count for# each recognized face facefor i in matchedIdxs:name = data["names"][i]counts[name] = counts.get(name, 0) + 1# determine the recognized face with the largest number of# votes (note: in the event of an unlikely tie Python will# select first entry in the dictionary)name = max(counts, key=counts.get)# update the list of namesnames.append(name)# loop over the recognized facesfor ((top, right, bottom, left), name) in zip(boxes, names):# draw the predicted face name on the imagecv2.rectangle(image, (left, top), (right, bottom), (0, 255, 0), 2)y = top - 15 if top - 15 > 15 else top + 15cv2.putText(image, name, (left, y), cv2.FONT_HERSHEY_SIMPLEX,0.75, (0, 255, 0), 2)# show the output imagecv2.imshow("Image", image)cv2.waitKey(0)
五、摄像头实时测试
下面是实时视频的参考代码,使用cpu+hpg也是很卡,得上gpu测试。
# 使用Face recognition获取人脸128位from imutils.video import VideoStream
from imutils import paths
import face_recognition
import argparse
import pickle
import cv2
import os
import imutils# 实时视频,但是需要gpu,即使用cpu+hog,也会卡顿
def revideo():# load the known faces and embeddingsprint("[INFO] loading encodings...")data = pickle.loads(open('C:/Users/zyh/Desktop/encodings.pickle', "rb").read())# initialize the video stream and pointer to output video file, then# allow the camera sensor to warm upprint("[INFO] starting video stream...")vs = VideoStream(src=0).start()writer = None#time.sleep(2.0)# loop over frames from the video file streamwhile True:# grab the frame from the threaded video streamframe = vs.read()# convert the input frame from BGR to RGB then resize it to have# a width of 750px (to speedup processing)rgb = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)rgb = imutils.resize(frame, width=750)r = frame.shape[1] / float(rgb.shape[1])# detect the (x, y)-coordinates of the bounding boxes# corresponding to each face in the input frame, then compute# the facial embeddings for each faceboxes = face_recognition.face_locations(rgb, model='hog')encodings = face_recognition.face_encodings(rgb, boxes)names = []# loop over the facial embeddingsfor encoding in encodings:# attempt to match each face in the input image to our known# encodingsmatches = face_recognition.compare_faces(data["encodings"],encoding)name = "Unknown"# check to see if we have found a matchif True in matches:# find the indexes of all matched faces then initialize a# dictionary to count the total number of times each face# was matchedmatchedIdxs = [i for (i, b) in enumerate(matches) if b]counts = {}# loop over the matched indexes and maintain a count for# each recognized face facefor i in matchedIdxs:name = data["names"][i]counts[name] = counts.get(name, 0) + 1# determine the recognized face with the largest number# of votes (note: in the event of an unlikely tie Python# will select first entry in the dictionary)name = max(counts, key=counts.get)# update the list of namesnames.append(name)# loop over the recognized facesfor ((top, right, bottom, left), name) in zip(boxes, names):# rescale the face coordinatestop = int(top * r)right = int(right * r)bottom = int(bottom * r)left = int(left * r)# draw the predicted face name on the imagecv2.rectangle(frame, (left, top), (right, bottom),(0, 255, 0), 2)y = top - 15 if top - 15 > 15 else top + 15cv2.putText(frame, name, (left, y), cv2.FONT_HERSHEY_SIMPLEX,0.75, (0, 255, 0), 2)# 写入视频# # if the video writer is None *AND* we are supposed to write# # the output video to disk initialize the writer# if writer is None and args["output"] is not None:# fourcc = cv2.VideoWriter_fourcc(*"MJPG")# writer = cv2.VideoWriter(args["output"], fourcc, 20,# (frame.shape[1], frame.shape[0]), True)# # if the writer is not None, write the frame with recognized# # faces to disk# if writer is not None:# writer.write(frame)# check to see if we are supposed to display the output frame to# the screenif 1 > 0:cv2.imshow("Frame", frame)key = cv2.waitKey(1) & 0xFF# if the `q` key was pressed, break from the loopif key == ord("q"):break# do a bit of cleanupcv2.destroyAllWindows()vs.stop()# check to see if the video writer point needs to be releasedif writer is not None:writer.release()
六、打开视频测试
# 使用Face recognition获取人脸128位from imutils.video import VideoStream
from imutils import paths
import face_recognition
import argparse
import pickle
import cv2
import os
import imutilsdef revideo1():# load the known faces and embeddingsprint("[INFO] loading encodings...")data = pickle.loads(open('C:/Users/xia/Desktop/encodings.pickle', "rb").read())# initialize the video stream and pointer to output video file, thencap = cv2.VideoCapture('C:/Users/xia/Desktop/123.mp4')# loop over frames from the video file streamwhile(cap.isOpened()):# grab the frame from the threaded video streamret, frame = cap.read()# convert the input frame from BGR to RGB then resize it to have# a width of 750px (to speedup processing)rgb = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)rgb = imutils.resize(frame, width=750)r = frame.shape[1] / float(rgb.shape[1])# detect the (x, y)-coordinates of the bounding boxes# corresponding to each face in the input frame, then compute# the facial embeddings for each faceboxes = face_recognition.face_locations(rgb, model='hog')encodings = face_recognition.face_encodings(rgb, boxes)names = []# loop over the facial embeddingsfor encoding in encodings:# attempt to match each face in the input image to our known# encodingsmatches = face_recognition.compare_faces(data["encodings"], encoding)name = "Unknown"# check to see if we have found a matchif True in matches:# find the indexes of all matched faces then initialize a# dictionary to count the total number of times each face# was matchedmatchedIdxs = [i for (i, b) in enumerate(matches) if b]counts = {}# loop over the matched indexes and maintain a count for# each recognized face facefor i in matchedIdxs:name = data["names"][i]counts[name] = counts.get(name, 0) + 1# determine the recognized face with the largest number# of votes (note: in the event of an unlikely tie Python# will select first entry in the dictionary)name = max(counts, key=counts.get)# update the list of namesnames.append(name)# loop over the recognized facesfor ((top, right, bottom, left), name) in zip(boxes, names):# rescale the face coordinatestop = int(top * r)right = int(right * r)bottom = int(bottom * r)left = int(left * r)# draw the predicted face name on the imagecv2.rectangle(frame, (left, top), (right, bottom),(0, 255, 0), 2)y = top - 15 if top - 15 > 15 else top + 15cv2.putText(frame, name, (left, y), cv2.FONT_HERSHEY_SIMPLEX,0.75, (0, 255, 0), 2)# check to see if we are supposed to display the output frame to# the screenif 1 > 0:cv2.imshow("Frame", frame)key = cv2.waitKey(1) & 0xFF# if the `q` key was pressed, break from the loopif key == ord("q"):break# do a bit of cleanupcap.release()cv2.destroyAllWindows()
七、理论参考
https://medium.com/@ageitgey/machine-learning-is-fun-part-4-modern-face-recognition-with-deep-learning-c3cffc121d78https://medium.com/@ageitgey/machine-learning-is-fun-part-4-modern-face-recognition-with-deep-learning-c3cffc121d78
dlib C++ Library: High Quality Face Recognition with Deep Metric Learninghttp://blog.dlib.net/2017/02/high-quality-face-recognition-with-deep.html
八、其它(安装dlib gpu版本):
git clone https://github.com/davisking/dlib.git
cd dlib
mkdir build
cd build
cmake .. -DDLIB_USE_CUDA=1 -DUSE_AVX_INSTRUCTIONS=1
cmake --build .
cd ..
python3.6 setup.py install --yes USE_AVX_INSTRUCTIONS --yes DLIB_USE_CUDA
机器学习笔记 - 使用Face recognition、OpenCV、Python进行人脸识别相关推荐
- 基于 OpenCV + Python 的人脸识别上课签到系统
目录 前言 安装第三方库 第一步:采集人脸图像 (1)修改姓名学号 (2)运行capture_face.py (3)采集人脸图像 (4)查看采集到的人脸图像 第二步:训练模型 第三步:识别签到 (1) ...
- python人脸识别opencv_手把手教你如何用 OpenCV + Python 实现人脸识别
必备知识 Haar-like 通俗的来讲,就是作为人脸特征即可. Haar特征值反映了图像的灰度变化情况.例如:脸部的一些特征能由矩形特征简单的描述,如:眼睛要比脸颊颜色要深,鼻梁两侧比鼻梁颜色要深, ...
- opencv 人脸识别_人工智能-OpenCV+Python实现人脸识别(视频人脸检测)
上期文章我们分享了opencv识别图片中的人脸,OpenCV图片人脸检测,本期我们分享一下如何从视频中检测到人脸 视频人脸检测 OpenCV打开摄像头特别简单,只需要如下一句代码 capture = ...
- 人工智能-OpenCV+Python实现人脸识别(视频人脸检测)
上期文章我们分享了opencv识别图片中的人脸,OpenCV图片人脸检测,本期我们分享一下如何从视频中检测到人脸 视频人脸检测 OpenCV打开摄像头特别简单,只需要如下一句代码 capture = ...
- Python+OpenCv实现AI人脸识别身份认证系统(2)——人脸数据采集、存储
原 Python+OpenCv实现AI人脸识别身份认证系统(2)--人脸数据采集.存储 2019年07月02日 08:47:52 不脱发的程序猿 阅读数 602更多 所属专栏: 人脸识别身份认证系统设 ...
- 为树莓派安装opencv并进行人脸识别
为树莓派安装opencv并进行人脸识别 1.更改modules文件 2.确认树莓派摄像头安装成功 3.在树莓派上安装运行在Python2上的OpenCV 4.下载人脸识别代码和xml文件并运行 参考: ...
- Python+OpenCv实现AI人脸识别身份认证系统(1)——人脸识别原理
原 Python+OpenCv实现AI人脸识别身份认证系统(1)--人脸识别原理 置顶 2019年07月02日 08:47:40 不脱发的程序猿 阅读数 1255更多 所属专栏: 人脸识别身份认证系统 ...
- Python+OpenCv实现AI人脸识别身份认证系统(3)——训练人脸识别模型
目录 案例引入 本节项目 最近有小伙伴们一直在催本项目的进度,好吧,今晚熬夜加班编写,在上一节中,实现了人脸数据的采集,在本节中将对采集的人脸数据进行训练,生成识别模型. 案例引入 首先简要讲解数据集 ...
- python人脸识别理论_使用OpenCV和Python进行人脸识别
介绍 人脸识别是什么?或识别是什么?当你看到一个苹果时,你的大脑会立刻告诉你这是一个苹果.在这个过程中,你的大脑告诉你这是一个苹果水果,用简单的语言来说就是识别.那么什么是人脸识别呢?我肯定你猜对了. ...
- OpenCV + python 实现人脸检测(基于照片和视频进行检测)
OpenCV + python 实现人脸检测(基于照片和视频进行检测) Haar-like 通俗的来讲,就是作为人脸特征即可. Haar特征值反映了图像的灰度变化情况.例如:脸部的一些特征能由矩形特征 ...
最新文章
- Windbg中使用查找内存并设置访问断点
- python123动物重量排序_python基本常识
- 翟树卿:如何让数据挖掘助力精准化营销
- Java中windows路径转换成linux路径等工具类
- string 转 int_面试官:String长度有限制吗?是多少?还好我看过
- 【学习笔记】【Design idea】一、Java异常的设计思想、性能相关、笔记
- php mysql连续签到跨月_PHP连续签到功能实现方法详解
- aix内核是linux,查看Linux及AIX硬件信息方法总结
- python串口数据绘图_使用Python串口实时显示数据并绘图的例子
- 速收藏,《机器学习实战》Python3环境算法实现代码
- ios 获取沙盒文件名_IOS获取各种文件目录路径的方法
- android之activity跳转
- origin函数绘图_资料|史上最全10套Origin科研必备绘图教学,无需代码
- 海洋网络收音机海洋影音盒
- 磁盘,分区,文件系统
- 如何一键远程开机,远程唤醒功能
- manifestintert-filter详解
- 手写简易版链表及原理分析
- “熵”详细学习笔记——什么是熵?有什么性质?联合熵等其他熵的作用
- Dell 灵越7559笔记本电脑加M.2固态硬盘