├── .gitignore ├── README.md ├── checkpoint ├── iteration.50000.ckpt └── ver1.0_iteration.64000.ckpt ├── collect_glasses_data.py ├── eye_region_dataset ├── dataset_size_check.py ├── eye_detection.py ├── eye_extractor.py └── eye_recording.py ├── face_load_dataset.py ├── face_model.py ├── getting_glasses_region.py ├── haarcascade_eye.xml ├── haarcascade_frontalface_default.xml ├── inference.py ├── load_dataset.py ├── loss_curve.csv ├── model.py ├── only_eye_region ├── readme.md └── readme.txt └── test.py /.gitignore: -------------------------------------------------------------------------------- 1 | *.jpeg 2 | 3 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # *Deep RDD* : Realtime Drowsiness Detector given user face image stream. 2 | Detecting whether driver is in drowsiness or not using Convoluitional neural network. 3 | 4 | You can see demo video at : 5 | # Training Usage 6 | if you want to train this model,(you might need tensorflow & python-OpenCV dependancy) 7 | ```bash 8 | python model.py 9 | ``` 10 | # Running Realtime drowsiness detector 11 | if you want to run RDD, 12 | ```bash 13 | python inference.py 14 | ``` 15 | -------------------------------------------------------------------------------- /checkpoint/iteration.50000.ckpt: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/seilna/Deep-RDD/747b6675b870fe26235c596277badd77ca7d5bf3/checkpoint/iteration.50000.ckpt -------------------------------------------------------------------------------- /checkpoint/ver1.0_iteration.64000.ckpt: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/seilna/Deep-RDD/747b6675b870fe26235c596277badd77ca7d5bf3/checkpoint/ver1.0_iteration.64000.ckpt -------------------------------------------------------------------------------- /collect_glasses_data.py: -------------------------------------------------------------------------------- 1 | import glob 2 | import cv2 3 | import load_dataset 4 | import numpy as np 5 | import tensorflow as tf 6 | import sys 7 | from getting_glasses_region import glasses_region 8 | from getting_glasses_region import relative_region 9 | # loading opencv cascade model for detecting face and eye regions. 10 | face_cascade = cv2.CascadeClassifier("./haarcascade_frontalface_default.xml") 11 | eye_cascade = cv2.CascadeClassifier("./haarcascade_eye.xml") 12 | 13 | 14 | """ 15 | CNN models definition. 16 | models consist of 9 convolution layers, 17 | 3 pooling layers, 18 | 2 fc layers. 19 | """ 20 | 21 | IM_SIZE = 32 22 | BATCH_SIZE = 100 23 | WINDOW_SIZE = 4 24 | 25 | """ 26 | There are 2 inference mode. 27 | one is usaul state, 28 | another is wearing glasses state. 29 | you might change mode executing program with giving options. 30 | """ 31 | glasses = False 32 | 33 | 34 | """ 35 | Determines model's sensitivity. (3~5 recommended.) 36 | """ 37 | if len(sys.argv) == 2: 38 | WINDOW_SIZE = int(sys.argv[1]) 39 | 40 | if len(sys.argv) == 3: 41 | assert sys.argv[2] == "glasses", "invalid option used." 42 | glasses = True 43 | 44 | assert len(sys.argv) < 4, "Too many options given." 45 | print "model sensitivity >> %d" % WINDOW_SIZE 46 | 47 | sess = tf.InteractiveSession() 48 | def weight_variable(shape): 49 | initial = tf.truncated_normal(shape, stddev=0.05) 50 | return tf.Variable(initial) 51 | 52 | def bias_variable(shape): 53 | initial = tf.constant(0.1, shape=shape) 54 | return tf.Variable(initial) 55 | 56 | def conv2d(x, W): 57 | return tf.nn.conv2d(x, W, strides=[1,1,1,1], padding='SAME') 58 | 59 | def max_pool_2x2(x): 60 | return tf.nn.max_pool(x, ksize=[1,2,2,1], strides=[1,2,2,1], padding='SAME') 61 | 62 | x = tf.placeholder(tf.float32, shape=[None,IM_SIZE,IM_SIZE,3]) 63 | y_ = tf.placeholder(tf.float32, shape=[None,2]) 64 | keep_prob = tf.placeholder(tf.float32) 65 | 66 | x_reshape = tf.reshape(x, [-1,IM_SIZE,IM_SIZE,3]) 67 | 68 | W_conv1 = weight_variable([5,5,3,16]) 69 | b_conv1 = bias_variable([16]) 70 | h_conv1 = tf.nn.relu(conv2d(x_reshape, W_conv1) + b_conv1) 71 | 72 | W_conv2 = weight_variable([5,5,16,16]) 73 | b_conv2 = bias_variable([16]) 74 | h_conv2 = tf.nn.relu(conv2d(h_conv1, W_conv2) + b_conv2) 75 | 76 | W_conv3 = weight_variable([5,5,16,16]) 77 | b_conv3 = bias_variable([16]) 78 | h_conv3 = tf.nn.relu(conv2d(h_conv2, W_conv3) + b_conv3) 79 | 80 | 81 | """ 82 | 1 pooling 83 | """ 84 | h_pool3 = max_pool_2x2(h_conv3) 85 | 86 | 87 | W_conv4 = weight_variable([5,5,16,32]) 88 | b_conv4 = bias_variable([32]) 89 | h_conv4 = tf.nn.relu(conv2d(h_pool3, W_conv4) + b_conv4) 90 | 91 | W_conv5 = weight_variable([5,5,32,32]) 92 | b_conv5 = bias_variable([32]) 93 | h_conv5 = tf.nn.relu(conv2d(h_conv4, W_conv5) + b_conv5) 94 | 95 | W_conv6 = weight_variable([5,5,32,32]) 96 | b_conv6 = bias_variable([32]) 97 | h_conv6 = tf.nn.relu(conv2d(h_conv5, W_conv6) + b_conv6) 98 | 99 | 100 | """ 101 | 2 pooling 102 | """ 103 | h_pool6 = max_pool_2x2(h_conv6) 104 | 105 | W_conv7 = weight_variable([5,5,32,64]) 106 | b_conv7 = bias_variable([64]) 107 | h_conv7 = tf.nn.relu(conv2d(h_pool6, W_conv7) + b_conv7) 108 | 109 | W_conv8 = weight_variable([5,5,64,64]) 110 | b_conv8 = weight_variable([64]) 111 | h_conv8 = tf.nn.relu(conv2d(h_conv7, W_conv8) + b_conv8) 112 | 113 | W_conv9 = weight_variable([5,5,64,64]) 114 | b_conv9 = bias_variable([64]) 115 | h_conv9 = tf.nn.relu(conv2d(h_conv8, W_conv9) + b_conv9) 116 | 117 | 118 | """ 119 | 3 pooling 120 | """ 121 | h_pool9 = max_pool_2x2(h_conv9) 122 | h_pool9_flat = tf.reshape(h_pool9, [-1, (IM_SIZE/8)*(IM_SIZE/8)*64]) 123 | 124 | 125 | 126 | W_fc1 = weight_variable([(IM_SIZE/8)*(IM_SIZE/8)*64, 1024]) 127 | b_fc1 = bias_variable([1024]) 128 | h_fc1 = tf.nn.relu(tf.matmul(h_pool9_flat, W_fc1) + b_fc1) 129 | h_fc1_drop = tf.nn.dropout(h_fc1, keep_prob) 130 | 131 | W_fc2 = weight_variable([1024,1024]) 132 | b_fc2 = bias_variable([1024]) 133 | h_fc2 = tf.nn.relu(tf.matmul(h_fc1_drop, W_fc2) + b_fc2) 134 | h_fc2_drop = tf.nn.dropout(h_fc2, keep_prob) 135 | 136 | W_fc3 = weight_variable([1024,2]) 137 | b_fc3 = bias_variable([2]) 138 | 139 | y_matmul = tf.matmul(h_fc2_drop, W_fc3) + b_fc3 140 | 141 | y_conv = tf.nn.softmax(y_matmul) 142 | 143 | l2_loss = tf.reduce_mean(-tf.reduce_sum(y_*tf.log(y_conv+1e-7), reduction_indices=[1])) 144 | train_step = tf.train.AdamOptimizer(1e-3).minimize(l2_loss) 145 | correct_prediction = tf.equal(tf.argmax(y_conv, 1), tf.argmax(y_, 1)) 146 | accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32)) 147 | tf.initialize_all_variables().run() 148 | 149 | saver = tf.train.Saver() 150 | SAVE_PATH = "./checkpoint/ver1.0_iteration.64000.ckpt" 151 | saver.restore(sess, SAVE_PATH) 152 | 153 | 154 | """ 155 | getting realtime video of user, 156 | detecting whether eye is closed or not using trained CNN models. 157 | """ 158 | cap = cv2.VideoCapture(0) 159 | #cap = cv2.VideoCapture("/Users/naseil/Desktop/original.mov") 160 | prev_face = [(0,0,30,30)] 161 | prev_eyes = [(1,1,1,1), (1,1,1,1)] 162 | drowsiness_check_list = [0] * WINDOW_SIZE 163 | drowsiness_check_idx = 0 164 | 165 | 166 | def rotate_check(face_size): 167 | face_size /= 10 168 | if not hasattr(rotate_check, "full_count"): 169 | rotate_check.full_count = 0 170 | if not hasattr(rotate_check, "size_distribution"): 171 | rotate_check.size_distribution = [0] * 5000 172 | rotate_check.full_count += 1 173 | rotate_check.size_distribution[face_size/100] += 1 174 | 175 | percentage = rotate_check.size_distribution[face_size/100] * 100 / rotate_check.full_count 176 | if percentage < 10: 177 | return False 178 | rotate_check.size_distribution[face_size/100] += 1 179 | return True 180 | 181 | relative_region_list = [] 182 | if glasses == True: 183 | relative_region_list = relative_region() 184 | print relative_region_list 185 | continuous_error_count = 0 186 | total_cnt = 0 187 | error_classified_cnt = 0 188 | 189 | error_path = "./errorounes_classified/" 190 | error_file_num = 0 191 | while True: 192 | ret, frame = cap.read() 193 | total_cnt += 1 194 | frame = cv2.resize(frame,(250,250)) 195 | gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) 196 | face = face_cascade.detectMultiScale(gray, 1.1, 3) 197 | 198 | error_check = False 199 | if len(face) != 1: 200 | face=prev_face 201 | error_check = True 202 | continuous_error_count += 1 203 | else: 204 | continuous_error_count = 0 205 | prev_face = face 206 | for a,b,w,h in face: 207 | face_size = w*h 208 | prev_face = face 209 | roi_gray = gray[b:b+h, a:a+w] 210 | roi_color = frame[b:b+h, a:a+w] 211 | eyes = [] 212 | if glasses == False: 213 | eyes = eye_cascade.detectMultiScale(roi_gray) 214 | elif glasses == True and error_check == False: 215 | eyes = glasses_region(face, relative_region_list) 216 | if len(eyes) != 2: 217 | error_check = True 218 | eyes = prev_eyes 219 | continuous_error_count += 1 220 | else: 221 | continuous_error_count = 0 222 | prev_eyes = eyes 223 | 224 | cv2.rectangle(frame, (a,b), (a+w,b+h), (255,0,0), 1) 225 | eye_full_cnt = 0 226 | for f_ex,f_ey,f_ew,f_eh in eyes: 227 | ex, ey, ew, eh = int(f_ex), int(f_ey), int(f_ew), int(f_eh) 228 | eye_region_image = roi_color[ey:ey+eh, ex:ex+ew] 229 | prev_eyes = eyes 230 | p,q,r = eye_region_image.shape 231 | if p==0 or q==0 : break 232 | input_images = cv2.resize(eye_region_image, (32,32)) 233 | input_images.resize((1,32,32,3)) 234 | 235 | input_images = np.divide(input_images, 255.0) 236 | 237 | # Detecting drowsiness using CNN models. 238 | label = sess.run(tf.argmax(y_conv, 1), feed_dict={keep_prob:1.0, x:input_images}) 239 | drowsiness_check_list[drowsiness_check_idx%WINDOW_SIZE] = label[0] 240 | drowsiness_check_idx += 1 241 | print drowsiness_check_list 242 | # if drowsiness if detected, 243 | # imaegs will be shown with red boxing. 244 | if rotate_check(face_size) == True and continuous_error_count < 5 and drowsiness_check_list == [1]*WINDOW_SIZE: 245 | cv2.rectangle(roi_color, (int(ex),int(ey)), (int(ex+ew), int(ey+eh)), (0,255,0), 1) 246 | eye_full_cnt += 1 247 | p,q,r = frame.shape 248 | error_classified_cnt += 1 249 | error_file_num += 1 250 | error_file_name = error_path + str(error_file_num) + ".jpeg" 251 | #cv2.imwrite(error_file_name, frame) 252 | 253 | """ 254 | for i in xrange(p): 255 | for j in xrange(q): 256 | frame[i,j,2] = 150 257 | """ 258 | elif rotate_check(face_size) == True: 259 | cv2.rectangle(roi_color, (int(ex),int(ey)), (int(ex+ew), int(ey+eh)), (0,255,0), 1) 260 | if eye_full_cnt == 2: 261 | p,q,r = frame.shape 262 | for i in xrange(p): 263 | for j in xrange(q): 264 | frame[i,j,2] = 150 265 | 266 | cv2.imshow("Deep-CNN", frame) 267 | if cv2.waitKey(1) & 0xFF == ord('q'):break 268 | cap.release() 269 | cv2.destroyAllWindows() 270 | 271 | 272 | -------------------------------------------------------------------------------- /eye_region_dataset/dataset_size_check.py: -------------------------------------------------------------------------------- 1 | import glob 2 | 3 | total = 0 4 | dir_list = glob.glob('./snapshot_dir/*') 5 | for dir in dir_list: 6 | file_list = glob.glob(dir+'/*') 7 | print 'len >> ', 8 | print len(file_list) 9 | print dir 10 | total += len(file_list) 11 | 12 | print 'total training dataset >>', 13 | print total 14 | -------------------------------------------------------------------------------- /eye_region_dataset/eye_detection.py: -------------------------------------------------------------------------------- 1 | import cv2 2 | import numpy as np 3 | import sys, getopt 4 | 5 | options = getopt.getopt(sys.argv[1:], shortopts=None, longopts=["glasses"]) 6 | op_list = options[0] 7 | 8 | snapshot_dir = './snapshot/' 9 | snapshot_index = 0 10 | 11 | face_cascade = cv2.CascadeClassifier('/usr/local/Cellar/opencv/2.4.12_2/data/haarcascades/haarcascade_frontalface_default.xml') 12 | 13 | eye_cascade = cv2.CascadeClassifier('/usr/local/Cellar/opencv/2.4.12_2/data/haarcascades/haarcascade_eye.xml') 14 | 15 | 16 | assert len(op_list) >= 1, 'please check the usage again. You should add the option [--glasses]' 17 | for op, args in op_list: 18 | print op, args 19 | if op == '--glasses' : 20 | if args == 'True': 21 | eye_cascade = cv2.CascadeClassifier('/usr/local/Cellar/opencv/2.4.12_2/data/haarcascades/haarcascade_eye_tree_eyeglasses.xml') 22 | else : 23 | print 'please check the Usage. options are [--glasses ]' 24 | 25 | cap = cv2.VideoCapture(0) 26 | idx = 0 27 | 28 | while True: 29 | idx += 1 30 | ret, frame = cap.read() 31 | 32 | frame = cv2.resize(frame, (500, 500)) 33 | gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) 34 | 35 | #gray = cv2.resize(gray, (255,255)) 36 | 37 | face = face_cascade.detectMultiScale(gray, 1.1, 3) 38 | if len(face) == 0 : 39 | print 'in %dth snapshot, face not detected...!!' % idx 40 | continue 41 | else: 42 | print '%dth snapshot spark!!! ' % idx 43 | 44 | for x,y,w,h in face: 45 | roi_gray = frame[y:y+h, x:x+w] 46 | eyes = eye_cascade.detectMultiScale(roi_gray) 47 | if len(eyes) != 2: continue 48 | for ex,ey,ew,eh in eyes: 49 | cv2.rectangle(roi_gray, (ex,ey), (ex+ew, ey+eh), (0,255,0), 2) 50 | cv2.imshow('frame',frame) 51 | if cv2.waitKey(1) & 0xFF == ord('q'): 52 | break 53 | 54 | cap.release() 55 | cv2.destroyAllWindows() 56 | -------------------------------------------------------------------------------- /eye_region_dataset/eye_extractor.py: -------------------------------------------------------------------------------- 1 | import cv2 2 | import glob 3 | import numpy as np 4 | face_module_path = '/usr/local/share/OpenCV/haarcascades/' 5 | 6 | face_cascade = cv2.CascadeClassifier(face_module_path + 'haarcascade_frontalface_default.xml') 7 | eye_cascade = cv2.CascadeClassifier(face_module_path + 'haarcascade_eye_tree_eyeglasses.xml') 8 | 9 | file_path = './usaul_dataset/' 10 | file_idx = 0 11 | image_dir_list = glob.glob('./snapshot_dir/*') 12 | np.random.shuffle(image_dir_list) 13 | 14 | for image_dir in image_dir_list: 15 | if image_dir.find('drowsiness') != -1 : continue 16 | print 'image directory >> ' + image_dir 17 | image_list = glob.glob(image_dir + '/*') 18 | 19 | prev_faces = None 20 | prev_eyes = None 21 | 22 | length = len(image_list) 23 | 24 | 25 | for index in xrange(length): 26 | draw_flag = False 27 | file_name = image_dir +'/' + str(index) + '.jpeg' 28 | im = cv2.imread(file_name) 29 | 30 | print im.shape 31 | face = face_cascade.detectMultiScale(im, 1.1,5) 32 | 33 | if len(face) > 0 : draw_flag = True 34 | 35 | for x,y,w,h in face: 36 | prev_faces = face 37 | face_region = im[y:y+h, x:x+w] 38 | eyes = eye_cascade.detectMultiScale(face_region) 39 | 40 | cv2.imshow('face_region', face_region) 41 | cv2.waitKey() 42 | """ 43 | cv2.rectangle(face_region, (x,y), (x+w, y+h), (255,0,0), 2) 44 | if len(eyes) != 2: continue 45 | prev_eyes = eyes 46 | for ex,ey,ew,eh in eyes: 47 | draw_flag = True 48 | cv2.rectangle(face_region, (ex,ey), (ex+ew, ey+eh), (0,255,0), 2) 49 | #cv2.imshow('face', face_region) 50 | #cv2.imshow('dasdas', face_region[ey:ey+eh, ex:ex+ew]) 51 | #cv2.waitKey() 52 | 53 | output_file_name = file_path + str(file_idx) + '.jpeg' 54 | print 'output file name >> ' + output_file_name 55 | #cv2.imwrite(output_file_name, face_region[ey:ey+eh, ex:ex+ew]) 56 | file_idx += 1 57 | #output_file_name = image_dir + 'extracted_' + str(index) + '.jpeg' 58 | 59 | """ 60 | if draw_flag == True: 61 | break 62 | 63 | if draw_flag == False and prev_faces != None: 64 | for x,y,w,h in prev_faces: 65 | 66 | face_region = im[y:y+h, x:x+w] 67 | 68 | cv2.imshow('face_region', face_region) 69 | cv2.waitKey() 70 | """ 71 | for ex,ey,ew,eh in prev_eyes: 72 | 73 | output_file_name = file_path + str(file_idx) + '.jpeg' 74 | cv2.imwrite(output_file_name, face_region[ey:ey+eh, ex:ex+ew]) 75 | file_idx += 1 76 | 77 | #cv2.rectangle(face_region, (ex,ey), (ex+ew, ey+eh), (0,0,255), 2) 78 | 79 | #cv2.imshow('dasd', face_region[ey:ey+eh, ex:ex+ew]) 80 | #cv2.waitKey() 81 | """ 82 | 83 | """ 84 | if prev_eyes != None and prev_faces != None: 85 | for ex,ey,ew,eh in prev_eyes: 86 | cv2.imshow('dsad', prev_faces[ey:ey+eh, ex:ex+ew]) 87 | cv2.waitKey() 88 | """ 89 | cv2.imshow('dsad', im) 90 | cv2.waitKey() 91 | -------------------------------------------------------------------------------- /eye_region_dataset/eye_recording.py: -------------------------------------------------------------------------------- 1 | import cv2 2 | import numpy as np 3 | import subprocess 4 | import sys, getopt 5 | 6 | options = getopt.getopt(sys.argv[1:], shortopts=None, longopts=["drowsiness="]) 7 | op_list = options[0] 8 | snapshot_dir = None 9 | print 'please enter your name. >>> ', 10 | name = raw_input() 11 | 12 | assert len(op_list) == 1, 'please check the implementation usage.' 13 | 14 | for op, args in op_list: 15 | print op, args 16 | if op == '--drowsiness' : 17 | if args == 'True': 18 | snapshot_dir = '/Users/naseil/Desktop/Hanyang University/Projects/Drowsiness_Detector/eye_region_dataset/snapshot_dir/' + name + '_drowsiness/' 19 | elif args == 'False': 20 | snapshot_dir = '/Users/naseil/Desktop/Hanyang University/Projects/Drowsiness_Detector/eye_region_dataset/snapshot_dir/' + name + '/' 21 | 22 | 23 | subprocess.call(['mkdir', snapshot_dir]) 24 | 25 | print 'snapshot directory >> ' + snapshot_dir 26 | 27 | a = raw_input() 28 | snapshot_index = 0 29 | 30 | 31 | cap = cv2.VideoCapture(0) 32 | 33 | while True: 34 | ret, frame = cap.read() 35 | 36 | frame = cv2.resize(frame, (500,500)) 37 | gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) 38 | 39 | #cv2.imshow('frame',gray) 40 | filename = snapshot_dir + str(snapshot_index) + '.jpeg' 41 | cv2.imwrite(filename, frame) 42 | snapshot_index += 1 43 | if snapshot_index % 100 == 0: 44 | print '%dth snapshot saved...' % snapshot_index 45 | if cv2.waitKey(1) & 0xFF == ord('q'): 46 | break 47 | 48 | cap.release() 49 | cv2.destroyAllWindows() 50 | 51 | 52 | 53 | ''' 54 | face_cascade = cv2.CascadeClassifier('/usr/local/Cellar/opencv/2.4.12_2/data/haarcascades/haarcascade_frontalface_default.xml') 55 | 56 | eye_cascade = cv2.CascadeClassifier('/usr/local/Cellar/opencv/2.4.12_2/data/haarcascades/haarcascade_eye.xml') 57 | 58 | im = cv2.imread('face.jpg') 59 | gray = cv2.cvtColor(im, cv2.COLOR_BGR2GRAY) 60 | 61 | 62 | face = face_cascade.detectMultiScale(gray, 1.3, 5) 63 | 64 | for x,y,w,h in face: 65 | cv2.rectangle(im, (x,y), (x+w, y+h), (255,0,0), 2) 66 | roi_gray = gray[y:y+h, x:x+w] 67 | roi_color = im[y:y+h, x:x+w] 68 | 69 | eyes = eye_cascade.detectMultiScale(roi_gray) 70 | for ex,ey,ew,eh in eyes: 71 | cv2.rectangle(roi_color, (ex,ey), (ex+ew, ey+eh), (0,255,0), 2) 72 | 73 | cv2.imshow('img', im) 74 | cv2.waitKey(0) 75 | cv2.destroyAllWindows() 76 | ''' 77 | -------------------------------------------------------------------------------- /face_load_dataset.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | import glob 3 | import cv2 4 | import subprocess 5 | 6 | IM_SIZE = 64 7 | class DataSet(object): 8 | def __init__(self, images, labels): 9 | self.images = images 10 | self.labels = labels 11 | self.epoch_completed = 0 12 | self.index_in_epoch = 0 13 | 14 | assert images.shape[0] == labels.shape[0], "assertion error : # of images and labels are not equated." 15 | self.num_examples = images.shape[0] 16 | 17 | def next_batch(self, batch_size): 18 | assert batch_size <= self.num_examples 19 | 20 | start = self.index_in_epoch 21 | self.index_in_epoch += batch_size 22 | 23 | if self.index_in_epoch > self.num_examples: 24 | """ 25 | if batch index touch the num_exmaples, 26 | shuffle the training dataset and start next new batch 27 | """ 28 | perm = np.arange(self.num_examples) 29 | np.random.shuffle(perm) 30 | self.images = self.images[perm] 31 | self.labels = self.labels[perm] 32 | 33 | #start the next batch 34 | start = 0 35 | self.index_in_epoch = batch_size 36 | 37 | end = self.index_in_epoch 38 | return self.images[start:end], self.labels[start:end] 39 | 40 | def image_to_dataset(): 41 | 42 | image, label = [], [] 43 | images = None 44 | images = glob.glob('./face_region/usual/*') 45 | 46 | print "usual size >> " + str(len(images)) 47 | for file in images: 48 | im = cv2.imread(file) 49 | im = cv2.resize(im, (IM_SIZE, IM_SIZE)) 50 | image.append(im) 51 | label.append([1,0]) 52 | 53 | images = glob.glob('./face_region/rotate/*') 54 | 55 | print "drowsiness size >> " + str(len(images)) 56 | for file in images: 57 | im = cv2.imread(file) 58 | im = cv2.resize(im, (IM_SIZE, IM_SIZE)) 59 | image.append(im) 60 | label.append([0,1]) 61 | 62 | image = [x/float(255) for x in image] 63 | image = np.array(image) 64 | label = np.array(label) 65 | 66 | #print image 67 | perm = np.arange(image.shape[0]) 68 | np.random.shuffle(perm) 69 | print perm 70 | image = image[perm] 71 | label = label[perm] 72 | 73 | return image, label 74 | 75 | def read_dataset(validation_rate): 76 | 77 | assert validation_rate > 0 and validation_rate < 100 78 | 79 | class DataSets(object): 80 | pass 81 | data_sets = DataSets() 82 | 83 | image, label = image_to_dataset() 84 | 85 | VALIDATION_SIZE = validation_rate * image.shape[0] / 100 86 | 87 | train_image = image[VALIDATION_SIZE:] 88 | train_label = label[VALIDATION_SIZE:] 89 | 90 | validation_image = image[:VALIDATION_SIZE] 91 | validation_label = label[:VALIDATION_SIZE] 92 | 93 | data_sets.train = DataSet(train_image, train_label) 94 | data_sets.validation = DataSet(validation_image, validation_label) 95 | 96 | return data_sets 97 | 98 | #data_sets = read_dataset(10) 99 | 100 | -------------------------------------------------------------------------------- /face_model.py: -------------------------------------------------------------------------------- 1 | import glob 2 | import cv2 3 | import face_load_dataset 4 | import tensorflow as tf 5 | import numpy as np 6 | 7 | IM_SIZE = 64 8 | BATCH_SIZE = 100 9 | 10 | sess = tf.InteractiveSession() 11 | 12 | data_sets = face_load_dataset.read_dataset(10) 13 | print "training dataset shape >> " + str(data_sets.train.images.shape) 14 | print "validation dataset shape >> " + str(data_sets.validation.images.shape) 15 | 16 | print data_sets.train.labels 17 | print data_sets.validation.labels 18 | 19 | def weight_variable(shape): 20 | initial = tf.truncated_normal(shape, stddev=0.05) 21 | return tf.Variable(initial) 22 | 23 | def bias_variable(shape): 24 | initial = tf.constant(0.1, shape=shape) 25 | return tf.Variable(initial) 26 | 27 | def conv2d(x, W): 28 | return tf.nn.conv2d(x, W, strides=[1,1,1,1], padding='SAME') 29 | 30 | def max_pool_2x2(x): 31 | return tf.nn.max_pool(x, ksize=[1,2,2,1], strides=[1,2,2,1], padding='SAME') 32 | 33 | x = tf.placeholder(tf.float32, shape=[None,IM_SIZE,IM_SIZE,3]) 34 | y_ = tf.placeholder(tf.float32, shape=[None,2]) 35 | keep_prob = tf.placeholder("float") 36 | 37 | 38 | x_reshape = tf.reshape(x, [-1,IM_SIZE,IM_SIZE,3]) 39 | 40 | 41 | W_conv1 = weight_variable([5,5,3,16]) 42 | b_conv1 = bias_variable([16]) 43 | h_conv1 = tf.nn.relu(conv2d(x_reshape, W_conv1) + b_conv1) 44 | 45 | W_conv2 = weight_variable([5,5,16,16]) 46 | b_conv2 = bias_variable([16]) 47 | h_conv2 = tf.nn.relu(conv2d(h_conv1, W_conv2) + b_conv2) 48 | 49 | W_conv3 = weight_variable([5,5,16,16]) 50 | b_conv3 = bias_variable([16]) 51 | h_conv3 = tf.nn.relu(conv2d(h_conv2, W_conv3) + b_conv3) 52 | 53 | 54 | """ 55 | 1 pooling 56 | """ 57 | h_pool3 = max_pool_2x2(h_conv3) 58 | 59 | 60 | W_conv4 = weight_variable([5,5,16,32]) 61 | b_conv4 = bias_variable([32]) 62 | h_conv4 = tf.nn.relu(conv2d(h_pool3, W_conv4) + b_conv4) 63 | 64 | W_conv5 = weight_variable([5,5,32,32]) 65 | b_conv5 = bias_variable([32]) 66 | h_conv5 = tf.nn.relu(conv2d(h_conv4, W_conv5) + b_conv5) 67 | 68 | W_conv6 = weight_variable([5,5,32,32]) 69 | b_conv6 = bias_variable([32]) 70 | h_conv6 = tf.nn.relu(conv2d(h_conv5, W_conv6) + b_conv6) 71 | 72 | 73 | """ 74 | 2 pooling 75 | """ 76 | h_pool6 = max_pool_2x2(h_conv6) 77 | 78 | W_conv7 = weight_variable([5,5,32,64]) 79 | b_conv7 = bias_variable([64]) 80 | h_conv7 = tf.nn.relu(conv2d(h_pool6, W_conv7) + b_conv7) 81 | 82 | W_conv8 = weight_variable([5,5,64,64]) 83 | b_conv8 = weight_variable([64]) 84 | h_conv8 = tf.nn.relu(conv2d(h_conv7, W_conv8) + b_conv8) 85 | 86 | W_conv9 = weight_variable([5,5,64,64]) 87 | b_conv9 = bias_variable([64]) 88 | h_conv9 = tf.nn.relu(conv2d(h_conv8, W_conv9) + b_conv9) 89 | 90 | 91 | """ 92 | 3 pooling 93 | """ 94 | h_pool9 = max_pool_2x2(h_conv9) 95 | h_pool9_flat = tf.reshape(h_pool9, [-1, (IM_SIZE/8)*(IM_SIZE/8)*64]) 96 | 97 | 98 | 99 | W_fc1 = weight_variable([(IM_SIZE/8)*(IM_SIZE/8)*64, 1024]) 100 | b_fc1 = bias_variable([1024]) 101 | h_fc1 = tf.nn.relu(tf.matmul(h_pool9_flat, W_fc1) + b_fc1) 102 | h_fc1_drop = tf.nn.dropout(h_fc1, keep_prob) 103 | 104 | W_fc2 = weight_variable([1024,1024]) 105 | b_fc2 = bias_variable([1024]) 106 | h_fc2 = tf.nn.relu(tf.matmul(h_fc1_drop, W_fc2) + b_fc2) 107 | h_fc2_drop = tf.nn.dropout(h_fc2, keep_prob) 108 | 109 | W_fc3 = weight_variable([1024,2]) 110 | b_fc3 = bias_variable([2]) 111 | 112 | y_matmul = tf.matmul(h_fc2_drop, W_fc3) + b_fc3 113 | 114 | y_conv = tf.nn.softmax(y_matmul) 115 | 116 | l2_loss = tf.reduce_mean(-tf.reduce_sum(y_*tf.log(y_conv+1e-7), reduction_indices=[1])) 117 | train_step = tf.train.AdamOptimizer(1e-3).minimize(l2_loss) 118 | correct_prediction = tf.equal(tf.argmax(y_conv, 1), tf.argmax(y_, 1)) 119 | accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32)) 120 | tf.initialize_all_variables().run() 121 | 122 | loss_curve = open('./face_loss_curve.csv', 'w') 123 | saver = tf.train.Saver() 124 | 125 | SAVE_PATH = "./face_checkpoint/" 126 | for iteration in xrange(1000000): 127 | if iteration % 70 == 0: 128 | valid_batch_x, valid_batch_y = data_sets.validation.next_batch(BATCH_SIZE) 129 | acc = accuracy.eval(feed_dict={x: valid_batch_x, 130 | y_: valid_batch_y, keep_prob:1.0}) 131 | loss = sess.run(l2_loss, feed_dict={x: valid_batch_x, 132 | y_: valid_batch_y, keep_prob:1.0}) 133 | 134 | print '%dth iteration... accuracy >> %lf, loss .. %lf' % (iteration, acc, loss) 135 | contents = str(loss) + ',\n' 136 | loss_curve.write(contents) 137 | 138 | batch_x, batch_y = data_sets.train.next_batch(BATCH_SIZE) 139 | train_step.run(feed_dict={x:batch_x, y_:batch_y, keep_prob:0.5}) 140 | 141 | if iteration % 4000 == 0 and iteration > 0 == 0 : 142 | checkpoint_file = SAVE_PATH + "ver1.0_iteration." + str(iteration) + ".ckpt" 143 | saver.save(sess,checkpoint_file) 144 | print "CNN models are saved in %s." % checkpoint_file 145 | 146 | 147 | 148 | -------------------------------------------------------------------------------- /getting_glasses_region.py: -------------------------------------------------------------------------------- 1 | import cv2 2 | import numpy as np 3 | import sys 4 | def relative_region(glass_region_list=[]): 5 | 6 | def draw_point(event, x, y, flags, param): 7 | if event == 1: 8 | print "draw!" 9 | cv2.circle(frames, (x,y), 3, (0,255,0), 1) 10 | glass_region_list.append((x,y)) 11 | 12 | 13 | cv2.namedWindow("image") 14 | cv2.setMouseCallback("image", draw_point) 15 | 16 | 17 | face_cascade = cv2.CascadeClassifier("./haarcascade_frontalface_default.xml") 18 | cap = cv2.VideoCapture(0) 19 | while True: 20 | ret, frames = cap.read() 21 | frames = cv2.resize(frames, (250,250)) 22 | face = face_cascade.detectMultiScale(frames, 1.1, 3) 23 | for a,b,w,h in face: 24 | cv2.rectangle(frames, (a,b), (a+w,b+h), (255,0,0), 1) 25 | cv2.imshow("image", frames) 26 | if ord('q') == (cv2.waitKey(20) & 0xFF): break 27 | 28 | 29 | while True: 30 | if len(glass_region_list) == 4: 31 | relative_region = [] 32 | for a,b,w,h in face: 33 | for i in xrange(2): 34 | x1,y1 = glass_region_list[i*2] 35 | x2,y2 = glass_region_list[i*2+1] 36 | 37 | relative_width = float(x2-x1)/float(w) 38 | relative_height = float(y2-y1)/float(h) 39 | 40 | relative_start_x = float(x1-a)/float(w) 41 | relative_start_y = float(y1-b)/float(h) 42 | relative_region.append((relative_width, relative_height, relative_start_x, relative_start_y)) 43 | if len(relative_region) != 2: 44 | print "relative region error" 45 | print relative_region 46 | assert(len(relative_region) == 2), "relative region error" 47 | return relative_region 48 | 49 | gray = cv2.cvtColor(frames, cv2.COLOR_BGR2GRAY) 50 | face = face_cascade.detectMultiScale(gray, 1.1, 3) 51 | 52 | """ 53 | for a,b,w,h in face: 54 | cv2.rectangle(frames, (a,b), (a+w,b+h), (255,0,0), 1) 55 | """ 56 | 57 | cv2.imshow("image", frames) 58 | k = cv2.waitKey(20) & 0xFF 59 | if k == 27: break 60 | elif k == ord('a'): 61 | print "error!!!!!!" 62 | sys.exit(0) 63 | print glass_region_list 64 | assert len(glass_region_list) == 4, "region fixing error" 65 | return glass_region_list 66 | 67 | def glasses_region(face, relative_region_list): 68 | print "in getting glasses region, >> ", 69 | print face 70 | print relative_region_list 71 | glasses_region_list = [] 72 | for a,b,w,h in face: 73 | for r_w,r_h,r_sx,r_sy in relative_region_list: 74 | new_sx = w*r_sx 75 | new_sy = h*r_sy 76 | new_w = r_w * w 77 | new_h = r_h * h 78 | glasses_region_list.append((new_sx, new_sy, new_w, new_h)) 79 | 80 | print "final glasses region >> " , 81 | print glasses_region_list 82 | assert(len(glasses_region_list) == 2), "glasses region proposal error." 83 | return glasses_region_list 84 | 85 | 86 | 87 | 88 | #grl = glasses_region() 89 | #print grl 90 | -------------------------------------------------------------------------------- /inference.py: -------------------------------------------------------------------------------- 1 | import glob 2 | import cv2 3 | import load_dataset 4 | import numpy as np 5 | import tensorflow as tf 6 | import sys 7 | from getting_glasses_region import glasses_region 8 | from getting_glasses_region import relative_region 9 | # loading opencv cascade model for detecting face and eye regions. 10 | face_cascade = cv2.CascadeClassifier("./haarcascade_frontalface_default.xml") 11 | eye_cascade = cv2.CascadeClassifier("./haarcascade_eye.xml") 12 | 13 | 14 | """ 15 | CNN models definition. 16 | models consist of 9 convolution layers, 17 | 3 pooling layers, 18 | 2 fc layers. 19 | """ 20 | 21 | IM_SIZE = 32 22 | BATCH_SIZE = 100 23 | WINDOW_SIZE = 4 24 | 25 | """ 26 | There are 2 inference mode. 27 | one is usaul state, 28 | another is wearing glasses state. 29 | you might change mode executing program with giving options. 30 | """ 31 | glasses = False 32 | 33 | 34 | """ 35 | Determines model's sensitivity. (3~5 recommended.) 36 | """ 37 | if len(sys.argv) == 2: 38 | WINDOW_SIZE = int(sys.argv[1]) 39 | 40 | if len(sys.argv) == 3: 41 | assert sys.argv[2] == "glasses", "invalid option used." 42 | glasses = True 43 | 44 | assert len(sys.argv) < 4, "Too many options given." 45 | print "model sensitivity >> %d" % WINDOW_SIZE 46 | 47 | sess = tf.InteractiveSession() 48 | def weight_variable(shape): 49 | initial = tf.truncated_normal(shape, stddev=0.05) 50 | return tf.Variable(initial) 51 | 52 | def bias_variable(shape): 53 | initial = tf.constant(0.1, shape=shape) 54 | return tf.Variable(initial) 55 | 56 | def conv2d(x, W): 57 | return tf.nn.conv2d(x, W, strides=[1,1,1,1], padding='SAME') 58 | 59 | def max_pool_2x2(x): 60 | return tf.nn.max_pool(x, ksize=[1,2,2,1], strides=[1,2,2,1], padding='SAME') 61 | 62 | x = tf.placeholder(tf.float32, shape=[None,IM_SIZE,IM_SIZE,3]) 63 | y_ = tf.placeholder(tf.float32, shape=[None,2]) 64 | keep_prob = tf.placeholder(tf.float32) 65 | 66 | x_reshape = tf.reshape(x, [-1,IM_SIZE,IM_SIZE,3]) 67 | 68 | W_conv1 = weight_variable([5,5,3,16]) 69 | b_conv1 = bias_variable([16]) 70 | h_conv1 = tf.nn.relu(conv2d(x_reshape, W_conv1) + b_conv1) 71 | 72 | W_conv2 = weight_variable([5,5,16,16]) 73 | b_conv2 = bias_variable([16]) 74 | h_conv2 = tf.nn.relu(conv2d(h_conv1, W_conv2) + b_conv2) 75 | 76 | W_conv3 = weight_variable([5,5,16,16]) 77 | b_conv3 = bias_variable([16]) 78 | h_conv3 = tf.nn.relu(conv2d(h_conv2, W_conv3) + b_conv3) 79 | 80 | 81 | """ 82 | 1 pooling 83 | """ 84 | h_pool3 = max_pool_2x2(h_conv3) 85 | 86 | 87 | W_conv4 = weight_variable([5,5,16,32]) 88 | b_conv4 = bias_variable([32]) 89 | h_conv4 = tf.nn.relu(conv2d(h_pool3, W_conv4) + b_conv4) 90 | 91 | W_conv5 = weight_variable([5,5,32,32]) 92 | b_conv5 = bias_variable([32]) 93 | h_conv5 = tf.nn.relu(conv2d(h_conv4, W_conv5) + b_conv5) 94 | 95 | W_conv6 = weight_variable([5,5,32,32]) 96 | b_conv6 = bias_variable([32]) 97 | h_conv6 = tf.nn.relu(conv2d(h_conv5, W_conv6) + b_conv6) 98 | 99 | 100 | """ 101 | 2 pooling 102 | """ 103 | h_pool6 = max_pool_2x2(h_conv6) 104 | 105 | W_conv7 = weight_variable([5,5,32,64]) 106 | b_conv7 = bias_variable([64]) 107 | h_conv7 = tf.nn.relu(conv2d(h_pool6, W_conv7) + b_conv7) 108 | 109 | W_conv8 = weight_variable([5,5,64,64]) 110 | b_conv8 = weight_variable([64]) 111 | h_conv8 = tf.nn.relu(conv2d(h_conv7, W_conv8) + b_conv8) 112 | 113 | W_conv9 = weight_variable([5,5,64,64]) 114 | b_conv9 = bias_variable([64]) 115 | h_conv9 = tf.nn.relu(conv2d(h_conv8, W_conv9) + b_conv9) 116 | 117 | 118 | """ 119 | 3 pooling 120 | """ 121 | h_pool9 = max_pool_2x2(h_conv9) 122 | h_pool9_flat = tf.reshape(h_pool9, [-1, (IM_SIZE/8)*(IM_SIZE/8)*64]) 123 | 124 | 125 | 126 | W_fc1 = weight_variable([(IM_SIZE/8)*(IM_SIZE/8)*64, 1024]) 127 | b_fc1 = bias_variable([1024]) 128 | h_fc1 = tf.nn.relu(tf.matmul(h_pool9_flat, W_fc1) + b_fc1) 129 | h_fc1_drop = tf.nn.dropout(h_fc1, keep_prob) 130 | 131 | W_fc2 = weight_variable([1024,1024]) 132 | b_fc2 = bias_variable([1024]) 133 | h_fc2 = tf.nn.relu(tf.matmul(h_fc1_drop, W_fc2) + b_fc2) 134 | h_fc2_drop = tf.nn.dropout(h_fc2, keep_prob) 135 | 136 | W_fc3 = weight_variable([1024,2]) 137 | b_fc3 = bias_variable([2]) 138 | 139 | y_matmul = tf.matmul(h_fc2_drop, W_fc3) + b_fc3 140 | 141 | y_conv = tf.nn.softmax(y_matmul) 142 | 143 | l2_loss = tf.reduce_mean(-tf.reduce_sum(y_*tf.log(y_conv+1e-7), reduction_indices=[1])) 144 | train_step = tf.train.AdamOptimizer(1e-3).minimize(l2_loss) 145 | correct_prediction = tf.equal(tf.argmax(y_conv, 1), tf.argmax(y_, 1)) 146 | accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32)) 147 | tf.initialize_all_variables().run() 148 | 149 | saver = tf.train.Saver() 150 | SAVE_PATH = "./checkpoint/ver1.0_iteration.64000.ckpt" 151 | saver.restore(sess, SAVE_PATH) 152 | 153 | 154 | """ 155 | getting realtime video of user, 156 | detecting whether eye is closed or not using trained CNN models. 157 | """ 158 | cap = cv2.VideoCapture(0) 159 | #cap = cv2.VideoCapture("/Users/naseil/Desktop/original.mov") 160 | prev_face = [(0,0,30,30)] 161 | prev_eyes = [(1,1,1,1), (1,1,1,1)] 162 | drowsiness_check_list = [0] * WINDOW_SIZE 163 | drowsiness_check_idx = 0 164 | 165 | 166 | def rotate_check(face_size): 167 | face_size /= 10 168 | print "face size >> %d" % face_size 169 | if not hasattr(rotate_check, "full_count"): 170 | rotate_check.full_count = 0 171 | if not hasattr(rotate_check, "size_distribution"): 172 | rotate_check.size_distribution = [0] * 5000 173 | rotate_check.full_count += 1 174 | rotate_check.size_distribution[face_size/100] += 1 175 | 176 | percentage = rotate_check.size_distribution[face_size/100] * 100 / rotate_check.full_count 177 | print "(%d/%d)" % (rotate_check.size_distribution[face_size/100], rotate_check.full_count) 178 | if percentage < 10: 179 | print "%d , Fail!" % percentage 180 | return False 181 | rotate_check.size_distribution[face_size/100] += 1 182 | print "Succes!" 183 | return True 184 | 185 | relative_region_list = [] 186 | if glasses == True: 187 | relative_region_list = relative_region() 188 | print relative_region_list 189 | continuous_error_count = 0 190 | total_cnt = 0 191 | error_classified_cnt = 0 192 | 193 | error_path = "./errorounes_classified/" 194 | error_file_num = 0 195 | while True: 196 | ret, frame = cap.read() 197 | total_cnt += 1 198 | frame = cv2.resize(frame,(250,250)) 199 | gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) 200 | face = face_cascade.detectMultiScale(gray, 1.1, 3) 201 | 202 | error_check = False 203 | if len(face) != 1: 204 | face=prev_face 205 | error_check = True 206 | continuous_error_count += 1 207 | else: 208 | continuous_error_count = 0 209 | prev_face = face 210 | for a,b,w,h in face: 211 | face_size = w*h 212 | prev_face = face 213 | roi_gray = gray[b:b+h, a:a+w] 214 | roi_color = frame[b:b+h, a:a+w] 215 | eyes = [] 216 | if glasses == False: 217 | eyes = eye_cascade.detectMultiScale(roi_gray) 218 | elif glasses == True and error_check == False: 219 | eyes = glasses_region(face, relative_region_list) 220 | if len(eyes) != 2: 221 | error_check = True 222 | eyes = prev_eyes 223 | continuous_error_count += 1 224 | else: 225 | continuous_error_count = 0 226 | prev_eyes = eyes 227 | 228 | cv2.rectangle(frame, (a,b), (a+w,b+h), (255,0,0), 1) 229 | print "real eyes >> ", 230 | print eyes 231 | eye_full_cnt = 0 232 | for f_ex,f_ey,f_ew,f_eh in eyes: 233 | ex, ey, ew, eh = int(f_ex), int(f_ey), int(f_ew), int(f_eh) 234 | eye_region_image = roi_color[ey:ey+eh, ex:ex+ew] 235 | prev_eyes = eyes 236 | p,q,r = eye_region_image.shape 237 | if p==0 or q==0 : break 238 | input_images = cv2.resize(eye_region_image, (32,32)) 239 | input_images.resize((1,32,32,3)) 240 | 241 | input_images = np.divide(input_images, 255.0) 242 | 243 | # Detecting drowsiness using CNN models. 244 | label = sess.run(tf.argmax(y_conv, 1), feed_dict={keep_prob:1.0, x:input_images}) 245 | print label 246 | drowsiness_check_list[drowsiness_check_idx%WINDOW_SIZE] = label[0] 247 | drowsiness_check_idx += 1 248 | print drowsiness_check_list 249 | # if drowsiness if detected, 250 | # imaegs will be shown with red boxing. 251 | if rotate_check(face_size) == True and continuous_error_count < 5 and drowsiness_check_list == [1]*WINDOW_SIZE: 252 | cv2.rectangle(roi_color, (int(ex),int(ey)), (int(ex+ew), int(ey+eh)), (0,255,0), 1) 253 | eye_full_cnt += 1 254 | p,q,r = frame.shape 255 | error_classified_cnt += 1 256 | error_file_num += 1 257 | error_file_name = error_path + str(error_file_num) + ".jpeg" 258 | #cv2.imwrite(error_file_name, frame) 259 | 260 | """ 261 | for i in xrange(p): 262 | for j in xrange(q): 263 | frame[i,j,2] = 150 264 | """ 265 | elif rotate_check(face_size) == True: 266 | cv2.rectangle(roi_color, (int(ex),int(ey)), (int(ex+ew), int(ey+eh)), (0,255,0), 1) 267 | if eye_full_cnt == 2: 268 | p,q,r = frame.shape 269 | for i in xrange(p): 270 | for j in xrange(q): 271 | frame[i,j,2] = 150 272 | 273 | cv2.imshow("Deep-CNN", frame) 274 | if cv2.waitKey(1) & 0xFF == ord('q'):break 275 | cap.release() 276 | cv2.destroyAllWindows() 277 | 278 | 279 | -------------------------------------------------------------------------------- /load_dataset.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | import glob 3 | import cv2 4 | import subprocess 5 | 6 | class DataSet(object): 7 | def __init__(self, images, labels): 8 | self.images = images 9 | self.labels = labels 10 | self.epoch_completed = 0 11 | self.index_in_epoch = 0 12 | 13 | assert images.shape[0] == labels.shape[0], "assertion error : # of images and labels are not equated." 14 | self.num_examples = images.shape[0] 15 | 16 | def next_batch(self, batch_size): 17 | assert batch_size <= self.num_examples 18 | 19 | start = self.index_in_epoch 20 | self.index_in_epoch += batch_size 21 | 22 | if self.index_in_epoch > self.num_examples: 23 | """ 24 | if batch index touch the num_exmaples, 25 | shuffle the training dataset and start next new batch 26 | """ 27 | perm = np.arange(self.num_examples) 28 | np.random.shuffle(perm) 29 | self.images = self.images[perm] 30 | self.labels = self.labels[perm] 31 | 32 | #start the next batch 33 | start = 0 34 | self.index_in_epoch = batch_size 35 | 36 | end = self.index_in_epoch 37 | return self.images[start:end], self.labels[start:end] 38 | 39 | def image_to_dataset(): 40 | 41 | image, label = [], [] 42 | images = None 43 | images = glob.glob('./only_eye_region/usual_dataset/*') 44 | 45 | print "usual size >> " + str(len(images)) 46 | for file in images: 47 | im = cv2.imread(file) 48 | im = cv2.resize(im, (32,32)) 49 | image.append(im) 50 | label.append([1,0]) 51 | images = glob.glob('./only_eye_region/drowsiness_dataset/*') 52 | 53 | print "drowsiness size >> " + str(len(images)) 54 | for file in images: 55 | im = cv2.imread(file) 56 | im = cv2.resize(im, (32,32)) 57 | image.append(im) 58 | label.append([0,1]) 59 | 60 | image = [x/float(255) for x in image] 61 | image = np.array(image) 62 | label = np.array(label) 63 | 64 | #print image 65 | perm = np.arange(image.shape[0]) 66 | np.random.shuffle(perm) 67 | print perm 68 | image = image[perm] 69 | label = label[perm] 70 | 71 | return image, label 72 | 73 | def read_dataset(validation_rate): 74 | 75 | assert validation_rate > 0 and validation_rate < 100 76 | 77 | class DataSets(object): 78 | pass 79 | data_sets = DataSets() 80 | 81 | image, label = image_to_dataset() 82 | 83 | VALIDATION_SIZE = validation_rate * image.shape[0] / 100 84 | 85 | train_image = image[VALIDATION_SIZE:] 86 | train_label = label[VALIDATION_SIZE:] 87 | 88 | validation_image = image[:VALIDATION_SIZE] 89 | validation_label = label[:VALIDATION_SIZE] 90 | 91 | data_sets.train = DataSet(train_image, train_label) 92 | data_sets.validation = DataSet(validation_image, validation_label) 93 | 94 | return data_sets 95 | 96 | #data_sets = read_dataset(10) 97 | 98 | -------------------------------------------------------------------------------- /loss_curve.csv: -------------------------------------------------------------------------------- 1 | 0.935858, 2 | 0.728304, 3 | 0.618329, 4 | 0.324861, 5 | 0.131439, 6 | 0.107422, 7 | 0.0212457, 8 | 0.122033, 9 | 0.051386, 10 | 0.0495638, 11 | 0.082748, 12 | 0.0582402, 13 | 0.0780789, 14 | 0.0341042, 15 | 0.0594337, 16 | 0.0175211, 17 | 0.00280772, 18 | 0.0550337, 19 | 0.0180233, 20 | 0.0128125, 21 | 0.189891, 22 | 0.0357083, 23 | 0.105227, 24 | 0.00653139, 25 | 0.00348436, 26 | 0.0501877, 27 | 0.0237136, 28 | 0.00669765, 29 | 0.00115841, 30 | 0.0061043, 31 | 0.0107083, 32 | 0.00755448, 33 | 0.0350103, 34 | 0.0172555, 35 | 0.0286125, 36 | 0.00153319, 37 | 0.0127185, 38 | 0.00436143, 39 | 0.0245835, 40 | 0.0210352, 41 | 0.0519435, 42 | 0.00162414, 43 | 0.0221096, 44 | 0.022608, 45 | 0.00745089, 46 | 0.0399171, 47 | 0.00110525, 48 | 0.0185975, 49 | 0.0141955, 50 | 0.0176487, 51 | 0.00168236, 52 | 0.00423504, 53 | 0.110424, 54 | 0.0180714, 55 | 0.108867, 56 | 0.0113352, 57 | 0.00879847, 58 | 0.0267101, 59 | 0.00934098, 60 | 0.000958946, 61 | 0.0535652, 62 | 0.0246584, 63 | 0.00131024, 64 | 0.0514622, 65 | 0.0996295, 66 | 0.0778368, 67 | 0.027615, 68 | 0.00517737, 69 | 0.000195907, 70 | 0.0235146, 71 | 0.0120585, 72 | 0.0662484, 73 | 0.0200844, 74 | 0.00399831, 75 | 0.0173991, 76 | 0.0196831, 77 | 0.00446155, 78 | 0.00864592, 79 | 0.0176434, 80 | 0.0560154, 81 | 0.00212082, 82 | 0.0361411, 83 | 0.000110559, 84 | 3.53187e-05, 85 | 0.0637895, 86 | 0.00956539, 87 | 0.000378064, 88 | 0.016691, 89 | 0.114617, 90 | 0.00497355, 91 | 0.0329096, 92 | 2.41253e-05, 93 | 0.00911122, 94 | 0.00740115, 95 | 0.0170861, 96 | 0.00325326, 97 | 0.000119486, 98 | 1.64637e-06, 99 | 0.0336251, 100 | 0.0125539, 101 | 0.0368186, 102 | 0.00128165, 103 | 0.000784497, 104 | 0.0161546, 105 | 0.0953486, 106 | 0.00168694, 107 | 0.12197, 108 | 0.00707923, 109 | 0.000166138, 110 | 0.00692056, 111 | 0.0091052, 112 | 0.0449537, 113 | 0.0717729, 114 | 0.0291977, 115 | 4.11201e-05, 116 | 0.0404635, 117 | 0.0114272, 118 | 0.17627, 119 | 0.202893, 120 | 0.019416, 121 | 0.0151188, 122 | 0.0012648, 123 | 0.0114228, 124 | 0.0157741, 125 | 2.15898e-06, 126 | 0.090263, 127 | 2.61385e-05, 128 | 0.0464712, 129 | 0.00900361, 130 | 0.000314695, 131 | 0.0260793, 132 | 0.00521387, 133 | 0.0626242, 134 | 0.0437706, 135 | 0.00139968, 136 | 0.147529, 137 | 0.0043091, 138 | 0.285017, 139 | 0.0204993, 140 | 0.0231481, 141 | 0.00192497, 142 | 0.00305187, 143 | 0.0122176, 144 | 0.0520254, 145 | 0.000910292, 146 | 0.00314873, 147 | 0.0389502, 148 | 0.0249631, 149 | 0.0132772, 150 | 0.0327988, 151 | 0.00981461, 152 | 9.42115e-05, 153 | 0.0428959, 154 | 0.05894, 155 | 0.0753152, 156 | 0.0748152, 157 | 0.17874, 158 | 0.00505886, 159 | 0.0255468, 160 | 0.0139299, 161 | 0.00992534, 162 | 0.00314354, 163 | 0.0281126, 164 | 0.0454506, 165 | 0.00206038, 166 | 0.00173575, 167 | 0.0424695, 168 | 0.0509062, 169 | 0.0945587, 170 | 0.107465, 171 | 0.0185592, 172 | 0.00932612, 173 | 0.0194054, 174 | 0.00231743, 175 | 0.195179, 176 | 0.00592329, 177 | 0.0163839, 178 | 0.00256616, 179 | 0.00187204, 180 | 0.0019267, 181 | 0.0658667, 182 | 0.00464042, 183 | 0.0343892, 184 | 5.5694e-05, 185 | 0.00485437, 186 | 0.000533764, 187 | 0.00686406, 188 | 0.0214659, 189 | 5.50763e-07, 190 | 0.103146, 191 | 0.0481206, 192 | 0.0628694, 193 | 0.00124454, 194 | 0.178509, 195 | 0.0509045, 196 | 0.333183, 197 | 0.00898579, 198 | 0.220921, 199 | 0.000883187, 200 | 0.000332371, 201 | 0.129867, 202 | 0.0427931, 203 | 0.0414055, 204 | 0.0168891, 205 | 0.00193037, 206 | 0.000210338, 207 | 0.0211515, 208 | 0.00362221, 209 | 0.001402, 210 | 0.000232343, 211 | 0.0345473, 212 | 0.00674197, 213 | 0.0002648, 214 | 2.25036e-06, 215 | 0.00165782, 216 | 0.00213101, 217 | 0.0214652, 218 | 0.159776, 219 | 0.0114738, 220 | 0.165982, 221 | 0.0193432, 222 | 0.000303796, 223 | 0.000761783, 224 | 0.159477, 225 | 0.000271647, 226 | 0.0154408, 227 | 1.92411e-06, 228 | 0.0161427, 229 | 0.0404387, 230 | 0.0270759, 231 | 0.0121774, 232 | 0.00474922, 233 | 1.34565e-05, 234 | 0.017497, 235 | 0.0149063, 236 | 0.0043264, 237 | 0.0173714, 238 | 0.0818036, 239 | 0.159181, 240 | 0.00316963, 241 | 0.0862833, 242 | -8.82148e-08, 243 | -1.19209e-07, 244 | 0.0211416, 245 | 1.57963e-06, 246 | 0.16177, 247 | 0.0206286, 248 | 5.45935e-05, 249 | 0.00123591, 250 | 0.16402, 251 | 0.0116097, 252 | 0.000404464, 253 | 4.85197e-07, 254 | 0.202561, 255 | 0.00588497, 256 | 0.00122168, 257 | 0.0127497, 258 | 0.058217, 259 | 0.00309862, 260 | 0.00534107, 261 | 0.0115082, 262 | 0.00149975, 263 | 0.173409, 264 | 3.38953e-05, 265 | 0.017494, 266 | 0.0260379, 267 | 0.0258194, 268 | 0.0407926, 269 | 0.00733505, 270 | 0.140339, 271 | 8.72682e-06, 272 | 0.0496133, 273 | 0.00313702, 274 | 0.0151454, 275 | 0.0272276, 276 | 0.020831, 277 | 0.0139406, 278 | 0.0190624, 279 | 4.43466e-07, 280 | 0.082053, 281 | 0.0218214, 282 | 0.00425981, 283 | 0.122556, 284 | 0.161336, 285 | 0.0101393, 286 | 0.000706268, 287 | 0.0331653, 288 | -1.07288e-07, 289 | 0.000644806, 290 | -1.16825e-07, 291 | 0.0605381, 292 | 0.000938019, 293 | 0.0458133, 294 | 0.00153604, 295 | 0.0528645, 296 | 0.0474688, 297 | 8.30574e-06, 298 | 0.00963924, 299 | 0.031632, 300 | 0.000474743, 301 | 0.0116607, 302 | 0.0779457, 303 | 0.0547198, 304 | -1.19209e-07, 305 | 0.161181, 306 | 0.323762, 307 | 0.00203938, 308 | 0.204063, 309 | 0.154578, 310 | 0.210143, 311 | 0.000101205, 312 | 0.00165886, 313 | 0.0193059, 314 | 0.0385441, 315 | 0.0164674, 316 | 0.00098509, 317 | 0.0901715, 318 | 0.0343188, 319 | 4.78895e-05, 320 | 0.0604044, 321 | 0.00799826, 322 | 0.0362518, 323 | 0.0355415, 324 | -1.66888e-08, 325 | 0.201495, 326 | 0.0585662, 327 | 0.169078, 328 | 0.00104316, 329 | 0.0554466, 330 | 0.00564205, 331 | 0.00498784, 332 | 0.0238115, 333 | 0.0342256, 334 | 0.0135751, 335 | -1.13249e-07, 336 | 0.0244672, 337 | -1.19209e-07, 338 | 0.00924526, 339 | 6.91084e-05, 340 | 7.85359e-05, 341 | 0.0741621, 342 | 0.0151893, 343 | 0.00413935, 344 | 0.00330403, 345 | 0.0088302, 346 | 1.97611e-06, 347 | 0.0191761, 348 | 0.00414968, 349 | -1.19209e-07, 350 | 0.296814, 351 | 3.89237e-05, 352 | 0.005096, 353 | 0.0723076, 354 | 0.144718, 355 | 0.0166497, 356 | 0.0131819, 357 | 0.0248181, 358 | 3.45364e-05, 359 | 0.0805843, 360 | 0.00241412, 361 | 0.273232, 362 | 0.123185, 363 | 0.00278283, 364 | 0.163955, 365 | 0.000747989, 366 | 0.0221371, 367 | 0.0175522, 368 | 0.114447, 369 | 0.0121239, 370 | 1.18747e-05, 371 | 0.0962772, 372 | 0.027887, 373 | 0.0190162, 374 | -1.18017e-07, 375 | 0.0447849, 376 | 0.248869, 377 | 0.0132835, 378 | 0.000774292, 379 | 0.0149866, 380 | 0.161644, 381 | 0.134514, 382 | 0.0313084, 383 | 0.00124518, 384 | 0.0087318, 385 | 0.000114578, 386 | 0.320326, 387 | -1.03712e-07, 388 | 0.000772162, 389 | 7.62829e-05, 390 | 0.00770404, 391 | 0.0561251, 392 | -1.19209e-07, 393 | 0.0943104, 394 | 0.189431, 395 | 0.00231649, 396 | 0.00231564, 397 | 7.74879e-08, 398 | 0.119111, 399 | 0.00955179, 400 | -1.07288e-07, 401 | -1.16825e-07, 402 | 0.16159, 403 | 0.131676, 404 | 0.0710118, 405 | 0.0879036, 406 | 0.100466, 407 | 3.30183e-06, 408 | 1.06218e-06, 409 | 0.0139538, 410 | 0.0264699, 411 | 0.0709691, 412 | -1.19209e-07, 413 | 0.00811426, 414 | 0.00122421, 415 | 0.0833175, 416 | 0.000839382, 417 | 0.199849, 418 | 0.0134827, 419 | 0.00275369, 420 | 5.94718e-05, 421 | 0.0165627, 422 | 0.0114281, 423 | 0.00218264, 424 | 0.161722, 425 | 6.99533e-06, 426 | 0.163071, 427 | 0.00890661, 428 | 0.0310322, 429 | 0.00400971, 430 | 0.0450399, 431 | 0.0600381, 432 | 0.00100803, 433 | 3.91624e-06, 434 | 0.0187193, 435 | 0.12646, 436 | 0.00234907, 437 | 0.0126846, 438 | 0.0136988, 439 | 0.00955539, 440 | 0.117647, 441 | 0.0226445, 442 | 1.49662e-05, 443 | 0.0113028, 444 | 0.0104624, 445 | 0.00103238, 446 | 0.000323703, 447 | 1.59636e-06, 448 | 0.00754713, 449 | 0.0812165, 450 | -1.19209e-07, 451 | 0.000222425, 452 | 0.00151731, 453 | 0.0151391, 454 | 4.26559e-06, 455 | 0.00777928, 456 | 0.154604, 457 | 0.184528, 458 | 0.00927442, 459 | 0.161269, 460 | 0.0125924, 461 | 0.0142884, 462 | 0.239888, 463 | 0.341461, 464 | -1.19209e-07, 465 | 0.160949, 466 | 0.00779458, 467 | 0.014307, 468 | 0.0218123, 469 | 0.000375142, 470 | 5.36463e-07, 471 | 7.7558e-05, 472 | 0.0224835, 473 | 1.27557e-07, 474 | 0.165827, 475 | 0.111204, 476 | 4.10586e-06, 477 | 0.00698386, 478 | 0.240528, 479 | 0.0137826, 480 | 0.0139386, 481 | 0.0434573, 482 | 1.19212e-07, 483 | 0.234958, 484 | 0.0276399, 485 | 0.00210911, 486 | -1.18017e-07, 487 | 5.42129e-05, 488 | 0.16421, 489 | 0.134501, 490 | 0.167535, 491 | 0.00249393, 492 | 0.00572512, 493 | 1.20087e-05, 494 | 0.322362, 495 | 0.00592716, 496 | 0.0120351, 497 | 0.0346667, 498 | 0.024741, 499 | 0.00346603, 500 | 0.00910312, 501 | 0.00691519, 502 | 0.0948907, 503 | 2.34689e-06, 504 | -1.19209e-07, 505 | -1.19209e-07, 506 | 7.26009e-07, 507 | 0.0856968, 508 | 0.00208571, 509 | 0.161181, 510 | 0.197928, 511 | 0.171509, 512 | -1.19209e-07, 513 | 0.161582, 514 | -1.19209e-07, 515 | 3.92173e-06, 516 | 7.57613e-05, 517 | -1.19209e-07, 518 | 0.161431, 519 | 5.89113e-05, 520 | 0.000843528, 521 | 0.213719, 522 | 0.323088, 523 | 0.16143, 524 | 0.000312682, 525 | 0.00700809, 526 | 0.000689798, 527 | 1.65108e-05, 528 | -1.13249e-07, 529 | 0.161181, 530 | 6.19905e-08, 531 | 0.0114644, 532 | 0.00465456, 533 | -1.19209e-07, 534 | 0.000561932, 535 | 0.161206, 536 | 0.00189308, 537 | 1.35552e-06, 538 | 0.0523023, 539 | -1.19209e-07, 540 | 0.00252629, 541 | 0.0400325, 542 | 0.166937, 543 | 0.00930205, 544 | 0.0125013, 545 | 0.0295803, 546 | 0.00287915, 547 | 0.00159639, 548 | 0.00777659, 549 | 0.00820229, 550 | 0.0404754, 551 | 0.00132123, 552 | 0.0764037, 553 | 0.00688586, 554 | 0.000374692, 555 | 0.000430502, 556 | 0.169281, 557 | 0.00548283, 558 | 0.00928116, 559 | 0.0496395, 560 | 0.024906, 561 | 0.000966669, 562 | 0.320436, 563 | 0.0033322, 564 | 0.0791945, 565 | 0.184873, 566 | 0.00135081, 567 | 0.161181, 568 | 1.95966e-05, 569 | 0.00743188, 570 | 0.164962, 571 | 0.0181673, 572 | 0.162252, 573 | 0.000655763, 574 | -3.0994e-08, 575 | 0.0129738, 576 | 0.00152799, 577 | 0.00717308, 578 | -1.19209e-07, 579 | 0.0591343, 580 | 0.160725, 581 | -1.19209e-07, 582 | 7.51037e-08, 583 | 0.00688382, 584 | 0.00217753, 585 | 0.0248607, 586 | 0.0751983, 587 | 0.0146643, 588 | 0.011871, 589 | 0.143518, 590 | 0.01924, 591 | 4.07155e-05, 592 | 0.000105512, 593 | 0.0138594, 594 | 0.00513165, 595 | 0.00814757, 596 | 0.0432252, 597 | -1.03712e-07, 598 | -9.77516e-08, 599 | 0.108587, 600 | 0.000989352, 601 | 0.0884992, 602 | 0.00304043, 603 | 0.0101591, 604 | 0.0519026, 605 | 0.0387209, 606 | 0.0861243, 607 | 0.161181, 608 | 0.161407, 609 | 0.161182, 610 | -1.19209e-07, 611 | 0.17856, 612 | 0.0953166, 613 | 0.000265173, 614 | 1.37139e-05, 615 | 0.112258, 616 | -1.19209e-07, 617 | 0.161181, 618 | 0.0093785, 619 | 0.00948989, 620 | 9.11971e-07, 621 | 0.00010948, 622 | -8.94069e-08, 623 | 0.00095875, 624 | -1.19209e-07, 625 | 0.223154, 626 | 0.000218994, 627 | 0.163252, 628 | -1.19209e-07, 629 | 0.0490139, 630 | 0.155844, 631 | 0.157601, 632 | -1.19209e-07, 633 | 0.0228256, 634 | 0.00565146, 635 | 0.0100204, 636 | 1.62988e-05, 637 | 0.00620851, 638 | -1.15633e-07, 639 | 0.0012592, 640 | 0.0125851, 641 | 0.00393923, 642 | 0.00115999, 643 | 0.000332167, 644 | 0.0796445, 645 | 8.86959e-05, 646 | 0.161181, 647 | 0.010513, 648 | 0.00778048, 649 | -1.19209e-07, 650 | 0.0218024, 651 | -1.06096e-07, 652 | 0.323047, 653 | 0.00305698, 654 | 2.78956e-07, 655 | 7.01516e-05, 656 | 0.132281, 657 | 0.15421, 658 | 0.0234539, 659 | 0.000765954, 660 | 0.042082, 661 | 8.70438e-06, 662 | 0.000167205, 663 | 0.163279, 664 | 0.02467, 665 | 0.0438638, 666 | 0.00018801, 667 | -6.43729e-08, 668 | 0.05272, 669 | 0.00971363, 670 | 0.000358782, 671 | 0.225021, 672 | 0.012478, 673 | 0.0127545, 674 | 0.0172974, 675 | 0.000166972, 676 | -1.19209e-07, 677 | 0.060399, 678 | 0.161924, 679 | 3.16316e-06, 680 | 0.00029956, 681 | 0.00136515, 682 | -1.19209e-07, 683 | -1.0848e-07, 684 | 0.00816613, 685 | 0.00364236, 686 | 7.27446e-05, 687 | 0.00198715, 688 | 0.0132788, 689 | 0.234895, 690 | 0.00933224, 691 | 0.0084137, 692 | -1.18017e-07, 693 | 6.21108e-07, 694 | 0.244323, 695 | 0.18247, 696 | 6.21108e-07, 697 | 0.407786, 698 | 0.000433934, 699 | 0.00718717, 700 | 0.0316962, 701 | 0.322384, 702 | 0.0778751, 703 | 0.0112343, 704 | 0.175414, 705 | -1.19209e-07, 706 | 0.0959622, 707 | 0.00913301, 708 | -1.19209e-07, 709 | -1.19209e-07, 710 | 3.20683e-07, 711 | 0.114911, 712 | 0.163124, 713 | 0.00580556, 714 | 0.0233372, 715 | 0.169264, 716 | 0.0072742, 717 | -1.10865e-07, 718 | 0.00309616, 719 | 0.0388235, 720 | 0.169798, 721 | 0.0524153, 722 | 0.0262785, 723 | 1.74242e-06, 724 | 0.00748321, 725 | 0.00573982, 726 | 0.174636, 727 | 0.0294731, 728 | 0.197619, 729 | 0.165171, 730 | 0.0034606, 731 | 0.192365, 732 | -1.19209e-07, 733 | 0.000565389, 734 | 0.0079417, 735 | 0.0741697, 736 | 0.0372211, 737 | 0.00448336, 738 | 0.168315, 739 | 0.000191018, 740 | 0.382887, 741 | -1.19209e-07, 742 | 4.45859e-07, 743 | -1.19209e-07, 744 | 0.00450264, 745 | 0.00778887, 746 | 0.0357445, 747 | 0.0114854, 748 | 0.00279828, 749 | 0.322362, 750 | 0.17351, 751 | 0.0379352, 752 | 7.86576e-06, 753 | 0.00501033, 754 | 0.0089053, 755 | 0.00395721, 756 | 0.0799163, 757 | 0.126943, 758 | 0.0105134, 759 | 0.000409888, 760 | 0.0331065, 761 | 8.37887e-06, 762 | 0.0231617, 763 | 0.00463873, 764 | 0.0578595, 765 | -1.19209e-07, 766 | 0.165756, 767 | 0.198155, 768 | 0.164819, 769 | 0.0197429, 770 | 0.181034, 771 | 0.00027114, 772 | 5.62707e-05, 773 | 0.00423737, 774 | 0.111301, 775 | -1.19209e-07, 776 | -6.67571e-08, 777 | -8.34464e-08, 778 | 2.22282e-05, 779 | -1.19209e-07, 780 | -1.19209e-07, 781 | 0.161181, 782 | 0.296601, 783 | 0.000416788, 784 | -1.19209e-07, 785 | 0.00334503, 786 | 0.000609217, 787 | 0.00354643, 788 | -1.19209e-07, 789 | 0.0115962, 790 | 0.161181, 791 | -1.19209e-07, 792 | -1.18017e-07, 793 | 0.00152103, 794 | 0.00284527, 795 | 2.05362e-06, 796 | 0.000100854, 797 | 0.00563432, 798 | -9.41753e-08, 799 | 0.0156467, 800 | 0.0180573, 801 | 0.00325211, 802 | 0.182965, 803 | 3.07248e-05, 804 | 0.16118, 805 | 6.99177e-06, 806 | 0.00998901, 807 | 0.0117475, 808 | 0.000977455, 809 | 0.000116102, 810 | 0.00037508, 811 | 0.176639, 812 | 0.0291519, 813 | 0.0485508, 814 | -1.19209e-07, 815 | 0.0926395, 816 | 0.287739, 817 | 1.08483e-07, 818 | -1.19209e-07, 819 | 0.0101575, 820 | 0.0157325, 821 | 0.00632757, 822 | 0.00304737, 823 | 0.00499583, 824 | 0.00435831, 825 | 0.118751, 826 | 0.168659, 827 | -1.19209e-07, 828 | 0.00314191, 829 | -9.77516e-08, 830 | 0.00436476, 831 | 0.189702, 832 | -1.19209e-07, 833 | 0.00282001, 834 | 0.0572801, 835 | -1.19209e-07, 836 | 0.161181, 837 | 8.17046e-06, 838 | -1.19209e-07, 839 | -1.19209e-07, 840 | 0.483668, 841 | 7.55127e-06, 842 | 0.165453, 843 | 0.0686433, 844 | 0.00385028, 845 | -1.19209e-07, 846 | -1.19209e-07, 847 | -1.19209e-07, 848 | 0.171431, 849 | 0.1613, 850 | 0.0363742, 851 | 0.0331779, 852 | 0.00989657, 853 | 0.0368697, 854 | -1.19209e-07, 855 | 0.00595139, 856 | 0.000824153, 857 | 0.0157963, 858 | 0.0297559, 859 | -1.19209e-07, 860 | 0.0166192, 861 | 0.0207834, 862 | 0.0515526, 863 | 0.355895, 864 | -1.19209e-07, 865 | 0.00448397, 866 | 5.37655e-07, 867 | -1.19209e-07, 868 | 0.00433007, 869 | 0.00828691, 870 | 0.0448462, 871 | 1.47713e-06, 872 | 0.161181, 873 | -1.19209e-07, 874 | 0.081314, 875 | 0.145838, 876 | 0.161182, 877 | 0.00161065, 878 | 0.161355, 879 | -1.19209e-07, 880 | -1.19209e-07, 881 | -1.19209e-07, 882 | 0.247316, 883 | 0.452363, 884 | -1.19209e-07, 885 | -1.19209e-07, 886 | -1.19209e-07, 887 | 0.0178929, 888 | -1.19209e-07, 889 | 0.0150771, 890 | -1.19209e-07, 891 | 0.00370061, 892 | 0.380984, 893 | -1.19209e-07, 894 | 0.0151635, 895 | -1.19209e-07, 896 | 0.00830182, 897 | -1.19209e-07, 898 | 0.0672497, 899 | 0.161181, 900 | 0.00545293, 901 | -1.15633e-07, 902 | -1.19209e-07, 903 | -1.19209e-07, 904 | 0.162243, 905 | -1.19209e-07, 906 | 0.00663234, 907 | -1.19209e-07, 908 | 0.154684, 909 | 0.0153321, 910 | 0.161181, 911 | -1.19209e-07, 912 | -1.19209e-07, 913 | -1.19209e-07, 914 | 0.322362, 915 | -1.19209e-07, 916 | 0.169492, 917 | 0.287824, 918 | -1.00136e-07, 919 | 0.0665158, 920 | 0.163838, 921 | 0.161181, 922 | -1.19209e-07, 923 | -1.19209e-07, 924 | 2.04408e-06, 925 | 0.00034586, 926 | -1.19209e-07, 927 | -1.19209e-07, 928 | 0.0181375, 929 | -1.19209e-07, 930 | 0.162355, 931 | 0.117032, 932 | 1.31139e-08, 933 | 0.00130493, 934 | 0.322362, 935 | -1.18017e-07, 936 | 0.0115383, 937 | -1.19209e-07, 938 | -1.19209e-07, 939 | 0.0114006, 940 | 0.183721, 941 | -2.62256e-08, 942 | 0.161181, 943 | -7.74859e-08, 944 | -1.19209e-07, 945 | 0.0276425, 946 | 0.00435693, 947 | -1.19209e-07, 948 | -1.19209e-07, 949 | -1.19209e-07, 950 | 0.169145, 951 | 3.8113e-06, 952 | -1.19209e-07, 953 | -1.19209e-07, 954 | 1.07291e-07, 955 | -1.19209e-07, 956 | 0.316012, 957 | 0.214695, 958 | -1.19209e-07, 959 | 0.00760226, 960 | 0.161181, 961 | 0.161181, 962 | 0.161181, 963 | -1.19209e-07, 964 | 0.100666, 965 | -1.19209e-07, 966 | -9.41753e-08, 967 | -1.19209e-07, 968 | -------------------------------------------------------------------------------- /model.py: -------------------------------------------------------------------------------- 1 | import glob 2 | import cv2 3 | import load_dataset 4 | import tensorflow as tf 5 | import numpy as np 6 | 7 | IM_SIZE = 32 8 | BATCH_SIZE = 100 9 | 10 | sess = tf.InteractiveSession() 11 | 12 | data_sets = load_dataset.read_dataset(20) 13 | print "training dataset shape >> " + str(data_sets.train.images.shape) 14 | print "validation dataset shape >> " + str(data_sets.validation.images.shape) 15 | 16 | print data_sets.train.labels 17 | print data_sets.validation.labels 18 | 19 | def weight_variable(shape): 20 | initial = tf.truncated_normal(shape, stddev=0.05) 21 | return tf.Variable(initial) 22 | 23 | def bias_variable(shape): 24 | initial = tf.constant(0.1, shape=shape) 25 | return tf.Variable(initial) 26 | 27 | def conv2d(x, W): 28 | return tf.nn.conv2d(x, W, strides=[1,1,1,1], padding='SAME') 29 | 30 | def max_pool_2x2(x): 31 | return tf.nn.max_pool(x, ksize=[1,2,2,1], strides=[1,2,2,1], padding='SAME') 32 | 33 | x = tf.placeholder(tf.float32, shape=[None,IM_SIZE,IM_SIZE,3]) 34 | y_ = tf.placeholder(tf.float32, shape=[None,2]) 35 | keep_prob = tf.placeholder("float") 36 | 37 | 38 | x_reshape = tf.reshape(x, [-1,IM_SIZE,IM_SIZE,3]) 39 | 40 | 41 | W_conv1 = weight_variable([5,5,3,16]) 42 | b_conv1 = bias_variable([16]) 43 | h_conv1 = tf.nn.relu(conv2d(x_reshape, W_conv1) + b_conv1) 44 | 45 | W_conv2 = weight_variable([5,5,16,16]) 46 | b_conv2 = bias_variable([16]) 47 | h_conv2 = tf.nn.relu(conv2d(h_conv1, W_conv2) + b_conv2) 48 | 49 | W_conv3 = weight_variable([5,5,16,16]) 50 | b_conv3 = bias_variable([16]) 51 | h_conv3 = tf.nn.relu(conv2d(h_conv2, W_conv3) + b_conv3) 52 | 53 | 54 | """ 55 | 1 pooling 56 | """ 57 | h_pool3 = max_pool_2x2(h_conv3) 58 | 59 | 60 | W_conv4 = weight_variable([5,5,16,32]) 61 | b_conv4 = bias_variable([32]) 62 | h_conv4 = tf.nn.relu(conv2d(h_pool3, W_conv4) + b_conv4) 63 | 64 | W_conv5 = weight_variable([5,5,32,32]) 65 | b_conv5 = bias_variable([32]) 66 | h_conv5 = tf.nn.relu(conv2d(h_conv4, W_conv5) + b_conv5) 67 | 68 | W_conv6 = weight_variable([5,5,32,32]) 69 | b_conv6 = bias_variable([32]) 70 | h_conv6 = tf.nn.relu(conv2d(h_conv5, W_conv6) + b_conv6) 71 | 72 | 73 | """ 74 | 2 pooling 75 | """ 76 | h_pool6 = max_pool_2x2(h_conv6) 77 | 78 | W_conv7 = weight_variable([5,5,32,64]) 79 | b_conv7 = bias_variable([64]) 80 | h_conv7 = tf.nn.relu(conv2d(h_pool6, W_conv7) + b_conv7) 81 | 82 | W_conv8 = weight_variable([5,5,64,64]) 83 | b_conv8 = weight_variable([64]) 84 | h_conv8 = tf.nn.relu(conv2d(h_conv7, W_conv8) + b_conv8) 85 | 86 | W_conv9 = weight_variable([5,5,64,64]) 87 | b_conv9 = bias_variable([64]) 88 | h_conv9 = tf.nn.relu(conv2d(h_conv8, W_conv9) + b_conv9) 89 | 90 | 91 | """ 92 | 3 pooling 93 | """ 94 | h_pool9 = max_pool_2x2(h_conv9) 95 | h_pool9_flat = tf.reshape(h_pool9, [-1, (IM_SIZE/8)*(IM_SIZE/8)*64]) 96 | 97 | 98 | 99 | W_fc1 = weight_variable([(IM_SIZE/8)*(IM_SIZE/8)*64, 1024]) 100 | b_fc1 = bias_variable([1024]) 101 | h_fc1 = tf.nn.relu(tf.matmul(h_pool9_flat, W_fc1) + b_fc1) 102 | h_fc1_drop = tf.nn.dropout(h_fc1, keep_prob) 103 | 104 | W_fc2 = weight_variable([1024,1024]) 105 | b_fc2 = bias_variable([1024]) 106 | h_fc2 = tf.nn.relu(tf.matmul(h_fc1_drop, W_fc2) + b_fc2) 107 | h_fc2_drop = tf.nn.dropout(h_fc2, keep_prob) 108 | 109 | W_fc3 = weight_variable([1024,2]) 110 | b_fc3 = bias_variable([2]) 111 | 112 | y_matmul = tf.matmul(h_fc2_drop, W_fc3) + b_fc3 113 | 114 | y_conv = tf.nn.softmax(y_matmul) 115 | 116 | l2_loss = tf.reduce_mean(-tf.reduce_sum(y_*tf.log(y_conv+1e-7), reduction_indices=[1])) 117 | train_step = tf.train.AdamOptimizer(1e-3).minimize(l2_loss) 118 | correct_prediction = tf.equal(tf.argmax(y_conv, 1), tf.argmax(y_, 1)) 119 | accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32)) 120 | tf.initialize_all_variables().run() 121 | 122 | loss_curve = open('./loss_curve.csv', 'w') 123 | saver = tf.train.Saver() 124 | 125 | SAVE_PATH = "./checkpoint/" 126 | for iteration in xrange(1000000): 127 | if iteration % 70 == 0: 128 | valid_batch_x, valid_batch_y = data_sets.validation.next_batch(BATCH_SIZE) 129 | acc = accuracy.eval(feed_dict={x: valid_batch_x, 130 | y_: valid_batch_y, keep_prob:1.0}) 131 | loss = sess.run(l2_loss, feed_dict={x: valid_batch_x, 132 | y_: valid_batch_y, keep_prob:1.0}) 133 | 134 | print '%dth iteration... accuracy >> %lf, loss .. %lf' % (iteration, acc, loss) 135 | contents = str(loss) + ',\n' 136 | loss_curve.write(contents) 137 | 138 | batch_x, batch_y = data_sets.train.next_batch(BATCH_SIZE) 139 | train_step.run(feed_dict={x:batch_x, y_:batch_y, keep_prob:0.5}) 140 | 141 | if iteration % 4000 == 0 and iteration > 0 == 0 : 142 | checkpoint_file = SAVE_PATH + "ver1.0_iteration." + str(iteration) + ".ckpt" 143 | saver.save(sess,checkpoint_file) 144 | print "CNN models are saved in %s." % checkpoint_file 145 | 146 | 147 | 148 | -------------------------------------------------------------------------------- /only_eye_region/readme.md: -------------------------------------------------------------------------------- 1 | #only_eye_region directory 2 | This directory consist of 2 type of eye region images. 3 | 4 | 1. open-eyed images.(about 4200 images.) 5 | 2. closed-eyed images. (about 3300 images.) 6 | 7 | 8 | -------------------------------------------------------------------------------- /only_eye_region/readme.txt: -------------------------------------------------------------------------------- 1 | #only_eye_region directory 2 | This directory consist of 2 type of eye region images. 3 | 1. open-eyed images.(about 4200 images.) 4 | 2. closed-eyed images. (about 3300 images.) 5 | 6 | 7 | -------------------------------------------------------------------------------- /test.py: -------------------------------------------------------------------------------- 1 | import cv2 2 | import numpy as np 3 | 4 | ix, iy = -1, -1 5 | 6 | def draw_circle(event, x, y, flags, param): 7 | global ix, iy 8 | if event == cv2.EVENT_LBUTTONDBLCLK: 9 | cv2.circle(img, (x,y), 100, (255,0,0), -1) 10 | ix, iy = x,y 11 | 12 | 13 | image = np.zeros((512,512,3), np.uin8) 14 | cv2.namedWindow("image") 15 | cv2.SetMouseCallback("image", draw_circle) 16 | 17 | while(1): 18 | cv2.imshow("image", ig) 19 | k = cv2.waitKey(20) & 0xFF 20 | if k == 27: break 21 | elif k == ord('a'): print ix,iy 22 | cv2.destroyAllWindows() 23 | --------------------------------------------------------------------------------