python摄像头识别检测已有照片人脸

B站影视 2024-12-12 12:20 2

摘要:import cv2import face_recognition# 1. 加载目标人脸照片和提取特征known_face_encodings = known_face_names = # 加载 mwj 的照片mwj_image = face_recognit

需要安装的库:dlib==19.24.99,face_recognition,opencv等,python环境3.9.19

以识别2个人脸为例,代码如下

import cv2import face_recognition# 1. 加载目标人脸照片和提取特征known_face_encodings = known_face_names = # 加载 mwj 的照片mwj_image = face_recognition.load_image_file("lucy.jpg")mwj_encoding = face_recognition.face_encodings(mwj_image)[0]known_face_encodings.append(mwj_encoding)known_face_names.append("lucy")# 加载 sky 的照片sky_image = face_recognition.load_image_file("lena.jpg")sky_encoding = face_recognition.face_encodings(sky_image)[0]known_face_encodings.append(sky_encoding)known_face_names.append("lena")# 2. 打开视频video_capture = cv2.VideoCapture(0) # 替换为 0 使用摄像头# video_capture = cv2.VideoCapture("video.mp4") # 替换为 0 使用摄像头while video_capture.isOpened: ret, frame = video_capture.read if not ret: break # 3. 在视频帧中检测人脸 rgb_frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB) # 转为 RGB face_locations = face_recognition.face_locations(rgb_frame) face_encodings = face_recognition.face_encodings(rgb_frame, face_locations) for (top, right, bottom, left), face_encoding in zip(face_locations, face_encodings): # 4. 将当前人脸与目标人脸进行比较 matches = face_recognition.compare_faces(known_face_encodings, face_encoding) name = "Unknown" # 如果匹配到人脸,标记名字 if True in matches: match_index = matches.index(True) name = known_face_names[match_index] # 5. 在视频帧中标记人脸 cv2.rectangle(frame, (left, top), (right, bottom), (0, 255, 0), 2) cv2.putText(frame, name, (left, top - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.9, (0, 255, 0), 2) # 显示视频帧 cv2.imshow("Video", frame) # 按 'q' 键退出 if cv2.waitKey(1) & 0xFF == ord('q'): break# 释放资源video_capture.releasecv2.destroyAllWindows如果要检测2人以上的人脸可以使用以下方式实现,代码目录新建一个文件夹faces,里面放命名好人名的头像照片,这样不管3张5张还是更多都可以检测了import cv2import face_recognitionimport os# 1. 加载目标人脸照片和提取特征known_face_encodings = known_face_names = # 设置存放人脸照片的目录faces_directory = "faces" # 替换为你的目录路径# 遍历目录中的所有图片文件for filename in os.listdir(faces_directory): if filename.endswith(".jpg") or filename.endswith(".png"): # 加载图片并提取特征 image_path = os.path.join(faces_directory, filename) image = face_recognition.load_image_file(image_path) encoding = face_recognition.face_encodings(image)[0] # 将特征和名字添加到列表 known_face_encodings.append(encoding) name = os.path.splitext(filename)[0] # 使用文件名作为名字 known_face_names.append(name)print(f"已加载以下人脸: {known_face_names}")# 2. 打开视频video_capture = cv2.VideoCapture(0) # 使用摄像头# video_capture = cv2.VideoCapture("video.mp4") # 使用视频文件while video_capture.isOpened: ret, frame = video_capture.read if not ret: break # 3. 在视频帧中检测人脸 rgb_frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB) # 转为 RGB face_locations = face_recognition.face_locations(rgb_frame) face_encodings = face_recognition.face_encodings(rgb_frame, face_locations) for (top, right, bottom, left), face_encoding in zip(face_locations, face_encodings): # 4. 将当前人脸与目标人脸进行比较 matches = face_recognition.compare_faces(known_face_encodings, face_encoding) name = "Unknown" # 如果匹配到人脸,标记名字 if True in matches: match_index = matches.index(True) name = known_face_names[match_index] # 5. 在视频帧中标记人脸 cv2.rectangle(frame, (left, top), (right, bottom), (0, 255, 0), 2) cv2.putText(frame, name, (left, top - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.9, (0, 255, 0), 2) # 显示视频帧 cv2.imshow("Video", frame) # 按 'q' 键退出 # if cv2.waitKey(1) & 0xFF == ord('q'): # 按 'Esc' 键退出 if cv2.waitKey(1) & 0xFF == 27: # 27 是 Esc 键的键值 break# 释放资源video_capture.releasecv2.destroyAllWindows

来源:新手村养牛

相关推荐