一、HOG,Dlib,卷积神经网络介绍
1、HoG
①方法简介
方向梯度直方图(Histogram of Oriented Gradient, HOG)特征是一种在计算机视觉和图像处理中用来进行物体检测的描述子。通过计算和统计局部区域的梯度方向直方图来构成特征。Hog特征结合SVM分类器已经被广泛应用于图像识别中,尤其在行人检测中获得了极大的成功。现如今如今虽然有很多行人检测算法不断提出,但基本都是以HOG+SVM的思路为主。
②主要思想
在一幅图像中,局部目标的表象和形状(appearance and shape)能够被梯度或边缘的方向密度分布很好地描述。其本质是梯度的统计信息,而梯度主要存在于边缘所在的地方。
③实现过程
简单来说,首先需要将图像分成小的连通区域,称之为细胞单元。然后采集细胞单元中各像素点的梯度或边缘的方向直方图。最后把这些直方图组合起来就可以构成特征描述器。
④算法优点
与其他的特征描述方法相比,HOG有较多优点。由于HOG是在图像的局部方格单元上操作,所以它对图像几何的和光学的形变都能保持很好的不变性,这两种形变只会出现在更大的空间领域上。其次,在粗的空域抽样、精细的方向抽样以及较强的局部光学归一化等条件下,只要行人大体上能够保持直立的姿势,可以容许行人有一些细微的肢体动作,这些细微的动作可以被忽略而不影响检测效果。因此HOG特征是特别适合于做图像中的人体检测的。
HOG特征提取算法的整个实现过程大致如下: 读入所需要的检测目标即输入的image 将图像进行灰度化(将输入的彩色的图像的r,g,b值通过特定公式转换为灰度值) 采用Gamma校正法对输入图像进行颜色空间的标准化(归一化) 计算图像每个像素的梯度(包括大小和方向),捕获轮廓信息 统计每个cell的梯度直方图(不同梯度的个数),形成每个cell的descriptor 将每几个cell组成一个block(以3*3为例),一个block内所有cell的特征串联起来得到该block的HOG特征descriptor 将图像image内所有block的HOG特征descriptor串联起来得到该image(检测目标)的HOG特征descriptor,这就是最终分类的特征向量
以下代码片段可以提取一张图片的HoG特征:
import numpy as np
from scipy import signal
import scipy.misc
def s_x(img):kernel = np.array([[-1, 0, 1]])imgx = signal.convolve2d(img, kernel, boundary='symm', mode='same')return imgx
def s_y(img):kernel = np.array([[-1, 0, 1]]).Timgy = signal.convolve2d(img, kernel, boundary='symm', mode='same')return imgy
def grad(img):imgx = s_x(img)imgy = s_y(img)s = np.sqrt(imgx**2 + imgy**2)theta = np.arctan2(imgx, imgy) #imgy, imgx)theta[theta<0] = np.pi + theta[theta<0]return (s, theta)
2、Dlib
一个机器学习的开源库,包含了机器学习的很多算法,使用起来很方便,直接包含头文件即可,并且不依赖于其他库(自带图像编解码库源码)。 Dlib可以帮助您创建很多复杂的机器学习方面的软件来帮助解决实际问题。目前Dlib已经被广泛的用在行业和学术领域,包括机器人,嵌入式设备,移动电话和大型高性能计算环境。 dlib也是人脸识别常用的一个库,可以检测出人脸上的68个点,并且进行标注,当我们准备自己的人脸数据时,常常用dlib进行数据提取。
3、卷积神经网络
卷积神经网络(Convolutional Neural Networks, CNN)是一类包含卷积计算且具有深度结构的前馈神经网络(Feedforward Neural Networks),是深度学习(deep learning)的代表算法之一 。卷积神经网络具有表征学习(representation learning)能力,能够按其阶层结构对输入信息进行平移不变分类(shift-invariant classification),因此也被称为“平移不变人工神经网络(Shift-Invariant Artificial Neural Networks, SIANN)。 卷积神经网络仿造生物的视知觉(visual perception)机制构建,可以进行监督学习和非监督学习,其隐含层内的卷积核参数共享和层间连接的稀疏性使得卷积神经网络能够以较小的计算量对格点化(grid-like topology)特征,例如像素和音频进行学习、有稳定的效果且对数据没有额外的特征工程(feature engineering)要求。
二、笑脸数据集的划分、训练、测试
下载数据集:https://github.com/truongnmt/smile-detection
下载好之后解压保存,我是保存在D盘,并重命名为smile:
划分:
import keras
import os, shutil
# The path to the directory where the original
# dataset was uncompressed
original_dataset_dir = 'D:\\smile\\datasets\\train_folder'
# The directory where we will
# store our smaller dataset
base_dir = 'D:\\smile2'
os.mkdir(base_dir)
# Directories for our training,
# validation and test splits
train_dir = os.path.join(base_dir, 'train')
os.mkdir(train_dir)
validation_dir = os.path.join(base_dir, 'validation')
os.mkdir(validation_dir)
test_dir = os.path.join(base_dir, 'test')
os.mkdir(test_dir)
# Directory with our training smile pictures
train_smiles_dir = os.path.join(train_dir, 'smiles')
os.mkdir(train_smiles_dir)
# Directory with our training unsmile pictures
train_unsmiles_dir = os.path.join(train_dir, 'unsmiles')
os.mkdir(train_unsmiles_dir)
# Directory with our validation smile pictures
validation_smiles_dir = os.path.join(validation_dir, 'smiles')
os.mkdir(validation_smiles_dir)
# Directory with our validation unsmile pictures
validation_unsmiles_dir = os.path.join(validation_dir, 'unsmiles')
os.mkdir(validation_unsmiles_dir)
# Directory with our validation smile pictures
test_smiles_dir = os.path.join(test_dir, 'smiles')
os.mkdir(test_smiles_dir)
# Directory with our validation unsmile pictures
test_unsmiles_dir = os.path.join(test_dir, 'unsmiles')
os.mkdir(test_unsmiles_dir)
然后把Datasets里面的图片放进去相应的文件夹(手动保存),打印新数据集的尺寸:
print('total training smile images:', len(os.listdir(train_smiles_dir)))
print('total training unsmile images:', len(os.listdir(train_unsmiles_dir)))
print('total validation smile images:', len(os.listdir(validation_smiles_dir)))
print('total validation unsmile images:', len(os.listdir(validation_unsmiles_dir)))
print('total test smile images:', len(os.listdir(test_smiles_dir)))
print('total test unsmile images:', len(os.listdir(test_unsmiles_dir)))
构建卷积神经网络:
from keras import layers
from keras import models
model = models.Sequential()
model.add(layers.Conv2D(32, (3, 3), activation='relu',input_shape=(150, 150, 3)))
model.add(layers.MaxPooling2D((2, 2)))
model.add(layers.Conv2D(64, (3, 3), activation='relu'))
model.add(layers.MaxPooling2D((2, 2)))
model.add(layers.Conv2D(128, (3, 3), activation='relu'))
model.add(layers.MaxPooling2D((2, 2)))
model.add(layers.Conv2D(128, (3, 3), activation='relu'))
model.add(layers.MaxPooling2D((2, 2)))
model.add(layers.Flatten())
model.add(layers.Dense(512, activation='relu'))
model.add(layers.Dense(1, activation='sigmoid'))
model.summary()
数据预处理:
from keras import optimizersmodel.compile(loss='binary_crossentropy',optimizer=optimizers.RMSprop(lr=1e-4),metrics=['acc'])
from keras.preprocessing.image import ImageDataGenerator# All images will be rescaled by 1./255
train_datagen = ImageDataGenerator(rescale=1./255)
test_datagen = ImageDataGenerator(rescale=1./255)train_generator = train_datagen.flow_from_directory(# This is the target directorytrain_dir,# All images will be resized to 150x150target_size=(150, 150),batch_size=20,# Since we use binary_crossentropy loss, we need binary labelsclass_mode='binary')validation_generator = test_datagen.flow_from_directory(validation_dir,target_size=(150, 150),batch_size=20,class_mode='binary')
for data_batch, labels_batch in train_generator:print('data batch shape:', data_batch.shape)print('labels batch shape:', labels_batch.shape)break
训练:
history = model.fit_generator(train_generator,steps_per_epoch=100,epochs=30,validation_data=validation_generator,validation_steps=50)
model.save('D:/smile2/smiles_and_unsmiles_small_1.h5')
绘制模型的损失和准确性:
import matplotlib.pyplot as plt
acc = history.history['acc']
val_acc = history.history['val_acc']
loss = history.history['loss']
val_loss = history.history['val_loss']
epochs = range(len(acc))
plt.plot(epochs, acc, 'bo', label='Training acc')
plt.plot(epochs, val_acc, 'b', label='Validation acc')
plt.title('Training and validation accuracy')
plt.legend()
plt.figure()
plt.plot(epochs, loss, 'bo', label='Training loss')
plt.plot(epochs, val_loss, 'b', label='Validation loss')
plt.title('Training and validation loss')
plt.legend()
plt.show()
数据增强:
datagen = ImageDataGenerator(rotation_range=40,width_shift_range=0.2,height_shift_range=0.2,shear_range=0.2,zoom_range=0.2,horizontal_flip=True,fill_mode='nearest')
为了进一步对抗过拟合,我们还将在我们的模型中增加一个Dropout层:
model = models.Sequential()
model.add(layers.Conv2D(32, (3, 3), activation='relu',input_shape=(150, 150, 3)))
model.add(layers.MaxPooling2D((2, 2)))
model.add(layers.Conv2D(64, (3, 3), activation='relu'))
model.add(layers.MaxPooling2D((2, 2)))
model.add(layers.Conv2D(128, (3, 3), activation='relu'))
model.add(layers.MaxPooling2D((2, 2)))
model.add(layers.Conv2D(128, (3, 3), activation='relu'))
model.add(layers.MaxPooling2D((2, 2)))
model.add(layers.Flatten())
model.add(layers.Dropout(0.5))
model.add(layers.Dense(512, activation='relu'))
model.add(layers.Dense(1, activation='sigmoid'))model.compile(loss='binary_crossentropy',optimizer=optimizers.RMSprop(lr=1e-4),metrics=['acc'])
开始训练:
train_datagen = ImageDataGenerator(rescale=1./255,rotation_range=40,width_shift_range=0.2,height_shift_range=0.2,shear_range=0.2,zoom_range=0.2,horizontal_flip=True,)
# Note that the validation data should not be augmented!
test_datagen = ImageDataGenerator(rescale=1./255)
train_generator = train_datagen.flow_from_directory(# This is the target directorytrain_dir,# All images will be resized to 150x150target_size=(150, 150),batch_size=32,# Since we use binary_crossentropy loss, we need binary labelsclass_mode='binary')
validation_generator = test_datagen.flow_from_directory(validation_dir,target_size=(150, 150),batch_size=32,class_mode='binary')
history = model.fit_generator(train_generator,steps_per_epoch=100,epochs=100,validation_data=validation_generator,validation_steps=50)
保存:
model.save('D:/smile2/smiles_unsmiles_small_2.h5')
绘制模型的损失和准确性:
acc = history.history['acc']
val_acc = history.history['val_acc']
loss = history.history['loss']
val_loss = history.history['val_loss']
epochs = range(len(acc))
plt.plot(epochs, acc, 'bo', label='Training acc')
plt.plot(epochs, val_acc, 'b', label='Validation acc')
plt.title('Training and validation accuracy')
plt.legend()
plt.figure()
plt.plot(epochs, loss, 'bo', label='Training loss')
plt.plot(epochs, val_loss, 'b', label='Validation loss')
plt.title('Training and validation loss')
plt.legend()
plt.show()
三、摄像头实时识别笑脸
使用上面训练出来的模型进行判断:
#检测视频或者摄像头中的人脸
import cv2
from keras.preprocessing import image
from keras.models import load_model
import numpy as np
import dlib
from PIL import Image
model = load_model('D:/smile2/smiles_unsmiles_small_2.h5')
detector = dlib.get_frontal_face_detector()
video=cv2.VideoCapture(0)
font = cv2.FONT_HERSHEY_SIMPLEX
def rec(img):gray=cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)dets=detector(gray,1)if dets is not None:for face in dets:left=face.left()top=face.top()right=face.right()bottom=face.bottom()cv2.rectangle(img,(left,top),(right,bottom),(0,255,0),2)img1=cv2.resize(img[top:bottom,left:right],dsize=(150,150))img1=cv2.cvtColor(img1,cv2.COLOR_BGR2RGB)img1 = np.array(img1)/255.img_tensor = img1.reshape(-1,150,150,3)prediction =model.predict(img_tensor) if prediction[0][0]>0.5:result='unsmile'else:result='smile'cv2.putText(img, result, (left,top), font, 2, (0, 255, 0), 2, cv2.LINE_AA)cv2.imshow('Video', img)
while video.isOpened():res, img_rd = video.read()if not res:breakrec(img_rd)if cv2.waitKey(5) & 0xFF == ord('q'):break
video.release()
cv2.destroyAllWindows()
使用opencv自带的笑脸识别:
import cv2face_cascade = cv2.CascadeClassifier(cv2.data.haarcascades+'haarcascade_frontalface_default.xml')eye_cascade = cv2.CascadeClassifier(cv2.data.haarcascades+'haarcascade_eye.xml')smile_cascade = cv2.CascadeClassifier(cv2.data.haarcascades+'haarcascade_smile.xml')
# 调用摄像头摄像头
cap = cv2.VideoCapture(0)while(True):# 获取摄像头拍摄到的画面ret, frame = cap.read()faces = face_cascade.detectMultiScale(frame, 1.3, 2)img = framefor (x,y,w,h) in faces:# 画出人脸框,颜色自己定义,画笔宽度微img = cv2.rectangle(img,(x,y),(x+w,y+h),(255,225,0),2)# 框选出人脸区域,在人脸区域而不是全图中进行人眼检测,节省计算资源face_area = img[y:y+h, x:x+w]## 人眼检测# 用人眼级联分类器引擎在人脸区域进行人眼识别,返回的eyes为眼睛坐标列表eyes = eye_cascade.detectMultiScale(face_area,1.3,10)for (ex,ey,ew,eh) in eyes:#画出人眼框,绿色,画笔宽度为1cv2.rectangle(face_area,(ex,ey),(ex+ew,ey+eh),(0,255,0),1)## 微笑检测# 用微笑级联分类器引擎在人脸区域进行人眼识别,返回的eyes为眼睛坐标列表smiles = smile_cascade.detectMultiScale(face_area,scaleFactor= 1.16,minNeighbors=65,minSize=(25, 25),flags=cv2.CASCADE_SCALE_IMAGE)for (ex,ey,ew,eh) in smiles:#画出微笑框,(BGR色彩体系),画笔宽度为1cv2.rectangle(face_area,(ex,ey),(ex+ew,ey+eh),(0,0,255),1)cv2.putText(img,'Smile',(x,y-7), 3, 1.2, (222, 0, 255), 2, cv2.LINE_AA)# 实时展示效果画面cv2.imshow('frame2',img)# 每5毫秒监听一次键盘动作if cv2.waitKey(5) & 0xFF == ord('q'):break# 最后,关闭所有窗口
cap.release()
cv2.destroyAllWindows()
使用Dlib:
import sys
import dlib # 人脸识别的库dlib
import numpy as np # 数据处理的库numpy
import cv2 # 图像处理的库OpenCvclass face_emotion():def __init__(self):# 使用特征提取器get_frontal_face_detectorself.detector = dlib.get_frontal_face_detector()# dlib的68点模型,使用作者训练好的特征预测器self.predictor = dlib.shape_predictor("C:/data/data_dlib_model/shape_predictor_68_face_landmarks.dat")# 建cv2摄像头对象,这里使用电脑自带摄像头,如果接了外部摄像头,则自动切换到外部摄像头self.cap = cv2.VideoCapture(0)# 设置视频参数,propId设置的视频参数,value设置的参数值self.cap.set(3, 480)# 截图screenshoot的计数器self.cnt = 0def learning_face(self):# 眉毛直线拟合数据缓冲line_brow_x = []line_brow_y = []# cap.isOpened() 返回true/false 检查初始化是否成功while (self.cap.isOpened()):# cap.read()# 返回两个值:# 一个布尔值true/false,用来判断读取视频是否成功/是否到视频末尾# 图像对象,图像的三维矩阵flag, im_rd = self.cap.read()# 每帧数据延时1ms,延时为0读取的是静态帧k = cv2.waitKey(1)# 取灰度img_gray = cv2.cvtColor(im_rd, cv2.COLOR_RGB2GRAY)# 使用人脸检测器检测每一帧图像中的人脸。并返回人脸数rectsfaces = self.detector(img_gray, 0)# 待会要显示在屏幕上的字体font = cv2.FONT_HERSHEY_SIMPLEX# 如果检测到人脸if (len(faces) != 0):# 对每个人脸都标出68个特征点for i in range(len(faces)):# enumerate方法同时返回数据对象的索引和数据,k为索引,d为faces中的对象for k, d in enumerate(faces):# 用红色矩形框出人脸cv2.rectangle(im_rd, (d.left(), d.top()), (d.right(), d.bottom()), (0, 0, 255))# 计算人脸热别框边长self.face_width = d.right() - d.left()# 使用预测器得到68点数据的坐标shape = self.predictor(im_rd, d)# 圆圈显示每个特征点for i in range(68):cv2.circle(im_rd, (shape.part(i).x, shape.part(i).y), 2, (222, 222, 0), -1, 8)#cv2.putText(im_rd, str(i), (shape.part(i).x, shape.part(i).y), cv2.FONT_HERSHEY_SIMPLEX, 0.5,# (255, 255, 255))# 分析任意n点的位置关系来作为表情识别的依据mouth_width = (shape.part(54).x - shape.part(48).x) / self.face_width # 嘴巴咧开程度mouth_higth = (shape.part(66).y - shape.part(62).y) / self.face_width # 嘴巴张开程度# 通过两个眉毛上的10个特征点,分析挑眉程度和皱眉程度brow_sum = 0 # 高度之和frown_sum = 0 # 两边眉毛距离之和for j in range(17, 21):brow_sum += (shape.part(j).y - d.top()) + (shape.part(j + 5).y - d.top())frown_sum += shape.part(j + 5).x - shape.part(j).xline_brow_x.append(shape.part(j).x)line_brow_y.append(shape.part(j).y)# self.brow_k, self.brow_d = self.fit_slr(line_brow_x, line_brow_y) # 计算眉毛的倾斜程度tempx = np.array(line_brow_x)tempy = np.array(line_brow_y)z1 = np.polyfit(tempx, tempy, 1) # 拟合成一次直线self.brow_k = -round(z1[0], 3) # 拟合出曲线的斜率和实际眉毛的倾斜方向是相反的brow_hight = (brow_sum / 10) / self.face_width # 眉毛高度占比brow_width = (frown_sum / 5) / self.face_width # 眉毛距离占比# 眼睛睁开程度eye_sum = (shape.part(41).y - shape.part(37).y + shape.part(40).y - shape.part(38).y +shape.part(47).y - shape.part(43).y + shape.part(46).y - shape.part(44).y)eye_hight = (eye_sum / 4) / self.face_width# 分情况讨论# 张嘴,可能是开心或者惊讶if round(mouth_higth >= 0.03):if eye_hight >= 0.056:cv2.putText(im_rd, "amazing", (d.left(), d.bottom() + 20), cv2.FONT_HERSHEY_SIMPLEX,0.8,(0, 0, 255), 2, 4)else:cv2.putText(im_rd, "smile", (d.left(), d.bottom() + 20), cv2.FONT_HERSHEY_SIMPLEX, 0.8,(0, 0, 255), 2, 4)# 没有张嘴,可能是正常和生气else:if self.brow_k <= -0.3:cv2.putText(im_rd, "angry", (d.left(), d.bottom() + 20), cv2.FONT_HERSHEY_SIMPLEX, 0.8,(0, 0, 255), 2, 4)else:cv2.putText(im_rd, "", (d.left(), d.bottom() + 20), cv2.FONT_HERSHEY_SIMPLEX, 0.8,(0, 0, 255), 2, 4)# 标出人脸数cv2.putText(im_rd, "Faces: " + str(len(faces)), (20, 50), font, 1, (0, 0, 255), 1, cv2.LINE_AA)else:# 没有检测到人脸cv2.putText(im_rd, "No Face", (20, 50), font, 1, (0, 0, 255), 1, cv2.LINE_AA)# 添加说明#im_rd = cv2.putText(im_rd, "S: screenshot", (20, 400), font, 0.8, (0, 0, 255), 1, cv2.LINE_AA)im_rd = cv2.putText(im_rd, "Q: quit", (20, 450), font, 0.8, (0, 0, 255), 1, cv2.LINE_AA)# 按下s键截图保存#if (k == ord('s')):#self.cnt += 1#cv2.imwrite("screenshoot" + str(self.cnt) + ".jpg", im_rd)# 按下q键退出if (k == ord('q')):break# 窗口显示cv2.imshow("camera", im_rd)# 释放摄像头self.cap.release()# 删除建立的窗口cv2.destroyAllWindows()if __name__ == "__main__":my_face = face_emotion()my_face.learning_face()
四、口罩数据集的划分、训练、测试
数据集下载:
这里有戴口罩的图片,在RWMFD_part_1文件夹里面,只不过是散开的,你需要把他们合在一起,文件名重复你可以使用FreeRename软件进行批量的数字序列重命名。
不带口罩的图片可以借用上面的笑脸数据集里面的图片。
划分数据集:
import keras
import os, shutil
# The path to the directory where the original
# dataset was uncompressed
original_dataset_dir = 'D:\\mask\\train'
# The directory where we will
# store our smaller dataset
base_dir = 'D:\\mask1'
os.mkdir(base_dir)
# Directories for our training,
# validation and test splits
train_dir = os.path.join(base_dir, 'train')
os.mkdir(train_dir)
validation_dir = os.path.join(base_dir, 'validation')
os.mkdir(validation_dir)
test_dir = os.path.join(base_dir, 'test')
os.mkdir(test_dir)
# Directory with our training masks pictures
train_masks_dir = os.path.join(train_dir, 'masks')
os.mkdir(train_masks_dir)
# Directory with our training unmasks pictures
train_unmasks_dir = os.path.join(train_dir, 'unmasks')
os.mkdir(train_unmasks_dir)
# Directory with our validation masks pictures
validation_masks_dir = os.path.join(validation_dir, 'masks')
os.mkdir(validation_masks_dir)
# Directory with our validation unmasks pictures
validation_unmasks_dir = os.path.join(validation_dir, 'unmasks')
os.mkdir(validation_unmasks_dir)
# Directory with our validation masks pictures
test_masks_dir = os.path.join(test_dir, 'masks')
os.mkdir(test_masks_dir)
# Directory with our validation unmasks pictures
test_unmasks_dir = os.path.join(test_dir, 'unmasks')
os.mkdir(test_unmasks_dir)
手动将图片保存到相应的文件夹里面,然后查看(这里给出我自己整理的数据集 链接:https://pan.baidu.com/s/1fr8mq7a-IBic2PGFb094JQ
提取码:dtxa):
print('total training mask images:', len(os.listdir(train_masks_dir)))
print('total training unmask images:', len(os.listdir(train_unmasks_dir)))
print('total validation mask images:', len(os.listdir(validation_masks_dir)))
print('total validation unmask images:', len(os.listdir(validation_unmasks_dir)))
print('total test mask images:', len(os.listdir(test_masks_dir)))
print('total test unmask images:', len(os.listdir(test_unmasks_dir)))
构建卷积网络:
from keras import layers
from keras import models
model = models.Sequential()
model.add(layers.Conv2D(32, (3, 3), activation='relu',input_shape=(150, 150, 3)))
model.add(layers.MaxPooling2D((2, 2)))
model.add(layers.Conv2D(64, (3, 3), activation='relu'))
model.add(layers.MaxPooling2D((2, 2)))
model.add(layers.Conv2D(128, (3, 3), activation='relu'))
model.add(layers.MaxPooling2D((2, 2)))
model.add(layers.Conv2D(128, (3, 3), activation='relu'))
model.add(layers.MaxPooling2D((2, 2)))
model.add(layers.Flatten())
model.add(layers.Dense(512, activation='relu'))
model.add(layers.Dense(1, activation='sigmoid'))
model.summary()
数据预处理:
from keras import optimizersmodel.compile(loss='binary_crossentropy',optimizer=optimizers.RMSprop(lr=1e-4),metrics=['acc'])
from keras.preprocessing.image import ImageDataGenerator# All images will be rescaled by 1./255
train_datagen = ImageDataGenerator(rescale=1./255)
test_datagen = ImageDataGenerator(rescale=1./255)train_generator = train_datagen.flow_from_directory(# This is the target directorytrain_dir,# All images will be resized to 150x150target_size=(150, 150),batch_size=20,# Since we use binary_crossentropy loss, we need binary labelsclass_mode='binary')validation_generator = test_datagen.flow_from_directory(validation_dir,target_size=(150, 150),batch_size=20,class_mode='binary')
for data_batch, labels_batch in train_generator:print('data batch shape:', data_batch.shape)print('labels batch shape:', labels_batch.shape)break
开始训练:
history = model.fit_generator(train_generator,steps_per_epoch=100,epochs=30,validation_data=validation_generator,validation_steps=50)
保存模型:
model.save('D:/mask1/masks_and_unmasks_small_1.h5')
绘制模型的损失和准确性:
import matplotlib.pyplot as plt
acc = history.history['acc']
val_acc = history.history['val_acc']
loss = history.history['loss']
val_loss = history.history['val_loss']
epochs = range(len(acc))
plt.plot(epochs, acc, 'bo', label='Training acc')
plt.plot(epochs, val_acc, 'b', label='Validation acc')
plt.title('Training and validation accuracy')
plt.legend()
plt.figure()
plt.plot(epochs, loss, 'bo', label='Training loss')
plt.plot(epochs, val_loss, 'b', label='Validation loss')
plt.title('Training and validation loss')
plt.legend()
plt.show()
数据增强:
datagen = ImageDataGenerator(rotation_range=40,width_shift_range=0.2,height_shift_range=0.2,shear_range=0.2,zoom_range=0.2,horizontal_flip=True,fill_mode='nearest')
为了进一步对抗过拟合,我们还将在我们的模型中增加一个Dropout层:
model = models.Sequential()
model.add(layers.Conv2D(32, (3, 3), activation='relu',input_shape=(150, 150, 3)))
model.add(layers.MaxPooling2D((2, 2)))
model.add(layers.Conv2D(64, (3, 3), activation='relu'))
model.add(layers.MaxPooling2D((2, 2)))
model.add(layers.Conv2D(128, (3, 3), activation='relu'))
model.add(layers.MaxPooling2D((2, 2)))
model.add(layers.Conv2D(128, (3, 3), activation='relu'))
model.add(layers.MaxPooling2D((2, 2)))
model.add(layers.Flatten())
model.add(layers.Dropout(0.5))
model.add(layers.Dense(512, activation='relu'))
model.add(layers.Dense(1, activation='sigmoid'))model.compile(loss='binary_crossentropy',optimizer=optimizers.RMSprop(lr=1e-4),metrics=['acc'])
训练:
train_datagen = ImageDataGenerator(rescale=1./255,rotation_range=40,width_shift_range=0.2,height_shift_range=0.2,shear_range=0.2,zoom_range=0.2,horizontal_flip=True,)
# Note that the validation data should not be augmented!
test_datagen = ImageDataGenerator(rescale=1./255)
train_generator = train_datagen.flow_from_directory(# This is the target directorytrain_dir,# All images will be resized to 150x150target_size=(150, 150),batch_size=32,# Since we use binary_crossentropy loss, we need binary labelsclass_mode='binary')
validation_generator = test_datagen.flow_from_directory(validation_dir,target_size=(150, 150),batch_size=32,class_mode='binary')
history = model.fit_generator(train_generator,steps_per_epoch=100,epochs=50,validation_data=validation_generator,validation_steps=50)
保存模型:
model.save('D:/mask1/masks_and_unmasks_small_2.h5')
绘制模型的损失和准确性:
acc = history.history['acc']
val_acc = history.history['val_acc']
loss = history.history['loss']
val_loss = history.history['val_loss']
epochs = range(len(acc))
plt.plot(epochs, acc, 'bo', label='Training acc')
plt.plot(epochs, val_acc, 'b', label='Validation acc')
plt.title('Training and validation accuracy')
plt.legend()
plt.figure()
plt.plot(epochs, loss, 'bo', label='Training loss')
plt.plot(epochs, val_loss, 'b', label='Validation loss')
plt.title('Training and validation loss')
plt.legend()
plt.show()
五、摄像头实时识别是否带口罩
#检测视频或者摄像头中的人脸
import cv2
from keras.preprocessing import image
from keras.models import load_model
import numpy as np
import dlib
from PIL import Image
model = load_model('D:/mask1/masks_and_unmasks_small_2.h5')
detector = dlib.get_frontal_face_detector()
video=cv2.VideoCapture(0)
font = cv2.FONT_HERSHEY_SIMPLEX
def rec(img):gray=cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)dets=detector(gray,1)if dets is not None:for face in dets:left=face.left()top=face.top()right=face.right()bottom=face.bottom()cv2.rectangle(img,(left,top),(right,bottom),(0,255,0),2)img1=cv2.resize(img[top:bottom,left:right],dsize=(150,150))img1=cv2.cvtColor(img1,cv2.COLOR_BGR2RGB)img1 = np.array(img1)/255.img_tensor = img1.reshape(-1,150,150,3)prediction =model.predict(img_tensor) if prediction[0][0]>0.5:result='unmask'else:result='mask'cv2.putText(img, result, (left,top), font, 2, (0, 255, 0), 2, cv2.LINE_AA)cv2.imshow('Video', img)
while video.isOpened():res, img_rd = video.read()if not res:breakrec(img_rd)if cv2.waitKey(5) & 0xFF == ord('q'):break
video.release()
cv2.destroyAllWindows()