笑脸和口罩数据集的划分,训练和测试
准备工作
我使用的环境是Python3.7.8搭配VSCode,Python现在已经出了3.8版本的,但是由于版本太新,安装dlib需要编译,不能直接安,使用退而求其次选择了3.7.8版本。
首先需要安装dlib,Keras,TensorFlow,OpenCV等库。
打开Windows PowerShell,输入以下命令来安装必要的库。
python -m pip install -i https://mirrors.aliyun.com/pypi/simple/ --user numpy
python -m pip install -i https://mirrors.aliyun.com/pypi/simple/ --user scipy
python -m pip install -i https://mirrors.aliyun.com/pypi/simple/ --user keras
python -m pip install -i https://mirrors.aliyun.com/pypi/simple/ --user opencv-python
dlib我使用的是直接下载whl文件放在硬盘中安装的方法。
文件链接:dlib-19.17.99-cp37-cp37m-win_amd64.whl
提取码:87qi
注:这个文件仅适用于Python3.7,其他版本会报错
然后在Windows PowerShell中使用如下命令安装
pip install D:\BaiduNetdiskDownload\dlib-19.17.99-cp37-cp37m-win_amd64.whl
后面的路径随你电脑中的保存路径更改。
TensorFlow的安装分两种,CPU版本和GPU版本,如果你的电脑中使用的不是英伟达的显卡,或者比较怕麻烦,那么就安装CPU版本,在Windows PowerShell中输入如下命令就可以完成安装
python -m pip install -i https://mirrors.aliyun.com/pypi/simple/ --user tensorflow
安装GPU版的TensorFlow相对来说要麻烦不少,不过程序运行速度会快非常多,同样的数据集训练,用CPU算需要900多秒,期间CPU跑满100%,而用GPU算的话只需要90秒不到。
不过安装GPU版的TensorFlow需要先安装配置下面几个
CUDA CUDA Toolkit 10.1 update2 Archive
cuDNN NVIDIA cuDNN
据我了解,目前TensorFlow好像只支持到CUDA10.1,所以好这里放的是CUDA10.1的下载链接。下载后直接安装即可。而下载cuDNN需要注册英伟达开发者,不过还是比较简单的,用英伟达账号登录后填一些信息就可以了。
按照TensorFlow官网的教程,在下载了这两个工具后还要进行一定的配置,CUDA下载后直接安装,并将下面三个路径添加到系统变量的Path中,全程默认安装的话路径是不会有变化的。
C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v10.1\bin
C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v10.1\extras\CUPTI\lib64
C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v10.1\include
cuDNN下载下来是一个压缩包,将其解压到C:\tools文件夹下,然后将这个路径也添加进系统变量的Path中
C:\tools\cuda\bin
完成这些准备工作后,在Windows PowerShell中输入如下命令就可以安装TensorFlow的GPU版了
python -m pip install -i https://mirrors.aliyun.com/pypi/simple/ --user tensorflow-gpu
笑脸数据集
首先下载笑脸数据集:
smile-detection-master.zip 提取码:2a4o
genki4k.tar 提取码:8vyt
两个数据集有相当多的重复,不过下面那个数据量要更大一些,缺点是没有将笑与不笑划分开单独的文件夹,需要自己找到两个表情的划分。所以我以第一个链接为主。
下载好之后解压保存:
划分数据集
import keras
import os, shutil
# The path to the directory where the original
# dataset was uncompressed
original_dataset_dir = 'D:\\python\\smask\\smile-detection-master\\datasets\\train_folder'
# The directory where we will
# store our smaller dataset
base_dir = 'D:\\python\\smask\\smile'
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文件夹里面的图片放入相应的文件夹
构建卷积神经网络并训练
import os, shutil
from keras import layers
from keras import models
from keras import optimizers
from keras.preprocessing.image import ImageDataGenerator
base_dir = 'D:\\python\\smask\\smile'
train_dir = 'D:\\python\\smask\\smile\\train'
validation_dir = 'D:\\python\\smask\\smile\\validation'
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()
model.compile(loss='binary_crossentropy',
optimizer=optimizers.RMSprop(lr=1e-4),
metrics=['acc'])
# 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 directory
train_dir,
# All images will be resized to 150x150
target_size=(150, 150),
batch_size=20,
# Since we use binary_crossentropy loss, we need binary labels
class_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:\\python\\smask\\smile\\smiles_and_unsmiles_small_1.h5')
由于这样子训练出来的模型过拟合比较严重,所以还需要进一步处理:数据增强并增加一个Dropout层
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')
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 directory
train_dir,
# All images will be resized to 150x150
target_size=(150, 150),
batch_size=32,
# Since we use binary_crossentropy loss, we need binary labels
class_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=30,
validation_data=validation_generator,
validation_steps=50)
model.save('D:\\python\\smask\\smile\\smiles_and_unsmiles_small_2.h5')
摄像头判别笑脸
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:\\python\\smask\\smile\\smiles_and_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:
break
rec(img_rd)
if cv2.waitKey(5) & 0xFF == ord('q'):
break
video.release()
cv2.destroyAllWindows()
这里就请我们敬爱的局座出面客串一下演示效果
口罩数据集
首先下载口罩数据集
这个数据集虽然非常全面,但也有个缺点,就是里面分了多个文件夹,不好进行归档,所以在这里我也把我自己归档重命名过的一个数据集分享出来,图片均为上面链接中的RWMFD_part_1中的图片
mask.zip提取码:kbzz
然后就是划分,操作和上面是一模一样的
import keras
import os, shutil
# The path to the directory where the original
# dataset was uncompressed
original_dataset_dir = 'D:\\python\\smask\\mask\\train'
# The directory where we will
# store our smaller dataset
base_dir = 'D:\\python\\smask\\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)
构建卷积神经网络并训练,数据增强并增加一个Dropout层后再进行一次训练
from keras import layers
from keras import models
from keras import optimizers
from keras.preprocessing.image import ImageDataGenerator
base_dir = 'D:\\python\\smask\\mask1'
train_dir = 'D:\\python\\smask\\mask1\\train'
validation_dir = 'D:\\python\\smask\\mask1\\validation'
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()
model.compile(loss='binary_crossentropy',
optimizer=optimizers.RMSprop(lr=1e-4),
metrics=['acc'])
# 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 directory
train_dir,
# All images will be resized to 150x150
target_size=(150, 150),
batch_size=20,
# Since we use binary_crossentropy loss, we need binary labels
class_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:/python/smask/mask1/masks_and_unmasks_small_1.h5')
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')
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 directory
train_dir,
# All images will be resized to 150x150
target_size=(150, 150),
batch_size=32,
# Since we use binary_crossentropy loss, we need binary labels
class_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:/python/smask/mask1/masks_and_unmasks_small_2.h5')
摄像头判别是否佩戴口罩
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:/python/smask/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:
break
rec(img_rd)
if cv2.waitKey(5) & 0xFF == ord('q'):
break
video.release()
cv2.destroyAllWindows()
运行效果
上一篇: POJ 3176 Cow Bowling
下一篇: MySql引擎