Title: Detecting cats in images with OpenCV - PyImageSearch

URL Source: https://pyimagesearch.com/2016/06/20/detecting-cats-in-images-with-opencv/

Published Time: 2016-06-20T14:00:55+00:00

Markdown Content:
![Image 1: (source)](data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20viewBox='0%200%20600%20383'%3E%3C/svg%3E)

([source](http://www.cats.org.uk/plymouth/adopt-a-cat))

Did you know that OpenCV can detect cat faces in images…_**right out-of-the-box with**_ **no _extras?_**

I didn’t either.

I had to check it out for myself…and do a little investigative work to see how this cat detector seemed to sneak its way into the OpenCV repository without me noticing (much like a cat sliding into an empty cereal box, just waiting to be discovered).

In the remainder of this blog post, I’ll demonstrate how to use OpenCV’s cat detector to detect cat faces in images. This same technique can be applied to video streams as well.

![Image 2](data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20viewBox='0%200%20128%20128'%3E%3C/svg%3E)

#### Looking for the source code to this post?

[Jump Right To The Downloads Section](#download-the-code)

If you take a look at the [OpenCV repository](https://github.com/Itseez/opencv), specifically within the [haarcascades directory](https://github.com/Itseez/opencv/tree/master/data/haarcascades) (where OpenCV stores all its pre-trained Haar classifiers to detect various objects, body parts, etc.), you’ll notice two files:

*   `haarcascade_frontalcatface.xml`
*   `haarcascade_frontalcatface_extended.xml`

Both of these Haar cascades can be used detecting “cat faces” in images. In fact, I used these very same cascades to generate the example image at the top of this blog post.

Doing a little investigative work, I found that the cascades were trained and contributed to the OpenCV repository by the legendary [Joseph Howse](http://nummist.com/) who’s authored a good many tutorials, books, and talks on computer vision.

In the remainder of this blog post, I’ll show you how to utilize Howse’s Haar cascades to detect cats in images.

### Cat detection code

Let’s get started detecting cats in images with OpenCV. Open up a new file, name it `cat_detector.py` , and insert the following code:

\# import the necessary packages
import argparse
import cv2
 
# construct the argument parse and parse the arguments
ap = argparse.ArgumentParser()
ap.add\_argument("-i", "--image", required=True,
	help="path to the input image")
ap.add\_argument("-c", "--cascade",
	default="haarcascade\_frontalcatface.xml",
	help="path to cat detector haar cascade")
args = vars(ap.parse\_args())

**Lines 2 and 3** import our necessary Python packages while **Lines 6-12** parse our command line arguments. We only require a single argument here, the input `--image` that we want to detect cat faces in using OpenCV.

We can also (optionally) supply a path our Haar cascade via the `--cascade` switch. We’ll default this path to `haarcascade_frontalcatface.xml` and assume you have the `haarcascade_frontalcatface.xml` file in the _same directory_ as your `cat_detector.py` script.

_**Note:** I’ve conveniently included the code, cat detector Haar cascade, and example images used in this tutorial in the “Downloads” section of this blog post. If you’re new to working with Python + OpenCV (or Haar cascades), I would suggest downloading the provided .zip file to make it easier to follow along._

Next, let’s detect the cats in our input image:

\# load the input image and convert it to grayscale
image = cv2.imread(args\["image"\])
gray = cv2.cvtColor(image, cv2.COLOR\_BGR2GRAY)

# load the cat detector Haar cascade, then detect cat faces
# in the input image
detector = cv2.CascadeClassifier(args\["cascade"\])
rects = detector.detectMultiScale(gray, scaleFactor=1.3,
	minNeighbors=10, minSize=(75, 75))

On **Lines 15 and 16** we load our input image from disk and convert it to grayscale (a normal pre-processing step before passing the image to a Haar cascade classifier, although not strictly required).

**Line 20** loads our Haar cascade from disk (in this case, the cat detector) and instantiates the `cv2.CascadeClassifier` object.

Detecting cat faces in images with OpenCV is accomplished on **Lines 21 and 22** by calling the `detectMultiScale` method of the `detector` object. We pass four parameters to the `detectMultiScale` method, including:

1.  Our image, `gray` , that we want to detect cat faces in.
2.  A `scaleFactor` of our [image pyramid](https://pyimagesearch.com/2015/03/16/image-pyramids-with-python-and-opencv/) used when detecting cat faces. A larger scale factor will increase the speed of the detector, but could harm our true-positive detection accuracy. Conversely, a smaller scale will slow down the detection process, but increase true-positive detections. However, this smaller scale can also increase the false-positive detection rate as well. See the _**“A note on Haar cascades”**_ section of this blog post for more information.
3.  The `minNeighbors` parameter controls the minimum number of detected bounding boxes in a given area for the region to be considered a “cat face”. This parameter is very helpful in pruning false-positive detections.
4.  Finally, the `minSize` parameter is pretty self-explanatory. This value ensures that each detected bounding box is at least _width x height_ pixels (in this case, _75 x 75_).

The `detectMultiScale` function returns `rects` , a list of 4-tuples. These tuples contain the _(x, y)_\-coordinates and _width_ and _height_ of each detected cat face.

Finally, let’s draw a rectangle surround each cat face in the image:

\# loop over the cat faces and draw a rectangle surrounding each
for (i, (x, y, w, h)) in enumerate(rects):
	cv2.rectangle(image, (x, y), (x + w, y + h), (0, 0, 255), 2)
	cv2.putText(image, "Cat #{}".format(i + 1), (x, y - 10),
		cv2.FONT\_HERSHEY\_SIMPLEX, 0.55, (0, 0, 255), 2)

# show the detected cat faces
cv2.imshow("Cat Faces", image)
cv2.waitKey(0)

Given our bounding boxes (i.e., `rects` ), we loop over each of them individually on **Line 25**.

We then draw a rectangle surrounding each cat face on **Line 26**, while **Lines 27 and 28** displays an integer, counting the number of cats in the image.

Finally, **Lines 31 and 32** display the output image to our screen.

### Cat detection results

To test our OpenCV cat detector, be sure to download the source code to this tutorial using the _**“Downloads”**_ section at the bottom of this post.

Then, after you have unzipped the archive, you should have the following three files/directories:

1.  `cat_detector.py` : Our Python + OpenCV script used to detect cats in images.
2.  `haarcascade_frontalcatface.xml` : The cat detector Haar cascade. _**Please use this Haar Cascade that is provided and not the one on OpenCV’s GitHub repository for compatibility purposes.**_
3.  `images` : A directory of testing images that we’re going to apply the cat detector cascade to.

From there, execute the following command:

$ python cat\_detector.py --image images/cat\_01.jpg

![Image 3: Figure 1: Detecting a cat face in an image, even with parts of the cat occluded.](data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20viewBox='0%200%20600%20499'%3E%3C/svg%3E)

**Figure 1:** Detecting a cat face in an image, even with parts of the cat occluded ([source](https://www.youtube.com/watch?v=tntOCGkgt98)).

Notice that we have been able to detect the cat face in the image, even though the rest of its body is obscured.

Let’s try another image:

python cat\_detector.py --image images/cat\_02.jpg

![Image 4: Figure 2: A second example of detecting a cat in an image with OpenCV, this time the cat face is slightly different.](data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20viewBox='0%200%20600%20499'%3E%3C/svg%3E)

**Figure 2:** A second example of detecting a cat in an image with OpenCV, this time the cat face is slightly different

This cat’s face is clearly different from the other one, as it’s in the middle of a “meow”. In either case, the cat detector cascade is able to correctly find the cat face in the image.

The same is true for this image as well:

$ python cat\_detector.py --image images/cat\_03.jpg

![Image 5: Figure 3: Cat detection with OpenCV and Python (source)](data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20viewBox='0%200%20800%20600'%3E%3C/svg%3E)

**Figure 3:** Cat detection with OpenCV and Python 

Our final example demonstrates detecting _multiple_ cats in an image using OpenCV and Python:

$ python cat\_detector.py --image images/cat\_04.jpg

![Image 6: Figure 4: Detecting multiple cats in the same image with OpenCV (source).](data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20viewBox='0%200%20600%20383'%3E%3C/svg%3E)

**Figure 4:** Detecting multiple cats in the same image with OpenCV ([source](https://www.cats.org.uk/plymouth/adopt-a-cat)).

Note that the Haar cascade can return bounding boxes in an order that you may not like. In this case, the middle cat is actually labeled as the third cat. You can resolve this “issue” by sorting the bounding boxes according to their _(x, y)_\-coordinates for a consistent ordering.

#### A quick note on accuracy

It’s important to note that in the comments section of the `.xml` files, Joseph Howe details that the cat detector Haar cascades can report _cat faces_ where there are actually _human faces_.

In this case, he recommends performing _both_ face detection _and_ cat detection, then discarding any cat bounding boxes that _overlap_ with the face bounding boxes.

### A note on Haar cascades

First published in 2001 by Paul Viola and Michael Jones, _[Rapid Object Detection using a Boosted Cascade of Simple Features](https://www.cs.cmu.edu/~efros/courses/LBMV07/Papers/viola-cvpr-01.pdf),_ this original work has become one of the most cited papers in computer vision.

This algorithm is capable of detecting objects in images, regardless of their location and scale. And perhaps most intriguing, the detector can run in real-time on modern hardware.

In their paper, Viola and Jones focused on training a _face detector_; however, the framework can also be used to train detectors for arbitrary “objects”, such as cars, bananas, road signs, etc.

#### The problem?

The biggest problem with Haar cascades is getting the `detectMultiScale` parameters right, specifically `scaleFactor` and `minNeighbors` . You can easily run into situations where you need to tune _both_ of these parameters on an image-by-image basis, which is far from ideal when utilizing an object detector.

The `scaleFactor` variable controls your [image pyramid](https://pyimagesearch.com/2015/03/16/image-pyramids-with-python-and-opencv/) used to detect objects at various scales of an image. If your `scaleFactor` is too large, then you’ll only evaluate a few layers of the image pyramid, potentially leading to you missing objects at scales that fall _in between_ the pyramid layers.

On the other hand, if you set `scaleFactor` too low, then you evaluate _many_ pyramid layers. This will help you detect more objects in your image, but it (1) makes the detection process slower and (2) _**substantially**_ increases the false-positive detection rate, something that Haar cascades are known for.

To remember this, we often apply **[Histogram of Oriented Gradients + Linear SVM detection](https://pyimagesearch.com/2014/11/10/histogram-oriented-gradients-object-detection/)** instead.

The HOG + Linear SVM framework parameters are normally much easier to tune — and best of all, HOG + Linear SVM enjoys a _much smaller false-positive detection rate_. The only downside is that it’s harder to get HOG + Linear SVM to run in real-time.

### What's next? We recommend [PyImageSearch University](https://pyimagesearch.com/pyimagesearch-university/?utm_source=blogPost&utm_medium=bottomBanner&utm_campaign=What%27s%20next%3F%20I%20recommend).

**Course information:**  
84 total classes • 114+ hours of on-demand code walkthrough videos • Last updated: February 2024  
★★★★★ 4.84 (128 Ratings) • 16,000+ Students Enrolled

**I strongly believe that if you had the right teacher you could _master_ computer vision and deep learning.**

Do you think learning computer vision and deep learning has to be time-consuming, overwhelming, and complicated? Or has to involve complex mathematics and equations? Or requires a degree in computer science?

That’s _not_ the case.

All you need to master computer vision and deep learning is for someone to explain things to you in _simple, intuitive_ terms. _And that’s exactly what I do_. My mission is to change education and how complex Artificial Intelligence topics are taught.

If you're serious about learning computer vision, your next stop should be PyImageSearch University, the most comprehensive computer vision, deep learning, and OpenCV course online today. Here you’ll learn how to _successfully_ and _confidently_ apply computer vision to your work, research, and projects. Join me in computer vision mastery.

**Inside PyImageSearch University you'll find:**

*   ✓ **84 courses** on essential computer vision, deep learning, and OpenCV topics
*   ✓ **84 Certificates** of Completion
*   ✓ **114+ hours** of on-demand video
*   ✓ **Brand new courses released _regularly_**, ensuring you can keep up with state-of-the-art techniques
*   ✓ **Pre-configured Jupyter Notebooks in Google Colab**
*   ✓ Run all code examples in your web browser — works on Windows, macOS, and Linux (no dev environment configuration required!)
*   ✓ Access to **centralized code repos for _all_ 536+ tutorials** on PyImageSearch
*   ✓ **Easy one-click downloads** for code, datasets, pre-trained models, etc.
*   ✓ **Access** on mobile, laptop, desktop, etc.

[Click here to join PyImageSearch University](https://pyimagesearch.com/pyimagesearch-university/?utm_source=blogPost&utm_medium=bottomBanner&utm_campaign=What%27s%20next%3F%20I%20recommend)

Summary
-------

In this blog post, we learned how to detect cats in images using the default Haar cascades shipped with OpenCV. These Haar cascades were trained and contributed to the OpenCV project by [Joseph Howse](https://twitter.com/CatsAndMonkeys), and were originally brought to my attention [in this post](http://kendricktan.github.io/find-cats-in-photos-using-computer-vision.html) by Kendrick Tan.

While Haar cascades are quite useful, we often use HOG + Linear SVM instead, as it’s a bit easier to tune the detector parameters, and more importantly, we can enjoy a _much_ lower false-positive detection rate.

**I detail how to build custom HOG + Linear SVM object detectors to recognize various objects in images, including cars, road signs, and much more _[inside the PyImageSearch Gurus course.](https://pyimagesearch.com/pyimagesearch-gurus/)_**

Anyway, I hope you enjoyed this blog post!

**Before you go, be sure to signup for the PyImageSearch Newsletter using the form below to be notified when new blog posts are published.**

![Image 7](data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20viewBox='0%200%20410%20567'%3E%3C/svg%3E)

#### Download the Source Code and FREE 17-page Resource Guide

Enter your email address below to get a .zip of the code and a **FREE 17-page Resource Guide on Computer Vision, OpenCV, and Deep Learning.** Inside you'll find my hand-picked tutorials, books, courses, and libraries to help you master CV and DL!