In this OpenCV with Python tutorial, we’re going to discuss object
detection with Haar Cascades. We’ll do face and eye detection to start.
In order to do object recognition/detection with 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.
First, I will show you how to use these cascade files, then I will show you how to embark on creating your very own cascades, so that you can detect any object you want, which is pretty darn cool!
You can use Google to find various Haar Cascades of things you may want to detect. You shouldn’t have too much trouble finding the aforementioned types. We will use a Face cascade , Mouth cascade , Nose Cascade and Eye cascade. You can find a few more at the root directory of Haar cascades. Note the license for using/distributing these Haar Cascades.
Let’s begin our code. I am assuming you have downloaded the
We use these values to draw a rectangle using the built-in
First, I will show you how to use these cascade files, then I will show you how to embark on creating your very own cascades, so that you can detect any object you want, which is pretty darn cool!
You can use Google to find various Haar Cascades of things you may want to detect. You shouldn’t have too much trouble finding the aforementioned types. We will use a Face cascade , Mouth cascade , Nose Cascade and Eye cascade. You can find a few more at the root directory of Haar cascades. Note the license for using/distributing these Haar Cascades.
Let’s begin our code. I am assuming you have downloaded the
haarcascade_eye.xml
, haarcascade_frontalface_default.xml, haarcascade_mcs_mouth.xml
and haarcascade_mcs_nose
.xml
from the links above, and have these files in your project’s directory.Understanding the code
Let’s dive straight into the codeimport sys
import cv2
# Get user supplied values
imagePath = sys.argv[1]
cascPath = sys.argv[2]
# Create the haar cascade
eye_cascade = cv2.CascadeClassifier(cascPath)
# Read the image
image = cv2.imread(imagePath)
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
# Draw a rectangle around the faces
roi_gray = gray[y:y+h, x:x+w]
roi_color = image[y:y+h, x:x+w]
eyes = eye_cascade.detectMultiScale(roi_gray)
for (ex,ey,ew,eh) in eyes:
cv2.rectangle(roi_color,(ex,ey),(ex+ew,ey+eh),(0,255,0),2)
cv2.imshow("Eyes found" ,image)
cv2.waitKey(0)
Let’s break down the actual code.You first pass in the image and cascade names as command-line arguments. We’ll use the Abba image as well as the default cascade for detecting eyes provided by OpenCV.# Get user supplied values imagePath = sys.argv[1] cascPath = sys.argv[2]
# Create the haar cascade
eye_cascade = cv2.CascadeClassifier(cascPath)
Now we create the cascade and initialize it with our eye cascade.
This loads the eye cascade into memory so it’s ready for use. Remember,
the cascade is just an XML file that contains the data to detect eyes.# Read the image
image = cv2.imread(imagePath)
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
Here we read the image and convert it to grayscale. Many operations in OpenCv are done in grayscale.# Detect eyes in the image
roi_gray = gray[y:y+h, x:x+w]
roi_color = image[y:y+h, x:x+w]
eyes = eye_cascade.detectMultiScale(roi_gray)
The function returns a list of rectangles where it believes it found
eyes. Next, we will loop over where it thinks it found something.for (ex,ey,ew,eh) in eyes:
cv2.rectangle(roi_color,(ex,ey),(ex+ew,ey+eh),(0,255,0),2)
This function returns 4 values: the ex and ey location of the rectangle, and the rectangle’s width and height (ew , eh).We use these values to draw a rectangle using the built-in
rectangle()
function.cv2.imshow("Eyes found" ,image)
cv2.waitKey(0)
Checking the results
Let’s test against the Abba photo:$ python eye_detect.py lena.png haarcascade_eye.xml
No comments:
Post a Comment