Eye Detection OpenCV Python With Source Code

The Real-Time Eye Detection OpenCV Python was developed using Python Detection OpenCV, this Machine Learning Eye Tracking OpenCV Python, we’re going to discuss object detection with HaarCascades frontalface.

We’ll do eye detection to start.

In order to do object recognition/detection with eye cascade files, you first need cascade files. For the extremely popular tasks, these already exist.

Detecting things like faces, cars, smiles, eyes, and license plates for example are all pretty prevalent.

Project Information’s

Project Name:Real-Time Eye Detection In Python
Language/s Used:Python OpenCV
Python version (Recommended):2.x or 3.x
Database:None
Type:Machine Learning
Developer:IT SOURCECODE
Updates:0
Eye Detection In Python – Project Information

An OpenCV Eye Detection Python you will learn about detecting a human eye with the feature mappers knows as haar cascades.

Here in the project, we will use the python language along with the OpenCV library for the algorithm execution and image processing respectively.

The haar cascades we are going to use in the project are pretrained and stored along with the OpenCV library as haarcascade_eye_default.xml

The project develops a basic understanding of the systems such as driver drowsiness detection, eye detection, face detection and also the haar cascades usage with the OpenCV library.

To start creating a Real-Time Eye Detection, make sure that you have installed Python in your computer.

Prerequisites:

Before starting with this Python project with source code, you should be familiar with the computer vision library of Python that is OpenCV.

OpenCV are the Python package that are necessary for this project in Python. To install this, simply run this pip command in your terminal:

pip install opencv-python

Steps on How To Detect Eyes OpenCV Python with Source Code

Time needed: 5 minutes

These are the steps on how to build Real-Time Eye Detection OpenCV Python With Source Code

  1. Step 1: Download and unzip the zip file.


    The project folder contains 2 files:

    1.) eye_detection.py – a main python file to detect the eye.
    2.) haarcascade_eye.xml – a file that load cascade from the camera.download source code

  2. Step 2: Creating a code for eye detection.

    Next, create a code that can detect human eye and name it as “eye_detection.py“.
    eye detection source code

  3. Step 3: Run eye_detection.py.

    The beginner Python project is now complete, you can run the Python file from the command prompt. Make sure to give an camera path using ‘-c’ argument.
    eye detection run python file

Complete Source Code:

import cv2
# Load the cascade
face_cascade = cv2.CascadeClassifier('haarcascade_eye.xml')
# To capture video stream from webcam. 
cap = cv2.VideoCapture(0)
# To use a video file as input 
# cap = cv2.VideoCapture('filename.mp4')
while True:
    # Read the frame
    _, img = cap.read()
    # Convert to grayscale
    gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
    # Detect the faces
    faces = face_cascade.detectMultiScale(gray, 1.1, 4)
    # Draw the rectangle around each face
    for (x, y, w, h) in faces:
        cv2.rectangle(img, (x, y), (x+w, y+h), (255, 0, 0), 2)
    # Display
    cv2.imshow('img', img)
    # Stop if escape key is pressed
    k = cv2.waitKey(30) & 0xff
    if k==27:
        break
# Release the VideoCapture object
cap.release()

Output:

Download Source Code below

Summary

In this tutorial about Eye Detection OpenCV Python you will learn about detecting a blink of human eye with the feature mappers knows as haar cascades.

Here in the project, we will use the python language along with the OpenCV library for the algorithm execution and image processing respectively.

The haar cascades we are going to use in the project are pretrained and stored along with the OpenCV library as haarcascade_eye.xml files.

1 thought on “Eye Detection OpenCV Python With Source Code”

Leave a Comment