python人脸识别比对

python人脸识别比对

安装相关库。

安装opencv-python库:pip install opencv-python。
安装face_recognition库:pip install face_recognition。
注:安装不成功,请下载visual studio,会自动配置环境即可成功安装face_recognition)

项目目录:

在这里插入图片描述

参考代码:

demo3.py

# 导入图片
image1 = face_recognition.load_image_file('imgs/jz.jpg')
image2 = face_recognition.load_image_file('imgs/zhangjie.jpeg')

# 人脸识别
encodings1 = face_recognition.face_encodings(image1)
encodings2 = face_recognition.face_encodings(image2)

# 人脸位置
locations1 = face_recognition.face_locations(image1)
locations2 = face_recognition.face_locations(image2)

# 人脸数据
face1 = encodings1[0]
face2 = encodings2[0]

res = face_recognition.compare_faces([face1], face2, 0.5)
if res == [True]:
    text = 'PASS'
else:
    text = 'NO'

# 将文字打印在图片上
for x, y, w, h in locations1:
    cv2.rectangle(image1, (y, w), (h, x), (255, 0, 0), 2)
    cv2.putText(image1, text, (y - 10, w - 10), cv2.FONT_HERSHEY_COMPLEX, 0.8, (0, 255, 0), 2)
frame1 = cv2.cvtColor(image1, cv2.COLOR_BGR2RGB)
# 将文字打印在图片上
for x, y, w, h in locations2:
    cv2.rectangle(image2, (y, w), (h, x), (255, 0, 0), 2)
    cv2.putText(image2, text, (y - 10, w - 10), cv2.FONT_HERSHEY_COMPLEX, 0.8, (0, 255, 0), 2)
frame2 = cv2.cvtColor(image2, cv2.COLOR_BGR2RGB)
cv2.imshow('1', frame1)
cv2.imshow('2', frame2)
cv2.waitKey(0)
cv2.destroyAllWindows()

运行结果:


识别出两张图片是同一人脸,显示结果为“pass”!!!

  • 0
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
以下是一个简单的 Python 人脸识别比对代码,使用了 OpenCV 和 face_recognition 库: ```python import cv2 import face_recognition # 加载已知人脸图像和对应标签 known_image = face_recognition.load_image_file("known_face.jpg") known_encoding = face_recognition.face_encodings(known_image)[0] known_label = "Tom" # 打开摄像头 capture = cv2.VideoCapture(0) while True: # 读取摄像头图像 ret, frame = 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 face_encoding in face_encodings: matches = face_recognition.compare_faces([known_encoding], face_encoding) label = "Unknown" # 如果是已知人脸,则显示对应标签 if matches[0]: label = known_label # 在图像中框出人脸并显示标签 top, right, bottom, left = face_locations[0] cv2.rectangle(frame, (left, top), (right, bottom), (0, 0, 255), 2) cv2.putText(frame, label, (left, top - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 0, 255), 2) # 显示图像 cv2.imshow("Face Recognition", frame) # 如果按下q键,则退出程序 if cv2.waitKey(1) & 0xFF == ord('q'): break # 释放摄像头并关闭窗口 capture.release() cv2.destroyAllWindows() ``` 在此代码中,我们首先加载了已知人脸的图像和标签,并使用 face_recognition 库将其编码为向量。然后,我们打开摄像头并循环读取摄像头图像。对于每张图像,我们使用 face_recognition 库查找人脸位置,并将其编码为向量。然后,我们将已知人脸的编码向量与每个人脸进行比对,如果匹配,则显示对应标签,并在图像中框出人脸。最后,我们使用 OpenCV 显示图像,并在按下q键时退出程序。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值