[Introduction to deep learning case 2] Building a face recognition model based on convolutional neural network and Keras

Article directory

1. Tools and environment

2. Construction of deep learning environment

3. Construction and testing of face recognition model based on convolutional neural network

1. Core code

Step 1: Collect facial feature data of yourself and others, corresponding to data labels 0 and 1 respectively

 Step 2: Train a model that recognizes facial features, and save the model as a file in .h5 format

 Step 3: Read the face recognition model in the .h5 file format, and recognize the face images recorded by the camera

2. Basic principles of recognition

3. Run and test

Step 1: Collect facial feature data of yourself and others (note that you need to turn on the computer camera, otherwise an error will be reported!)

Step 2: Perform 10 rounds of face data model training

Step 3: Face recognition and comparison test

4. Analysis and Summary

reference article


1. Tools and environment

  • Pycharm 2022.1.4
  • conda version : 4.5.4
  • python version : 3.6.5.final.0
  • platform : win-64

2. Construction of deep learning environment

For details, see the second part of this article I wrote, so I won’t go into details here

[Introduction to deep learning case 1] Handwritten digital image recognition based on Keras icon-default.png?t=N4P3https://blog.csdn.net/qq_52487066/article/details/131048466?spm=1001.2014.3001.5501 After the environment is built, you need to download the following toolkit , execute the following commands in Anaconda Prompt in turn

conda install py-opencv -i https://pypi.tuna.tsinghua.edu.cn/simple
pip install dlib==19.7.0 -i https://pypi.tuna.tsinghua.edu.cn/simple
pip install face_recognition -i https://pypi.tuna.tsinghua.edu.cn/simple
pip install Pillow -i https://pypi.tuna.tsinghua.edu.cn/simple

The toolkit download and import is complete, and the following is the construction and operation of the project


3. Construction and testing of face recognition model based on convolutional neural network

1. Core code

Step 1: Collect facial feature data of yourself and others, corresponding to data labels 0 and 1 respectively

get_my_face.py

import cv2
import dlib
import os
import random

output_dir = './my_crop_faces'
size = 160
if not os.path.exists(output_dir):
    os.makedirs(output_dir)

# 改变图片的亮度与对比度
def relight(img, light=1, bias=0):
    w = img.shape[1]
    h = img.shape[0]
    #image = []
    for i in range(0,w):
        for j in range(0,h):
            for c in range(3):
                tmp = int(img[j,i,c]*light + bias)
                if tmp > 255:
                    tmp = 255
                elif tmp < 0:
                    tmp = 0
                img[j,i,c] = tmp
    return img

#使用dlib自带的frontal_face_detector作为我们的特征提取器
detector = dlib.get_frontal_face_detector()
# 打开摄像头 参数为输入流,可以为摄像头或视频文件
camera = cv2.VideoCapture(0)

index = 0
while True:
    if (index <= 1000):
        print('Being processed picture %s' % index)
        # 从摄像头读取照片
        success, img = camera.read()
        # 转为灰度图片
        gray_img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
        # 使用detector进行人脸检测
        dets = detector(gray_img, 1)

        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 = relight(face, random.uniform(0.5, 1.5), random.randint(-50, 50))
            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
        if key == 27:
            break
    else:
        print('Finished!')
        break

 Step 2: Train a model that recognizes facial features, and save the model as a file in .h5 format

model_train.py

#-*- coding: utf-8 -*-
from keras.models import Sequential
from keras.layers import Dense, Dropout, Activation, Flatten
from keras.layers import Convolution2D, MaxPooling2D
from keras.optimizers import SGD
from keras.utils import np_utils
import os
import cv2
import numpy as np
from sklearn.model_selection import train_test_split
import random

def get_files(input_dir):
    file_list = []
    for (path, dirnames, filenames) in os.walk(input_dir):
        # print(path) #输出对应顶层文件夹
        # print(dirnames)#在当前文件夹下的文件夹
        # print(filenames)#在当前文件夹下的文件夹
        for filename in filenames:
            if filename.endswith('.jpg') or filename.endswith('.bmp'):
                # print(filename)
                full_path = os.path.join(path, filename)
                # print(full_path)
                file_list.append(full_path)
    return file_list

#设置hujianhua文件夹的对应标签为0
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 read_img_label(file_list, label):
    size = 64
    imgs = []
    labs = []
    #01
    num = 0
    for filename in file_list:
        # print(filename)
        img = cv2.imread(filename)
        # print(img.shape)
        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, (size, size))
        imgs.append(img)
        labs.append(label)
        num = num + 1

    # print(len(imgs))
    # print(len(labs))
    return imgs, labs


def read_dataset():
    input_dir = "./data_collection/my_crop_faces"


    all_imgs_list = []
    all_label_list = []
    my_file_list = get_files(input_dir)
    # 0->[0,1] 1->[1,0]
    label = 0 #[0, 1]
    my_imgs_list, my_labs_list = read_img_label(my_file_list, label)

    input_dir = "./data_collection/others_img_crop"

    others_file_list = get_files(input_dir)
    label = 1 #[1, 0] #->0
    others_imgs_list, others_labs_list = read_img_label(others_file_list, label)

    for img in my_imgs_list:
        all_imgs_list.append(img)
    for img in others_imgs_list:
        all_imgs_list.append(img)

    for label in my_labs_list:
        all_label_list.append(label)
    for label in others_labs_list:
        all_label_list.append(label)

    imgs_array = np.array(all_imgs_list)
    # print(imgs_array.shape)

    labs_array = np.array(all_label_list)
    # print(labs_array.shape)

    return imgs_array,labs_array


#加载数据集并按照交叉验证的原则划分数据集并进行相关预处理工作
def load_data(img_rows = 64, img_cols = 64,
         img_channels = 3, nb_classes = 2):
    #加载数据集到内存
    images, labels = read_dataset()
    print(images.shape)
    print(labels.shape)

    train_images, valid_images, train_labels, valid_labels = train_test_split(images, labels, test_size = 0.3, random_state = random.randint(0, 100))
    _, test_images, _, test_labels = train_test_split(images, labels, test_size = 0.5, random_state = random.randint(0, 100))

    train_images = train_images.reshape(train_images.shape[0], img_rows, img_cols, img_channels)
    valid_images = valid_images.reshape(valid_images.shape[0], img_rows, img_cols, img_channels)
    test_images = test_images.reshape(test_images.shape[0], img_rows, img_cols, img_channels)
    input_shape = (img_rows, img_cols, img_channels)

    #输出训练集、验证集、测试集的数量
    print(train_images.shape[0], 'train samples')
    print(valid_images.shape[0], 'valid samples')
    print(test_images.shape[0], 'test samples')

    #我们的模型使用categorical_crossentropy作为损失函数,因此需要根据类别数量nb_classes将
    #类别标签进行one-hot编码使其向量化,在这里我们的类别只有两种,经过转化后标签数据变为二维
    train_labels = np_utils.to_categorical(train_labels, nb_classes)
    valid_labels = np_utils.to_categorical(valid_labels, nb_classes)
    test_labels = np_utils.to_categorical(test_labels, nb_classes)
    print(train_labels.shape)
    print(valid_labels.shape)
    print(test_labels.shape)
    #像素数据浮点化以便归一化
    train_images = train_images.astype('float32')
    valid_images = valid_images.astype('float32')
    test_images = test_images.astype('float32')

    #将其归一化,图像的各像素值归一化到0~1区间
    train_images /= 255
    valid_images /= 255
    test_images /= 255

    return train_images, train_labels, valid_images, valid_labels, test_images, test_labels

#建立模型
def build_model(nb_classes = 2):
    #构建一个空的网络模型,它是一个线性堆叠模型,各神经网络层会被顺序添加,专业名称为序贯模型或线性堆叠模型
    model = Sequential()

    #以下代码将顺序添加CNN网络需要的各层,一个add就是一个网络层
    model.add(Convolution2D(32, 3, 3, border_mode='same',
                                 input_shape =  (64, 64, 3)))    #1 2维卷积层
    model.add(Activation('relu'))                                  #2 激活函数层

    model.add(Convolution2D(32, 3, 3))                             #3 2维卷积层
    model.add(Activation('relu'))                                  #4 激活函数层

    model.add(MaxPooling2D(pool_size=(2, 2)))                      #5 池化层
    model.add(Dropout(0.25))                                       #6 Dropout层

    model.add(Convolution2D(64, 3, 3, border_mode='same'))         #7  2维卷积层
    model.add(Activation('relu'))                                  #8  激活函数层

    model.add(Convolution2D(64, 3, 3))                             #9  2维卷积层
    model.add(Activation('relu'))                                  #10 激活函数层

    model.add(MaxPooling2D(pool_size=(2, 2)))                      #11 池化层
    model.add(Dropout(0.25))                                       #12 Dropout层

    model.add(Flatten())                                           #13 Flatten层
    model.add(Dense(512))                                          #14 Dense层,又被称作全连接层
    model.add(Activation('relu'))                                  #15 激活函数层
    model.add(Dropout(0.5))                                        #16 Dropout层
    model.add(Dense(nb_classes))                                   #17 Dense层
    model.add(Activation('softmax'))                               #18 分类层,输出最终结果
    #输出模型概况
    print(model.summary())
    return model

model = build_model()

sgd = SGD(lr=0.01, decay=1e-6,
          momentum=0.9, nesterov=True)  # 采用SGD+momentum的优化器进行训练,首先生成一个优化器对象
model.compile(loss='categorical_crossentropy',
                   optimizer=sgd,
                   metrics=['accuracy'])  # 完成实际的模型配置工作

train_images, train_labels, valid_images, valid_labels, test_images, test_labels = load_data()

batch_size = 20
nb_epoch = 10
train_history = model.fit(train_images,
               train_labels,
               batch_size=batch_size,
               nb_epoch=nb_epoch,
               validation_data=(valid_images, valid_labels),
               shuffle=True)

scores = model.evaluate(test_images, test_labels)
print('accuracy=', scores[1])
prediction = model.predict_classes(test_images)
# print(prediction)
model.save('./me.face.model.h5')

 Step 3: Read the face recognition model in the .h5 file format, and recognize the face images recorded by the camera

video_predict.py

import cv2
import dlib
from keras.models import load_model
import sys

size = 64

# 使用dlib自带的frontal_face_detector作为我们的特征提取器
detector = dlib.get_frontal_face_detector()

cam = cv2.VideoCapture(0)

model = load_model('./me.face.model.h5')

while True:
    _, img = cam.read()
    gray_image = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
    dets = detector(gray_image, 1)
    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))
        shape_img = (face.reshape(1, size, size, 3)).astype('float32') / 255

        prediction = model.predict_classes(shape_img)
        print(prediction[0])
        name = "unknown"
        if prediction[0] == 0:
            print("识别出本人")
            name = "Aricl."
        else:
            print("不是本人")
            name = "unknown"
        cv2.rectangle(img, (x2, x1), (y2, y1), (255, 0, 0), 3)
        font = cv2.FONT_HERSHEY_SIMPLEX
        cv2.putText(img, name, (x2, x1), font, 0.8, (255, 255, 255), 1)

    cv2.imshow('image', img)
    key = cv2.waitKey(30) & 0xff
    if key == 27:
        sys.exit(0)

2. Basic principles of recognition

        One of the core is to use OpenCV (Open source Computer Vision Library) , which is an open source computer vision library for image processing.

It is a set of open source API function library         about computer vision . This means, (1) Whether it is scientific research or commercial application, it can be used for development; (2) The source code of all API functions is public, and you can see the program steps of its internal implementation ;(3) You can modify the source code of OpenCV to compile and generate the specific API functions you need. However, as a library, what it provides is only the API of some commonly used, classic, and popular algorithms.

        A typical computer vision algorithm generally includes the following steps:

  1. data collection
  2. preprocessing
  3. feature extraction
  4. feature selection
  5. Classifier design and training
  6. Classification

For these six parts, OpenCV provides APIs for developers to call.

        The second core is to use Keras to build a convolutional neural network .

        Keras is a relatively good entry framework for artificial intelligence. It is an advanced Python neural network framework that has been added to TensorFlow as its default framework, providing a more advanced API for TensorFlow.

        If TensorFlow is compared to Java or C++ in the programming world, then Keras is the Python in the programming world. As a high-level package of TensorFlow , it can be used in conjunction with TensorFlow to quickly build models.

        And Keras is officially supported by TensorFlow. When there is an available GPU on the machine, the code will automatically call the GPU for parallel computing, which is very powerful!

3. Run and test

Step 1: Collect facial feature data of yourself and others (note that you need to turn on the computer camera, otherwise an error will be reported!)

 

Step 2: Perform 10 rounds of face data model training

Step 3: Face recognition and comparison test

The picture above is me (blogger Aricl.), and the picture below is my roommate (unknown face). It can be seen that the accuracy of face recognition is still good!


4. Analysis and Summary

        This time, by building a face recognition model based on convolutional neural network and Keras , the basic principle of implementation is: refer to the OpenCV library in Python , call the face classifier in it , and then call the computer's camera to read single-frame data in a loop . Perform real-time face detection, frame the face, and mark the information.

        First of all, we need to collect face data. A total of 1,000 pieces of face feature data of the person and others have been collected. After the collection is completed, the data model is trained. A total of 10 rounds of training are performed, with more than 6,000 rounds of training. Good models are saved as .h5 format files. Then call the camera to read the face photo of each frame, grayscale the picture, detect the face and frame the corresponding area, read the .h5 face recognition model file for face recognition and comparison, and Mark the information above as personal or unknown.

        In addition, when collecting, there are certain requirements for the person and the environment to be collected. The light should be bright so that the camera can see the face clearly, and the face should be facing the camera , so that the collected facial feature data is more accurate and the recognition effect is better. the better .

        This time is just a preliminary introduction to face recognition, and I have a strong interest in learning it, but the blogger is preparing for the postgraduate entrance examination, so I can only get a preliminary understanding. I will continue to learn and research related technologies in this field when I have the opportunity, come on! !

reference article

Python+Keras+opencv realizes face recognition icon-default.png?t=N4P3https://blog.csdn.net/gf19960103/article/details/91038858?ops_request_misc=&request_id=&biz_id=102&utm_term=%E4%BD%BF%E7%94%A8Keras%E6% 9E%84%E5%BB%BA%E4%BA%BA%E8%84%B8%E8%AF%86%E5%88%AB%E6%A8%A1%E5%9E%8B&utm_medium=distribute.pc_search_result. none-task-blog-2~all~sobaiduweb~default-3-91038858.nonecase&spm=1018.2226.3001.4187 face detection and recognition python implementation series (5) - use keras library to train face recognition modelicon-default.png?t=N4P3https://blog.csdn.net/weixin_44491431/article/details/113839543?ops_request_misc=%257B%2522request%255Fid%2522%253A%2522168601107216800185824691%2522%252C %2522scm%2522%253A%252220140713.130102334..%2522%257D&request_id =168601107216800185824691&biz_id=0&utm_medium=distribute.pc_search_result.none-task-blog-2~all~sobaiduend~default-1-113839543-null-null.142^v88^control_2,239^v2^insert_chatgpt &utm_term=%E4%BD%BF% E7%94%A8Keras%E6%9E%84%E5%BB%BA%E4%BA%BA%E8%84%B8%E8%AF%86%E5%88%AB%E6%A8%A1%E5% 9E%8B&spm=1018.2226.3001.4187 teach you how to use Keras for face detection and recognitionicon-default.png?t=N4P3https://blog.csdn.net/m0_55479420/article/details/115268470?ops_request_misc=%257B%2522request%255Fid%2522%253A%2522168601107216800185824691%2522%252C%2522scm%2522%253A%252220140713.130102334..%2522%257D&request_id=168601107216800185824691&biz_id=0&utm_medium=distribute.pc_search_result.none-task-blog-2~all~sobaiduend~default-2-115268470-null-null.142^v88^control_2,239^v2^insert_chatgpt&utm_term=%E4%BD%BF%E7%94%A8Keras%E6%9E%84%E5%BB%BA%E4%BA%BA%E8%84%B8%E8%AF%86%E5%88%AB%E6%A8%A1%E5%9E%8B&spm=1018.2226.3001.4187

Guess you like

Origin blog.csdn.net/qq_52487066/article/details/131060004