def FatigueDetect(img, pos): # print 'Detect...' print pos faces = detector(img, 0) #face_shape = dlib.rectangle(left = pos[0],top = pos[1],bottom = pos[0]+pos[2],right = pos[0]+pos[3]) shape = landmark_predictor(img, faces[0]) #img,shape= img_process.getRotImg(img,shape) left_eye, t = img_process.getLeftEye(img, shape) right_eye, t = img_process.getRightEye(img, shape) mouth, t = img_process.getMouth(img, shape) left_eye = cv2.resize(left_eye, (64, 32)) right_eye = cv2.resize(right_eye, (64, 32)) mouth = cv2.resize(mouth, (64, 64)) cv2.imshow('LeftEye', left_eye) cv2.imshow('RightEye', right_eye) cv2.imshow('Mouth', mouth) for i in range(68): cv2.circle(img, (shape.part(i).x, shape.part(i).y), 2, (255, 255, 255), 2) #print 'getLbp...' left_lbp = img_process.getLBPFeatures(left_eye, CELL_SIZE) right_lbp = img_process.getLBPFeatures(right_eye, CELL_SIZE) mouth_lbp = img_process.getLBPFeatures(mouth, CELL_SIZE) feature = np.concatenate((left_lbp, right_lbp, mouth_lbp)) #feature = feature.reshape(-1,1) #print 'get...End' #print 'Detect...End' return svm.predict(feature.reshape(1, -1))
def preData(): pbr = ProgressBar(maxval = 660) global All_block,X_train,Y_train now = time.time() print 'Data process...' for i in range(440): All_block.append(block(i,1)) for i in range(220): All_block.append(block(i+440,0)) random.shuffle(All_block) pbr.start() for i in range(660): pbr.update(i) id = All_block[i].id label = All_block[i].label Y_train.append(label) path = AWAKE_PATH if label == 0: id -= 440 path = SLEEP_PATH left_eye = cv2.imread(path+'LeftEyes/'+str(id)+'.jpeg',0) right_eye = cv2.imread(path + 'RightEyes/' + str(id) + '.jpeg',0) mouth = cv2.imread(path + 'Mouth/' + str(id) + '.jpeg',0) left_lbp = img_process.getLBPFeatures(left_eye,CELL_SIZE) right_lbp = img_process.getLBPFeatures(right_eye,CELL_SIZE) mouth_lbp = img_process.getLBPFeatures(mouth,CELL_SIZE) feature = np.concatenate((left_lbp,right_lbp,mouth_lbp)) X_train.append(feature) pbr.finish() X_train = np.array(X_train) print X_train.shape print 'End\nCost Time:' + str(time.time() - now)
def see(): open_eye = cv2.imread(AWAKE_PATH + 'LeftEyes/' + str(400) + '.jpeg', 0) close_eye = cv2.imread(SLEEP_PATH + 'LeftEyes/' + str(180) + '.jpeg', 0) close_mouth = cv2.imread(AWAKE_PATH + 'Mouth/' + str(0) + '.jpeg', 0) open_mouth = cv2.imread(SLEEP_PATH + 'Mouth/' + str(19) + '.jpeg', 0) plt.figure(1) plt.subplot(2, 2, 1) plt.gray() plt.imshow(LBP(open_eye)) plt.subplot(2, 2, 2) plt.imshow(LBP(close_eye)) plt.subplot(2, 2, 3) plt.xlim(0, 256 * 2) plt.ylim(0, 0.20) plt.bar(range(256 * 2), img_process.getLBPFeatures(open_eye, CELL_SIZE)) plt.gray() plt.grid() plt.subplot(2, 2, 4) plt.xlim(0, 256 * 2) plt.ylim(0, 0.20) plt.bar(range(256 * 2), img_process.getLBPFeatures(close_eye, CELL_SIZE)) plt.gray() plt.grid() plt.figure(2) plt.subplot(2, 2, 1) plt.gray() plt.imshow(LBP(open_mouth)) plt.subplot(2, 2, 2) plt.gray() plt.imshow(LBP(close_mouth)) plt.subplot(2, 2, 3) plt.gray() plt.grid() plt.xlim(0, 256 * 4) plt.ylim(0, 0.4) plt.bar(range(256 * 4), img_process.getLBPFeatures(open_mouth, CELL_SIZE)) plt.subplot(2, 2, 4) plt.gray() plt.grid() plt.xlim(0, 256 * 4) plt.ylim(0, 0.4) plt.bar(range(256 * 4), img_process.getLBPFeatures(close_mouth, CELL_SIZE)) plt.show()
def preTest(): global X_test,Y_test pbr = ProgressBar(maxval = 30) pbr.start() for i in range(1,31): pbr.update(i) id = i label = 1 path = TEST_PATH + 'Awake/' if i > 23: path = TEST_PATH + 'Sleep/' id -= 23 label = 0 img = cv2.imread(path+str(id)+'.jpeg',0) img = cv2.resize(img,(320,320)) shape = landmark_predictor(img,face) img,shape= img_process.getRotImg(img,shape) left_eye,t= img_process.getLeftEye(img,shape) right_eye,t = img_process.getRightEye(img,shape) mouth,t = img_process.getMouth(img,shape) left_eye = cv2.resize(left_eye,(64,32)) right_eye = cv2.resize(right_eye,(64,32)) mouth = cv2.resize(mouth,(64,64)) #cv2.imshow('GG1',left_eye) #cv2.imshow('GG2',right_eye) #cv2.imshow('GG3',mouth) #key = cv2.waitKey(0) #if key == ord('q'): # break left_lbp = img_process.getLBPFeatures(left_eye,CELL_SIZE) right_lbp = img_process.getLBPFeatures(right_eye,CELL_SIZE) mouth_lbp = img_process.getLBPFeatures(mouth,CELL_SIZE) feature = np.concatenate((left_lbp,right_lbp,mouth_lbp)) X_test.append(feature) Y_test.append(label) pbr.finish() X_test = np.array(X_test) Y_test = np.array(Y_test) print X_test,Y_test
def preTest(): global X_test, Y_test for i in range(1, 31): id = i label = 1 path = TEST_PATH + 'Awake/' if i > 23: path = TEST_PATH + 'Sleep/' id -= 23 label = 0 img = cv2.imread(path + str(id) + '.jpeg', 0) img = cv2.resize(img, (320, 320)) shape = landmark_predictor(img, face) img, shape = img_process.getRotImg(img, shape) left_eye, t = img_process.getLeftEye(img, shape) right_eye, t = img_process.getRightEye(img, shape) mouth, t = img_process.getMouth(img, shape) left_eye = cv2.resize(left_eye, (64, 32)) right_eye = cv2.resize(right_eye, (64, 32)) mouth = cv2.resize(mouth, (64, 64)) """ cv2.imshow('img',img) cv2.imshow('GG1',left_eye) cv2.imshow('GG2',right_eye) cv2.imshow('GG3',mouth) key = cv2.waitKey(0) if key == ord('q'): break """ left_lbp = img_process.getLBPFeatures(left_eye, CELL_SIZE) right_lbp = img_process.getLBPFeatures(right_eye, CELL_SIZE) mouth_lbp = img_process.getLBPFeatures(mouth, CELL_SIZE) feature = np.concatenate((left_lbp, right_lbp, mouth_lbp)) X_test.append(feature) Y_test.append(label) X_test = np.array(X_test) Y_test = np.array(Y_test)