人脸识别(摄像头)——基于face_recognition库

  • 1
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,下面是一个简单的基于face_recognition人脸识别方案代码示例: ```python import face_recognition import cv2 # 加载已知人脸的图像和名称 known_face_encodings = [] known_face_names = [] image1 = face_recognition.load_image_file("person1.jpg") image2 = face_recognition.load_image_file("person2.jpg") image3 = face_recognition.load_image_file("person3.jpg") known_face_encodings.append(face_recognition.face_encodings(image1)[0]) known_face_encodings.append(face_recognition.face_encodings(image2)[0]) known_face_encodings.append(face_recognition.face_encodings(image3)[0]) known_face_names = ["person1", "person2", "person3"] # 打开摄像头 cap = cv2.VideoCapture(0) while True: # 读取一帧图像 ret, frame = cap.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 face_encoding, face_location in zip(face_encodings, face_locations): # 尝试匹配人脸 matches = face_recognition.compare_faces(known_face_encodings, face_encoding) # 如果有匹配到的人脸,则取出其名称 if True in matches: match_index = matches.index(True) name = known_face_names[match_index] else: name = "unknown" # 绘制人脸矩形框和名称 top, right, bottom, left = face_location cv2.rectangle(frame, (left, top), (right, bottom), (0, 255, 0), 2) cv2.putText(frame, name, (left, top-10), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 255, 0), 2) # 显示图像 cv2.imshow('Face Recognition', frame) # 按下q键退出 if cv2.waitKey(1) & 0xFF == ord('q'): break # 释放摄像头和窗口 cap.release() cv2.destroyAllWindows() ``` 在这个代码示例中,我们使用face_recognition加载了已知人脸的图像和名称,并使用cv2.VideoCapture()方法打开了摄像头。在每一帧图像中,我们使用face_recognition检测人脸,并使用face_recognition.compare_faces()方法尝试匹配人脸。如果有匹配到的人脸,则取出其名称,并使用cv2.rectangle()和cv2.putText()方法绘制人脸矩形框和名称。最后,我们使用cv2.imshow()方法显示图像,并使用cv2.waitKey()方法等待用户按下q键退出程序。 值得注意的是,face_recognition是一个基于dlib人脸识别,其检测速度较慢,但是识别精度较高。在实际应用中,我们需要根据具体情况选择不同的人脸识别方法以达到更好的效果。 希望这个代码对您有所帮助!

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值