基于python的人脸识别

这篇博客介绍了如何基于Python和深度学习搭建人脸识别模型。作者使用ORL FACE数据集,通过8个步骤,包括数据集加载、数据预处理、模型搭建、训练和测试,最终得出具有高准确率的模型,并通过混淆矩阵分析了模型效果。

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

(学习笔记)

内容

  • 动搭建卷积神经网络
  • 利用数据集训练一个人脸识别分类器
  • 通过分类精度和混淆矩阵量化分析实验结果

操作环境

  • python 3.6
  • tensorflow 2.4.1
  • Keras 2.4.3
  • pandas 0.24.2
  • numpy 1.19.5
  • matplotlib 3.3.4

原理及步骤

数据集:ORL FACE 数据集

  • 本文训练数据集是有20*12张图像组成,即有20个人,每个人拍摄12张,所以其对应label019
  • 每张图像为灰度图,性质为:`92 * 112`
  • 测试集由`20 * 8`张图像组成,即20个人,每人8

步骤1:导入模块和包 

import keras
from keras.models import Sequential
from keras.layers import Conv2D, MaxPooling2D, Dense, Flatten, Dropout
from keras.optimizers import Adam
from keras.callbacks import TensorBoard
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from sklearn.model_selection import train_test_split
from sklearn.metrics import confusion_matrix
from sklearn.metrics import classification_report
from sklearn.metrics import roc_curve, auc
from sklearn.metrics import accuracy_score
from keras.utils import np_utils
import itertools

步骤2:加载数据集

  • 数据集要进行normalization操作
  • 图像是 uint8 的像素矩阵,为了计算,转换为 float double 格式
#load dataset
data = np.load('../Dataset/ORL_faces/ORL_faces.npz')
# load the "Train Images"
x_train = data['trainX']
#normalize every image
x_train = np.array(x_train,dtype='float32')/255
x_test = data['testX']
x_test = np.array(x_test,dtype='float32')/255
# load the Label of Images
y_train= data['trainY']
y_test= data['testY']
# show the train and test Data format
print('x_train : {}'.format(x_train.shape))
print('Y-train shape:{}'.format(y_train.shape))
print('x_test shape: {}'.format(x_test.shape))

数据集形状输出: 

x_train : (240, 10304)
Y-train shape:(240,)
x_test shape: (160, 10304)
  • 数据集包括训练集240张,测试集160
  • 如下,训练集包含 20*12 张图像,测试集包含 20*8 张图像

 y_train

 查看y_train输出:

array([ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1,
1,
        1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2,
2,
        2, 2, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 4, 4,
4,
        4, 4, 4, 4, 4, 4, 4, 4, 4, 5, 5, 5, 5, 5, 5, 5,
5,
        5, 5, 5, 5, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6,
7,
        7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 8, 8, 8, 8, 8,
8,
        8, 8, 8, 8, 8, 8, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9,
9,
        9, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 11, 11, 11,
11,
        11, 11, 11, 11, 11, 11, 11, 11, 12, 12, 12, 12, 12, 12, 12, 12,
12,
        12, 12, 12, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 14,
14,
        14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 15, 15, 15, 15, 15, 15,
15,
        15, 15, 15, 15, 15, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16,
16,
        17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 18, 18, 18, 18,
18,
        18, 18, 18, 18, 18, 18, 18, 19, 19, 19, 19, 19, 19, 19, 19, 19,
19,
        19, 19], dtype=uint8)

y_test  

查看y_test输出:

 

array([ 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1,
2,
        2, 2, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 3, 4,
4,
        4, 4, 4, 4, 4, 4, 5, 5, 5, 5, 5, 5, 5, 5, 6, 6,
6,
        6, 6, 6, 6, 6, 7, 7, 7, 7, 7, 7, 7, 7, 8, 8, 8,
8,
        8, 8, 8, 8, 9, 9, 9, 9, 9, 9, 9, 9, 10, 10, 10, 10,
10,
        10, 10, 10, 11, 11, 11, 11, 11, 11, 11, 11, 12, 12, 12, 12, 12,
12,
        12, 12, 13, 13, 13, 13, 13, 13, 13, 13, 14, 14, 14, 14, 14, 14,
14,
        14, 15, 15, 15, 15, 15, 15, 15, 15, 16, 16, 16, 16, 16, 16, 16,
16,
        17, 17, 17, 17, 17, 17, 17, 17, 18, 18, 18, 18, 18, 18, 18, 18,
19,
        19, 19, 19, 19, 19, 19, 19], dtype=uint8)

步骤3:数据集划分

sklearn.model_selection 中调用 train_test_split 函数进行训练集和验证集划分 
用法:
X_train,X_test, y_train, y_test
=sklearn.model_selection.train_test_split(train_data,train_target,test_size=0.4,
random_state=0,stratify=y_train)
关键参数:
train_data :所要划分的样本特征集
train_target :所要划分的样本结果
test_size :样本占比,如果是整数的话就是样本的数量
random_state :随机数的种子
stratify 是为了保持 split 前类的分布。如 train_test_split(... test_size=0.25, stratify = y_all), 那么split之后数据如下:
training: 75 个数据,其中 60 个属于 A 类, 15
基于Python人脸识别技术主要包括三个部分:人脸检测、人脸特征提取和人脸识别。常用的库包括OpenCV和Dlib等。 以下是一个基于Python人脸识别的示例代码: ```python import cv2 import dlib # 加载正脸检测器 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") # 加载图片 img = cv2.imread("test.jpg") # 转换为灰度图像 gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) # 检测人脸 faces = detector(gray, 1) # 遍历每个人脸 for face in faces: # 获取人脸关键点 landmarks = predictor(gray, face) # 获取人脸特征 face_descriptor = facerec.compute_face_descriptor(img, landmarks) # 在图像中绘制人脸框和关键点 cv2.rectangle(img, (face.left(), face.top()), (face.right(), face.bottom()), (0, 255, 0), 2) for i in range(68): x = landmarks.part(i).x y = landmarks.part(i).y cv2.circle(img, (x, y), 2, (0, 0, 255), -1) # 显示图像 cv2.imshow("image", img) cv2.waitKey(0) cv2.destroyAllWindows() ``` 该代码使用了Dlib库进行人脸检测、关键点检测和人脸识别,并使用OpenCV库进行图像处理和显示。具体步骤包括: 1. 加载正脸检测器、人脸关键点检测器和人脸识别模型。 2. 加载图片并转换为灰度图像。 3. 使用正脸检测器检测人脸。 4. 遍历每个人脸,获取人脸关键点和人脸特征,并在图像中绘制人脸框和关键点。 5. 显示图像。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值