├── README.md ├── cv_close_eye_detect.py ├── haarcascade_eye_tree_eyeglasses.xml └── haarcascade_frontalface_alt.xml /README.md: -------------------------------------------------------------------------------- 1 | # Closed-Eye-Detection-with-opencv 2 | First, using 'haarcascade_frontalface_alt.xml' to check if there is a face or not. Second, using 'haarcascade_eye_tree_eyeglasses.xml' to check if there is a opened-eye or not. Thus, If I get data: face(o) -> eyes(x), that is the person who is closing his eyes. 3 | -------------------------------------------------------------------------------- /cv_close_eye_detect.py: -------------------------------------------------------------------------------- 1 | import cv2 2 | eye_cascPath = 'D:/DeepLearning/face/opencv-face-recognition-python-master/opencv-files/haarcascade_eye_tree_eyeglasses.xml' #eye detect model 3 | face_cascPath = 'D:/DeepLearning/face/opencv-face-recognition-python-master/opencv-files/haarcascade_frontalface_alt.xml' #face detect model 4 | faceCascade = cv2.CascadeClassifier(face_cascPath) 5 | eyeCascade = cv2.CascadeClassifier(eye_cascPath) 6 | 7 | cap = cv2.VideoCapture(0) 8 | while 1: 9 | ret, img = cap.read() 10 | if ret: 11 | frame = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) 12 | # Detect faces in the image 13 | faces = faceCascade.detectMultiScale( 14 | frame, 15 | scaleFactor=1.1, 16 | minNeighbors=5, 17 | minSize=(30, 30), 18 | # flags = cv2.CV_HAAR_SCALE_IMAGE 19 | ) 20 | # print("Found {0} faces!".format(len(faces))) 21 | if len(faces) > 0: 22 | # Draw a rectangle around the faces 23 | for (x, y, w, h) in faces: 24 | cv2.rectangle(img, (x, y), (x + w, y + h), (0, 255, 0), 2) 25 | frame_tmp = img[faces[0][1]:faces[0][1] + faces[0][3], faces[0][0]:faces[0][0] + faces[0][2]:1, :] 26 | frame = frame[faces[0][1]:faces[0][1] + faces[0][3], faces[0][0]:faces[0][0] + faces[0][2]:1] 27 | eyes = eyeCascade.detectMultiScale( 28 | frame, 29 | scaleFactor=1.1, 30 | minNeighbors=5, 31 | minSize=(30, 30), 32 | # flags = cv2.CV_HAAR_SCALE_IMAGE 33 | ) 34 | if len(eyes) == 0: 35 | print('no eyes!!!') 36 | else: 37 | print('eyes!!!') 38 | frame_tmp = cv2.resize(frame_tmp, (400, 400), interpolation=cv2.INTER_LINEAR) 39 | cv2.imshow('Face Recognition', frame_tmp) 40 | waitkey = cv2.waitKey(1) 41 | if waitkey == ord('q') or waitkey == ord('Q'): 42 | cv2.destroyAllWindows() 43 | break 44 | --------------------------------------------------------------------------------