Eye Detection Program in Python OpenCV
In this article, you will learn a very simple eye detection program in Python using the OpenCV. We will explain all the processes step by step, which will help you implement them easily.
There is a variety of computer applications that identify the eye in digital images, like- security systems, criminal identification, healthcare, and so on. The eye-detection algorithms focus on the detection of the frontal human eye. The functions of the Python OpenCV library are mainly aimed at real-time computer vision. It is mainly used to do all the operations for image processing as well as detect objects. OpenCV already contains many pre-trained classifiers for faces, eyes, smiles, etc.
Module & XML Required
For the eye detection program, we need the following module and XML file:
- OpenCV Module
- haarcascade_eye.xml
Install Module and Activate VirtualEnv
We hope you have already installed the OpenCV module and activated the virtual environment. If you do not have it, please follow this article.
Python OpenCV Overview and Examples
Download haarcascade_eye.xml
You can download the file haarcascade_eye.xml from the GitHub repository. Once downloaded, save this file in your working directory. The haarcascade_eye.xml is a haar cascade designed by OpenCV to detect the eye in an image or video.
Eye Detection Code Explanation
First, we will load the image using the OpenCV imread() method-
img = cv2.imread('lady.jpg')
The cv2.CascadeClassifier() method is used to load the necessary XML classifier for detecting objects. The Haar Cascade Model is a machine learning object detection algorithm used to identify objects in an image. Initially, the algorithm needs a lot of positive images of faces and negative images without faces to train the classifier.
eye_cascade = cv2.CascadeClassifier('haarcascade_eye.xml')
The eye detection is performed by the cv2.detectMultiScale() function. It returns boundary rectangles for the detected eye. In this method, we have passed three parameters. The image is the input image, scaleFactor specifies how much the image size is reduced at each image scale, and minNeighbors specifies how many neighbors each candidate rectangle should have to retain it. This parameter will affect the quality of the detected object.
eyes = eye_cascade.detectMultiScale(image, scaleFactor = 1.2, minNeighbors = 4)
Next, we will draw a rectangle on the detected boundary using the cv2.rectangle() method.
for (x,y,w,h) in eyes:
cv2.rectangle(image,(x,y),(x+w,y+h),(0, 255, 0),5)
Next, we will display the image using imshow() method.
cv2.imshow("Eyes Detected", image)
Complete Code: Eye Detection Program
import cv2
image = cv2.imread("beauty.jpg")
eye_cascade = cv2.CascadeClassifier('haarcascade_eye.xml')
eyes = eye_cascade.detectMultiScale(image, scaleFactor = 1.2,
minNeighbors = 4)
for (x,y,w,h) in eyes:
cv2.rectangle(image,(x,y),(x+w,y+h),(0, 255, 0),5)
cv2.imshow("Eye Detected", image)
cv2.waitKey(0)
cv2.destroyAllWindows()
The above code returns output something like this-
Related Articles
Blur image in OpenCV PythonPython project ideas for beginners
Extract text from image using Python
How to save figure in matplotlib pyplot
Adaptive Thresholding in Python OpenCV
Detect Specific Color From Image using Python OpenCV
Python OpenCV Specific Color Detection from Capture Video
OpenCV bitwise and
How to capture a video in Python OpenCV and save
Python OpenCV Overlaying or Blending Two Images
Contour Detection using Python OpenCV
Harris Corner Detection using Python OpenCV
OpenCV hog
Face Recognition OpenCV Source Code
Canny Edge Detector OpenCV Python
Python NumPy: Overview and Examples
Image processing using Python Pillow
Python OpenCV Histogram Equalization
Python OpenCV Histogram of Color Image
Python OpenCV Histogram of Grayscale Image
Python OpenCV Image Filtering
Python OpenCV ColorMap
Python OpenCV Gaussian Blur Filtering
Python OpenCV Overview and Examples