M. Beyeler (2017). Machine Learning for OpenCV: Intelligent image processing with Python. Packt Publishing Ltd., ISBN 978-178398028-4.
MIT License
This is the Jupyter notebook version of the following book:
Michael Beyeler Machine Learning for OpenCV Intelligent Image Processing with Python 14 July 2017 Packt Publishing Ltd., London, England Paperback: 382 pages ISBN 978-178398028-4
The content is available on GitHub. The code is released under the MIT license.
A second edition has now been released (using OpenCV4). The book is also available as a Udemy course.
If you use either book or code in a scholarly publication, please cite as:
M. Beyeler, (2017). Machine Learning for OpenCV. Packt Publishing Ltd., London, England, 380 pages, ISBN 978-178398028-4.
Or use the following bibtex:
@book{MachineLearningOpenCV,
title = {{Machine Learning for OpenCV}},
subtitle = {{Intelligent image processing with Python}},
author = {Michael Beyeler},
year = {2017},
pages = {380},
publisher = {Packt Publishing Ltd.},
isbn = {978-178398028-4}
}
The book has been referenced more than 45 times in scholarly work (source: Google Scholar). Citing studies include:
There are at least two ways you can run the code:
The code in this book was tested with Python 3.5, although Python 3.6 and 2.7 should work as well.
Binder allows you to run Jupyter notebooks in an interactive Docker container. No installation required!
Launch the project: mbeyeler/opencv-machine-learning
You basically want to follow the installation instructions in Chapter 1 of the book.
In short:
Download and install Python Anaconda.
On Unix, when asked if the Anaconda path should be added to your PATH
variable, choose yes. Then either open a new terminal or run $ source ~/.bashrc
.
Fork and clone the GitHub repo:
Fork
YourUsername
is your actual GitHub user name:$ git clone https://github.com/YourUsername/opencv-machine-learning
$ cd opencv-machine-learning
$ git remote add upstream https://github.com/mbeyeler/opencv-machine-learning
Add Conda-Forge to your trusted channels (to simplify installation of OpenCV on Windows platforms):
$ conda config --add channels conda-forge
Create a conda environment for Python 3 with all required packages:
$ conda create -n Python3 python=3.6 --file requirements.txt
Activate the conda environment. On Linux / Mac OS X:
$ source activate Python3
On Windows:
$ activate Python3
You can learn more about conda environments in the Managing Environments section of the conda documentation.
Launch Jupyter notebook:
$ jupyter notebook
This will open up a browser window in your current directory.
Navigate to the folder opencv-machine-learning
.
The README file has a table of contents.
Else navigate to the notebooks
folder, click on the notebook of your choice,
and select Kernel > Restart & Run All
from the top menu.
If you followed the instructions above and:
upstream
remote repository,then you can always grab the latest changes by running a git pull:
$ cd opencv-machine-learning
$ git pull upstream master
The following errata have been reported that apply to the print version of the book. Some of these are typos, others are bugs in the code. Please note that all known bugs have been fixed in the code of this repository.
Out[15]
should read '3' instead of 'int_arr[3]'.Out[22]
should read array([9, 8, 7, 6, 5, 4, 3, 2, 1, 0])
instead of array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
.np.linspace
by default includes the endpoint.In [15]
shoud read precision = true_positive / (true_positive + false_positive)
instead of precision = true_positive / (true_positive + true_negative)
.Out[15]
should read 0.2 instead of 1.0.In [6]
should read ridgereg = linear_model.Ridge()
instead of ridgereg = linear_model.RidgeRegression()
.In [8]
should read min_max_scaler = preprocessing.MinMaxScaler(feature_range=(-10,10))
instead of min_max_scaler = preprocessing.MinMaxScaler(feature_range (-10,10))
.We also specify an empty array, np.array([]), for the mean argument, which tells OpenCV to compute the mean from the data:
instead of We also specify an empty array, np.array([]), for the mask argument, which tells OpenCV to use all data points in the feature matrix:
.In [3]
should read vec.get_feature_names()[:5]
instead of function:vec.get_feature_names()[:5]
.In [16]
should read dtree = cv2.ml.DTrees_create()
instead of dtree = cv2.ml.dtree_create()
.In [26]
should read with open("tree.dot", 'w'): f = tree.export_graphviz(dtc, out_file=f, feature_names=vec.get_feature_names(), class_names=['A', 'B', 'C', 'D'])
instead of with open("tree.dot", 'w'): f = tree.export_graphviz(clf, out_file=f)
. Also, the second line should be indented.X_hypo = np.c_[xx.ravel().astype(np.float32), yy.ravel().astype(np.float32)]
and _, zz = svm.predict(X_hypo)
should be removed, as they mistakenly appear twice.In [28]
is missing from sklearn import metrics
.In [3]
should read "Then we can pass the preceding data matrix (X
) to cv2.kmeans
", not cv2.means
.In [2]
has wrong indentation: class Perceptron(object)
correctly has indentation level 1, but def __init__
should have indentation level 2, and the two commands self.lr = lr; self.n_iter = n_iter
should have indentation level 3.In [5]
should read from keras.models import Sequential
instead of from keras.model import Sequential
.In [6]
should read model.add(Conv2D(n_filters, (kernel_size[0], kernel_size[1]), padding='valid', input_shape=input_shape))
instead of model.add(Convolution2D(n_filters, kernel_size[0], kernel_size[1], border_mode='valid', input_shape=input_shape))
.In [8]
should read model.add(Conv2D(n_filters, (kernel_size[0], kernel_size[1])))
instead of model.add(Convolution2D(n_filters, (kernel_size[0], kernel_size[1])))
.In [12]
should read model.fit(X_train, Y_train, batch_size=128, epochs=12, verbose=1, validation_data=(X_test, Y_test))
instead of model.fit(X_train, Y_train, batch_size=128, nb_epoch=12, verbose=1, validation_data=(X_test, Y_test))
.ret = classifier.predict(X_hypo)
instead of zz = classifier.predict(X_hypo); zz = zz.reshape(xx.shape)
.plt.imshow(X[i, :].reshape((64, 64)), cmap='gray')
should be indented so that it is aligned with the previous line.In [14]
should read _, y_hat = rtree.predict(X_test)
instead of _, y_hat = tree.predict(X_test)
.In [2]
should read from sklearn.model_selection import train_test_split
instead of from sklearn.model_selection import model_selection
.In [18]
should read knn.train(X_boot, cv2.ml.ROW_SAMPLE, y_boot)
instead of knn.train(X_train, cv2.ml.ROW_SAMPLE, y_boot)
.In [20]
should have a line model.train(X_boot, cv2.ml.ROW_SAMPLE, y_boot)
instead of knn.train(X_boot, cv2.ml.ROW_SAMPLE, y_boot)
, as well as _, y_hat = model.predict(X_oob)
instead of _, y_hat = knn.predict(X_oob)
.In [5]
is missing the statement from sklearn.preprocessing import MinMaxScaler
.In [5]
should have a line pipe = Pipeline([("scaler", MinMaxScaler()), ("svm", SVC())])
instead of pipe = Pipeline(["scaler", MinMaxScaler(), ("svm", SVC())])
.This book was inspired in many ways by the following authors and their corresponding publications:
These books all come with their own open-source code - check them out when you get a chance!