参考 : https://github.com/AnirudhGP/DrowsyDriverDetection
眼睛的睁闭
对眼睛进行预处理,得到相应的验证集与测试集,将其用pickle保存起来
import matplotlib.pyplot as plt
import numpy as np
import os
from six.moves import cPickle as pickle
import cv2
dirs = ['Dataset/openLeftEyes', 'Dataset/openRightEyes']
dirs2 = ['Dataset/closedLeftEyes', 'Dataset/closedRightEyes']
def generate_dataset():
dataset = np.ndarray([1231 * 2, 24, 24, 1], dtype='float32')
i = 0
for dir in dirs:
for filename in os.listdir(dir):
if filename.endswith('.jpg'):
im = cv2.imread(dir + '/' + filename)
im = np.dot(np.array(im, dtype='float32'), [[0.2989], [0.5870], [0.1140]]) / 255
dataset[i, :, :, :] = im[:, :, :]
i += 1
labels = np.ones([len(dataset), 1], dtype=int)
return dataset, labels
def generate_dataset_closed():
dataset = np.ndarray([1192 * 2, 24, 24, 1], dtype='float32')
i = 0
for dir in dirs2:
for filename in os.listdir(dir):
if filename.endswith('.jpg'):
im = cv2.imread(dir + '/' + filename)
im = np.dot(np.array(im, dtype='float32'), [[0.2989], [0.5870], [0.1140]]) / 255
dataset[i, :, :, :] = im[:, :, :]
i += 1
labels = np.zeros([len(dataset), 1], dtype=int)
return dataset, labels
dataset_open, labels_open = generate_dataset()
dataset_closed, labels_closed = generate_dataset_closed()
print("done")
split = int(len(dataset_closed) * 0.8)
train_dataset_closed = dataset_closed[:split]
train_labels_closed = labels_closed[:split]
test_dataset_closed = dataset_closed[split:]
test_labels_closed = labels_closed[split:]
pickle_file = 'closed_eyes.pickle'
try:
f = open(pickle_file, 'wb')
save = {
'train_dataset': train_dataset_closed,
'train_labels': train_labels_closed,
'test_dataset': test_dataset_closed,
'test_labels': test_labels_closed,
}
pickle.dump(save, f, pickle.HIGHEST_PROTOCOL)
f.close()
except Exception as e:
print('Unable to save data to', pickle_file, ':', e)
raise
statinfo = os.stat(pickle_file)
print('Compressed pickle size:', statinfo.st_size)
split = int(len(dataset_open) * 0.8)
train_dataset_open = dataset_open[:split]
train_labels_open = labels_open[:split]
test_dataset_open = dataset_open[split:]
test_labels_open = labels_open[split:]
pickle_file = 'open_eyes.pickle'
try:
f = open(pickle_file, 'wb')
save = {
'train_dataset': train_dataset_open,
'train_labels': train_labels_open,
'test_dataset': test_dataset_open,
'test_labels': test_labels_open,
}
pickle.dump(save, f, pickle.HIGHEST_PROTOCOL)
f.close()
except Exception as e:
print('Unable to save data to', pickle_file, ':', e)
raise
statinfo = os.stat(pickle_file)
print('Compressed pickle size:', statinfo.st_size)
用卷积神经网络进行训练
from __future__ import absolute_import
from __future__ import print_function
import numpy as np
import os
from keras.utils import plot_model
import cv2
np.random.seed(1337) # for reproducibility
from keras.models import Sequential
from keras.layers.core import Dense, Dropout, Activation, Flatten
from keras.layers.convolutional import Convolution2D, MaxPooling2D
from keras.utils import np_utils
from keras.optimizers import SGD, Adadelta, Adagrad
from six.moves import cPickle as pickle
import tensorflow as tf
sess = tf.Session(config=tf.ConfigProto(log_device_placement=True))
# os.environ["PATH"] += os.pathsep + 'C:/Program Files (x86)/Graphviz2.38/bin/'
pickle_files = ['open_eyes.pickle', 'closed_eyes.pickle']
i = 0
for pickle_file in pickle_files:
with open(pickle_file, 'rb') as f:
save = pickle.load(f)
if i == 0:
train_dataset = save['train_dataset']
train_labels = save['train_labels']
test_dataset = save['test_dataset']
test_labels = save['test_labels']
else:
print("here")
train_dataset = np.concatenate((train_dataset, save['train_dataset']))
train_labels = np.concatenate((train_labels, save['train_labels']))
test_dataset = np.concatenate((test_dataset, save['test_dataset']))
test_labels = np.concatenate((test_labels, save['test_labels']))
del save # hint to help gc free up memory
i += 1
print('Training set', train_dataset.shape, train_labels.shape)
print('Test set', test_dataset.shape, test_labels.shape)
batch_size = 30
nb_classes = 1
epochs = 12
X_train = train_dataset
X_train = X_train.reshape((X_train.shape[0], X_train.shape[3]) + X_train.shape[1:3]) #(3876, 1, 24, 24)
Y_train = train_labels
X_test = test_dataset
X_test = X_test.reshape((X_test.shape[0], X_test.shape[3]) + X_test.shape[1:3])
Y_test = test_labels
# print shape of data while model is building
print("{1} train samples, {4} channel{0}, {2}x{3}".format("" if X_train.shape[1] == 1 else "s", *X_train.shape))
print("{1} test samples, {4} channel{0}, {2}x{3}".format("" if X_test.shape[1] == 1 else "s", *X_test.shape))
# input image dimensions
_, img_channels, img_rows, img_cols = X_train.shape
# convert class vectors to binary class matrices
# Y_train = np_utils.to_categorical(y_train, nb_classes)
# Y_test = np_utils.to_categorical(y_test, nb_classes)
model = Sequential()
model.add(Convolution2D(32, (3, 3), padding='same',
input_shape=(img_channels, img_rows, img_cols),data_format='channels_first'))
model.add(Activation('relu'))
model.add(Convolution2D(24, (3, 3), data_format='channels_first'),)
model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Dropout(0.25))
model.add(Convolution2D(64, (3, 3), padding='same', data_format='channels_first'))
model.add(Activation('relu'))
model.add(Convolution2D(64, (3, 3)))
model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Dropout(0.25))
model.add(Flatten())
model.add(Dense(512))
model.add(Activation('relu'))
model.add(Dropout(0.5))
model.add(Dense(nb_classes))
model.add(Activation('sigmoid'))
# let's train the model using SGD + momentum (how original).
sgd = SGD(lr=0.01, decay=1e-6, momentum=0.9, nesterov=True)
model.compile(loss='binary_crossentropy', optimizer=sgd, metrics=['accuracy'])
model.fit(X_train, Y_train, batch_size=batch_size, epochs=epochs, verbose=2, validation_data=(X_test, Y_test))
model.save('eye_model.h5')
score = model.evaluate(X_test, Y_test, verbose=1)
print('Test score:', score[0])
print('Test accuracy:', score[1])
测试代码
#test code
import cv2
import numpy as np
im = cv2.imread('test.jpg')
im = np.dot(np.array(im, dtype='float32'), [[0.2989], [0.5870], [0.1140]]) / 255
im=im.reshape(1,24,24)
im=np.expand_dims(im,0)
print(im.shape)
#
#model.predict(im, batch_size=1, verbose=1, steps=None)
from keras.models import load_model
my_model = load_model('eye_model.h5')
pre=my_model.predict(im)
print(pre)