Keras对人工智能来说,是一款比较好的入门框架。它是一个高级的Python神经网络框架,已经被添加到TensorFlow中,成为其默认的框架,为TensorFlow提供更高级的API。如果将TensorFlow比喻为编程界的Java或者C++,那么Keras就是编程界的Python,它作为TensorFlow的高层封装,可以与TensorFlow联合使用,用它可以快速搭建模型。并且Keras是TensorFlow官方支持的。当机器上有可用的GPU时,代码会自动调用GPU进行并行计算。
OpenCV的全称,是Open source Computer Vision Library,开放源代码计算机视觉库。也就是说,它是一套关于计算机视觉的开放源代码的API函数库。这也就意味着,(1)不管是科学研究,还是商业应用,都可以利用它来作开发;(2)所有API函数的源代码都是公开的,你可以看到其内部实现的程序步骤;(3)你可以修改OpenCV的源代码,编译生成你需要的特定API函数。但是,作为一个库,它所提供的,仅仅是一些常用的,经典的,大众化的算法的API。一个典型的计算机视觉算法,应该包含以下一些步骤:(1)数据获取(对OpenCV来说,就是图片);(2)预处理;(3)特征提取;(4)特征选择;(5)分类器设计与训练;(6)分类判别;而OpenCV对这六个部分,分别提供了API。
介绍完了Keras与opencv,先看一下模型最终效果:(马赛克打的有点糙,见谅)
训练数据大概用了几百张包括自己和朋友,主要测了一下效果,如下图,me为博主本人,others为朋友。实现了人脸识别的功能。自己也是对计算机视觉感兴趣,想尝试一下。(代码只是实现了功能,仍需向做深度学习的大佬不断学习~)
实现步骤
第一步:人脸检测
这一步需要用到opencv包下面的haarcascade_frontalface_alt2.xml分类器,该分类器的可以用来探测人脸,然后我们自己编写代码圈出人脸,实现人脸识别的第一步,人脸检测。
新建face_recognition_file工程,在该工程下新建data_preparation.py文件准备人脸数据
def detect_face():
# 调用分类器
face_cascade = cv2.CascadeClassifier('/anaconda3/share/OpenCV/haarcascades/haarcascade_frontalface_default.xml')
# 打开摄像头
camera = cv2.VideoCapture(0)
# while循环,一帧一帧将读取视频流数据
while True:
ret, frame = camera.read()
# 将图片灰度化
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
# 检测人脸
faces = face_cascade.detectMultiScale(gray, 1.1, 5)
#x,y为矩形的对角线顶点的坐标,w为宽度,h为高度
for (x, y, w, h) in faces:
img = cv2.rectangle(frame, (x, y), (x + w, y + h), (255, 0, 0), 2)
cv2.imshow('camera', frame)
if cv2.waitKey(1) & 0xff == ord("q"):
break
camera.release()
cv2.destroyAllWindows()
效果如下:
第二步:数据准备
此步骤只需将第一步的代码稍作调整,将视频流中的图片进行保存即可
修改data_preparation.py代码:
#!/usr/bin/env python
# -*- coding: utf-8 -*-
__author__ = 'Seven'
import cv2
def CatchPICFromVideo(catch_num, path_name):
face_cascade = cv2.CascadeClassifier('/anaconda3/share/OpenCV/haarcascades/haarcascade_frontalface_alt2.xml') # haarcascade_frontalface_alt2
camera = cv2.VideoCapture(0)
num = 0
while True:
ret, frame = camera.read()
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
faces = face_cascade.detectMultiScale(gray, 1.1, 5)
for (x, y, w, h) in faces:
img_name = f'{path_name}/{str(num)}.jpg'
image = frame[y:y + h, x:x + w]
print(img_name)
cv2.imwrite(img_name, image)
num += 1
if num > catch_num:
break
# 画出矩形框圈出人脸
cv2.rectangle(frame, (x, y), (x + w, y + h), (255, 0, 0), 2)
# 显示捕捉了多少张人脸
font = cv2.FONT_HERSHEY_SIMPLEX
cv2.putText(frame, f'num:{str(num)}', (x + 30, y + 30), font, 1, (255, 0, 255), 4)
if num > catch_num:
break
# 显示图像
cv2.imshow('camera', frame)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
camera.release()
cv2.destroyAllWindows()
if __name__ == '__main__':
CatchPICFromVideo(100, './data/me')
在当前工作目录下新建data文件夹,并在data文件夹下新建me文件夹保存自己的图片,新建others保存其他人的图片
第三步:图片数据预处理
输入到卷积神经网络中的图片需要有固定的尺寸,我们要用OpenCV统一图片尺寸大小以便输入到卷积神经网络中,我们在工作目录下建立一个face_dataset.py
文件,定义一个resize_image
函数来调整图片尺寸,然后将图片读取到Python列表中以便用Python对其进一步处理,并对数据进行标注,“我”标注为0,其他人标注为“1”。在face_dataset.py
里接着定义一个read_path
函数从指定路径中读取图片到Python list,然后再定义一个load_dataset
函数调用read_path
函数读取图片并转化为更适用于机器学习的numpy多维数组,然后通过判断图片路径名的区别将图片数据标注为0和1
#!/usr/bin/env python
# -*- coding: utf-8 -*-
__author__ = 'Seven'
import os
import numpy as np
import cv2
# 定义图片尺寸
IMAGE_SIZE = 64
# 按照定义图像大小进行尺度调整
def resize_image(image, height=IMAGE_SIZE, width=IMAGE_SIZE):
top, bottom, left, right = 0, 0, 0, 0
# 获取图像尺寸
h, w, _ = image.shape
# 找到图片最长的一边
longest_edge = max(h, w)
# 计算短边需要填充多少使其与长边等长
if h < longest_edge:
d = longest_edge - h
top = d // 2
bottom = d // 2
elif w < longest_edge:
d = longest_edge - w
left = d // 2
right = d // 2
else:
pass
# 设置填充颜色
BLACK = [0, 0, 0]
# 对原始图片进行填充操作
constant = cv2.copyMakeBorder(image, top, bottom, left, right, cv2.BORDER_CONSTANT, value=BLACK)
# 调整图像大小并返回
return cv2.resize(constant, (height, width))
images, labels = list(), list()
# 读取训练数据
def read_path(path_name):
for dir_item in os.listdir(path_name):
# 合并成可识别的操作路径
full_path = os.path.abspath(os.path.join(path_name, dir_item))
# 如果是文件夹,则继续递归调用
if os.path.isdir(full_path):
read_path(full_path)
else:
if dir_item.endswith('.jpg'):
# print(dir_item)
image = cv2.imread(full_path)
image = resize_image(image, IMAGE_SIZE, IMAGE_SIZE)
images.append(image)
labels.append(path_name)
print(labels)
return images, labels
# 从指定路径读取训练数据
def load_dataset(path_name):
images, labels = read_path(path_name)
# 由于图片是基于矩阵计算的, 将其转为矩阵
images = np.array(images)
print(images.shape)
labels = np.array([0 if label.endswith('me') else 1 for label in labels])
return images, labels
if __name__ == '__main__':
images, labels = load_dataset(os.getcwd()+'/data')
print('load over')
第四步:利用Keras搭建卷积神经网络
在工作目录下建立一个face_train.py
文件,代码如下:
#!/usr/bin/env python
# -*- coding: utf-8 -*-
__author__ = 'Seven'
import random
import numpy as np
from sklearn.model_selection import train_test_split
from keras.preprocessing.image import ImageDataGenerator
from keras.models import Sequential
from keras.layers import Dense, Activation, Flatten, Dropout
from keras.layers import Conv2D, MaxPool2D
from keras.optimizers import SGD
from keras.utils import np_utils
from keras.models import load_model
from keras import backend as K
from face_dataset import load_dataset, resize_image, IMAGE_SIZE
import warnings
warnings.filterwarnings('ignore')
class Dataset:
def __init__(self, path_name):
# 训练集
self.train_images = None
self.train_labels = None
# 验证集
# self.valid_images = None
# self.valid_labels = None
# 测试集
self.test_images = None
self.test_labels = None
# 数据加载路径
self.path_name = path_name
# 当前库采用的维度顺序
self.input_shape = None
def load(self, img_rows=IMAGE_SIZE, img_cols=IMAGE_SIZE, img_channels=3, nb_classes=2):
# 加载数据集至内存
images, labels = load_dataset(self.path_name)
train_images, test_images, train_labels, test_labels = train_test_split(images, labels, test_size=0.3,
random_state=random.randint(0, 10))
if K.image_dim_ordering() == 'th':
train_images = train_images.reshape(train_images.shape[0], img_channels, img_rows, img_cols)
test_images = test_images.reshape(test_images.shape[0], img_channels, img_rows, img_cols)
self.input_shape = (img_channels, img_rows, img_cols)
else:
train_images = train_images.reshape(train_images.shape[0], img_rows, img_cols, img_channels)
test_images = test_images.reshape(test_images.shape[0], img_rows, img_cols, img_channels)
self.input_shape = (img_rows, img_cols, img_channels)
# 输出训练集、测试集的数量
print(train_images.shape[0], 'train samples')
print(test_images.shape[0], 'test samples')
# 我们的模型使用categorical_crossentropy作为损失函数,因此需要根据类别数量nb_classes将
# 类别标签进行one-hot编码使其向量化,在这里我们的类别只有两种,经过转化后标签数据变为二维
train_labels = np_utils.to_categorical(train_labels, nb_classes)
test_labels = np_utils.to_categorical(test_labels, nb_classes)
# 像素数据浮点化以便归一化
train_images = train_images.astype('float32')
test_images = test_images.astype('float32')
# 将其归一化,图像的各像素值归一化到0~1区间
train_images /= 255.0
test_images /= 255.0
self.train_images = train_images
self.test_images = test_images
self.train_labels = train_labels
self.test_labels = test_labels
# CNN网络模型类
class Model:
def __init__(self):
self.model = None
# 建立模型
def build_model(self, dataset, nb_classes=2):
# 构建一个空的网络模型,它是一个线性堆叠模型,各神经网络层会被顺序添加,专业名称为序贯模型或线性堆叠模型
self.model = Sequential()
# 以下代码将顺序添加CNN网络需要的各层,一个add就是一个网络层
self.model.add(Conv2D(32, 3, 3, border_mode='same',
input_shape=dataset.input_shape)) # 1 2维卷积层
self.model.add(Activation('relu')) # 2 激活函数层
self.model.add(Conv2D(32, 3, 3)) # 3 2维卷积层
self.model.add(Activation('relu')) # 4 激活函数层
self.model.add(MaxPool2D(pool_size=(2, 2))) # 5 池化层
self.model.add(Dropout(0.25)) # 6 Dropout层
self.model.add(Conv2D(64, 3, 3, border_mode='same')) # 7 2维卷积层
self.model.add(Activation('relu')) # 8 激活函数层
self.model.add(Conv2D(64, 3, 3)) # 9 2维卷积层
self.model.add(Activation('relu')) # 10 激活函数层
self.model.add(MaxPool2D(pool_size=(2, 2))) # 11 池化层
self.model.add(Dropout(0.25)) # 12 Dropout层
self.model.add(Flatten()) # 13 Flatten层
self.model.add(Dense(512)) # 14 Dense层,又被称作全连接层
self.model.add(Activation('relu')) # 15 激活函数层
self.model.add(Dropout(0.5)) # 16 Dropout层
self.model.add(Dense(nb_classes)) # 17 Dense层
self.model.add(Activation('softmax')) # 18 分类层,输出最终结果
# 输出模型概况
self.model.summary()
# 训练模型
def train(self, dataset, batch_size=20, nb_epoch=100, data_augmentation=True):
sgd = SGD(lr=0.01, decay=1e-6,
momentum=0.9, nesterov=True) # 采用SGD+momentum的优化器进行训练,首先生成一个优化器对象
self.model.compile(loss='categorical_crossentropy',
optimizer=sgd,
metrics=['accuracy']) # 完成实际的模型配置工作
# 不使用数据提升,所谓的提升就是从我们提供的训练数据中利用旋转、翻转、加噪声等方法创造新的
# 训练数据,有意识的提升训练数据规模,增加模型训练量
if not data_augmentation:
self.model.fit(dataset.train_images,
dataset.train_labels,
batch_size=batch_size,
nb_epoch=nb_epoch,
validation_data=(dataset.test_images, dataset.test_labels),
shuffle=True)
# 使用实时数据提升
else:
# 定义数据生成器用于数据提升,其返回一个生成器对象datagen,datagen每被调用一
# 次其生成一组数据(顺序生成),节省内存,其实就是python的数据生成器
datagen = ImageDataGenerator(
featurewise_center=False, # 是否使输入数据去中心化(均值为0),
samplewise_center=False, # 是否使输入数据的每个样本均值为0
featurewise_std_normalization=False, # 是否数据标准化(输入数据除以数据集的标准差)
samplewise_std_normalization=False, # 是否将每个样本数据除以自身的标准差
zca_whitening=False, # 是否对输入数据施以ZCA白化
rotation_range=20, # 数据提升时图片随机转动的角度(范围为0~180)
width_shift_range=0.2, # 数据提升时图片水平偏移的幅度(单位为图片宽度的占比,0~1之间的浮点数)
height_shift_range=0.2, # 同上,只不过这里是垂直
horizontal_flip=True, # 是否进行随机水平翻转
vertical_flip=False) # 是否进行随机垂直翻转
# 计算整个训练样本集的数量以用于特征值归一化、ZCA白化等处理
datagen.fit(dataset.train_images)
# 利用生成器开始训练模型
self.model.fit_generator(datagen.flow(dataset.train_images, dataset.train_labels,
batch_size=batch_size),
samples_per_epoch=dataset.train_images.shape[0],
nb_epoch=nb_epoch,
validation_data=(dataset.test_images, dataset.test_labels))
MODEL_PATH = './face.model.h5'
def save_model(self, file_path=MODEL_PATH):
self.model.save(file_path)
def load_model(self, file_path=MODEL_PATH):
self.model = load_model(file_path)
def evaluate(self, dataset):
score = self.model.evaluate(dataset.test_images, dataset.test_labels, verbose=1)
# print("%s: %.2f%%" % (self.model.metrics_names[1], score[1] * 100))
print(f'{self.model.metrics_names[1]}:{score[1] * 100}%')
# 识别人脸
def face_predict(self, image):
# 依然是根据后端系统确定维度顺序
if K.image_dim_ordering() == 'th' and image.shape != (1, 3, IMAGE_SIZE, IMAGE_SIZE):
image = resize_image(image) # 尺寸必须与训练集一致都应该是IMAGE_SIZE x IMAGE_SIZE
image = image.reshape((1, 3, IMAGE_SIZE, IMAGE_SIZE)) # 与模型训练不同,这次只是针对1张图片进行预测
elif K.image_dim_ordering() == 'tf' and image.shape != (1, IMAGE_SIZE, IMAGE_SIZE, 3):
image = resize_image(image)
image = image.reshape((1, IMAGE_SIZE, IMAGE_SIZE, 3))
# 浮点并归一化
image = image.astype('float32')
image /= 255.0
# 给出输入属于各个类别的概率,我们是二值类别,则该函数会给出输入图像属于0和1的概率各为多少
result = self.model.predict_proba(image)
print('result:', result)
# 给出类别预测:0或者1
result = self.model.predict_classes(image)
# 返回类别预测结果
return result[0]
if __name__ == '__main__':
dataset = Dataset('./data/')
dataset.load()
# 训练模型,这段代码不用,注释掉
model = Model()
model.build_model(dataset)
# 测试训练函数的代码
model.train(dataset)
model.save_model(file_path='./model/me.face.model.h5')
# 评估模型
# model = Model()
# model.load_model(file_path='./model/me.face.model.h5')
# model.evaluate(dataset)
在上面的一段代码里,加载完数据后我首先是用scikit-learn将数据集划分为训练集(用于训练模型)和测试集(用于评估训练好的模型的效果),scikit-learn是一个基于Python的传统机器学习库,能和Numpy交互使用,这里用其中的train_test_split
函数,把数据集按70%/30%的比例划分为训练集和测试集,需要注意的是这个函数默认会把数据集随机打散,这样训练集和测试集的数据分布就更加相近。划分好数据集以后还要考虑一个Keras后端所遵循的图片数据格式的问题,当Keras后端引擎是Tensorflow时,后端的image_data_format
属性是channel_last
,也就是说图片数据的维度格式是(rows, cols, channels)
,如果后端引擎是Theano的话,image_data_format
属性是channel_first
,图片数据的维度格式是(channels, rows, cols)
,因此,为了使程序更健壮,我们要通过判断image_data_format
属性的值来调整图片数据集的维度顺序。
第五步:测试
新建findme.py对此工程的功能进行测试
#!/usr/bin/env python
# -*- coding: utf-8 -*-
__author__ = 'Seven'
import cv2
from face_train import Model
if __name__ == '__main__':
# 加载模型
model = Model()
model.load_model(file_path='./model/me.face.model.h5')
# 框住人脸的矩形边框颜色
color = (0, 255, 0)
# 捕获指定摄像头的实时视频流
camera = cv2.VideoCapture(0)
# 人脸识别分类器本地存储路径
cascade_path = "/anaconda3/share/OpenCV/haarcascades/haarcascade_frontalface_alt2.xml"
# 循环检测识别人脸
while True:
ret, frame = camera.read() # 读取一帧视频
# 图像灰化,降低计算复杂度
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
# 使用人脸识别分类器,读入分类器
cascade = cv2.CascadeClassifier(cascade_path)
# 利用分类器识别出哪个区域为人脸
faces = cascade.detectMultiScale(gray, 1.1, 5)
if len(faces) > 0:
for (x, y, w, h) in faces:
# 截取脸部图像提交给模型识别这是谁
image = frame[y: y + h, x: x + w]
faceID = model.face_predict(image)
# 如果是“我”
if faceID == 0:
cv2.rectangle(frame, (x, y), (x + w, y + h), color, thickness=2)
# 文字提示是谁
cv2.putText(frame, 'me',
(x + 30, y + 30), # 坐标
cv2.FONT_HERSHEY_SIMPLEX, # 字体
1, # 字号
(255, 0, 255), # 颜色
2) # 字的线宽
else:
cv2.rectangle(frame, (x, y), (x + w, y + h), color, thickness=2)
# 文字提示是谁
cv2.putText(frame, 'others',
(x + 30, y + 30), # 坐标
cv2.FONT_HERSHEY_SIMPLEX, # 字体
1, # 字号
(255, 0, 255), # 颜色
2)
cv2.imshow("camera", frame)
# 等待1毫秒看是否有按键输入
k = cv2.waitKey(1)
# 如果输入q则退出循环
if k & 0xFF == ord('q'):
break
# 释放摄像头并销毁所有窗口
camera.release()
cv2.destroyAllWindows()
总结:
总的来说,你不得不佩服Python的强大,简答易懂的代码加上强大的第三方库支持,让Python在机器学习、深度学习方向更具优势。当然Java在处理大数据时比Python更高效。(?)
不同的语言各有优劣,希望自己能涉猎更多,不断学习,啊哈哈哈哈哈