├── README.md ├── get_my_faces.py ├── is_my_face.py ├── set_other_faces.py └── train_faces.py /README.md: -------------------------------------------------------------------------------- 1 | # FaceRecognition-tensorflow 2 | 基于TensorFlow训练的人脸识别神经网络 3 | 4 | 文章地址:https://www.cnblogs.com/mu---mu/p/FaceRecognition-tensorflow.html 5 | -------------------------------------------------------------------------------- /get_my_faces.py: -------------------------------------------------------------------------------- 1 | import cv2 2 | import dlib 3 | import os 4 | import sys 5 | import random 6 | 7 | output_dir = './my_faces' 8 | size = 64 9 | 10 | if not os.path.exists(output_dir): 11 | os.makedirs(output_dir) 12 | 13 | # 改变图片的亮度与对比度 14 | def relight(img, light=1, bias=0): 15 | w = img.shape[1] 16 | h = img.shape[0] 17 | #image = [] 18 | for i in range(0,w): 19 | for j in range(0,h): 20 | for c in range(3): 21 | tmp = int(img[j,i,c]*light + bias) 22 | if tmp > 255: 23 | tmp = 255 24 | elif tmp < 0: 25 | tmp = 0 26 | img[j,i,c] = tmp 27 | return img 28 | 29 | #使用dlib自带的frontal_face_detector作为我们的特征提取器 30 | detector = dlib.get_frontal_face_detector() 31 | # 打开摄像头 参数为输入流,可以为摄像头或视频文件 32 | camera = cv2.VideoCapture(0) 33 | 34 | index = 1 35 | while True: 36 | if (index <= 10000): 37 | print('Being processed picture %s' % index) 38 | # 从摄像头读取照片 39 | success, img = camera.read() 40 | # 转为灰度图片 41 | gray_img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) 42 | # 使用detector进行人脸检测 43 | dets = detector(gray_img, 1) 44 | 45 | for i, d in enumerate(dets): 46 | x1 = d.top() if d.top() > 0 else 0 47 | y1 = d.bottom() if d.bottom() > 0 else 0 48 | x2 = d.left() if d.left() > 0 else 0 49 | y2 = d.right() if d.right() > 0 else 0 50 | 51 | face = img[x1:y1,x2:y2] 52 | # 调整图片的对比度与亮度, 对比度与亮度值都取随机数,这样能增加样本的多样性 53 | face = relight(face, random.uniform(0.5, 1.5), random.randint(-50, 50)) 54 | 55 | face = cv2.resize(face, (size,size)) 56 | 57 | cv2.imshow('image', face) 58 | 59 | cv2.imwrite(output_dir+'/'+str(index)+'.jpg', face) 60 | 61 | index += 1 62 | key = cv2.waitKey(30) & 0xff 63 | if key == 27: 64 | break 65 | else: 66 | print('Finished!') 67 | break 68 | -------------------------------------------------------------------------------- /is_my_face.py: -------------------------------------------------------------------------------- 1 | import tensorflow as tf 2 | import cv2 3 | import dlib 4 | import numpy as np 5 | import os 6 | import random 7 | import sys 8 | from sklearn.model_selection import train_test_split 9 | 10 | my_faces_path = './my_faces' 11 | other_faces_path = './other_faces' 12 | size = 64 13 | 14 | imgs = [] 15 | labs = [] 16 | 17 | def getPaddingSize(img): 18 | h, w, _ = img.shape 19 | top, bottom, left, right = (0,0,0,0) 20 | longest = max(h, w) 21 | 22 | if w < longest: 23 | tmp = longest - w 24 | # //表示整除符号 25 | left = tmp // 2 26 | right = tmp - left 27 | elif h < longest: 28 | tmp = longest - h 29 | top = tmp // 2 30 | bottom = tmp - top 31 | else: 32 | pass 33 | return top, bottom, left, right 34 | 35 | def readData(path , h=size, w=size): 36 | for filename in os.listdir(path): 37 | if filename.endswith('.jpg'): 38 | filename = path + '/' + filename 39 | 40 | img = cv2.imread(filename) 41 | 42 | top,bottom,left,right = getPaddingSize(img) 43 | # 将图片放大, 扩充图片边缘部分 44 | img = cv2.copyMakeBorder(img, top, bottom, left, right, cv2.BORDER_CONSTANT, value=[0,0,0]) 45 | img = cv2.resize(img, (h, w)) 46 | 47 | imgs.append(img) 48 | labs.append(path) 49 | 50 | readData(my_faces_path) 51 | readData(other_faces_path) 52 | # 将图片数据与标签转换成数组 53 | imgs = np.array(imgs) 54 | labs = np.array([[0,1] if lab == my_faces_path else [1,0] for lab in labs]) 55 | # 随机划分测试集与训练集 56 | train_x,test_x,train_y,test_y = train_test_split(imgs, labs, test_size=0.05, random_state=random.randint(0,100)) 57 | # 参数:图片数据的总数,图片的高、宽、通道 58 | train_x = train_x.reshape(train_x.shape[0], size, size, 3) 59 | test_x = test_x.reshape(test_x.shape[0], size, size, 3) 60 | # 将数据转换成小于1的数 61 | train_x = train_x.astype('float32')/255.0 62 | test_x = test_x.astype('float32')/255.0 63 | 64 | print('train size:%s, test size:%s' % (len(train_x), len(test_x))) 65 | # 图片块,每次取128张图片 66 | batch_size = 128 67 | num_batch = len(train_x) // 128 68 | 69 | x = tf.placeholder(tf.float32, [None, size, size, 3]) 70 | y_ = tf.placeholder(tf.float32, [None, 2]) 71 | 72 | keep_prob_5 = tf.placeholder(tf.float32) 73 | keep_prob_75 = tf.placeholder(tf.float32) 74 | 75 | def weightVariable(shape): 76 | init = tf.random_normal(shape, stddev=0.01) 77 | return tf.Variable(init) 78 | 79 | def biasVariable(shape): 80 | init = tf.random_normal(shape) 81 | return tf.Variable(init) 82 | 83 | def conv2d(x, W): 84 | return tf.nn.conv2d(x, W, strides=[1,1,1,1], padding='SAME') 85 | 86 | def maxPool(x): 87 | return tf.nn.max_pool(x, ksize=[1,2,2,1], strides=[1,2,2,1], padding='SAME') 88 | 89 | def dropout(x, keep): 90 | return tf.nn.dropout(x, keep) 91 | 92 | def cnnLayer(): 93 | # 第一层 94 | W1 = weightVariable([3,3,3,32]) # 卷积核大小(3,3), 输入通道(3), 输出通道(32) 95 | b1 = biasVariable([32]) 96 | # 卷积 97 | conv1 = tf.nn.relu(conv2d(x, W1) + b1) 98 | # 池化 99 | pool1 = maxPool(conv1) 100 | # 减少过拟合,随机让某些权重不更新 101 | drop1 = dropout(pool1, keep_prob_5) 102 | 103 | # 第二层 104 | W2 = weightVariable([3,3,32,64]) 105 | b2 = biasVariable([64]) 106 | conv2 = tf.nn.relu(conv2d(drop1, W2) + b2) 107 | pool2 = maxPool(conv2) 108 | drop2 = dropout(pool2, keep_prob_5) 109 | 110 | # 第三层 111 | W3 = weightVariable([3,3,64,64]) 112 | b3 = biasVariable([64]) 113 | conv3 = tf.nn.relu(conv2d(drop2, W3) + b3) 114 | pool3 = maxPool(conv3) 115 | drop3 = dropout(pool3, keep_prob_5) 116 | 117 | # 全连接层 118 | Wf = weightVariable([8*16*32, 512]) 119 | bf = biasVariable([512]) 120 | drop3_flat = tf.reshape(drop3, [-1, 8*16*32]) 121 | dense = tf.nn.relu(tf.matmul(drop3_flat, Wf) + bf) 122 | dropf = dropout(dense, keep_prob_75) 123 | 124 | # 输出层 125 | Wout = weightVariable([512,2]) 126 | bout = biasVariable([2]) 127 | out = tf.add(tf.matmul(dropf, Wout), bout) 128 | return out 129 | 130 | output = cnnLayer() 131 | predict = tf.argmax(output, 1) 132 | 133 | saver = tf.train.Saver() 134 | sess = tf.Session() 135 | saver.restore(sess, tf.train.latest_checkpoint('.')) 136 | 137 | def is_my_face(image): 138 | res = sess.run(predict, feed_dict={x: [image/255.0], keep_prob_5:1.0, keep_prob_75: 1.0}) 139 | if res[0] == 1: 140 | return True 141 | else: 142 | return False 143 | 144 | #使用dlib自带的frontal_face_detector作为我们的特征提取器 145 | detector = dlib.get_frontal_face_detector() 146 | 147 | cam = cv2.VideoCapture(0) 148 | 149 | while True: 150 | _, img = cam.read() 151 | gray_image = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) 152 | dets = detector(gray_image, 1) 153 | if not len(dets): 154 | #print('Can`t get face.') 155 | cv2.imshow('img', img) 156 | key = cv2.waitKey(30) & 0xff 157 | if key == 27: 158 | sys.exit(0) 159 | 160 | for i, d in enumerate(dets): 161 | x1 = d.top() if d.top() > 0 else 0 162 | y1 = d.bottom() if d.bottom() > 0 else 0 163 | x2 = d.left() if d.left() > 0 else 0 164 | y2 = d.right() if d.right() > 0 else 0 165 | face = img[x1:y1,x2:y2] 166 | # 调整图片的尺寸 167 | face = cv2.resize(face, (size,size)) 168 | print('Is this my face? %s' % is_my_face(face)) 169 | 170 | cv2.rectangle(img, (x2,x1),(y2,y1), (255,0,0),3) 171 | cv2.imshow('image',img) 172 | key = cv2.waitKey(30) & 0xff 173 | if key == 27: 174 | sys.exit(0) 175 | 176 | sess.close() 177 | -------------------------------------------------------------------------------- /set_other_faces.py: -------------------------------------------------------------------------------- 1 | # -*- codeing: utf-8 -*- 2 | import sys 3 | import os 4 | import cv2 5 | import dlib 6 | 7 | input_dir = './input_img' 8 | output_dir = './other_faces' 9 | size = 64 10 | 11 | if not os.path.exists(output_dir): 12 | os.makedirs(output_dir) 13 | 14 | #使用dlib自带的frontal_face_detector作为我们的特征提取器 15 | detector = dlib.get_frontal_face_detector() 16 | 17 | index = 1 18 | for (path, dirnames, filenames) in os.walk(input_dir): 19 | for filename in filenames: 20 | if filename.endswith('.jpg'): 21 | print('Being processed picture %s' % index) 22 | img_path = path+'/'+filename 23 | # 从文件读取图片 24 | img = cv2.imread(img_path) 25 | # 转为灰度图片 26 | gray_img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) 27 | # 使用detector进行人脸检测 dets为返回的结果 28 | dets = detector(gray_img, 1) 29 | 30 | #使用enumerate 函数遍历序列中的元素以及它们的下标 31 | #下标i即为人脸序号 32 | #left:人脸左边距离图片左边界的距离 ;right:人脸右边距离图片左边界的距离 33 | #top:人脸上边距离图片上边界的距离 ;bottom:人脸下边距离图片上边界的距离 34 | for i, d in enumerate(dets): 35 | x1 = d.top() if d.top() > 0 else 0 36 | y1 = d.bottom() if d.bottom() > 0 else 0 37 | x2 = d.left() if d.left() > 0 else 0 38 | y2 = d.right() if d.right() > 0 else 0 39 | # img[y:y+h,x:x+w] 40 | face = img[x1:y1,x2:y2] 41 | # 调整图片的尺寸 42 | face = cv2.resize(face, (size,size)) 43 | cv2.imshow('image',face) 44 | # 保存图片 45 | cv2.imwrite(output_dir+'/'+str(index)+'.jpg', face) 46 | index += 1 47 | 48 | key = cv2.waitKey(30) & 0xff 49 | if key == 27: 50 | sys.exit(0) 51 | -------------------------------------------------------------------------------- /train_faces.py: -------------------------------------------------------------------------------- 1 | import tensorflow as tf 2 | import cv2 3 | import numpy as np 4 | import os 5 | import random 6 | import sys 7 | from sklearn.model_selection import train_test_split 8 | 9 | my_faces_path = './my_faces' 10 | other_faces_path = './other_faces' 11 | size = 64 12 | 13 | imgs = [] 14 | labs = [] 15 | 16 | def getPaddingSize(img): 17 | h, w, _ = img.shape 18 | top, bottom, left, right = (0,0,0,0) 19 | longest = max(h, w) 20 | 21 | if w < longest: 22 | tmp = longest - w 23 | # //表示整除符号 24 | left = tmp // 2 25 | right = tmp - left 26 | elif h < longest: 27 | tmp = longest - h 28 | top = tmp // 2 29 | bottom = tmp - top 30 | else: 31 | pass 32 | return top, bottom, left, right 33 | 34 | def readData(path , h=size, w=size): 35 | for filename in os.listdir(path): 36 | if filename.endswith('.jpg'): 37 | filename = path + '/' + filename 38 | 39 | img = cv2.imread(filename) 40 | 41 | top,bottom,left,right = getPaddingSize(img) 42 | # 将图片放大, 扩充图片边缘部分 43 | img = cv2.copyMakeBorder(img, top, bottom, left, right, cv2.BORDER_CONSTANT, value=[0,0,0]) 44 | img = cv2.resize(img, (h, w)) 45 | 46 | imgs.append(img) 47 | labs.append(path) 48 | 49 | readData(my_faces_path) 50 | readData(other_faces_path) 51 | # 将图片数据与标签转换成数组 52 | imgs = np.array(imgs) 53 | labs = np.array([[0,1] if lab == my_faces_path else [1,0] for lab in labs]) 54 | # 随机划分测试集与训练集 55 | train_x,test_x,train_y,test_y = train_test_split(imgs, labs, test_size=0.05, random_state=random.randint(0,100)) 56 | # 参数:图片数据的总数,图片的高、宽、通道 57 | train_x = train_x.reshape(train_x.shape[0], size, size, 3) 58 | test_x = test_x.reshape(test_x.shape[0], size, size, 3) 59 | # 将数据转换成小于1的数 60 | train_x = train_x.astype('float32')/255.0 61 | test_x = test_x.astype('float32')/255.0 62 | 63 | print('train size:%s, test size:%s' % (len(train_x), len(test_x))) 64 | # 图片块,每次取100张图片 65 | batch_size = 100 66 | num_batch = len(train_x) // batch_size 67 | 68 | x = tf.placeholder(tf.float32, [None, size, size, 3]) 69 | y_ = tf.placeholder(tf.float32, [None, 2]) 70 | 71 | keep_prob_5 = tf.placeholder(tf.float32) 72 | keep_prob_75 = tf.placeholder(tf.float32) 73 | 74 | def weightVariable(shape): 75 | init = tf.random_normal(shape, stddev=0.01) 76 | return tf.Variable(init) 77 | 78 | def biasVariable(shape): 79 | init = tf.random_normal(shape) 80 | return tf.Variable(init) 81 | 82 | def conv2d(x, W): 83 | return tf.nn.conv2d(x, W, strides=[1,1,1,1], padding='SAME') 84 | 85 | def maxPool(x): 86 | return tf.nn.max_pool(x, ksize=[1,2,2,1], strides=[1,2,2,1], padding='SAME') 87 | 88 | def dropout(x, keep): 89 | return tf.nn.dropout(x, keep) 90 | 91 | def cnnLayer(): 92 | # 第一层 93 | W1 = weightVariable([3,3,3,32]) # 卷积核大小(3,3), 输入通道(3), 输出通道(32) 94 | b1 = biasVariable([32]) 95 | # 卷积 96 | conv1 = tf.nn.relu(conv2d(x, W1) + b1) 97 | # 池化 98 | pool1 = maxPool(conv1) 99 | # 减少过拟合,随机让某些权重不更新 100 | drop1 = dropout(pool1, keep_prob_5) 101 | 102 | # 第二层 103 | W2 = weightVariable([3,3,32,64]) 104 | b2 = biasVariable([64]) 105 | conv2 = tf.nn.relu(conv2d(drop1, W2) + b2) 106 | pool2 = maxPool(conv2) 107 | drop2 = dropout(pool2, keep_prob_5) 108 | 109 | # 第三层 110 | W3 = weightVariable([3,3,64,64]) 111 | b3 = biasVariable([64]) 112 | conv3 = tf.nn.relu(conv2d(drop2, W3) + b3) 113 | pool3 = maxPool(conv3) 114 | drop3 = dropout(pool3, keep_prob_5) 115 | 116 | # 全连接层 117 | Wf = weightVariable([8*8*64, 512]) 118 | bf = biasVariable([512]) 119 | drop3_flat = tf.reshape(drop3, [-1, 8*8*64]) 120 | dense = tf.nn.relu(tf.matmul(drop3_flat, Wf) + bf) 121 | dropf = dropout(dense, keep_prob_75) 122 | 123 | # 输出层 124 | Wout = weightVariable([512,2]) 125 | bout = biasVariable([2]) 126 | #out = tf.matmul(dropf, Wout) + bout 127 | out = tf.add(tf.matmul(dropf, Wout), bout) 128 | return out 129 | 130 | def cnnTrain(): 131 | out = cnnLayer() 132 | 133 | cross_entropy = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits=out, labels=y_)) 134 | 135 | train_step = tf.train.AdamOptimizer(0.01).minimize(cross_entropy) 136 | # 比较标签是否相等,再求的所有数的平均值,tf.cast(强制转换类型) 137 | accuracy = tf.reduce_mean(tf.cast(tf.equal(tf.argmax(out, 1), tf.argmax(y_, 1)), tf.float32)) 138 | # 将loss与accuracy保存以供tensorboard使用 139 | tf.summary.scalar('loss', cross_entropy) 140 | tf.summary.scalar('accuracy', accuracy) 141 | merged_summary_op = tf.summary.merge_all() 142 | # 数据保存器的初始化 143 | saver = tf.train.Saver() 144 | 145 | with tf.Session() as sess: 146 | 147 | sess.run(tf.global_variables_initializer()) 148 | 149 | summary_writer = tf.summary.FileWriter('./tmp', graph=tf.get_default_graph()) 150 | 151 | for n in range(10): 152 | # 每次取128(batch_size)张图片 153 | for i in range(num_batch): 154 | batch_x = train_x[i*batch_size : (i+1)*batch_size] 155 | batch_y = train_y[i*batch_size : (i+1)*batch_size] 156 | # 开始训练数据,同时训练三个变量,返回三个数据 157 | _,loss,summary = sess.run([train_step, cross_entropy, merged_summary_op], 158 | feed_dict={x:batch_x,y_:batch_y, keep_prob_5:0.5,keep_prob_75:0.75}) 159 | summary_writer.add_summary(summary, n*num_batch+i) 160 | # 打印损失 161 | print(n*num_batch+i, loss) 162 | 163 | if (n*num_batch+i) % 100 == 0: 164 | # 获取测试数据的准确率 165 | acc = accuracy.eval({x:test_x, y_:test_y, keep_prob_5:1.0, keep_prob_75:1.0}) 166 | print(n*num_batch+i, acc) 167 | # 准确率大于0.98时保存并退出 168 | if acc > 0.98 and n > 2: 169 | saver.save(sess, './train_faces.model', global_step=n*num_batch+i) 170 | sys.exit(0) 171 | print('accuracy less 0.98, exited!') 172 | 173 | cnnTrain() 174 | --------------------------------------------------------------------------------