python实现人脸识别比对_人脸识别并比对实现(基于face_recognition)

本文介绍了如何在Python环境中利用Face Recognition库进行人脸识别比对。首先确保安装Python和pip3,接着详细阐述了安装dlib和face_recognition的步骤,包括下载源代码、编译和安装。最后,通过示例代码展示了如何加载图片、计算人脸编码距离,并根据距离判断人脸是否匹配。
摘要由CSDN通过智能技术生成

Face Recognition 是一个基于python的人脸识别框架,我们在此之上来实现人脸比对。

环境要求

Python 3.3+ or Python 2.7

macOS or Linux (Windows没有正式支持,但可能可以用)

我的环境

Python 3.5.2

ubuntu 16.04

首先,我们要安装python(有可能已经默认装好了)sudo apt-get install python3

安装pip3sudo apt-get install python3-pip

安装dlib,这个过程有点长

先安装基本包sudo apt-get install -y --fix-missing

build-essential

cmake

gfortran

git

wget

curl

graphicsmagick

libgraphicsmagick1-dev

libatlas-dev

libavcodec-dev

libavformat-dev

libgtk2.0-dev

libjpeg-dev

liblapack-dev

libswscale-dev

pkg-config

python3-dev

python3-numpy

software-properties-common

zip

克隆原代码git clone https://github.com/davisking/dlib.git

切换目录,编译cd dlib

mkdir build; c

以下是一个简单的 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键时退出程序。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值