Eye Blink Detection and Tracking Using OpenCV
by Pushkar_Shepal in Circuits > Raspberry Pi
364 Views, 1 Favorites, 0 Comments
Eye Blink Detection and Tracking Using OpenCV
Eye blink detection and tracking is a computer vision technique that analyzes eye movements and blinks to provide valuable insights into a person's attention and alertness levels. By monitoring and analyzing the behavior of eye blinks, we can gain insights into various aspects of human interaction and behavior.
The ability to accurately detect and track eye blinks has several practical applications. One important application is drowsiness detection, particularly in the context of driving or operating heavy machinery. By continuously monitoring a person's eye blinks, we can identify signs of fatigue and drowsiness, helping to prevent accidents and improve safety.
Eye blink detection and tracking also plays a crucial role in attention monitoring systems. By analyzing the frequency and duration of eye blinks, we can assess an individual's level of engagement and focus. This can be valuable in fields such as education, user experience design, and market research, where understanding attention and engagement is key.
Furthermore, eye blink detection can be used in human-computer interaction systems. By tracking eye blinks, we can develop interfaces that respond to specific eye movements, enabling users to interact with computers or devices in a more intuitive and hands-free manner. This opens up possibilities for applications such as eye-controlled navigation, gaming, and accessibility for individuals with motor impairments.
In this blog, we will guide you through the process of implementing eye blink detection and tracking using the OpenCV library. OpenCV is a popular computer vision library that provides powerful tools for image and video processing. By following the steps outlined in this guide, you will learn how to build a real-time eye blink detection system that can be used for various practical applications.
Let's dive into the implementation details and explore the exciting world of eye blink detection and tracking using OpenCV!
Supplies
Libraries: OpenCV , Dlib, Face_Recognition
Hardware: BrainyPI
Set Up the Environment
To get started, ensure that you have OpenCV and dlib installed on your system. Import the necessary libraries: cv2 for OpenCV functions, numpy for array operations, module for custom functions, and time for measuring frame rate dlib for face detection.
To install dlib use the following command
pip3 install dlib
pip3 install opencv-python
pip3 install face_recognition
Note: - Setting up dlib can take some time so be patient!!
Initialize Variables and Parameters
Initialize the variables that will be used throughout the code.
COUNTER = 0
TOTAL_BLINKS = 0
CLOSED_EYES_FRAME = 3
cameraID = 0
videoPath = "Video/Your Eyes Independently_Trim5.mp4"
FRAME_COUNTER = 0
START_TIME = time.time()
FPS = 0
Adjust the values of these variables based on your specific requirements
Start the Main Loop
Inside the loop, perform the following steps for each frame:
- Increment the frame counter.
- Read a frame from the camera or video file.
- Convert the frame to grayscale using cv.cvtColor(frame, cv.COLOR_BGR2GRAY).
- Perform face detection using a face detection module or function. Pass the grayscale frame to the function and obtain the face region.
- If a face is detected:
- Perform facial landmark detection to locate the eyes. Pass the original frame, grayscale frame, and face region to the landmark detection function.
- Calculate blink ratios based on the eye landmarks. Use a blink detection function that takes the eye landmarks as input and returns the blink ratio.
- Visualize the blink ratio using circles of varying sizes. Use cv.circle() to draw the circles on the frame.
- Increment the blink counter if a blink is detected.
- Display the frame with visualizations and text overlays using cv.imshow('Frame', frame).
- Calculate and display the frames per second (FPS) of the video stream.
- Check for user input to exit the loop if needed.
Implementing on BrainyPI:
To implement the eye tracking code on BrainyPI using GitLab, you can follow these steps:
a. Ensure that you have Git installed on your local machine. You can download Git from the official website and follow the installation instructions.
b. Create a Git repository on a GitLab server to host your eye tracking code. You can create a new repository or use an existing one.
c. Clone the Git repository to your local machine using the following command:
git clone git@gitlab.com:your-username/your-repository.git
Replace your-username with your GitLab username and your-repository with the name of your GitLab repository.
d. Copy the eye tracking Python file to the local Git repository directory.
e. Commit the changes and push them to the remote GitLab repository using the following commands:
git add eye_tracking.py
git commit -m "Added eye Tracking code"
git push origin main
Replace main with the branch name of your GitLab repository if it's different.
f. Open a terminal or command prompt on your device. Use the following command to establish an ssh connection with your BrainyPI device.
ssh -X pi@auth.iotiot.in -p 65530
g. Now, on your BrainyPI device, make sure you have Git installed. If not, you can install it using the package manager of your operating system.
h. Open a terminal or command prompt on your BrainyPI device.
i. Clone the Git repository from your GitLab server to your BrainyPI device using the following command:
git clone git@gitlab.com:your-username/your-repository.git
Replace your-username with your GitLab username and your-repository with the name of your GitLab repository.
j. Navigate to the cloned repository directory on your BrainyPI device.
k. Run the Object Tracking code using the following command:
python3 eye_Tracking.py
The eye tracking program will start running, and you will see the output on the terminal of your BrainyPI device.
Conclusion
Eye blink detection and tracking using OpenCV can provide valuable insights into a person's eye movements and blinks. It has various applications, including drowsiness detection, attention monitoring, and human-computer interaction. By following the steps outlined in this blog, you can implement your own eye blink detection system.