Practical Machine Learning and Image Processing: For Facial Recognition, Object Detection, and Pattern Recognition Using Python Himanshu Singh
Practical Machine Learning and Image Processing: For Facial Recognition, Object Detection, and Pattern Recognition Using Python Himanshu Singh
com
https://textbookfull.com/product/computer-vision-pattern-recognition-
image-processing-and-graphics-renu-rameshan/
textbookfull.com
https://textbookfull.com/product/image-processing-computer-vision-and-
pattern-recognition-hamid-r-arabnia/
textbookfull.com
https://textbookfull.com/product/current-achievements-challenges-and-
digital-chances-of-knowledge-based-economy-svetlana-igorevna-
ashmarina/
textbookfull.com
Blacksound: Making Race and Popular Music in the United
States 1st Edition Morrison
https://textbookfull.com/product/blacksound-making-race-and-popular-
music-in-the-united-states-1st-edition-morrison/
textbookfull.com
https://textbookfull.com/product/murder-at-the-manchester-museum-
museum-mystery-series-4-jim-eldridge-et-el/
textbookfull.com
https://textbookfull.com/product/nobody-does-it-better-the-complete-
uncensored-unauthorized-oral-history-of-the-james-bond-first-edition-
edward-gross/
textbookfull.com
https://textbookfull.com/product/practical-amazon-ec2-sqs-kinesis-
and-s3-a-hands-on-approach-to-aws-1st-edition-sunil-gulabani-auth/
textbookfull.com
Psychoanalytic Practice Today 1st Edition Antonino Ferro
(Editor)
https://textbookfull.com/product/psychoanalytic-practice-today-1st-
edition-antonino-ferro-editor/
textbookfull.com
Practical Machine
Learning and
Image Processing
For Facial Recognition, Object Detection,
and Pattern Recognition Using Python
—
Himanshu Singh
Practical Machine
Learning and Image
Processing
For Facial Recognition, Object
Detection, and Pattern
Recognition Using Python
Himanshu Singh
Practical Machine Learning and Image Processing
Himanshu Singh
Allahabad, Uttar Pradesh, India
iii
Table of Contents
iv
Table of Contents
v
Table of Contents
vi
Table of Contents
Sobel Transformation����������������������������������������������������������������������������������������157
Haar Cascade����������������������������������������������������������������������������������������������������158
LBPH Face Recognition�������������������������������������������������������������������������������������158
Image Moments������������������������������������������������������������������������������������������������158
Image Contours�������������������������������������������������������������������������������������������������159
Chessboard Corners Function���������������������������������������������������������������������������160
Calibrate Camera Function��������������������������������������������������������������������������������161
Perspective Transformation Function���������������������������������������������������������������162
Index�������������������������������������������������������������������������������������������������165
vii
About the Author
Himanshu Singh has more than 6+ years of
experience as a data science professional.
Currently, he is a senior data scientist at
V-Soft Labs. He provides corporate training
on data science, machine learning, and deep
learning. He is also a visiting faculty member
in analytics at the Narsee Monjee Institute of
Management Studies, considered one of the
premium management institutes in India. He is founder of Black Feathers
Analytics and Rise of Literati Clubs.
ix
About the Technical Reviewer
Santanu Pattanayak currently works at GE,
Digital, as a staff data scientist and is author
of Pro Deep Learning with TensorFlow: A
Mathematical Approach to Advanced Artificial
Intelligence in Python. He has approximately
12 years of overall work experience, with eight
of years of experience in the data analytics/
data science field, and also has a background
in development and database technologies.
Before joining GE, Santanu worked in
companies such as RBS, Capgemini, and
IBM. He graduated with a degree in electrical
engineering from Jadavpur University, Kolkata,
and is an avid math enthusiast. Santanu is currently pursuing a master’s
degree in data science from the Indian Institute of Technology, Hyderabad.
He also devotes his time to data science hackathons and Kaggle
competitions in which he ranks within the top 500 worldwide. Santanu
was born and brought up in West Bengal, India, and currently resides in
Bangalore, India, with his wife.
xi
Visit https://textbookfull.com
now to explore a rich
collection of eBooks, textbook
and enjoy exciting offers!
Acknowledgments
First of all, I thank the Apress Team, Celestian John and Aditee Mirashi, for
giving me a platform to contribute my image processing knowledge and
share it with readers. Second, I thank my colleagues, without whom this
book would not have been possible: Aravind Kota, Yamuna, and my boss
and mentor, Yunis Ahmad Lone. I also thank my students. They helped
me see which issues are challenging for them, and enabled me to devise
a specific means of explaining the concepts to them in a manner that
facilitates their learning.
Last, but not the least, I thank my wife, Shikha Singh. Her constant
support and help has allowed this project to come to fruition. She assisted
me in all aspects of writing this book, sometimes proofreading and writing
technical details herself.
Many thanks to everyone for your constant support.
xiii
Introduction
Practical Machine Learning and Image Processing gives readers deep
insight into the basics of image processing and various image processing
methodologies and algorithms, applications using various Python
libraries, and real-time use case implementation using machine learning
approaches.
The book begins with a discussion of the setup environment for
different operating systems, presents basic image processing terminology,
and explores useful Python concepts for algorithm application. It
then delves into various image processing algorithms and practical
implementation of them in Python using two libraries: Scikit Image and
OpenCV. Next, advanced machine learning and deep learning methods
are presented for image processing and classification. Concepts such as
Adaboost, XG Boost, convolutional neural networks, and more, for image-
specific applications are explained. Later, the process for making models in
real time and then deploying them is described.
All the concepts in the book are explained using real-life scenarios.
By the end of the book, readers should be able to apply image processing
techniques and make machine learning models for customized
applications.
xv
CHAPTER 1
Setup Environment
In this chapter we prepare our system to run the code included in this
book. Let’s look at how to install the following:
• Anaconda
• OpenCV
• Keras
Aside from the last two packages in the list, most of what we need
comes preinstalled with Anaconda. Let’s start with Anaconda, then follow
with OpenCV and Keras.
I nstall Anaconda
The Anaconda installation page proclaims it is “The Most Popular Python
Data Science Platform.” Using Anaconda, installing supporting software,
setting up virtual environments, and so on, are all quite easy, and the
package comes with one of the best integrated development environments
(IDEs) for Python data science: Jupyter Notebook. Jupyter not only helps
you write Python code, but also it makes your code look beautiful and
presentable. So, let’s get started with the Anaconda installation.
W
indows
If you are using Windows, here’s the process:
1. Go to www.anaconda.com.
2
Chapter 1 Setup Environment
3
Chapter 1 Setup Environment
m
acOS
If you are using macOS, here is the Anaconda installation process:
U
buntu
The process for downloading Anaconda in Ubuntu is as follows:
I nstall OpenCV
Now that we have installed Anaconda and Jupyter Notebook. The next
thing to do is to install its supporting software. For OpenCV, do the
following:
4
Chapter 1 Setup Environment
Install Keras
To install Keras, follow these procedures:
5
Chapter 1 Setup Environment
Virtual Environments
Now that we have installed the software we need, let’s take a look at virtual
environments. Virtual environments are very important when you want
to develop multiple projects. What should we do if we are developing
a product using Python 3, but we want to create another project using
Python 2.7? If we do it directly, we may encounter problems because
different versions of Python are installed. Or, we could create a virtual
environment, install Python 2.7, and develop the product inside that
environment. Regardless of what you develop inside a virtual environment,
it never influences any code outside the environment. Let’s see how we
can create a virtual environment:
1. Type conda create -n environment_name
python=version anaconda. In place of
environment_name, type any name you wish to give
to your environment. In place of version, type any
version of Python that you wish to use (for example,
2.7, 3.5, 3.6, and so on).
2. Now that we have created the environment, we have
to activate it. We do this by typing source activate
environment_name.
3. We can now open Jupyter Notebook and start
working in this environment.
4. To deactivate the environment, type source deactivate.
6
CHAPTER 2
Introduction to Image
Processing
In this chapter we examine exactly what an image is, and its related
properties. By the end of the chapter, you should have an understanding of
the following concepts:
• Images
• Pixels
• Image resolution
• Bitmap images
• Lossless compression and lossy compression
I mages
Visual representation of a real-life object (a person or any other object)
in a two-dimensional form is called an image. An image is nothing but a
collection of pixels in different color spaces. Figure 2-1 is an example of a
normal image.
P
ixels
You might think of a complete image as a set that consists of small
samples. These samples are called pixels. They are the smallest elements in
any digital image. Have you ever zoomed in on an image to such an extent
that you see small squares? Those are pixels. So, pixels are subsamples of
an image that, when get combined, give us the complete image. Figure 2-2
shows how pixels, with various colors, may look.
8
Visit https://textbookfull.com
now to explore a rich
collection of eBooks, textbook
and enjoy exciting offers!
Chapter 2 Introduction to Image Processing
I mage Resolution
Image resolution is the number of pixels present in an image. The greater
the number of pixels, the better quality. Image resolutions are described,
for example, as 320 × 240, 640 × 480, 800 × 600, 1024 × 768, and so on.
This means, for example, that there are 1024 pixel columns and 768 pixel
rows. The total number of pixels is obtained by multiplying both numbers,
which gives us 786,432 pixels. Figure 2-3 shows comparative depictions of
different image resolutions.
1920x1080
1280x720
640x480
9
Chapter 2 Introduction to Image Processing
P
PI and DPI
As noted at the beginning of the chapter, PPI means “pixels per inch”
whereas DPI means “dots per inch.” They are the units for measuring
image resolution.
If we consider an inch of an image, the number of square pixels we are
able to see inside it is represented by PPI. DPI, on the other hand, is related
to printing. When we print an image and look at an inch of the print, the
number of dots of ink used is represented by DPI.
As shown in Figure 2-4, PPI looks more smooth whereas DPI is crispier.
B
itmap Images
In general, when we look at pixel values, they are a range of integers. But,
when we convert the range of integers into bytes, we then have a bitmap
image.
One kind of bitmap is a binary image in which each pixel has one of
two numbers: either a zero or a one. They represent black or white and are
often used for storing images efficiently. Figure 2-5 shows a binary bitmap
image.
10
Chapter 2 Introduction to Image Processing
Lossless Compression
When we want to reduce the size of a file (which can be an image), but
we don’t want to compromise quality, this kind of compression is called
a lossless compression. The compressed file can be saved, but when we
require it, during the decompression process, all the information is restored
and we get the actual image (Figure 2-6). This first type of compression
gives priority to the information contained in the file—especially when
compressing text, where we cannot afford to lose even a single piece of
information.
11
Chapter 2 Introduction to Image Processing
L ossy Compression
With lossy compression, on the other hand, some of the data may be lost.
Lossy compression prioritizes saving space, rather than the accuracy of
the retrieved file. Some files, such as those that contain music or images,
can be trimmed and still be unaffected by the compression. There may be
some loss, but it isn’t worrisome (Figure 2-7).
Partially
Original Compressed
Restored
• BMP: Bitmap
12
Chapter 2 Introduction to Image Processing
Color Spaces
The organization of the colors of in an image in a specific format is called
color space. The way in which a color is represented is called a color model.
Each and every image uses one of the following color spaces for effective
picture representation:
13
Random documents with unrelated
content Scribd suggests to you:
The Project Gutenberg eBook of
Kapinapäällikön pojat: Seikkailuja Etelä-
Afrikan rajaseuduilla
This ebook is for the use of anyone anywhere in the United States
and most other parts of the world at no cost and with almost no
restrictions whatsoever. You may copy it, give it away or re-use it
under the terms of the Project Gutenberg License included with this
ebook or online at www.gutenberg.org. If you are not located in the
United States, you will have to check the laws of the country where
you are located before using this eBook.
Language: Finnish
Kirj.
Esteri Lokka
V. H.-A.
1. luku.
BUURIT.
2. luku.
KRAALI.
Entinen kenttäkornetti istuskeli kraalinsa edustalla — sitenhän
nimitetään eteläafrikalaista asuntoa. Hänen huuliensa välistä pisti
esiin iso piippu suunnattomine merenvahapesineen. Jokainen buuri
on tupakoitsija.
Kuka on Swartboy?
3. luku.
"SPRINGHAAN."
"Ne panemas maata yötä vasten — nyt me saada niit säkki täys!"
sanoi Swartboy tyytyväisin ilmein; sillä Swartboy oli varsinainen
heinäsirkkainsyöjä, pitäen niistä yhtä paljon kuin sekä kotka että
poutahaukka — vieläpä kuin itse "springhaanvogel".
Niin kävi kuin Swartboy oli sanonut. Parvi laskeutui todellakin
tasangolle.
"Ei voi lentää ilman aurinko", jatkoi bushmanni. "Liian kylmä nyt.
Ne kuolla ennen aamu."
4. luku.
PAKINAA HEINÄSIRKOISTA.