900字范文,内容丰富有趣,生活中的好帮手!
900字范文 > 基于face_recognition库的摄像头实时人脸识别测试

基于face_recognition库的摄像头实时人脸识别测试

时间:2019-08-31 15:31:09

相关推荐

基于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 /davisking/dlib.git

2、编译dlib。

cd dlibmkdir buildcd buildcmake .. -DDLIB_USE_CUDA=0 -DUSE_AVX_INSTRUCTIONS=1cd ..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_recognitionimport 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 = pare_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_recognitionimport 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 namesknown_face_encodings = [obama_face_encoding,biden_face_encoding]known_face_names = ["Barack Obama","zhang chi"]# Initialize some variablesface_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 = pare_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 webcamvideo_capture.release()cv2.destroyAllWindows()

运行结果如图:

开心,毕竟修补了很多坑,有空再来细看程序.

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。