1. 程式人生 > >python基於神經網路實現人臉識別

python基於神經網路實現人臉識別

一.專案簡介

1.資料

需要資料QQ聯絡:1209028652

資料集:總共資料集由兩部分組成:他人臉圖片集及我自己的部分圖片
自己圖片目錄:face_recog/my_faces
他人圖片目錄:face_recog/other_faces
我的測試圖片目錄:face_recog/test_faces

2.人臉識別

獲取資料後,第一件事就對對圖片進行處理,即人臉識別,把人臉的範圍確定下來,人臉識別有很多方法,這裡使用的是 dlib 來識別人臉部分,當然也可以使用 opencv 來識別人臉,在實際使用過程中,dlib 的識別效果比 opencv 的好一些


識別處理後的圖片存放路徑為:my_faces(存放預處理我的圖片,裡面還複製一些圖片),other_faces(存放預處理別人圖片)

3.建立模型,訓練資料

這裡使用卷積神經網路來建立模型,用了 3 個卷積層(採用了池化、dropout 等技術),一個全連線層,分類層、輸出層。

4.效能評估

5. 用測試資料,驗證模型

下面開始程式碼實現啦

二.資料準備

匯入模組

import sys
import os
import cv2
import dlib
import matplotlib
import time
start=time.time()

1.獲取資料

# 1.定義輸入、輸出目錄,檔案解壓到當前目錄,my_faces目錄下,output_dir_myself 為檢測以後我的的頭像
input_dir_myself = 'face_recog/my_faces'
output_dir_myself = 'my_faces'
size = 64

*# 2.判斷輸出目錄是否存在,不存在,則建立
if not os.path.exists(output_dir_myself):
    os.makedirs(output_dir_myself)

# 3.利用 dlib 的人臉特徵提取器,使用 dlib 自帶的 frontal_face_detector 作為我們的特徵提取器
detector = dlib.get_frontal_face_detector()

2.預處理資料

# 接下來使用 dlib 來批量識別圖片中的人臉部分,並對原影象進行預處理,並儲存到指定目錄下。
#1.預處理我的頭像
index = 1
for (path, dirnames, filenames) in os.walk(input_dir_myself):
    for filename in filenames:
        if filename.endswith('.jpg'):
            print('Being processed picture %s' % index)
            img_path = path+'/'+filename
            # 從檔案讀取圖片
            img = cv2.imread(img_path)
            # 轉為灰度圖片
            gray_img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
            # 使用 detector 進行人臉檢測 dets 為返回的結果
            dets = detector(gray_img, 1)
            #使用 enumerate 函式遍歷序列中的元素以及它們的下標
            #下標 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
                # img[y:y+h,x:x+w]
                face = img[x1:y1,x2:y2]
                # 調整圖片的尺寸
                face = cv2.resize(face, (size,size))
                cv2.imshow('image',face)
                # 儲存圖片
                cv2.imwrite(output_dir_myself + '/' + str(index) + '.jpg', face)
                index += 1
                # 不斷重新整理影象,頻率時間為 30ms
                key = cv2.waitKey(30) & 0xff
                if key == 27:
                    sys.exit(0)


# 2.用同樣方法預處理別人的頭像(我只選用別人部分頭像)
# 定義輸入、輸出目錄,檔案解壓到當前目錄,other_faces目錄下
input_dir_other = 'face_recog/other_faces'
output_dir_other = 'other_faces'
size = 64
# 判斷輸出目錄是否存在,不存在,則建立
if not os.path.exists(output_dir_other):
    os.makedirs(output_dir_other)
#使用 dlib 自帶的 frontal_face_detector 作為我們的特徵提取器
detector = dlib.get_frontal_face_detector()
# 預處理別人的頭像
index = 1
for (path, dirnames, filenames) in os.walk(input_dir_other):
    for filename in filenames:
        if filename.endswith('.jpg'):
            print('Being processed picture %s' % index)
            img_path = path+'/'+filename
            # 從檔案讀取圖片
            img = cv2.imread(img_path)
            # 轉為灰度圖片
            gray_img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
            # 使用 detector 進行人臉檢測 dets 為返回的結果
            dets = detector(gray_img, 1)
            #使用 enumerate 函式遍歷序列中的元素以及它們的下標
            #下標 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
                # img[y:y+h,x:x+w]
                face = img[x1:y1,x2:y2]
                # 調整圖片的尺寸
                face = cv2.resize(face, (size,size))
                cv2.imshow('image',face)
                # 儲存圖片
                cv2.imwrite(output_dir_other + '/' + str(index) + '.jpg', face)
                index += 1
                # 不斷重新整理影象,頻率時間為 30ms
                key = cv2.waitKey(30) & 0xff
                if key == 27:
                    sys.exit(0)

三.訓練模型

1.匯入需要的庫

import tensorflow as tf
import cv2
import numpy as np
import os
import random
import sys
from sklearn.model_selection import train_test_split

2.定義預處理後圖片(我的和別人的)所在目錄

my_faces_path = 'my_faces'
other_faces_path = 'other_faces'
size = 64

3.調整或規範圖片大小

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

4.讀取測試圖片

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

5.定義變數及神經網路層-

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)

6.定義卷積神經網路框架

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 = weightVariable([2])
    # out = tf.matmul(dropf, Wout) + bout
    out = tf.add(tf.matmul(dropf, Wout), bout)
    return out

7.訓練模型

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):
            # 每次取 128(batch_size)張圖片
            for i in range(num_batch):
                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(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(n * num_batch + i, acc)
                    # 由於資料不多,這裡設為準確率大於 0.80 時儲存並退出
                    if acc > 0.8 and n > 2:
                # saver.save(sess,'./train_face_model/train_faces.model', global_step = n * num_batch + i)
                        saver.save(sess,'./train_face_model/train_faces.model')
                        # sys.exit(0)
        # print('accuracy less 0.80, exited!')
cnnTrain()

四.測試模型

input_dir='face_recog/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/'))
#saver.restore(sess,tf.train.latest_checkpoint('./my_test_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)
#while True:
    #_, img = cam.read()
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()

成果展示

引用塊內容

總結

由於圖片數量比較少,最終結果不是很理想,但是整個流程的邏輯是很透徹的,本人電腦比較渣,跑的時候比較慢。樣本圖片越多,最終的結果也越準確。