DEV Community

Cover image for Eyeblink detection using OpenCV, and Python
Adarsh Goyal
Adarsh Goyal

Posted on

Eyeblink detection using OpenCV, and Python

Complete code can also be found here

To test the following on the command line

(for live video) ~# python3 --shape-predictor shape_predictor_68_face_landmarks.dat


(for demo video) ~# python3 --shape-predictor shape_predictor_68_face_landmarks.dat --video “replace this with demo video file path”

breaking up the above code below

1. Import required libraries

imutils library -FileVideoStream, VideoStream

dlib —contains an implementation of facial landmark detection

cv2- OpenCV library

2. Function to calculate EAR as per the research paper.

3. Passing Command Line Arguments

— shape-predictor: This is the path to dlib's pre-trained facial landmark detector.

— video: Optional switch controls the path to an input video file residing on disk.

4. Important variables to tune the implementation

EYE_AR_THRESH - If the eye aspect ratio falls below a certain threshold and then rises above the threshold, then we’ll register a “blink”

EYE_AR_CONSEC_FRAME - this value is set to 3 to indicate that three successive frames with an eye aspect ratio less than
EYE_AR_THRESH must happen in order for a blink to be registered.

COUNTER — total number of successive frames that have an eye aspect ratio less than EYE_AR_THRESH

TOTAL — number of blinks that have taken place while the script has been running.

5. Initialize dlib’s face detector and facial landmark detector

6. Start the video streaming thread

FileVideoStream for disk videos and VideoStream for video from the webcam

7. Main loop

We start looping over frames from our video stream. If we are accessing a video file stream and there are no more frames left in the video, we break from the loop.

Line 61 reads the frame by resizing it and converting it to grayscale (Lines 62 and 63), then detect faces in the grayscale frame on Line 65 via dlib’s built-in face detector.

8. Loop over each of the faces in the frame and then apply facial landmark detection to each of them

9. Visualizing the facial landmarks for the eye regions themselves:

10. Counting Blinks

11. Drawing Blinks and EAR on the stream

can someone suggest a better solution using SVM put up in the comment please

Top comments (0)