Practical Machine Learning and Image Processing: For Facial Recognition, Object Detection, and Pattern Recognition Using Python Himanshu Singh 2024 Scribd Download
Practical Machine Learning and Image Processing: For Facial Recognition, Object Detection, and Pattern Recognition Using Python Himanshu Singh 2024 Scribd Download
com
https://textbookfull.com/product/practical-machine-learning-
and-image-processing-for-facial-recognition-object-
detection-and-pattern-recognition-using-python-himanshu-
singh/
OR CLICK BUTTON
DOWNLOAD NOW
https://textbookfull.com/product/computer-vision-pattern-recognition-
image-processing-and-graphics-renu-rameshan/
textboxfull.com
https://textbookfull.com/product/image-processing-computer-vision-and-
pattern-recognition-hamid-r-arabnia/
textboxfull.com
https://textbookfull.com/product/biota-grow-2c-gather-2c-cook-loucas/
textboxfull.com
Image Processing and Pattern Recognition Based on Parallel
Shift Technology First Edition Bilan
https://textbookfull.com/product/image-processing-and-pattern-
recognition-based-on-parallel-shift-technology-first-edition-bilan/
textboxfull.com
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
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
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
Chapter 2 Introduction to Image Processing
RGB
Using the RGB color space, red, green, and blue are mixed in different ways
to make different color combinations. Why do we use RGB? Because our
eyes have color receptors that can perceive these three colors and their
combinations quite effectively.
We can form any color, theoretically, from these three colors. Each
color’s intensity is defined within a range of 0 to 255. This range is called
color depth.
RGB color space has two more components :
14
Chapter 2 Introduction to Image Processing
X
YZ
RGB colors have a threshold of saturation. They cannot go beyond what
we can see. The XYZ color space helps us go beyond this threshold. Now,
you may wonder why we would want to go beyond the threshold. Well,
it may not be possible for our human eyes to perceive certain colors, but
in the digital world, you may need these colors to be used. For example,
XYZ can be used for color matching; we can enter a color code and then
reproduce later it in different application, such as printing. Using XYZ, we
can encode all the colors that exist in the real world. This color space is
called XYZ because it extrapolates RGB colors in three dimensions: x, y,
and z. Figure 2-9 presents an XYZ representation of an image.
15
Chapter 2 Introduction to Image Processing
16
Chapter 2 Introduction to Image Processing
H
SV/HSL
HSV/HSL is an alternative representation of the RGB color space. It
consists of the following components:
• Hue
• Saturation
• Value
• Lightness
Hue is a property that describes three colors: green, red, and magenta. It
can also be a mixture of two pure colors: red and yellow, and yellow and green
Saturation measures the intensity of an image. It tells us how far a color
is from gray. A lower value means the color is approaching gray.
Lightness refers to the intensity of color with respect to white. It tells us
how far a color is from white.
Value is another measure of intensity. It tells us how far a color is from
black. Figure 2-10 shows an HSV representation of an image
17
Exploring the Variety of Random
Documents with Different Content
The Project Gutenberg eBook of Survival factor
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: English
By CHARLES V. DE VET
Illustrated by ORBAN
They followed the native for several miles over a faint game trail that
wound leisurely through brush and skimpy, small-leaved trees, before
either of the men recovered his composure enough to speak.
"He said 'Come'," Saxton mused. "Yet we're the first humans this far
over the Rim. Where did he learn our language?"
Wallace shrugged. "I've been wondering too," he answered.
"Should we try to talk to him?" Saxton asked, glancing ahead at their
companion.
The native, apparently, had no interest in their conversation. "Better
wait," Wallace suggested.
"I don't understand it." Saxton's tone was querulous. "No one's
allowed over the Rim ahead of us. A section has to be surveyed, and
worlds declared fit for habitation, before colonists can move in. Yet we
land here and find a native speaking our language."
"Perhaps he isn't a native," Wallace said.
"What do you mean?"
"When Earth first discovered spacebridge there were no laws
regulating its use. Limits were put on colonizing areas only after some
of the earlier expeditions failed to report back. One of them might
have been marooned here."
"Then this fellow's human?"
"He could be."
"If he is, would he be naked?" Saxton asked.
"Some of those lost expeditions disappeared as long as two thousand
years ago," Wallace answered. "A colony could have slipped back a
long ways in that time."
"But not this far," Saxton demurred. "They'd still have some traces of
their original culture left."
"A one-ship colony would have very limited mechanical resources,"
Wallace said. "And they'd be isolated here. As soon as the tools and
machines they brought with them wore out they'd be almost
impossible to replace. The odds are they'd slip back fast."
"I don't know." Clearly Saxton wasn't satisfied—but he let the subject
hang. "When we saw him kneeling on the ground, I thought that he
was worshipping us. But since then he's been acting as if he thought
he was the god instead of us."
They were halfway across a small clearing now and before Wallace
could answer the native ahead stopped abruptly. He stood
motionless, with his head tilted to one side, as though listening. After
a moment he motioned them to move to the left.
As Wallace and Saxton obeyed, Al-fin pointed urgently toward their
guns. They drew, and the native turned to stare at the bushes at the
far side of the clearing.
"What does he want?" Saxton asked.
"I don't—" Wallace's answer was cut off as a huge "cat," with long
stilt-like legs spread wide, sprang out of the bushes—directly at them.
Wallace and Saxton sprayed the beams of their guns across the cat's
chest, burning a wide, smoking gash. The beast landed, sprang
again, and died.
During the morning Saxton killed a small rodent, but found its flesh as
inedible as that of the cat. Wallace stayed inside studying the charts
and instruments.
They had their noonday meal in a small clearing by the side of the
ship. Wallace had been able to find no way of solving their difficulty.
For want of a better plan they'd decided to wait—while keeping close
track of their stalker.
"I've been thinking about those natives," Wallace said, as they lay
stretched on the grass. "If they are lost colonists—have you
wondered how they managed to survive here so long?"
"I did wonder how they protected themselves against the cats,"
Saxton answered. "They don't seem to have any weapons."
"Al-fin demonstrated that they must have exceptionally good hearing,"
Wallace said. "But would that be enough? You'd think the cats would
get them—when they're sleeping, if not during the day—or kill off their
young."
"That's what I meant," Saxton said. "We saw no weapons, so they
must have some other means of defense."
"They live pretty much like animals," Wallace observed. "Maybe they
stay alive the same way. If animals aren't powerful, they're usually
swift. Or they have some other survival characteristics, such as
prolific propagation. But what do these savages have—except
perhaps the sharp hearing that you mentioned? That alone shouldn't
be a deciding factor. Yet they were able to survive here for two
thousand years."
"How about an instinct of dispersal?" Saxton asked. "There might be
hundreds of groups like the one we saw."
"That would help. But my thought was that if they don't use weapons
they might have gone at it from another angle: they adjusted
themselves, instead of their tools, to their environment."
"Special ability stuff?" Saxton asked.
Wallace glanced over at the other man. By the look of abstraction on
Saxton's face he knew that no answer was necessary. Saxton's
imagination was a moving force. When a subject intrigued him he
could no more abandon it and turn to something else than he could
stop breathing. The trait was one that made him an ideal partner for
Wallace, with his more logical reasoning, and his insistence on
weighing fact against fact and belief against belief. It was, in fact, the
reason the two men had been teamed. One was the intuitive, the
other the harmonizing, controlling, factor in their combination.
Saxton rose and stretched. "I think I'll go inside," he said. "I want to
poke around in the library a while."
Wallace smiled and followed his companion into the ship. This at
least would take Saxton's mind off their troubles. Their enforced
inactivity would be less tedious for the more imaginative man.
Saxton selected several tapes from the book shelf and put them in
the magnifier. "When I find something that sounds likely," he said, "I'll
read it. Stop me if you want to discuss anything I find."
A half-hour later Saxton said, "Socrates maintained that the fewer our
needs, the nearer we resemble gods. Do you suppose Al-fin and his
tribe are approaching godhood?"
Wallace's answer, from the bunk where he lay, was a discourteous
grunt.
"I thought so too," Saxton quipped. He went on reading.
Almost an hour went by before he spoke again. "This might help put
our savages in the proper place in their cycle," he said. "Quote:
'Giambattista Vica, a native of Naples, held a theory that human
history progressed in cycles, each of which followed the same
course. The first move in a civilization began when man, terrified by
the forces of nature, invented and worshipped gods in order to
placate them. Next, he made up myths of demi-gods and heroes, and
arrived at the idea of kingship. Finally, from kingship he came to
democracy, which degenerated into chaos; after which the next cycle
started and the process was repeated."
"Interesting," Wallace said. "But even if it fits, I think we understand
well enough where these people are in their cycle. What we want now
is a clue as to what makes them different."
Wallace was about to doze off when Saxton said, "Listen to this: '... in
which he first injected the hormone that produces milk in the breasts
of nursing mothers into the bloodstream of starved virgin rats and
then introduced newly hatched squabs into their cages. Instead of
devouring the luscious meal placed before them, the starved virgin
animals acted as tender foster mothers to the helpless creatures.'" He
looked across at Wallace expectantly.
"I'm afraid I don't—" Wallace began.
"Don't you see?" Saxton asked. "Something about the food here has
made the natives different. We've got to find that food."
"That might be true also," Wallace answered slowly. "But I'm not as
interested in finding what caused the difference as I am in finding the
difference itself."
"Find one and you find the other," Saxton argued. He held up his
hand as Wallace made as though to speak. "Sleep on it," he said.
"Maybe we'll have some ideas by tomorrow."
They were able to extract no new clues from the tracking of the
bloodhound by the next forenoon. Neither man could arrive at any
means of thwarting the alien machine. Wallace had checked the
graph track minutely, looking for signs of a cycle, or cycles, in its
movements. He ended up convinced that none existed. It apparently
operated entirely at random.
At the mid-day meal Saxton suggested, "Let's pay those fellows in the
woods another visit."
"We may as well," Wallace agreed. "We're helpless here until we can
come up with some new idea."
They finished eating and strapped on their sidearms. They were not
certain that the path they took through the woods was the same they
had taken with Al-fin two days before, but at least it led in the same
general direction.
An hour later they were lost. Their way had not led them to the tribe
of naked savages and they had no idea where else to look. They
were debating whether or not to return to their ship when they
stepped out into a clearing—one larger than any they had come on
earlier.
In the center of the clearing rested a spaceship! From where they
stood they could see that its hull was rusted and weather-beaten.
"That hasn't flown in a long time," Saxton said, after the first few
minutes of wonder.
"Probably not since it first landed here," Wallace answered.
The clearing about the vessel had been kept free of brush and
bushes, and when they went across, and through the open portal of
the ship, they found the inside immaculate.
"They certainly keep it clean," Saxton observed.
"It may be a shrine to them," Wallace said. "That would explain why
we found Al-fin kneeling when we landed, and yet why he treated us
so nonchalantly. He was worshipping our ship, not us."
"I hope they don't find us here," Saxton remarked. "We might be
violating some taboo."
Most of the interior fittings of the vessel, they found, had long ago
rotted away. Only the metal parts still remained intact. The instrument
board was unfamiliar to them. "Pretty definitely an early model,"
Wallace said.
Saxton found something on one wall that held his absorbed interest.
"Come here, Ivan," he called.
"What is it?" Wallace asked, going over to stand beside him.
"Read that."
Wallace read aloud from the engraved plaque: "Spring, 2676. We, the
Dukobors, leave our Earth homes in the hope that we may find a
dwelling place for ourselves and our children, where we may worship
our God as we believe proper. We place ourselves in His hands and
pray that He will watch over us on our journey, and in the time to
come."
"That's over nineteen hundred years ago," Saxton said.
"Soon after the discovery of spacebridge," Wallace added. Without
being aware of it they both spoke in whispers.
They inspected the vessel for some time more, but found little of any
further interest.
A short time after they left the ancient spaceship Wallace and Saxton
stumbled on Al-fin and his group of naked natives.
This time they made a concerted effort to communicate with Al-fin,
and one or two of the others, but with no more success than before.
Neither side could understand more than a few words of the other's
language, and they could accomplish very little with signs.
Al-fin sat with them for a time, until they saw him tilt his head in the
gesture they remembered. On his face was the same expression of
listening. After a moment he rose leisurely and indicated that they
were to follow him. Most of the other natives, Wallace noticed on
rising, had already gone over and bunched together at one end of the
clearing. They appeared restless, but not frightened.
Welcome to our website – the ideal destination for book lovers and
knowledge seekers. With a mission to inspire endlessly, we offer a
vast collection of books, ranging from classic literary works to
specialized publications, self-development books, and children's
literature. Each book is a new journey of discovery, expanding
knowledge and enriching the soul of the reade
Our website is not just a platform for buying books, but a bridge
connecting readers to the timeless values of culture and wisdom. With
an elegant, user-friendly interface and an intelligent search system,
we are committed to providing a quick and convenient shopping
experience. Additionally, our special promotions and home delivery
services ensure that you save time and fully enjoy the joy of reading.
textbookfull.com