How to detect eyes in an image using OpenCV Python?
If you are working on a computer vision project, detecting eyes in an image is a basic but crucial task. OpenCV is a popular library in Python for image processing tasks, including eye detection.
In this article, we will explore how to detect eyes in an image using OpenCV Python. We will use a Haar Cascade classifier to train our model, which is a machine learning algorithm.
Requirements
Before we start, make sure you have installed the OpenCV library in your Python environment. To install OpenCV, you can run the following command in your command prompt:
pip install opencv-python
You also need a Haar Cascade classifier for detecting eyes. You can download the pre-trained classifier from the official OpenCV Github repository using the following command:
wget https://github.com/opencv/opencv/blob/master/data/haarcascades/haarcascade_eye.xml
Steps
- Import the necessary libraries for image processing and OpenCV.
import cv2
import numpy as np
- Load the image that you want to detect eyes on.
#load image
image = cv2.imread('image.jpg')
- Convert the image to grayscale for easier processing.
#convert image to grayscale
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
- Load the Haar Cascade classifier that we downloaded earlier.
#load classifier
eye_cascade = cv2.CascadeClassifier('haarcascade_eye.xml')
- Use the Haar Cascade classifier to detect eyes in the image.
#detect eyes
eyes = eye_cascade.detectMultiScale(gray, 1.3, 5)
The detectMultiScale
function performs the actual detection of eyes in the image. The 1.3
and 5
parameters are tuning parameters for the algorithm, and they can be adjusted depending on the image and the specific task.
- Draw rectangles around the detected eyes.
#draw rectangles around eyes
for (x,y,w,h) in eyes:
cv2.rectangle(image,(x,y),(x+w,y+h),(255,0,0),2)
The cv2.rectangle
function draws rectangles around the detected eyes. The (255,0,0)
parameter is the color of the rectangle, and 2
is the thickness of the rectangle.
- Display the image with detected eyes.
#display image with detected eyes
cv2.imshow('image',image)
cv2.waitKey(0)
cv2.destroyAllWindows()
The cv2.imshow
function displays the image with detected eyes. The cv2.waitKey
waits for a key press to exit the image display window and cv2.destroyAllWindows
closes all the image display windows.
Here’s the complete code:
import cv2
import numpy as np
#load image
image = cv2.imread('image.jpg')
#convert image to grayscale
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
#load classifier
eye_cascade = cv2.CascadeClassifier('haarcascade_eye.xml')
#detect eyes
eyes = eye_cascade.detectMultiScale(gray, 1.3, 5)
#draw rectangles around eyes
for (x,y,w,h) in eyes:
cv2.rectangle(image,(x,y),(x+w,y+h),(255,0,0),2)
#display image with detected eyes
cv2.imshow('image',image)
cv2.waitKey(0)
cv2.destroyAllWindows()
Conclusion
In this article, we have explored how to detect eyes in an image using OpenCV Python. We have used a Haar Cascade classifier to train our model, which is a machine learning algorithm. With just a few lines of code, we were able to detect eyes in an image and draw rectangles around them. Now you can apply this knowledge to your computer vision projects and detect eyes in your own images.