This notebook contains an excerpt from the book Machine Learning for OpenCV by Michael Beyeler. The code is released under the MIT license, and is available on GitHub.

Note that this excerpt contains only the raw code - the book is rich with additional explanations and illustrations. If you find this content useful, please consider supporting the work by buying the book!

Machine Learning for OpenCV

A practical introduction to the world of machine learning using OpenCV and Python

I’m glad you’re here. It’s about time we talked about machine learning.

Machine learning is no longer just a buzzword, it is all around us: from protecting your email, to automatically tagging friends in pictures, to predicting what movies you like. As a subfield of data science, machine learning enables computers to learn through experience: to make predictions about the future using collected data from the past.

And the amount of data to be analyzed is enormous! Current estimates put the daily amount of produced data at 2.5 exabytes (or roughly 1 billion gigabytes). Can you believe it? This would be enough data to fill up 10 million blu-ray discs, or amount to 90 years of HD video. In order to deal with these vast amounts of data, companies such as Google, Amazon, Microsoft, and Facebook have been heavily investing in the development of data science platforms that allow us to benefit from machine learning wherever we go – scaling from your mobile phone application all the way to supercomputers connected through the cloud.

In other words: This is the time to invest in machine learning. And if it is your wish to become a machine learning practitioner, too – then this book is for you!

What this book covers

The first step of this book is to introduce you to the essential concepts of statistical learning, such as classification and regression, with the help of simple and intuitive examples. If you have already studied machine learning theory in detail, this book will show you how to put your knowledge into practice. Oh, and don't worry if you are completely new to the field of machine learning – all you need is the willingness to learn.

Once we covered all the basic concepts, we will start exploring various algorithms such as decision trees, support vector machines, and Bayesian networks, and learn how to combine them with other OpenCV functionality. Along the way, you will learn how to ‘understand the task by understanding the data’ and how to build fully functioning machine learning pipelines.

As the book progresses, so will your machine learning skills, until you are ready to take on today's hottest topic in the field: deep learning. Combined with the trained skill of knowing how to select the right tool for the task, we will make sure you get comfortable with all relevant machine learning fundamentals.

At the end of the book, you will be ready to take on your own machine learning problems, either by building on the existing source code or developing your own algorithm from scratch!

What you need for this book

You will need a computer, Python Anaconda, and enthusiasm. Lots of enthusiasm.

Why Python?, you may ask. The answer is simple: It has become the de facto language of data science, thanks to its great number of open-source libraries and tools to process and interact with data.

One of these tools is the Python Anaconda distribution, which provides all the scientific computing libraries we could possibly ask for, such as NumPy, SciPy, Matplotlib, scikit-learn, and Pandas. In addition, installing OpenCV is essentially a one-liner. No more flipping switches in ccmake or compiling from scratch! We will talk about how to install Python Anaconda in Chapter 1, A Taste of Machine Learning.

If you have mostly been using OpenCV in combination with C++, that’s fine. But, at least for the purpose of this book, I would strongly suggest that you switch to Python. C++ is fine when your task is to develop high-performance code or real-time applications. But, when it comes to picking up a new skill, I believe Python to be the fundamentally better choice of language, because you can do more by typing less. Rather than getting annoyed by the syntactic subtleties of C++, or wasting hours trying to convert data from one format into another, Python will help you concentrate on the topic at hand: to become an expert in machine learning.

If you get stuck or have questions about the source code, you are welcome to post in our Discussion group. Chances are, someone else has already shared a solution to your specific problem.

About the author

Michael Beyeler is a Postdoctoral Fellow in Neuroengineering and Data Science at the University of Washington, where he is working on computational models of bionic vision in order to improve the perceptual experience of blind patients implanted with a retinal prosthesis ("bionic eye"). His work lies at the intersection of neuroscience, computer engineering, computer vision, and machine learning. Michael is the author of OpenCV with Python Blueprints (Packt Publishing Ltd., 2015), a practical guide for building advanced computer vision projects. He is also an active contributor to several open-source software projects, and has professional programming experience in Python, C/C++, CUDA, MATLAB, and Android.

Michael received a Ph.D. in Computer Science from the University of California, Irvine as well as a M.Sc. in Biomedical Engineering and a B.Sc. in Electrical Engineering from ETH Zurich, Switzerland. When he is not "nerding out" on brains, he can be found on top of a snowy mountain, in front of a live band, or behind the piano.