python face_recognition实现人脸识别系统
    		       		warning:
    		            这篇文章距离上次修改已过438天,其中的内容可能已经有所变动。
    		        
        		                
                
import face_recognition
import cv2
 
# 加载已知的人脸图片
known_image = face_recognition.load_image_file("known_person.jpg")
known_face_encoding = face_recognition.face_encodings(known_image)[0]
 
# 视频中的人脸识别
video_capture = cv2.VideoCapture(0)  # 打开摄像头
 
while True:
    # 捕获视频帧
    ret, frame = video_capture.read()
    # 对视频帧中的人脸进行编码
    face_encodings = face_recognition.face_encodings(frame)
 
    # 如果找到了人脸编码,进行匹配
    if len(face_encodings) > 0:
        matches = face_recognition.compare_faces(known_face_encoding, face_encodings)
 
        # 用圆圈标记出识别到的人脸
        for i, match in enumerate(matches):
            if match:
                cv2.rectangle(frame, (i*100, 10), (i*100+100, 120), (0, 255, 0), 2)
                cv2.putText(frame, "Match", (i*100+50, 80), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 255, 0), 2)
            else:
                cv2.rectangle(frame, (i*100, 10), (i*100+100, 120), (0, 0, 255), 2)
                cv2.putText(frame, "Unknown", (i*100+50, 80), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 0, 255), 2)
 
    # 显示视频帧
    cv2.imshow('Video', frame)
 
    # 按 'q' 退出循环
    if cv2.waitKey(1) & 0xFF == ord('q'):
        break
 
# 释放摄像头资源
video_capture.release()
cv2.destroyAllWindows()这段代码使用了face_recognition库来识别来自摄像头的视频中的人脸。如果识别到已知的人脸,它会用绿色标记;如果不是已知的人脸,则用红色标记。按下'q'键可以退出视频显示。这个例子简单演示了如何将人脸识别系统集成到实时视频监控中。
评论已关闭