利用face_recognition,dlib与OpenCV调用摄像头进行人脸识别
Posted guihua-pingting
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了利用face_recognition,dlib与OpenCV调用摄像头进行人脸识别相关的知识,希望对你有一定的参考价值。
用已经搭建好
import face_recognition import cv2 video_capture = cv2.VideoCapture(0) # VideoCapture打开摄像头,0为笔记本内置摄像头,1为外USB摄像头,或写入视频路径 mayun_img = face_recognition.load_image_file("mayun.jpg") jobs_img = face_recognition.load_image_file("jobs.jpg") mayun_face_encoding = face_recognition.face_encodings(mayun_img)[0] jobs_face_encoding = face_recognition.face_encodings(jobs_img)[0] face_locations = [] face_encodings = [] face_names = [] process_this_frame = True while True: ret, frame = video_capture.read() # video_capture.read()按帧读取视频,ret,frame是获video_capture.read()方法的两个返回值。 # 其中ret是布尔值,如果读取帧是正确的则返回True, # 如果文件读取到结尾,它的返回值就为False。frame就是每一帧的图像,是个三维矩阵。 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([mayun_face_encoding, jobs_face_encoding], face_encoding) if match[0]: name = "mayun" elif match[1]: name = "jobs" else: name = "unknown" print(name) face_names.append(name) process_this_frame = not process_this_frame for (top, right, bottom, left), name in zip(face_locations, face_names): top *= 4 right *= 4 bottom *= 4 left *= 4 cv2.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_DUPLEX cv2.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‘): break video_capture.release() cv2.destroyAllWindows()
我使用手机中的照片来进行验证
因为是按每一帧进行读取:
有些人在使用时会出现摄像头打不开的情况
我使用的是win10系统给出一个建议:
在设置中找到隐私设置,在找到相机选项,把权限打开即可
以上是关于利用face_recognition,dlib与OpenCV调用摄像头进行人脸识别的主要内容,如果未能解决你的问题,请参考以下文章
人脸验证(图片/视频) tensorflowpytorch框架dlib库(face_recognition)和opencv库————附带详细步骤和代码,可实际运行
来自 dlib.image_window 的 Dlib face_recognition 错误
人脸识别----face_recognition安装与应用(附代码)