使用python写一个人脸识别程序
由于人脸识别程序需要用到深度学习框架和大量的人脸数据集,因此需要使用相关的库和工具。以下是一个基本的人脸识别程序的示例代码,使用了OpenCV和dlib库。
首先,需要安装相关库:
pip install opencv-python
pip install dlib
然后,导入需要的库:
import cv2
import dlib
import numpy as np
接下来,定义一个函数来加载人脸检测器和人脸识别模型:
def load_models():
detector = dlib.get_frontal_face_detector()
predictor = dlib.shape_predictor("shape_predictor_68_face_landmarks.dat")
facerec = dlib.face_recognition_model_v1("dlib_face_recognition_resnet_model_v1.dat")
return detector, predictor, facerec
这个函数使用了dlib库中的人脸检测器和人脸识别模型。其中,get_frontal_face_detector函数返回一个人脸检测器对象,shape_predictor函数返回一个关键点定位器对象,用于定位人脸的各个关键点,face_recognition_model_v1函数返回一个人脸识别模型对象,用于将人脸图像转换为特征向量。
然后,定义一个函数来提取人脸特征向量:
def extract_features(img, detector, predictor, facerec):
# 将图像转换为灰度图像
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
# 使用人脸检测器检测人脸
faces = detector(gray)
# 如果没有检测到人脸,则返回空列表
if len(faces) == 0:
return []
# 对于每个检测到的人脸,提取特征向量
features = []
for face in faces:
# 使用关键点定位器定位关键点
shape = predictor(gray, face)
# 使用人脸识别模型提取特征向量
feature = facerec.compute_face_descriptor(img, shape)
# 将特征向量添加到列表中
features.append(feature)
return features
这个函数使用了extract_features函数提取人脸特征向量。首先,将图像转换为灰度图像,然后使用人脸检测器检测人脸。如果没有检测到人脸,则返回空列表。对于每个检测到的人脸,使用关键点定位器定位关键点,然后使用人脸识别模型提取特征向量,并将特征向量添加到列表中。
最后,定义一个主函数来加载模型和图像,提取人脸特征向量,并进行人脸比对:
def main():
# 加载模型
detector, predictor, facerec = load_models()
# 加载图像
img1 = cv2.imread("img1.jpg")
img2 = cv2.imread("img2.jpg")
# 提取人脸特征向量
features1 = extract_features(img1, detector, predictor, facerec)
features2 = extract_features(img2, detector, predictor, facerec)
# 如果没有检测到人脸,则返回错误信息
if len(features1) == 0 or len(features2) == 0:
print("Error: no face detected.")
return
# 计算欧氏距离
dist = np.linalg.norm(np.array(features1) - np.array(features2))
# 输出结果
if dist < 0.6:
print("Same person.")
else:
print("Different person.")
这个函数首先加载人脸检测器、关键点定位器和人脸识别模型,并加载两张需要比对的图像。然后,使用extract_features函数提取两张图像的人脸特征向量。如果没有检测到人脸,则返回错误信息。接下来,计算两个特征向量之间的欧氏距离,如果小于0.6,则认为是同一个人,否则认为是不同的人。最后,输出比对结果。
完整代码如下:
import cv2
import dlib
import numpy as np
def load_models():
detector = dlib.get_frontal_face_detector()
predictor = dlib.shape_predictor("shape_predictor_68_face_landmarks.dat")
facerec = dlib.face_recognition_model_v1("dlib_face_recognition_resnet_model_v1.dat")
return detector, predictor, facerec
def extract_features(img, detector, predictor, facerec):
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
faces = detector(gray)
if len(faces) == 0:
return []
features = []
for face in faces:
shape = predictor(gray, face)
feature = facerec.compute_face_descriptor(img, shape)
features.append(feature)
return features
def main():
detector, predictor, facerec = load_models()
img1 = cv2.imread("img1.jpg")
img2 = cv2.imread("img2.jpg")
features1 = extract_features(img1, detector, predictor, facerec)
features2 = extract_features(img2, detector, predictor, facerec)
if len(features1) == 0 or len(features2) == 0:
print("Error: no face detected.")
return
dist = np.linalg.norm(np.array(features1) - np.array(features2))
if dist < 0.6:
print("Same person.")
else:
print("Different person.")
if __name__ == "__main__":
main()
注意,这个人脸识别程序只是一个简单的示例,实际的人脸识别程序需要更多的优化和改进。
原文地址: https://www.cveoy.top/t/topic/bpXA 著作权归作者所有。请勿转载和采集!