版权声明:转载请说明来源,谢谢 https://blog.csdn.net/wsp_1138886114/article/details/80704360
文章目录
一、概述
广义的人脸识别实际包括构建人脸识别系统的一系列相关技术,包括人脸图像采集、人脸定位或检测、人脸识别预处理、
身份确认以及身份查找等;而狭义的人脸识别特指通过人脸进行身份确认或者身份查找的技术或系统。 人脸识别是一项
热门的计算机技术研究领域,它属于生物特征识别技术,是对生物体(一般特指人)本身的生物特征来区分生物体个体。
本章主要内容如下:
1)先获取自己的头像,可以通过手机、电脑等拍摄;
2)下载别人的头像,具体网址详见下节;
3)利用dlib、opencv对人脸进行检测;
4)根据检测后的图片,利用卷积神经网络训练模型;
5)把新头像用模型进行识别,看模型是否能认出是你。
1.1 项目概况
1). 数据集:总共数据集由两部分组成:他人脸图片集及我自己的部分图片
图片从以下网站获取:
网站地址:http://vis-www.cs.umass.edu/lfw/
图片集下载:http://vis-www.cs.umass.edu/lfw/lfw.tgz
自己图片可以手机或其它方法拍摄,上传到电脑,输入数据放在:
data/face_recog目录下
别人输入图片存放目录
./data/face_recog/other_faces
我的测试图片目录:
./data/face_recog/test_faces
2)人脸识别
获取数据后,第一件事就对对图片进行处理,即人脸识别,把人脸的范围确定下来,人脸识别有很多方法,
这里使用的是dlib来识别人脸部分,当然也可以使用opencv来识别人脸,在实际使用过程中,dlib的识别效果
比opencv的好一些。识别处理后的图片存放路径为:
data/my_faces(存放预处理我的图片,里面还复制一些图片)
data/other_faces(存放预处理别人图片)
3)人脸识别后开始建立模型,训练数据
这里使用卷积神经网络来建立模型,用了3个卷积层(采用了池化、dropout等技术),一个全连接层,分类层、
输出层。
4)训练完成后,进行性能评估
5)用测试数据,验证模型
1.2 文件路径:
|----data
| |----face_recog
| | |-----my_faces # 自己的照片(源文件)
| | |-----other_faces # 别人的照片(源文件)
| |
| |----my_faces # 截取头像后的目录
| |----other_faces
| |----test_faces
|----face_recog.ipynb
2 代码
import sys
import os
import cv2
import dlib
input_dir = './data/face_recog/my_faces' # 原始文件目录(照片目录)
output_dir = './data/my_faces' # 截取(处理)头像后输出目录
size = 64 # 人脸框大小
if not os.path.exists(output_dir):
os.makedirs(output_dir)
detector = dlib.get_frontal_face_detector() # 使用dlib人脸特征提取
2.1 数据预处理:截取头像,转成灰度图
%matplotlib inline
index = 1
for (path, dirnames, filenames) in os.walk(input_dir):
for filename in filenames:
if filename.endswith('.jpg'): # 以 .jpg结尾的照片
print('Being processed picture %s' % index)
img_path = path+'/'+filename
img = cv2.imread(img_path) # 读取图片,转为灰度图片
gray_img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
dets = detector(gray_img, 1)
"""
使用 detector 进行人脸检测dets为返回的结果(gray_img, 1)=(灰度图,1个通道)
下标 i 即为人脸序号
left:人脸左边距离图片左边界的距离 ;right:人脸右边距离图片左边界的距离
top:人脸上边距离图片上边界的距离 ;bottom:人脸下边距离图片上边界的距离
"""
for i, d in enumerate(dets):
x1 = d.top() if d.top() > 0 else 0
y1 = d.bottom() if d.bottom() > 0 else 0
x2 = d.left() if d.left() > 0 else 0
y2 = d.right() if d.right() > 0 else 0
face = img[x1:y1,x2:y2] # img[y:y+h,x:x+w]
face = cv2.resize(face, (size,size)) # 调整图片的尺寸
cv2.imshow('image',face)
# 保存图片
cv2.imwrite(output_dir+'/'+str(index)+'.jpg', face)
index += 1
key = cv2.waitKey(30) & 0xff # 不断刷新图像,频率时间为 30ms
if key == 27:
sys.exit(0)
# 别人图片输入输出目录
input_dir = './data/face_recog/other_faces'
output_dir = './data/other_faces'
size = 64
if not os.path.exists(output_dir):
os.makedirs(output_dir)
#注意:再次执行-->2.1 数据预处理:截取头像,转成灰度图
2.2 对灰度图数据集处理
import tensorflow as tf
import cv2
import numpy as np
import os
import random
import sys
from sklearn.model_selection import train_test_split
# 截取头像(处理)后的图片路径
my_faces_path = './data/my_faces'
other_faces_path = './data/other_faces'
size = 64
imgs = []
labs = []
tf.reset_default_graph() #重新创建图形变量
def getPaddingSize(img): #获取需要填充图片的大小
h, w, _ = img.shape
top, bottom, left, right = (0,0,0,0)
longest = max(h, w)
if w < longest:
tmp = longest - w
left = tmp // 2 # //表示整除符号
right = tmp - left
elif h < longest:
tmp = longest - h
top = tmp // 2
bottom = tmp - top
else:
pass
return top, bottom, left, right
# 读取数据集
def readData(path , h=size, w=size):
for filename in os.listdir(path):
if filename.endswith('.jpg'):
filename = path + '/' + filename
img = cv2.imread(filename)
top,bottom,left,right = getPaddingSize(img)
# 将图片放大, 扩充图片边缘部分
img = cv2.copyMakeBorder(img, top, bottom, left, right,
cv2.BORDER_CONSTANT, value=[0,0,0])
img = cv2.resize(img, (h, w))
imgs.append(img) #图片集
labs.append(path) #路径文件
readData(my_faces_path)
readData(other_faces_path)
imgs = np.array(imgs) # 将图片数据与标签转换成数组
labs = np.array([[0,1] if lab == my_faces_path else [1,0] for lab in labs])
# 随机划分测试集与训练集
train_x,test_x,train_y,test_y = train_test_split(imgs, labs, test_size=0.05,
random_state=random.randint(0,100))
# 参数:图片数据的总数,图片的高、宽、通道
train_x = train_x.reshape(train_x.shape[0], size, size, 3)
test_x = test_x.reshape(test_x.shape[0], size, size, 3)
# 将数据转换成小于 1 的数
train_x = train_x.astype('float32')/255.0
test_x = test_x.astype('float32')/255.0
print('train size:%s, test size:%s' % (len(train_x), len(test_x)))
# 图片块,每次取 100 张图片
batch_size = 20
num_batch = len(train_x) // batch_size
2.3 定义变量及神经网络层
x = tf.placeholder(tf.float32, [None, size, size, 3])
y_ = tf.placeholder(tf.float32, [None, 2])
keep_prob_5 = tf.placeholder(tf.float32)
keep_prob_75 = tf.placeholder(tf.float32)
def weightVariable(shape):
init = tf.random_normal(shape, stddev=0.01)
return tf.Variable(init)
def biasVariable(shape):
init = tf.random_normal(shape)
return tf.Variable(init)
def conv2d(x, W):
return tf.nn.conv2d(x, W, strides=[1,1,1,1], padding='SAME')
def maxPool(x):
return tf.nn.max_pool(x, ksize=[1,2,2,1], strides=[1,2,2,1], padding='SAME')
def dropout(x, keep):
return tf.nn.dropout(x, keep)
def cnnLayer():
# 第一层
W1 = weightVariable([3,3,3,32]) # 卷积核大小(3,3),输入通道(3),输出通道(32)
b1 = biasVariable([32])
conv1 = tf.nn.relu(conv2d(x, W1) + b1)
pool1 = maxPool(conv1)
drop1 = dropout(pool1, keep_prob_5) # 减少过拟合,随机让某些权重不更新
# 第二层
W2 = weightVariable([3,3,32,64])
b2 = biasVariable([64])
conv2 = tf.nn.relu(conv2d(drop1, W2) + b2)
pool2 = maxPool(conv2)
drop2 = dropout(pool2, keep_prob_5)
# 第三层
W3 = weightVariable([3,3,64,64])
b3 = biasVariable([64])
conv3 = tf.nn.relu(conv2d(drop2, W3) + b3)
pool3 = maxPool(conv3)
drop3 = dropout(pool3, keep_prob_5)
# 全连接层
Wf = weightVariable([8*16*32, 512])
bf = biasVariable([512])
drop3_flat = tf.reshape(drop3, [-1, 8*16*32])
dense = tf.nn.relu(tf.matmul(drop3_flat, Wf) + bf)
dropf = dropout(dense, keep_prob_75)
# 输出层
Wout = weightVariable([512,2])
bout = biasVariable([2])
out = tf.add(tf.matmul(dropf, Wout), bout) #out = tf.matmul(dropf, Wout) + bout
return out
2.4 训练模型
def cnnTrain():
out = cnnLayer()
cross_entropy =tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits=out, labels=y_))
train_step = tf.train.AdamOptimizer(0.01).minimize(cross_entropy)
# 比较标签是否相等,再求的所有数的平均值,tf.cast(强制转换类型)
accuracy = tf.reduce_mean(tf.cast(tf.equal(tf.argmax(out, 1),
tf.argmax(y_,1)), tf.float32))
# 将 loss 与 accuracy 保存以供 tensorboard 使用
tf.summary.scalar('loss', cross_entropy)
tf.summary.scalar('accuracy', accuracy)
merged_summary_op = tf.summary.merge_all()
saver = tf.train.Saver() # 数据保存器的初始化
with tf.Session() as sess:
sess.run(tf.global_variables_initializer())
summary_writer = tf.summary.FileWriter('./tmp',graph=tf.get_default_graph())
for n in range(10):
for i in range(num_batch): # 每次取 128(batch_size)张图片
batch_x = train_x[i*batch_size : (i+1)*batch_size]
batch_y = train_y[i*batch_size : (i+1)*batch_size]
# 喂数据:开始训练数据,同时训练三个变量,返回三个数据
_,loss,summary = sess.run([train_step, cross_entropy,merged_summary_op],
feed_dict={x:batch_x,y_:batch_y,keep_prob_5:0.5,keep_prob_75:0.75})
summary_writer.add_summary(summary, n*num_batch+i)
print("batch_data{} , loss is {}".format(n*num_batch+i, loss)) # 打印损失
if (n*num_batch+i) % 40 == 0: # 获取测试数据的准确率
acc = accuracy.eval({x:test_x, y_:test_y, keep_prob_5:1.0,keep_prob_75:1.0})
print("batch_data {}step,accuracy is {}".format(n*num_batch+i, acc))
if acc > 0.8 and n > 2: # 由于数据不多,这里设为准确率大于 0.80 时保存并退出
#saver.save(sess,'./train_face_model/train_faces.model',global_step=n*num_batch+i)
saver.save(sess,'./train_face_model/train_faces.model')
cnnTrain()
2.5 测试模型
%matplotlib inline
input_dir='./data/test_faces'
index=1
output = cnnLayer()
predict = tf.argmax(output, 1)
#先加载 meta graph 并恢复权重变量
saver = tf.train.import_meta_graph('./train_face_model/train_faces.model.meta')
sess = tf.Session()
saver.restore(sess, tf.train.latest_checkpoint('./train_face_model/'))
def is_my_face(image):
sess.run(tf.global_variables_initializer())
res = sess.run(predict, feed_dict={x: [image/255.0], keep_prob_5:1.0,keep_prob_75: 1.0})
if res[0] == 1:
return True
else:
return False
#使用 dlib 自带的 frontal_face_detector 作为我们的特征提取器
detector = dlib.get_frontal_face_detector()
#cam = cv2.VideoCapture(0)
for (path, dirnames, filenames) in os.walk(input_dir):
for filename in filenames:
if filename.endswith('.jpg'):
print('Being processed picture %s' % index)
index+=1
img_path = path+'/'+filename
# 从文件读取图片
img = cv2.imread(img_path)
gray_image = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
dets = detector(gray_image, 1)
if not len(dets):
print('Can`t get face.')
cv2.imshow('img', img)
key = cv2.waitKey(30) & 0xff
if key == 27:
sys.exit(0)
for i, d in enumerate(dets):
x1 = d.top() if d.top() > 0 else 0
y1 = d.bottom() if d.bottom() > 0 else 0
x2 = d.left() if d.left() > 0 else 0
y2 = d.right() if d.right() > 0 else 0
face = img[x1:y1,x2:y2]
# 调整图片的尺寸
face = cv2.resize(face, (size,size))
print('Is this my face? %s' % is_my_face(face))
cv2.rectangle(img, (x2,x1),(y2,y1), (255,0,0),3)
cv2.imshow('image',img)
key = cv2.waitKey(30) & 0xff
if key == 27:
sys.exit(0)
sess.close()