python3人脸识别_python三步实现人脸识别

此博客围绕Python3进行人脸识别展开。Python3在信息技术领域应用广泛,人脸识别是热门技术。借助Python3可实现高效准确的人脸识别功能,在安防、门禁等场景有重要作用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

以下是使用Python实现人脸识别的代码示例,需要用到OpenCV和face_recognition库: ``` import cv2 import face_recognition # 加载已知人脸图像 obama_image = face_recognition.load_image_file("obama.jpg") obama_face_encoding = face_recognition.face_encodings(obama_image)[0] # 创建已知人脸编码列表和名称列表 known_face_encodings = [ obama_face_encoding ] known_face_names = [ "Barack Obama" ] # 加载摄像头 video_capture = cv2.VideoCapture(0) while True: # 读取摄像头帧 ret, frame = video_capture.read() # 将帧转换为RGB格式 rgb_frame = frame[:, :, ::-1] # 查找帧中所有人脸的位置和特征 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): # 尝试匹配人脸 matches = face_recognition.compare_faces(known_face_encodings, face_encoding) name = "Unknown" # 如果找到了匹配的人脸,则使用其名称 if True in matches: first_match_index = matches.index(True) name = known_face_names[first_match_index] # 绘制人脸边框和名称 cv2.rectangle(frame, (left, top), (right, bottom), (0, 0, 255), 2) cv2.putText(frame, name, (left + 6, bottom - 6), cv2.FONT_HERSHEY_DUPLEX, 0.5, (0, 0, 255), 1) # 显示结果 cv2.imshow('Video', frame) # 按下q退出 if cv2.waitKey(1) & 0xFF == ord('q'): break # 释放摄像头 video_capture.release() cv2.destroyAllWindows() ``` 在上述代码中,我们首先加载已知的人脸图像,并使用`face_recognition`库计算它们的特征向量。然后,我们使用摄像头读取帧,并使用`face_recognition`查找每个帧中的人脸位置和特征。对于每个人脸,我们尝试将其与已知人脸进行匹配,并在图像中绘制人脸框和名称。最后,我们使用OpenCV显示结果,并等待用户按下“q”键退出。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值