Eyeblink detection using OpenCV, and Python
Tags: python, opencv, dlib, machinelearning
Published: 2020-08-04 19:03:29


Complete code can also be found **[here](https://gist.github.com/Adarshg315/8329abf7c25664f701a158e94f29a49d#file-blink-py)**

{% gist https://gist.github.com/Adarshg315/8329abf7c25664f701a158e94f29a49d %}

To test the following on the command line

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

and

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

![](https://media.giphy.com/media/26n6V5D3LWkDxj5GE/giphy.gif)
breaking up the [above](https://gist.github.com/Adarshg315/8329abf7c25664f701a158e94f29a49d#file-blink-py) code below

**1. Import required libraries**
![](https://miro.medium.com/max/1400/0*SX5nD8xqmu97-eO5)

[imutils](https://github.com/jrosebr1/imutils) library -FileVideoStream, VideoStream

[dlib](http://dlib.net/) —contains an implementation of facial landmark detection

[cv2](https://opencv.org/)- OpenCV library

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

![](https://miro.medium.com/max/1264/0*p8A16mrPL6ZK6i8Q)
![](https://miro.medium.com/max/1400/0*zDaJrK95AOTmJPaR)


**3. Passing Command Line Arguments**

![](https://miro.medium.com/max/1400/0*Z0ecR2TcnMsHJ3ra)

**— 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**

![](https://miro.medium.com/max/1400/0*8k3jPJEhr-xeXF42)

**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**

![](https://miro.medium.com/max/1400/0*CnfImcOBVC61kY14)

**6. Start the video streaming thread**

![](https://miro.medium.com/max/1400/0*SnVuivGPrHF8GDzc)

FileVideoStream for disk videos and VideoStream for video from the webcam

**7. Main loop**

![](https://miro.medium.com/max/1400/0*w6iV8C6cTxiel7eR)

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**

![](https://miro.medium.com/max/1400/0*D2Wio0EeVCAlfKsn)

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

![](https://miro.medium.com/max/1400/0*CHpoJTtMCoL4hWRK)

**10. Counting Blinks**

![](https://miro.medium.com/max/1400/0*pIkM1w29cPT4Iylx)

**11. Drawing Blinks and EAR on the stream**

![](https://miro.medium.com/max/1400/0*hlWYgtVjCCDgMAHh)

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