• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar
  • Skip to footer

Learn OpenCV

OpenCV, PyTorch, Keras, Tensorflow examples and tutorials

  • Home
  • Getting Started
    • Installation
    • PyTorch
    • Keras & Tensorflow
    • Resource Guide
  • Courses
    • Opencv Courses
    • CV4Faces (Old)
  • Resources
  • AI Consulting
  • About

Minified OpenCV Haar and LBP Cascades

Satya Mallick
December 26, 2016 25 Comments
how-to Performance

December 26, 2016 By 25 Comments

In this post, I am sharing Haar and LBP object detection cascades that have the same performance as the OpenCV cascades, but they have much smaller file sizes. I will also explain the ideas used in this minification.

Why do you care about the file size?

Let’s say you are building a mobile app. Downloading the app is the first experience the user has with your app. The smaller the size of the app, the better their first experience. You do not want a 2MB face detector in your app. Smaller models will also load a bit faster because the parser has to parse a fewer number of lines. So, we strive for —

Less but better

You can download the minified cascades by clicking here. Minified cascade filenames have a prefix “mallick_”. You can also subscribe to our newsletter and receive more goodies by clicking here.

How to reduce the size of OpenCV Haar and LBP Cascades ?

I am sharing three ideas for reducing the size of Haar and LBP cascades. Two of these ideas are implemented in the cascades I have shared.

Idea 1 : Minify XML by removing white spaces

Haar and LBP cascades that come with OpenCV are simple XML files. They also have a ton of white spaces, new lines, tabs etc. which are completely useless for defining the cascade. So we just remove unnecessary white spaces. Note, not all white spaces can be removed. E.g. inside the cascades you will find structures of this form.

<internalNodes>
    0 -1 367 -1.2275323271751404e-02
</internalNodes>

You should not remove the white spaces between the numbers.

Idea 2 : Remove unnecessary precision

I occasionally tuned into the news about US Presidential Election 2016. Nate Silver, who is famous for calling many election results predicted 24.8% chance of a Trump win. One of my favorite authors, Nassim Nicholas Taleb, countered that prediction with

Quite insulting to probability when someone “estimates” noise with precision: 24.8%, not 25 or 24.

His point is that polling is extremely noisy. Why create a false sense of precision when none exists?

A lot of times, precision is unnecessary. In the OpenCV cascades, there are a lot of floating point numbers in double precision. You can easily truncate them without loss of accuracy. E.g. -1.2275323271751404e-02 can be replaced by -1.2275e-02

Idea 3 : Binarize the cascades

The final idea is to convert the XML file into its binary form. Of course, this will make the file incompatible with OpenCV loader and you will have to write your own loader for this cascade. But if the model size is large, it may be worth the effort.

Minified OpenCV Cascades

I have implemented ideas 1 and 2. You can download the minified cascades by clicking here. Minified cascade filenames have a prefix “mallick_”

I have listed a few examples of the decrease in file size.

Cascade NameFile sizeMinified file size
haarcascade_eye.xml336K180K
haarcascade_frontalface_default.xml912K488K
haarcascade_frontalface_alt_tree.xml2.6M1.4M
lbpcascade_frontalface.xml52K36K

In the shared respository, I have included a tester script (tester.py) that loads a haar cascade and its minified version and displays the results on the same frame of the your webcam. The blue box denotes output of the haar cascade and the red box denotes the output of the minified cascade. The blue box is deliberately made 4 pixels smaller in width and height for display purposes. Here is the usage

python tester.py haarcascades/haarcascade_frontalface.xml

Subscribe & Download Code

If you liked this article and would like to download code (C++ and Python) and example images used in this other posts of this blog, please subscribe to our newsletter. You will also receive a free Computer Vision Resource guide. In our newsletter we share OpenCV tutorials and examples written in C++/Python, and Computer Vision and Machine Learning algorithms and news.

Subscribe Now

Tags: cascades haar lbp

Filed Under: how-to, Performance

About

I am an entrepreneur with a love for Computer Vision and Machine Learning with a dozen years of experience (and a Ph.D.) in the field.

In 2007, right after finishing my Ph.D., I co-founded TAAZ Inc. with my advisor Dr. David Kriegman and Kevin Barnes. The scalability, and robustness of our computer vision and machine learning algorithms have been put to rigorous test by more than 100M users who have tried our products. Read More…

Getting Started

  • Installation
  • PyTorch
  • Keras & Tensorflow
  • Resource Guide

Resources

Download Code (C++ / Python)

ENROLL IN OFFICIAL OPENCV COURSES

I've partnered with OpenCV.org to bring you official courses in Computer Vision, Machine Learning, and AI.
Learn More

Recent Posts

  • Making A Low-Cost Stereo Camera Using OpenCV
  • Optical Flow in OpenCV (C++/Python)
  • Introduction to Epipolar Geometry and Stereo Vision
  • Depth Estimation using Stereo matching
  • Classification with Localization: Convert any Keras Classifier to a Detector

Disclaimer

All views expressed on this site are my own and do not represent the opinions of OpenCV.org or any entity whatsoever with which I have been, am now, or will be affiliated.

GETTING STARTED

  • Installation
  • PyTorch
  • Keras & Tensorflow
  • Resource Guide

COURSES

  • Opencv Courses
  • CV4Faces (Old)

COPYRIGHT © 2020 - BIG VISION LLC

Privacy Policy | Terms & Conditions

We use cookies to ensure that we give you the best experience on our website. If you continue to use this site we will assume that you are happy with it.AcceptPrivacy policy