100% found this document useful (4 votes)
33 views66 pages

Buy Ebook Building Computer Vision Applications Using Artificial Neural Networks, 2nd Edition Shamshad Ansari Cheap Price

Neural

Uploaded by

mutaztanga
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
100% found this document useful (4 votes)
33 views66 pages

Buy Ebook Building Computer Vision Applications Using Artificial Neural Networks, 2nd Edition Shamshad Ansari Cheap Price

Neural

Uploaded by

mutaztanga
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 66

Download Full Version ebookmass - Visit ebookmass.

com

Building Computer Vision Applications Using


Artificial Neural Networks, 2nd Edition Shamshad
Ansari

https://ebookmass.com/product/building-computer-vision-
applications-using-artificial-neural-networks-2nd-edition-
shamshad-ansari/

OR CLICK HERE

DOWLOAD NOW

Discover More Ebook - Explore Now at ebookmass.com


Instant digital products (PDF, ePub, MOBI) ready for you
Download now and discover formats that fit your needs...

Analysis and Visualization of Discrete Data Using Neural


Networks Koji Koyamada

https://ebookmass.com/product/analysis-and-visualization-of-discrete-
data-using-neural-networks-koji-koyamada/

ebookmass.com

3-D Computer Vision: Principles, Algorithms and


Applications 1st Edition Yu-Jin Zhang

https://ebookmass.com/product/3-d-computer-vision-principles-
algorithms-and-applications-1st-edition-yu-jin-zhang/

ebookmass.com

Accelerators for Convolutional Neural Networks Arslan


Munir

https://ebookmass.com/product/accelerators-for-convolutional-neural-
networks-arslan-munir/

ebookmass.com

Exercise Physiology: Theory and Application to Fitness and


Performance; Eleventh Edition Scott K. Powers

https://ebookmass.com/product/exercise-physiology-theory-and-
application-to-fitness-and-performance-eleventh-edition-scott-k-
powers/
ebookmass.com
The Realizations of the Self 1st ed. Edition Andrea
Altobrando

https://ebookmass.com/product/the-realizations-of-the-self-1st-ed-
edition-andrea-altobrando/

ebookmass.com

Iron Flame & Fourth Wing (Empyrian Books 1 and 2) 2nd


Edition Rebecca Yarros

https://ebookmass.com/product/iron-flame-fourth-wing-empyrian-
books-1-and-2-2nd-edition-rebecca-yarros/

ebookmass.com

Business and Society: Ethics, Sustainability, and


Stakeholder Management 9th Edition, (Ebook PDF)

https://ebookmass.com/product/business-and-society-ethics-
sustainability-and-stakeholder-management-9th-edition-ebook-pdf/

ebookmass.com

Embedded Mechatronic Systems, Volume 2: Analysis of


Failures, Modeling, Simulation and Optimization 2nd
Edition Abdelkhalak El Hami
https://ebookmass.com/product/embedded-mechatronic-systems-
volume-2-analysis-of-failures-modeling-simulation-and-
optimization-2nd-edition-abdelkhalak-el-hami/
ebookmass.com

Current Diagnosis and Treatment Surgery 14/E 14th Edition,


(Ebook PDF)

https://ebookmass.com/product/current-diagnosis-and-treatment-
surgery-14-e-14th-edition-ebook-pdf/

ebookmass.com
Make Virtual Learning Matter: How to Turn Virtual
Classrooms into a Remarkable, Authentic Experience for
Kids Paul Axtell
https://ebookmass.com/product/make-virtual-learning-matter-how-to-
turn-virtual-classrooms-into-a-remarkable-authentic-experience-for-
kids-paul-axtell/
ebookmass.com
Building Computer Vision
Applications Using
Artificial Neural Networks
With Examples in OpenCV
and TensorFlow with Python
Second Edition

Shamshad Ansari
Building Computer Vision Applications Using Artificial Neural Networks: With
Examples in OpenCV and TensorFlow with Python, Second Edition
Shamshad Ansari
Centreville, VA, USA

ISBN-13 (pbk): 978-1-4842-9865-7 ISBN-13 (electronic): 978-1-4842-9866-4


https://doi.org/10.1007/978-1-4842-9866-4

Copyright © 2023 by Shamshad Ansari


This work is subject to copyright. All rights are reserved by the Publisher, whether the whole or part of the
material is concerned, specifically the rights of translation, reprinting, reuse of illustrations, recitation,
broadcasting, reproduction on microfilms or in any other physical way, and transmission or information
storage and retrieval, electronic adaptation, computer software, or by similar or dissimilar methodology now
known or hereafter developed.
Trademarked names, logos, and images may appear in this book. Rather than use a trademark symbol with
every occurrence of a trademarked name, logo, or image we use the names, logos, and images only in an
editorial fashion and to the benefit of the trademark owner, with no intention of infringement of the
trademark.
The use in this publication of trade names, trademarks, service marks, and similar terms, even if they are not
identified as such, is not to be taken as an expression of opinion as to whether or not they are subject to
proprietary rights.
While the advice and information in this book are believed to be true and accurate at the date of publication,
neither the author nor the editors nor the publisher can accept any legal responsibility for any errors or
omissions that may be made. The publisher makes no warranty, express or implied, with respect to the
material contained herein.
Managing Director, Apress Media LLC: Welmoed Spahr
Acquisitions Editor: Celestin Suresh John
Development Editor: Laura Berendson
Coordinating Editor: Mark Powers
Cover designed by eStudioCalamar
Cover image designed by Freepik (www.freepik.com)
Distributed to the book trade worldwide by Apress Media, LLC, 1 New York Plaza, New York, NY 10004,
U.S.A. Phone 1-800-SPRINGER, fax (201) 348-4505, e-mail [email protected], or visit www.
springeronline.com. Apress Media, LLC is a California LLC and the sole member (owner) is Springer Science
+ Business Media Finance Inc (SSBM Finance Inc). SSBM Finance Inc is a Delaware corporation.
For information on translations, please e-mail [email protected]; for reprint,
paperback, or audio rights, please e-mail [email protected].
Apress titles may be purchased in bulk for academic, corporate, or promotional use. eBook versions and
licenses are also available for most titles. For more information, reference our Print and eBook Bulk Sales
web page at https://www.apress.com/bulk-sales.
Any source code or other supplementary material referenced by the author in this book is available to
readers on GitHub (https://github.com/Apress). For more detailed information, please visit
https://www.apress.com/gp/services/source-code.
Paper in this product is recyclable
In God we trust.
To my wonderful parents, Abdul Samad and Nazhat Parween,
who always corrected my mistakes and raised me to become a
good person.
To my lovely wife, Shazia, and our two beautiful daughters,
Dua and Erum. Without their love and support, this book would
not have been possible.
Table of Contents
About the Author�����������������������������������������������������������������������������������������������������xv

About the Technical Reviewers�����������������������������������������������������������������������������xvii

Acknowledgments��������������������������������������������������������������������������������������������������xix
Introduction������������������������������������������������������������������������������������������������������������xxi

Chapter 1: Prerequisites and Software Installation������������������������������������������������� 1


Python and PIP������������������������������������������������������������������������������������������������������������������������������ 2
Installing Python and PIP on Ubuntu���������������������������������������������������������������������������������������� 2
Installing Python and PIP on macOS���������������������������������������������������������������������������������������� 3
Installing Python and PIP on Red Hat Linux����������������������������������������������������������������������������� 3
Installing Python and PIP on Windows������������������������������������������������������������������������������������� 3
virtualenv�������������������������������������������������������������������������������������������������������������������������������������� 4
Installing and Activating virtualenv����������������������������������������������������������������������������������������� 4
TensorFlow������������������������������������������������������������������������������������������������������������������������������������ 5
Installing TensorFlow on Mac with M1 Chip���������������������������������������������������������������������������� 6
Installing TensorFlow for CPUs������������������������������������������������������������������������������������������������ 7
Installing TensorFlow for GPUs������������������������������������������������������������������������������������������������ 7
PyCharm IDE��������������������������������������������������������������������������������������������������������������������������������� 8
Installing PyCharm������������������������������������������������������������������������������������������������������������������ 8
Configuring PyCharm to Use virtualenv����������������������������������������������������������������������������������� 9
OpenCV��������������������������������������������������������������������������������������������������������������������������������������� 10
Working with OpenCV������������������������������������������������������������������������������������������������������������ 11
Installing OpenCV 4 with Python Bindings����������������������������������������������������������������������������� 11

v
Table of Contents

Additional Libraries��������������������������������������������������������������������������������������������������������������������� 12
Installing SciPy���������������������������������������������������������������������������������������������������������������������� 12
Installing Matplotlib��������������������������������������������������������������������������������������������������������������� 12
Summary������������������������������������������������������������������������������������������������������������������������������� 12

Chapter 2: Core Concepts of Image and Video Processing������������������������������������� 15


Image Processing����������������������������������������������������������������������������������������������������������������������� 15
Image Basics������������������������������������������������������������������������������������������������������������������������������� 16
Pixels������������������������������������������������������������������������������������������������������������������������������������� 16
Coordinate Systems�������������������������������������������������������������������������������������������������������������������� 17
Using Python and OpenCV Code to Manipulate Images������������������������������������������������������������� 20
Program: Loading, Exploring, and Showing an Image����������������������������������������������������������� 21
Program: OpenCV Code to Access and Manipulate Pixels����������������������������������������������������� 23
Drawing��������������������������������������������������������������������������������������������������������������������������������������� 25
Drawing a Line on an Image�������������������������������������������������������������������������������������������������� 25
Drawing a Rectangle on an Image���������������������������������������������������������������������������������������� 28
Drawing a Circle on an Image����������������������������������������������������������������������������������������������� 32
Summary������������������������������������������������������������������������������������������������������������������������������������ 33

Chapter 3: Techniques of Image Processing����������������������������������������������������������� 35


Transformation���������������������������������������������������������������������������������������������������������������������������� 36
Resizing��������������������������������������������������������������������������������������������������������������������������������� 36
Translation����������������������������������������������������������������������������������������������������������������������������� 40
Rotation��������������������������������������������������������������������������������������������������������������������������������� 43
Flipping���������������������������������������������������������������������������������������������������������������������������������� 45
Cropping�������������������������������������������������������������������������������������������������������������������������������� 48
Image Arithmetic and Bitwise Operations����������������������������������������������������������������������������������� 50
Addition��������������������������������������������������������������������������������������������������������������������������������� 51
Subtraction���������������������������������������������������������������������������������������������������������������������������� 55
Bitwise Operations���������������������������������������������������������������������������������������������������������������� 60
Masking�������������������������������������������������������������������������������������������������������������������������������������� 66
Splitting and Merging Channels�������������������������������������������������������������������������������������������������� 70

vi
Table of Contents

Noise Reduction Using Smoothing and Blurring������������������������������������������������������������������������� 73


Mean Filtering or Averaging�������������������������������������������������������������������������������������������������� 73
Gaussian Filtering������������������������������������������������������������������������������������������������������������������ 76
Median Blurring��������������������������������������������������������������������������������������������������������������������� 78
Bilateral Blurring������������������������������������������������������������������������������������������������������������������� 81
Binarization with Thresholding��������������������������������������������������������������������������������������������������� 83
Simple Thresholding�������������������������������������������������������������������������������������������������������������� 84
Adaptive Thresholding����������������������������������������������������������������������������������������������������������� 86
Otsu’s Binarization����������������������������������������������������������������������������������������������������������������� 89
Gradients and Edge Detection����������������������������������������������������������������������������������������������������� 92
Gradient-Based Edge Detection�������������������������������������������������������������������������������������������� 92
Canny Edge Detection����������������������������������������������������������������������������������������������������������� 99
Contours������������������������������������������������������������������������������������������������������������������������������������ 101
Morphological Transformation�������������������������������������������������������������������������������������������������� 104
Dilation�������������������������������������������������������������������������������������������������������������������������������� 105
Erosion��������������������������������������������������������������������������������������������������������������������������������� 106
Opening������������������������������������������������������������������������������������������������������������������������������� 107
Closing��������������������������������������������������������������������������������������������������������������������������������� 108
Morphological Gradient������������������������������������������������������������������������������������������������������� 109
Top Hat�������������������������������������������������������������������������������������������������������������������������������� 110
Black Hat����������������������������������������������������������������������������������������������������������������������������� 111
Template Matching������������������������������������������������������������������������������������������������������������������� 113
Template Matching with Multiple Objects��������������������������������������������������������������������������� 119
Summary���������������������������������������������������������������������������������������������������������������������������������� 124

Chapter 4: Building a Machine Learning–Based Computer Vision System����������� 125


Image Processing Pipeline�������������������������������������������������������������������������������������������������������� 125
Feature Extraction��������������������������������������������������������������������������������������������������������������������� 127
How to Represent Features������������������������������������������������������������������������������������������������� 129
Color Histogram������������������������������������������������������������������������������������������������������������������� 129
Histogram Equalizer������������������������������������������������������������������������������������������������������������ 136

vii
Table of Contents

GLCM����������������������������������������������������������������������������������������������������������������������������������� 140
HOGs������������������������������������������������������������������������������������������������������������������������������������ 145
LBP�������������������������������������������������������������������������������������������������������������������������������������� 152
Feature Selection���������������������������������������������������������������������������������������������������������������������� 159
Filter Method����������������������������������������������������������������������������������������������������������������������� 160
Wrapper Method������������������������������������������������������������������������������������������������������������������ 160
Embedded Method�������������������������������������������������������������������������������������������������������������� 161
Model Training��������������������������������������������������������������������������������������������������������������������������� 162
How to Do Machine Learning���������������������������������������������������������������������������������������������� 162
Supervised Learning������������������������������������������������������������������������������������������������������������ 163
Unsupervised Learning�������������������������������������������������������������������������������������������������������� 164
Model Deployment�������������������������������������������������������������������������������������������������������������������� 165
Summary���������������������������������������������������������������������������������������������������������������������������������� 167

Chapter 5: Deep Learning and Artificial Neural Networks������������������������������������ 169


Introduction to Artificial Neural Networks��������������������������������������������������������������������������������� 170
Perceptron��������������������������������������������������������������������������������������������������������������������������� 174
Multilayer Perceptron���������������������������������������������������������������������������������������������������������� 175
What Is Deep Learning?������������������������������������������������������������������������������������������������������ 176
Deep Learning or Multilayer Perceptron Architecture��������������������������������������������������������� 176
Activation Functions������������������������������������������������������������������������������������������������������������ 179
Feedforward������������������������������������������������������������������������������������������������������������������������ 187
Error Function���������������������������������������������������������������������������������������������������������������������� 187
Optimization Algorithms������������������������������������������������������������������������������������������������������ 192
Backpropagation����������������������������������������������������������������������������������������������������������������� 201
Introduction to TensorFlow�������������������������������������������������������������������������������������������������������� 202
TensorFlow Installation�������������������������������������������������������������������������������������������������������� 202
How to Use TensorFlow������������������������������������������������������������������������������������������������������� 202
TensorFlow Terminology������������������������������������������������������������������������������������������������������ 203

viii
Table of Contents

Our First Computer Vision Model with Deep Learning: Classification of Handwritten Digits���� 207
Model Overview������������������������������������������������������������������������������������������������������������������������������������������������ 207
Model Implementation��������������������������������������������������������������������������������������������������������� 208
Model Evaluation����������������������������������������������������������������������������������������������������������������������� 217
Overfitting���������������������������������������������������������������������������������������������������������������������������� 217
Underfitting�������������������������������������������������������������������������������������������������������������������������� 219
Hyperparameters���������������������������������������������������������������������������������������������������������������������� 223
TensorBoard������������������������������������������������������������������������������������������������������������������������ 224
Experiments for Hyperparameter Tuning����������������������������������������������������������������������������� 225
Saving and Restoring Model����������������������������������������������������������������������������������������������������� 229
Save Model Checkpoints During Training���������������������������������������������������������������������������� 230
Manually Save Weights������������������������������������������������������������������������������������������������������� 233
Load the Saved Weights and Retrain the Model������������������������������������������������������������������ 233
Saving the Entire Model������������������������������������������������������������������������������������������������������ 234
Retraining the Existing Model���������������������������������������������������������������������������������������������� 234
Using a Trained Model in Applications��������������������������������������������������������������������������������� 235
Convolutional Neural Network�������������������������������������������������������������������������������������������������� 236
Architecture of CNN������������������������������������������������������������������������������������������������������������� 236
How a CNN Works���������������������������������������������������������������������������������������������������������������� 238
Summary of CNN Concepts������������������������������������������������������������������������������������������������� 243
Training a CNN Model: Pneumonia Detection from Chest X-rays���������������������������������������� 244
Examples of Popular CNNs�������������������������������������������������������������������������������������������������� 255
Summary����������������������������������������������������������������������������������������������������������������������������� 260

Chapter 6: Deep Learning in Object Detection������������������������������������������������������ 261


Object Detection������������������������������������������������������������������������������������������������������������������������ 261
Intersection over Union������������������������������������������������������������������������������������������������������������� 262
Region-Based Convolutional Neural Network��������������������������������������������������������������������������� 264
Fast R-CNN������������������������������������������������������������������������������������������������������������������������������� 266
Faster R-CNN���������������������������������������������������������������������������������������������������������������������������� 267
Region Proposal Network���������������������������������������������������������������������������������������������������� 268
Fast R-CNN�������������������������������������������������������������������������������������������������������������������������� 269

ix
Table of Contents

Mask R-CNN������������������������������������������������������������������������������������������������������������������������������ 269


Backbone����������������������������������������������������������������������������������������������������������������������������� 270
RPN�������������������������������������������������������������������������������������������������������������������������������������� 271
Output Head������������������������������������������������������������������������������������������������������������������������� 271
What Is the Significance of the Masks?������������������������������������������������������������������������������ 271
Mask R-CNN in Human Pose Estimation����������������������������������������������������������������������������� 272
Single-Shot Multibox Detection������������������������������������������������������������������������������������������������ 273
SSD Network Architecture��������������������������������������������������������������������������������������������������� 273
Training�������������������������������������������������������������������������������������������������������������������������������� 276
SSD Results������������������������������������������������������������������������������������������������������������������������� 279
YOLO������������������������������������������������������������������������������������������������������������������������������������������ 279
YOLO Network Design���������������������������������������������������������������������������������������������������������� 282
Limitations of YOLO������������������������������������������������������������������������������������������������������������� 282
YOLO9000 or YOLOv2����������������������������������������������������������������������������������������������������������� 283
YOLOv3�������������������������������������������������������������������������������������������������������������������������������� 286
YOLOv4�������������������������������������������������������������������������������������������������������������������������������������� 289
YOLOv7�������������������������������������������������������������������������������������������������������������������������������������� 290
Comparison of Object Detection Algorithms����������������������������������������������������������������������������� 297
Comparison of Architecture������������������������������������������������������������������������������������������������� 298
Comparison of Performance������������������������������������������������������������������������������������������������ 299
Training Object Detection Model Using TensorFlow������������������������������������������������������������������ 300
TensorFlow on Google Colab with GPU�������������������������������������������������������������������������������� 301
Detecting Objects Using Trained Models����������������������������������������������������������������������������������� 332
Installing TensorFlow’s models Project������������������������������������������������������������������������������� 332
Code for Object Detection���������������������������������������������������������������������������������������������������� 335
Training a YOLOv7 Model for Object Detection�������������������������������������������������������������������������� 349
Dataset�������������������������������������������������������������������������������������������������������������������������������� 350
Preparing Colab Environment��������������������������������������������������������������������������������������������������� 352
Creating the data.yaml File������������������������������������������������������������������������������������������������������� 353
Cloning YOLOv7 GitHub Repository������������������������������������������������������������������������������������������� 353
Training YOLOv7 Model������������������������������������������������������������������������������������������������������������� 354

x
Table of Contents

Launching YOLOv7 Model Training��������������������������������������������������������������������������������������� 356


Monitoring Training Progress����������������������������������������������������������������������������������������������� 359
Monitoring Training Metrics Using TensorBoard������������������������������������������������������������������ 362
Inference or Object Detection Using the Training YOLOv7 Model���������������������������������������������� 362
Exporting YOLOv7 Model to ONNX��������������������������������������������������������������������������������������������� 364
Converting the ONNX Model to TensorFlow and TensorFlow Lite Formats�������������������������������� 366
Predicting Using TensorFlow Lite Model����������������������������������������������������������������������������������� 367
Summary���������������������������������������������������������������������������������������������������������������������������������� 371

Chapter 7: Practical Example: Object Tracking in Videos������������������������������������� 373


Preparing the Working Environment����������������������������������������������������������������������������������������� 374
Reading a Video Stream������������������������������������������������������������������������������������������������������������ 376
Loading the Object Detection Model����������������������������������������������������������������������������������������� 379
Detecting Objects in Video Frames������������������������������������������������������������������������������������������� 380
Creating a Unique Identity for Objects Using dHash����������������������������������������������������������������� 381
Using the Hamming Distance to Determine Image Similarity��������������������������������������������������� 383
Object Tracking������������������������������������������������������������������������������������������������������������������������� 384
Displaying a Live Video Stream in a Web Browser�������������������������������������������������������������������� 387
Installing Flask�������������������������������������������������������������������������������������������������������������������� 387
Flask Directory Structure���������������������������������������������������������������������������������������������������� 387
HTML for Displaying a Video Stream����������������������������������������������������������������������������������� 387
Flask to Load the HTML Page���������������������������������������������������������������������������������������������� 389
Flask to Serve the Video Stream����������������������������������������������������������������������������������������� 389
Running the Flask Server���������������������������������������������������������������������������������������������������� 390
Putting It All Together���������������������������������������������������������������������������������������������������������������� 390
Summary���������������������������������������������������������������������������������������������������������������������������������� 400

Chapter 8: Practical Example: Face Recognition�������������������������������������������������� 401


FaceNet������������������������������������������������������������������������������������������������������������������������������������� 402
FaceNet Neural Network Architecture��������������������������������������������������������������������������������� 403
Training a Face Recognition Model������������������������������������������������������������������������������������������� 408
Checking Out FaceNet from GitHub������������������������������������������������������������������������������������� 409

xi
Table of Contents

Dataset�������������������������������������������������������������������������������������������������������������������������������� 410
Downloading VGGFace2 Data���������������������������������������������������������������������������������������������� 412
Data Preparation������������������������������������������������������������������������������������������������������������������ 414
Model Training��������������������������������������������������������������������������������������������������������������������� 419
Evaluation���������������������������������������������������������������������������������������������������������������������������� 420
Developing a Real-Time Face Recognition System������������������������������������������������������������������� 421
Face Detection Model���������������������������������������������������������������������������������������������������������� 421
Classifier for Face Recognition�������������������������������������������������������������������������������������������� 422
Summary���������������������������������������������������������������������������������������������������������������������������������� 428

Chapter 9: Industrial Application: Real-Time Defect Detection in Industrial


Manufacturing������������������������������������������������������������������������������������������������������ 429
Real-Time Surface Defect Detection System���������������������������������������������������������������������������� 430
Dataset�������������������������������������������������������������������������������������������������������������������������������� 430
Google Colab Notebook������������������������������������������������������������������������������������������������������� 432
Data Transformation������������������������������������������������������������������������������������������������������������ 433
Training the SSD Model������������������������������������������������������������������������������������������������������� 442
Model Evaluation����������������������������������������������������������������������������������������������������������������� 445
Exporting the Model������������������������������������������������������������������������������������������������������������ 446
Prediction���������������������������������������������������������������������������������������������������������������������������� 447
Real-Time Defect Detector�������������������������������������������������������������������������������������������������� 448
Image Annotations�������������������������������������������������������������������������������������������������������������������� 448
Installing VoTT��������������������������������������������������������������������������������������������������������������������� 449
Create Connections������������������������������������������������������������������������������������������������������������� 450
Create a New Project����������������������������������������������������������������������������������������������������������� 451
Create Class Labels������������������������������������������������������������������������������������������������������������� 452
Label the Images����������������������������������������������������������������������������������������������������������������� 453
Export Labels����������������������������������������������������������������������������������������������������������������������� 454
Summary���������������������������������������������������������������������������������������������������������������������������������� 455

Chapter 10: Computer Vision Modeling on the Cloud������������������������������������������� 457


TensorFlow Distributed Training������������������������������������������������������������������������������������������������ 458
What Is Distributed Training?����������������������������������������������������������������������������������������������� 458

xii
Table of Contents

TensorFlow Distribution Strategy���������������������������������������������������������������������������������������� 461


TF_CONFIG: TensorFlow Cluster Configuration�������������������������������������������������������������������� 466
Example Code of Distributed Training with a Parameter Server����������������������������������������������� 468
Steps for Running Distributed Training on the Cloud���������������������������������������������������������������� 473
Distributed Training on Google Cloud���������������������������������������������������������������������������������������� 475
Signing Up for GCP Access�������������������������������������������������������������������������������������������������� 475
Creating a Google Cloud Storage Bucket����������������������������������������������������������������������������� 476
Launching GCP Virtual Machines����������������������������������������������������������������������������������������� 479
SSH to Log In to Each VMs�������������������������������������������������������������������������������������������������� 483
Uploading the Code for Distributed Training or Cloning the GitHub Repository������������������� 484
Installing Prerequisites and TensorFlow������������������������������������������������������������������������������ 484
Running Distributed Training����������������������������������������������������������������������������������������������� 485
Distributed Training on Azure���������������������������������������������������������������������������������������������������� 486
Creating a VM with Multiple GPUs on Azure������������������������������������������������������������������������ 487
Installing GPU Drivers and Libraries������������������������������������������������������������������������������������ 491
Creating virtualenv and Installing TensorFlow��������������������������������������������������������������������� 493
Implementing MirroredStrategy������������������������������������������������������������������������������������������ 494
Running Distributed Training����������������������������������������������������������������������������������������������� 494
Distributed Training on AWS������������������������������������������������������������������������������������������������������ 497
Horovod������������������������������������������������������������������������������������������������������������������������������� 497
How to Use Horovod������������������������������������������������������������������������������������������������������������ 498
Creating a Horovod Cluster on AWS������������������������������������������������������������������������������������� 502
Installing Horovod���������������������������������������������������������������������������������������������������������������� 510
Running Horovod to Execute Distributed Training��������������������������������������������������������������� 510
Summary���������������������������������������������������������������������������������������������������������������������������������� 512

Index��������������������������������������������������������������������������������������������������������������������� 513

xiii
About the Author
Shamshad Ansari is an author, inventor, and thought
leader in the fields of computer vision, machine learning,
artificial intelligence, and cognitive science. He has
extensive experience in high-scale, distributed, and parallel
computing. Sam currently serves as an adjunct professor at
George Mason University, teaching graduate-level programs
within the Data Analytics Engineering department of the
Volgenau School of Engineering. His areas of instruction
encompass machine learning, natural language processing,
and computer vision, where he imparts his knowledge and
expertise to aspiring professionals.
Having authored multiple publications on topics such as machine learning, RFID,
and high-scale enterprise computing, Sam’s contributions extend beyond academia. He
holds four US patents related to healthcare AI, showcasing his innovative mindset and
practical application of technology.
Throughout his extensive 20+ years of experience in enterprise software
development, Sam has been involved with several tech startups and early-stage
companies. He has played pivotal roles in building and expanding tech teams from
the ground up, contributing to their eventual acquisition by larger organizations.
At the beginning of his career, he worked with esteemed institutions such as the US
Department of Defense (DOD) and IBM, honing his skills and knowledge in the
industry.
Currently, Sam serves as the president and CEO of Accure, Inc., an AI company
that he founded. He is the creator and architect of, and continues to make significant
contributions to, Momentum AI, a no-code platform that encompasses data
engineering, machine learning, AI, MLOps, data warehousing, and business intelligence.
Throughout his career, Sam has made notable contributions in various domains
including healthcare, retail, supply chain, banking and finance, and manufacturing.

xv
About the Author

Demonstrating his leadership skills, he has successfully managed teams of software


engineers, data scientists, and DevSecOps professionals, leading them to deliver
exceptional results. Sam earned his bachelor’s degree in engineering from Birsa Institute
of Technology (BIT) Sindri and subsequently a master’s degree from the prestigious
Indian Institute of Information Technology and Management Kerala (IIITM-K), now
known as the Digital University of Kerala.

xvi
About the Technical Reviewers
Bharath Kumar Bolla has over 12 years of experience
and is a senior data scientist at Salesforce, Hyderabad.
Bharath obtained a master’s degree in data science from
the University of Arizona and a master’s degree in life
sciences from Mississippi State University. Bharath worked
as a research scientist for seven years at the University of
Georgia, Emory University, and Eurofins LLC. At Verizon,
Bharath led a team to build a “Smart Pricing” solution, and
at Happiest Minds, he worked on AI-based digital marketing
products. Along with his day-to-day responsibilities, Bharath is a mentor and an active
researcher with more than 20 publications in conferences and journals. Bharath received
the “40 Under 40 Data Scientists 2021” award from Analytics India Magazine for his
accomplishments.

F. Brett Berlin is currently a faculty member of the George


Mason University College of Engineering and Computing
Data Analytics Engineering master’s program. He brings five
decades of professional and life experience to his passion for
investing in people with purpose. He has served as professor,
executive consultant, mentor, applied global infosystem
strategist, high-performance computing executive and
policy leader, and early Internet policy partner. Strategically,
he seeks engagements with public/private/academic
collaborations for value creation with impact. His current eighth decade passion is
investing forward, by grace, serving ethically vision-driven digital/AI innovator aspirants
seeking to engage the challenges of the times.
Mr. Berlin is a graduate of the University of Texas Austin Graduate School of Arts
and Sciences (Computer Science, “Time-Extended Petri Nets”); the USAF Academy
(Computer Science, Mathematics & Engineering), and USAF Officer leadership schools.
He is married to Kathleen Berlin, with whom he shares children and grandchildren.

xvii
Acknowledgments
I wrote this book with two primary objectives in mind. First, I aimed to comprehensively
construct the foundations of computer vision concepts, starting from the basics and
progressing to an advanced level. Secondly, I aspired to furnish a practical guide for
the application of these concepts in real-world computer vision systems by including
practical use cases and code examples.
Achieving these objectives required meticulous organization of topics, integration
of content with meaningful and practical use cases, and thorough testing of the code,
all of which demanded my undivided attention throughout the writing process. This
accomplishment wouldn’t have been possible without the unwavering support of my
family. My gratitude is immeasurable towards my wife, Shazia, who managed the care of
our two daughters (Erum and Dua), ensuring they remained engaged while I dedicated
myself to the writing of this book. She ingeniously transformed this experience into a
positive experience for both them and myself. Remarkably, my children began tracking
my progress, celebrating each completion of a section, subsection, or chapter. Their
enthusiasm became a wellspring of energy and motivation that significantly enriched my
writing experience. I still do not know what magic my wife used to do this.
My life is indebted to Anumati Bhagi and Ashok Bhagi, who are no less than parents
to me; their love and support always motivate me.
This book is a collection of my lifetime experiences that I gained by working with
some of the greatest engineers, data scientists, and business professionals. I would
like to thank all my colleagues at Accure and all the past companies I have worked at. I
sincerely thank all my teachers, professors, and mentors who enlightened me with their
knowledge and wisdom.
Working with the Apress editorial team, which includes remarkable individuals like
Mark Powers and Laura Berendson, has been an incredible experience. I want to extend
a special thank you to Shonmirin PA, the project coordinator, who has been instrumental
in coordinating with the editorial team and technical reviewers and providing prompt
and helpful responses to my questions along the way.

xix
Acknowledgments

I would like to express my appreciation to John Celestin, Executive Editor at Apress.


His thoughtfulness and quick decision-making have left a profound impression on me.
Thank you, John, for having faith in my work. I also extend my gratitude to Apress for
making the publication of this book possible.
I wish to extend my gratitude to Bharat Bolla for his dedicated technical review of
this book. Additionally, I would like to express special appreciation to Professor Brett
Berlin for his meticulous review, invaluable insights, and constructive suggestions
that have significantly enhanced the quality of this book. Thank you, Bharat and
Professor Berlin.
Finally, I extend my thanks to the readers of this book. I am eagerly interested in
receiving your feedback. Kindly share your comments, suggestions, and questions with
me at [email protected]. Also, given the ever-evolving nature of technology, I may
need to update some of this book’s code examples via the book’s GitHub repository. I am
committed to maintaining the code updates, so check the book’s GitHub repository for
updates. I anticipate your valuable input and look forward to engaging with you.

xx
Introduction
For over 20 years, I have had the privilege of working with distinguished data scientists
and computer vision experts, some of the finest in their field. Throughout this journey,
I’ve gained invaluable insights, particularly in the realm of building scalable computer
vision systems following the best practices. This book encapsulates the wisdom acquired
from my work experience, as well as the collective knowledge amassed from the
exceptional individuals with whom I’ve had the fortune to work or collaborate. This book
also presents knowledge I’ve gained from the works of some of the greatest contributors
and thought leaders of computer vision. I have provided references to their work at
appropriate places throughout the book.
When I hire new engineers and scientists, one of my main challenges has been giving
them effective training so that they can quickly start contributing to the development
of computer vision systems. With numerous online resources and books available on
various computer vision topics, it’s easy to feel overwhelmed by the sheer amount of
information. This is especially true since the field of computer vision is extensive and
intricate.
In this book, my aim is to offer a structured and methodical approach to learning
computer visions systems. You will learn essential concepts first and then build on
those concepts by working through practical code examples that pertain to real-world
computer vision systems. This approach will help you connect the dots as you progress
through the chapters. I’ve structured this book to be as hands-on and practical as
possible to assist you effectively.
This book begins by explaining the core concepts of computer vision. It presents
code examples to help you understand these ideas better. In the beginning, the examples
mainly use OpenCV with the Python language.
This book also covers the basic concepts of machine learning and then gradually
moves into more complex concepts like artificial neural networks and deep learning.
Every concept is reinforced with one or more practical code examples that show how
the concept is applied in practice. Machine learning–related concepts are illustrated via
code examples in TensorFlow with Python.

xxi
Introduction

This book presents 11 examples, including working code, that demonstrate how
computer vision is used in real life. These examples are from different domains like
healthcare, surveillance, security, and manufacturing. To help you understand the code,
I explain each line step by step.
Chapters 7, 8, and 9 are dedicated to building practical computer vision–based
systems. These chapters show you all stages of how to create vision systems, from
obtaining pictures or videos, to building a data pipeline, to training a model, and finally
deploying the model for real situations.
Training state-of-the-art computer vision models requires a lot of hardware
resources. It is desirable and economically beneficial to train computer vision models on
a cloud infrastructure to leverage the latest hardware resources, such as GPUs, and pay-
as-­you-go cost models. The final chapter, Chapter 10, provides step-by-step instructions
for building machine learning–based computer vision applications on three popular
cloud infrastructures: Google Cloud Platform, Amazon AWS, and Microsoft Azure.
Even though this book starts from explaining a single pixel of an image and goes up
to teaching how to use cloud computers for neural networks training, there are certain
prerequisites to better understand the concepts presented in this book. You should
already know how to use the Python programming language.
The purpose of this book is to assist working professionals, programmers, data
scientists, and even students (undergraduate and graduate) acquire hands-on skills for
building computer vision applications using artificial neural networks.

xxii
CHAPTER 1

Prerequisites and
Software Installation
This book is a practical guide that explores the process of building computer vision
applications using the Python programming language. Throughout this book, you will
gain a comprehensive understanding of leveraging OpenCV for image manipulation and
harnessing the power of TensorFlow to build machine learning models.
OpenCV, originally developed by Intel and written in C++, is an open source
computer vision and machine learning library consisting of more than 2,500 optimized
algorithms for working with images and videos. TensorFlow is an open source
framework for high-performance numerical computation and large-scale machine
learning. It is written in C++ and provides native support for both CPUs and GPUs.
Python is the most widely used programming language for developing machine learning
applications. Both TensorFlow and OpenCV provide Python interfaces to access their
low-level functionality. While both TensorFlow and OpenCV also offer interfaces for
other programming languages like Java, C++, and MATLAB, we will focus on the use of
Python in this book. Python’s vast community support and intuitive syntax make it an
accessible language for learners and practitioners alike.
The prerequisites for this book are practical knowledge of Python and familiarity
with NumPy and pandas. The book assumes that you are familiar with built-in data
containers in Python, such as dictionaries, lists, sets, and tuples. If you need to brush up
on these prerequisites, the following resources can be helpful:

• Python: https://www.w3schools.com/python/

• pandas: https://pandas.pydata.org/docs/getting_started/
index.html

• NumPy: https://numpy.org/devdocs/user/quickstart.html

1
© Shamshad Ansari 2023
S. Ansari, Building Computer Vision Applications Using Artificial Neural Networks,
https://doi.org/10.1007/978-1-4842-9866-4_1
Chapter 1 Prerequisites and Software Installation

The first order of business is to prepare your working environment for the exercises
presented throughout this book. You will begin by downloading and installing the
necessary software libraries and packages.

Python and PIP


As mentioned, Python is the primary programming language that is used in this book.
For the installation and management of Python packages, we’ll rely on PIP, which is a
widely accepted package installer and a standard tool in the Python ecosystem.
To set up your working environment, you will begin by installing Python and PIP
on your computer. The installation steps depend on the operating system (OS) you are
using. The following sections provide, in order, the installation steps for Ubuntu, macOS,
Red Hat Enterprise Linux, and Windows. Make sure you follow the instructions for
your OS. If you already have Python and PIP installed, ensure that you are using Python
version 3.8 or greater and PIP version 19 or greater. To check the version number of
Python, execute the following command on your terminal:

$ python3 --version

The output of this command should be something like this: Python 3.8.10.
To check the version number of PIP, execute the following command on your
terminal:

$ pip3 --version

This command should show a version number of PIP 3, for example, PIP 20.0.2.

Installing Python and PIP on Ubuntu


Run the following commands in your Ubuntu terminal:

sudo apt update


sudo apt install python3 python3-dev python3-pip

2
Chapter 1 Prerequisites and Software Installation

Installing Python and PIP on macOS


Run the following commands on macOS:

brew update
brew install python

This will install both Python and PIP.

Installing Python and PIP on Red Hat Linux


Run the following commands on Red Hat Linux:

sudo yum update


sudo yum install python3
sudo yum groupinstall 'Development Tools'

Installing Python and PIP on Windows


Install the Microsoft Visual C++ 2017 Redistributable Update 3. This comes with Visual
Studio 2017 but can be installed separately by following these steps:

1. Go to the Visual Studio downloads at https://visualstudio.


microsoft.com/vs/older-downloads/.

2. Select Other Tools, Frameworks, and Redistributables.

3. Download and install the Microsoft Visual C++ 2017


Redistributable Update 3.

Make sure long paths are enabled on Windows. Here are the instructions to
do that: https://www.thewindowsclub.com/how-toenable-or-disable-win
32-long-paths-in-windows-11-10.
Install the 64-bit Python 3 release for Windows from https://www.python.org/
downloads/windows/ (select PIP as an optional feature).
If these installation instructions do not work in your situation, refer to the official
Python documentation at https://www.python.org/.

3
Chapter 1 Prerequisites and Software Installation

virtualenv
virtualenv is a tool designed to create isolated Python environments. When you use
virtualenv, it generates a directory that includes all the necessary executables for utilizing
the packages required by a Python project. virtualenv provides the following advantages:

• It enables the coexistence of two versions of the same library,


allowing two programs running different versions of the library to
function properly. For example, if you have one program that relies
on version 1 of a Python library and another program that relies
on version 2 of the same library, virtualenv allows you to run both
programs simultaneously.

• It establishes a self-contained and independent environment for your


development work, which can be utilized in a production setting
without the need to install dependencies separately.

Next, we will install virtualenv and set up the virtual environment with all the
necessary software. The rest of the book assumes that the dependencies for our
reference program will be contained in this virtualenv.

Installing and Activating virtualenv


To install virtualenv system-wide, execute the following PIP command, which works the
same for all operating systems:

$ sudo pip3 install -U virtualenv

After the installation is complete, create a directory of your choice where you intend
to set up the virtualenv. For purposes of following the examples in this book, name this
directory cv (short for “computer vision”):

$ mkdir cv

Then create the virtualenv in this cv directory:

$ virtualenv --system-site-packages -p python3 ./cv

4
Chapter 1 Prerequisites and Software Installation

The following is a sample output from running this command (on my MacBook):

Running virtualenv with interpreter /anaconda3/bin/python3


Already using interpreter /anaconda3/bin/python3
Using base prefix '/anaconda3'
New python executable in /Users/sansari/cv/bin/python3
Also creating executable in /Users/sansari/cv/bin/python
Installing setuptools, pip, wheel...
done.

Activate the virtual environment using a shell-specific command:

$ source ./cv/bin/activate  # for sh, bash, ksh, or zsh

When virtualenv is active, your shell prompt is prefixed with (cv). Here’s an example:

(cv) Shamshads-MacBook-Air:~ sansari$

Installing packages within a virtual environment does not affect the host system
setup. Start by upgrading PIP as follows. Please note that you should not execute any
command as root or sudo while inside the virtual environment.

$ pip install --upgrade pip


$ pip list  # show packages installed within the virtual environment

When you have finished your programming activities and you want to exit from
virtualenv, run the following:

$ deactivate

TensorFlow
TensorFlow is a widely used open source library for numerical computation and
large-scale machine learning. In the upcoming chapters, you will delve deeper into
TensorFlow and explore its capabilities. However, before we proceed, let’s begin by
installing TensorFlow and preparing it for our deep learning exercises. If you have a
GPU in your computer and you want to utilize it for deep learning tasks, you typically
don’t need to install both CPU and GPU versions of TensorFlow. You can install the GPU
version, which includes support for both GPU and CPU computation. TensorFlow with
GPU support automatically falls back to CPU execution when GPU resources are not

5
Chapter 1 Prerequisites and Software Installation

available or when the operations are not suitable for GPU acceleration. So, installing
TensorFlow with GPU support (tensorflow-gpu) should be sufficient. It will work on both
GPU and CPU, making it a versatile choice for most deep learning tasks.

Installing TensorFlow on Mac with M1 Chip


The steps for installing TensorFlow on an M1 chip are as follows:

1. Download the Conda environment from the following link:


https://repo.anaconda.com/miniconda/Miniconda3-latest-
MacOSX-arm64.sh.

2. Assuming that the downloaded file Miniconda3-latest-MacOSX-


arm64.sh is in the Downloads folder, execute the following
command to install Miniconda3:

$ sh Downloads/Miniconda3-latest-MacOSX-arm64.sh

3. Follow the onscreen instructions to complete the installation of


Miniconda3 on your computer. If you proceed with the default
options, Miniconda3 will be installed in your home directory.
For example, if your home directory is /Users/username, the
path for Miniconda3 after installation will be /Users/username/
miniconda3.

4. Once the installation is finished, restart the terminal.

5. Create a virtual environment using Miniconda3 by running the


following command:

$ conda create --name cv python=3.8

6. To activate or deactivate this virtual environment, use the


following commands:

$ conda activate cv
$ conda deactivate
7. Activate the virtual environment by running the command conda
activate cv, and then install TensorFlow by executing the
following commands:

6
Chapter 1 Prerequisites and Software Installation

$ conda install -c apple tensorflow-deps


$ pip install tensorflow-macos
$ pip install tensorflow-metal
$ conda install tensorflow

8. Test the TensorFlow installation by running the command:

$ python -c "import tensorflow as tf"

9. If this command executes without errors, it means that


TensorFlow has been successfully installed on your M1 chip.

10. If you encounter any errors, resolve incompatibility issues by, for
example, upgrading the Mac OS.

Installing TensorFlow for CPUs


To install the most recent version of TensorFlow from PyPI (https://pypi.org/
project/tensorflow/), specifically for CPUs, ensure that you are within the virtual
environment and execute the following command:

(cv) $ pip install --upgrade tensorflow

Test your TensorFlow installation by running this command:

(cv) $ python -c "import tensorflow as tf"

If TensorFlow is successfully installed, the output should not show any errors.

Installing TensorFlow for GPUs


To install TensorFlow for GPUs, you need to follow a slightly different installation
process. Here are the steps:

1. Ensure that you have the appropriate NVIDIA GPU and drivers
installed on your system. You can check the TensorFlow
documentation for the specific GPU requirements and compatible
driver versions.

2. Activate the virtual environment that we created before.

7
Chapter 1 Prerequisites and Software Installation

3. Install the necessary GPU drivers and dependencies. Refer to the


TensorFlow documentation for the specific instructions based on
your operating system.

4. Install the GPU version of TensorFlow using the following command:

(cv) $ pip install --upgrade tensorflow-gpu

This command installs the latest version of TensorFlow that


supports GPU acceleration.

5. After the installation, you can test your TensorFlow-GPU setup by


running the following command:

(cv) $ python -c "import tensorflow as tf; tf.config.


list_physical_devices('GPU')"

If TensorFlow-GPU is installed correctly and GPU support is enabled, it will display


information about the available GPUs on your system.
Please note that installing TensorFlow for GPUs requires additional setup and can be
more complex compared to the CPU version. It is recommended to refer to the official
TensorFlow documentation for detailed instructions and troubleshooting guidance
specific to your system configuration.

PyCharm IDE
For consistency, the integrated development environment (IDE) used throughout this
book for writing and managing Python code is the community version of PyCharm, a
popular Python IDE. You can choose to use your preferred IDE, but if you do so, you’ll
need to adapt relevant examples and exercises to your IDE.

Installing PyCharm
To acquire PyCharm from its official website, follow these steps:
1. Visit the PyCharm website at https://www.jetbrains.com/
pycharm/download/.

2. Select the appropriate operating system that corresponds to your


machine.
8
Chapter 1 Prerequisites and Software Installation

3. Scroll down to the PyCharm Community Edition section and click


the Download button.

4. Once the download is complete, locate the downloaded package


and execute it.

5. Follow the instructions provided onscreen to complete the


installation.

Here are the direct download links for different operating systems:

• Linux: https://www.jetbrains.com/pycharm/download/download-
thanks.html?platform=linux&code=PCC

• Mac: https://www.jetbrains.com/pycharm/download/download-
thanks.html?platform=mac&code=PCC

• Windows: https://www.jetbrains.com/pycharm/download/
download-thanks.html?platform=windows&code=PCC

Please note that these links may change over time, so it’s recommended to visit the
official PyCharm website to ensure you access the most up-to-date download links.

Configuring PyCharm to Use virtualenv


To use the virtualenv named cv that was created earlier, follow these steps in the
PyCharm IDE:

1. Launch PyCharm and open your project. Then choose File ➤


Settings on Windows and Linux or choose PyCharm ➤
Preferences on macOS.

2. In the Settings/Preferences dialog, navigate to Project


<project name> ➤ Project Interpreter.

3. Click the icon (usually represented by a gear or a plus sign) and


select Add from the drop-down menu.

4. In the left pane of the Add Python Interpreter dialog, select


Existing Environment.

9
Chapter 1 Prerequisites and Software Installation

5. Expand the Interpreter list and choose one of the existing


interpreters displayed, or click the icon to specify the path to
the Python executable in your file system, for example, /Users/
ansarisam/cv/bin/python3.8 (see Figure 1-1). This path should
correspond to the virtual environment cv that you created earlier.

Optionally, you can select the checkbox “Make available to all projects” if you want
this interpreter to be accessible for all your projects in PyCharm.
By following these steps, you will configure PyCharm to use the virtual environment
cv as the Python interpreter for your project.

Figure 1-1. Selecting an interpreter

OpenCV
OpenCV is one of the most popular and widely used libraries for image processing. All
code examples in this book are based on OpenCV 4. Therefore, the installation steps
presented in this section are for version 4 of OpenCV.
10
Chapter 1 Prerequisites and Software Installation

Working with OpenCV


OpenCV is primarily written in C/C++, and its installation instructions vary depending
on which operating system you are using. Since OpenCV is platform-dependent, it
needs to be built specifically for your OS to ensure smooth operation. In this book,
we will utilize Python bindings to interface with OpenCV for any image processing
requirements.
OpenCV is continuously evolving, so if the following installation instructions do not
work for your specific case, refer to the official OpenCV website for the most up-to-date
and accurate installation process.
To simplify the installation process, we will use PIP to install OpenCV 4 and its
Python 3 bindings. If you need additional OpenCV modules and features, you can install
the opencv-contrib-python package.
Now, let’s proceed with the installation process!

Installing OpenCV 4 with Python Bindings


Make sure you are in your virtual environment. Simply change directory to your
virtualenv directory (the cv directory) and type the following command:

$ source cv/bin/activate

Install OpenCV in a snap using the following command:

$ pip install opencv-python

To install the opencv-contrib-python package, which includes extra modules and


features that are not part of the main OpenCV package, use the following command:

$ pip install opencv-contrib-python

Once the installation is complete, you can verify it by importing OpenCV in your
Python command:

$ python -c "import cv2"

If there are no errors, it indicates that OpenCV has been successfully installed.

11
Chapter 1 Prerequisites and Software Installation

Note Package names for OpenCV may vary slightly based on your specific
operating system or Python version. If you encounter any issues during the
installation, refer to the official OpenCV documentation or the PyPI page for the
most accurate and up-to-date installation instructions.

Additional Libraries
SciPy and Matplotlib are two additional libraries that we will need as we work on some
of the examples. Let’s install and keep them in our virtualenv.

Installing SciPy
Install SciPy with the following:

$ pip install scipy

Installing Matplotlib
Install Matplotlib with the following:

$ pip install matplotlib

Note The libraries installed in this chapter are frequently updated. It is strongly


advised to check the official websites for updates, new versions of these libraries,
and the latest installation instructions.

Summary
The following table summarizes the installation commands for the required libraries
within your virtualenv. You can copy and paste these commands into your terminal to
install the libraries.

12
Chapter 1 Prerequisites and Software Installation

Table 1-1. Installation Commands for Required


Libraries in virtual environment
Library Command

TensorFlow pip install tensorflow


TensorFlow GPU pip install tensorflow-gpu
OpenCV pip install opencv-python
OpenCV Contrib pip install opencv-contrib-­python
SciPy pip install scipy
NumPy pip install numpy
Matplotlib pip install matplotlib

Ensure that your virtualenv is activated before running these commands. Once
you’ve installed these libraries, you’ll have all the necessary dependencies within your
virtualenv to work with the examples in the book.

13
CHAPTER 2

Core Concepts of Image


and Video Processing
This chapter introduces the building blocks of an image and describes various methods
to manipulate them. Our learning objectives in this chapter are as follows:

• To understand the smallest unit of an image (a pixel) and how colors


are represented

• To learn how pixels are organized in an image and how to access and
manipulate them

• To write code in Python and use OpenCV to work with examples to


access and manipulate images

• To draw different shapes, such as lines, rectangles, and circles, on


an image

Image Processing
Image processing is a method used to manipulate a digital image to obtain an improved
image or extract valuable information from it. It involves taking an image as input
and producing as output either another image or specific characteristics and features
associated with that image. Videos, being composed of a series of images or frames, can
also be processed using image processing techniques. In this chapter, we will delve into
the fundamental concepts of digital image processing. Additionally, you will acquire the
basic skills to work with images and write code to manipulate them.

15
© Shamshad Ansari 2023
S. Ansari, Building Computer Vision Applications Using Artificial Neural Networks,
https://doi.org/10.1007/978-1-4842-9866-4_2
Chapter 2 Core Concepts of Image and Video Processing

Image Basics
A digital image is an electronic representation of an object/scene or scanned document.
The digitalization of an image means converting it into a series of numbers and storing
these numbers in a computer storage system. Understanding how these numbers are
arranged and how to manipulate them is the primary objective of this chapter. This
chapter explains the fundamental components that make up an image and guides you
through the process of manipulating images using OpenCV and Python.

Pixels
An image can be visualized as a collection of dots arranged in rows and columns, where
each dot represents a pixel with a specific color. These pixels are assigned numerical
values, which determine their respective colors. You can imagine an image as a grid
of square cells, where each cell corresponds to a single pixel with a specific color. For
instance, if we have a 300×400-pixel image, it implies that the image is structured as a
grid with 300 rows and 400 columns, resulting in a total of 120,000 pixels.

Pixel Color
A pixel can be represented in two different ways: grayscale and color.

Grayscale
In a grayscale image, each pixel takes a value between 0 and 255. The value 0 represents
black, and 255 represents white. The values in between are varying shades of gray. The
values close to 0 are darker shades of gray, and values closer to 255 are brighter shades
of gray.

Color
The RGB (which stands for Red, Blue, and Green) color model is one of the most popular
color representations of a pixel. Other color models are available, but this book primarily
focuses on the RGB color model.

16
Chapter 2 Core Concepts of Image and Video Processing

In the RGB model, each pixel is represented as a tuple of three values, generally
represented as follows: (value for red component, value for green component, value for
blue component). Each of the three colors is represented by integers ranging from 0 to
255. Here are some examples:

(0,0,0) is a black color.

(255,0,0) is a pure red color.

(0,255,0) is a pure green color.

The W3Schools website is a great place to play with different combinations of RGB
tuples to explore more patterns (https://www.w3schools.com/colors/colors_rgb.asp).
Use the RGB Calculator to explore what color is represented by each of the following tuples:

(0,0,255)

(255,255,255)

(0,0,128)

(128,0,128)

(128,128,0)

Now try to make the color yellow. Here is a clue: red and green make yellow. That
means a pure red (255), a pure green (255), and no blue (0) will make yellow. So, the RGB
tuple for yellow is (255,255,0).
Now that you have a good understanding of pixels and their color, let’s explore how
pixels are arranged in an image and how to access them. The following section discusses
the concept of coordinate systems in image processing.

Coordinate Systems
As previously mentioned, pixels in an image are arranged in the form of a grid that is
made of rows and columns. Imagine a square grid of eight rows and eight columns. This
forms an 8×8 or 64-pixel image. Think of it as a 2D coordinate system in which the origin
(0,0) is the top-left corner. Figure 2-1 shows our example 8×8-pixel image.

17
Chapter 2 Core Concepts of Image and Video Processing

Figure 2-1. Pixel coordinate system

With the top-left corner as the start or origin of the image coordinate system, the
pixel at the top-right corner is represented by (0,7), the bottom-left pixel is (7,0), and the
bottom-right pixel is (7,7). This may be generalized as (x,y), where x is the position of the
cell from the left edge of the image and y is the vertical position down from the top edge
of the image. In Figure 2-1, the red pixel is in the fifth position from the left and fourth
from the top. Since the coordinate system begins at 0, the coordinate of the red pixel
shown in Figure 2-1 is (4,3).
To make this concept a little clearer, consider Figure 2-2, an image that is 8×8 pixels,
with the letter H written on it. Also, for simplicity, assume this is a grayscale image, with
the letter H written in black and the rest of the area of the image in white.

Figure 2-2. Pixel coordinate system example

Remember, in the grayscale model, a black pixel is represented by 0 and a white


pixel is represented by 255. Figure 2-3 shows the values of each of the pixels within the
8×8 grid.

18
Chapter 2 Core Concepts of Image and Video Processing

Figure 2-3. Pixel matrix and values

So, what’s the value of the pixel at position (1,4)? And at position (2,2)?
Now, you should have a clear understanding of how images are represented as
numbers arranged in a grid. These numerical values are serialized and stored in a
computer’s storage system. When the image is displayed on the screen, these numbers
are interpreted and rendered as an actual image.
At this point, you have learned how to access individual pixels using the coordinate
system, which allows you to pinpoint specific locations within the image grid.
Additionally, you have learned how to assign colors to these pixels by manipulating their
numerical values. By grasping these concepts, you now understand how images are
structured and how to interact with them programmatically.
With a solid foundation and understanding of image representation, you are now
ready to gain practical experience through Python and OpenCV coding. In the following
section, I will guide you step-by-step through the process of writing code that loads
images from the computer’s disk, accesses individual pixels, performs manipulations,
and saves the modified images back to the disk.
Let’s delve into the exciting world of image processing and begin our hands-on journey!

19
Chapter 2 Core Concepts of Image and Video Processing

 sing Python and OpenCV Code to


U
Manipulate Images
In OpenCV, the pixel values of an image are represented using a NumPy array. (Not
familiar with NumPy? You can find a “getting started” tutorial at https://numpy.org/
devdocs/user/quickstart.html.) This NumPy array serves as a multidimensional
representation of the image grid and its color channels. It allows us to efficiently store,
access, and manipulate the pixel values of the image. By leveraging the capabilities
of NumPy arrays, OpenCV provides a powerful framework for image processing and
analysis tasks. In other words, when you read an image, OpenCV creates a NumPy array.
The pixel values can be obtained from NumPy by simply supplying the (x,y) coordinates.
When you give the (x,y) coordinates, NumPy returns the values of colors of the pixel
at those coordinates as follows:

• For a grayscale image, the returned value from NumPy will be a single
value between 0 and 255.

• For a color image, the returned value from NumPy will be a tuple for
red, green, and blue. Note that OpenCV maintains the RGB sequence
in the reverse order. In other words, OpenCV stores the colors in BGR
sequence, not in RGB sequence. Remember this important feature of
OpenCV to avoid any confusion while working with OpenCV.

Before we write any code, let’s make sure we always use our virtual environment, in
the ~/cv directory, that we already set up with PyCharm in Chapter 1.
Launch your PyCharm IDE and make a new project (I named my project cviz, short
for “computer vision”). Refer to Figure 2-4 and ensure that you have selected Existing
Interpreter and have our virtualenv Python 3.8 (cv) selected.

20
Chapter 2 Core Concepts of Image and Video Processing

Figure 2-4. PyCharm IDE, showing the setup of the project with virtualenv

Program: Loading, Exploring, and Showing an Image


In this section, we will write code to read an image from a file and explore its properties.
We will utilize the OpenCV library, which provides powerful tools for image processing
and analysis. By loading the image into our program, we will be able to access
information such as its dimensions, size, and channel information. Additionally, we
will display the image on the screen and wait for a keypress to continue execution. This
exploration will give us a better understanding of the image and set the stage for further
image processing tasks. Let’s dive into the code and start exploring the image!
Listing 2-1 shows the Python code to load, explore, and display an image.

Listing 2-1. Python Code to Load, Explore, and Display an Image

Filename: Listing_2_1.py
1    import cv2
2
3    # image path
4    image_path = "images/marsrover.png"
5

21
Random documents with unrelated
content Scribd suggests to you:
without fear or misgiving, it went to the judgment of Him who is
preëminently the God of battles.
In the wars of this period a common sight was that of bishops and
archbishops, clad in coats of mail, riding through the streets of their
episcopal towns on fierce chargers, and returning to their palaces
clotted with dirt and blood. That was a deserved rebuke, as well as a
fine sarcasm, with which Richard Cœur de Lion sent the blood-
stained armor of the Bishop of Beauvais to the Pope, as the garment
of Joseph to Jacob, asking the Holy Father if he recognized his son’s
coat.
Even women on occasion put on armor and mingled in the mêlée.
Gaita, the wife of Robert Guiscard, fought in the front rank of the
Normans in their conflict with the Greeks. When the crusades were
in progress many a fair woman adopted the martial costume. The
Amazonian Brunhilde is scarcely overdrawn by Scott in “Count
Robert of Paris,” and the Moslem heroines of Tasso’s “Jerusalem
Delivered,” stripped of their supernatural resources, might have
figured in the Christian camp.
Walter Scott put into the mouth of the Greek Nicephorus a pertinent
description of his fellow-Christians of the West: “To whom the strife
of combat is as the breath of their nostrils, who, rather than not be
engaged in war, will do battle with their nearest neighbors and
challenge each other to mortal fight, as much in sport as we would
defy a comrade to a chariot-race.”
It is but just to say that, if the Greeks were amazed at the warlike
propensities of the Catholics, they expressed no wonder at their
cruelty. In this they themselves even excelled their more robust
rivals. The dungeons of Constantinople were filled with political
offenders whose eyes were torn from their sockets; and more than
one imperial candidate resumed his place of honor among a people
whose waving banners he was unable to see. The Greek differed
from the Frank and German, the Norman and Saxon, chiefly in being
a coward and choosing to glut his brutal instincts with the use of the
secret torture, the poisoned cup, or the dagger in the back of his
victim, rather than with the sword and battle-axe in open fight.
To a people such as we have described the appeal for the crusades,
in which the imagined cause of heaven marched in step with their
own tastes and habits, was irresistible.
CHAPTER III.
CHIVALRY—RULES—EDUCATION OF KNIGHT—
CEREMONIES—INFLUENCE ON CHARACTER.

The call for the crusades, while appealing powerfully to the warlike
disposition of the people, would not have succeeded in rousing
Europe had there not been in the popular heart at least the germs of
nobler sentiment. The vitality of conscience notwithstanding its
degradation, and an inclination towards the exercise of the finer
graces of conduct in spite of the prevalent grossness, manifested
themselves in the rise of Chivalry.
The picturesqueness of knight-errantry, and the glamour thrown over
the subject by poetry and romance, may mislead us as to the real
character of this institution. We must distinguish between the ideals
of knighthood and the actual lives of those who, from various
motives, thronged the profession. We must not confound the
Chivalry of these earlier and ruder ages with that of its more refined,
though somewhat effeminate, later days. It would be an equal
mistake to pose the half-savage Saxon for a picture of the gallant
Provençal, because they were fellows of the same order. But,
making all allowance for variations, defects, and perversions in
Chivalry, the institution went far towards redeeming the character of
the middle ages. Among the articles of the chivalric code were the
following:
To fight for the faith of Christ. In illustration of this part of his vow, the
knight always stood with bared head and unsheathed sword during
the reading of the lesson from the gospels in the church service.
To serve faithfully prince and fatherland.
To defend the weak, especially widow’s, orphans, and damsels.
To do nothing for greed, but everything for glory.
To keep one’s word, even returning to prison or death if, having been
captured in fair fight, one had promised to do so.
Together with these vows of real virtue were others, which signified
more for the carnal pride of the warrior, e.g.:
Never to fight in companies against one opponent.
To wear but one sword, unless the enemy displayed more than one.
Not to put off armor while upon an adventure, except for a night’s
rest.
Never to turn out of a straight road in order to avoid danger from
man, beast, or monster.
Never to decline a challenge to equal combat, unless compelled to
do so by wounds, sickness, or other equally reasonable hindrance.
The aspirant for knighthood began his career in early boyhood by
attending some superior as his page. Lads of noblest families sought
to be attached to the persons of those renowned in the order, though
not to their own fathers, lest their discipline should be over-indulgent.
Frequently knights of special note for valor and skill at arms opened
schools for the training of youth. The page was expected to wait
upon his lord as a body-servant in the bedchamber, the dining-hall,
and, when consistent with his tender years, upon the journey and in
the camp. It was a maxim of the code that one “should learn to obey
before attempting to govern.”
With the development of manly strength, at about his fourteenth year
the page became an esquire. He then burnished and repaired the
armor of his chief, broke his steeds, led his charger, and carried his
shield to the field of battle. In the mêlée he fought by his master’s
side, nursed him when wounded, and valued his own life as naught
when weighed against his lord’s safety or honor.
The faithful esquire was adubbed a knight at the will and by the hand
of his superior. This honor was sometimes awarded on the field of
conflict for a specially valiant deed. More commonly the heroic
subalterns were summoned to receive the coveted prize when the
fight was done. More than one instance is mentioned where the
esquire bowed his head beneath the dead hand of his master and
there assumed the duty of completing the enterprise in which his
chief had fallen. Ordinarily, however, the ceremony was held in the
castle hall, or in later times in the church, on the occasion of some
festival or upon the candidate’s reaching the year of his majority.
The rite of admission to knighthood was made as impressive as
possible. The young man, having come from the bath, was clothed in
a white tunic, expressive of the purity of his purpose; then in a red
robe, symbolical of the blood he was ready to shed; and in a black
coat, to remind him of the death that might speedily be his portion.
After fasting, the candidate spent the night in prayer. In the morning
the priest administered to him the holy communion, and blessed the
sword which hung from his neck. Attendant knights and ladies then
clothed him in his armor. Kneeling at the feet of the lord, he received
from him the accolade, three blows with the flat of the sword upon
his shoulder, with the repetition of the formula, “In the name of God,
St. Michael, and St. George, I make thee a knight.”
More impressive, because more unusual, was the ceremony of his
degradation, if he broke his plighted faith or forfeited his honor. He
was exposed on a platform, stripped of his armor, which was broken
to pieces and thrown upon a dunghill. His shield was dragged in the
dirt by a cart-horse, his own charger’s tail was cut off, while he was
himself carried into a church on a litter, and forced to listen to the
burial service, since he was now to move among men as one who
was dead to the honor for which he had vowed to live.
The chief defect of Chivalry was that, while it displayed some of the
finer sentiments of the soul in contrast with the general grossness of
the age, it did not aspire to the highest motives as these were felt in
the early days of Christianity and as they are again apprehended in
modern times. Notwithstanding the vow of devotion, there was little
that was altruistic about it. The thought of the devotee was ultimately
upon himself, his renown and glory. His crested helmet, his gilded
spurs, his horse in housing of gold, and the scarlet silk which marked
him as apart from and above his fellows, were not promotive of that
humility and self-forgetfulness from which all great moral actions
spring. Our modern characterization of the proud man is borrowed
from the knight’s leaving his palfrey and mounting his charger, or, as
it was called, getting “on his high horse.” In battle the personality of
the knight was not, as in the case of the modern soldier, merged in
the autonomy of the brigade or squadron; he appeared singly
against a selected antagonist of equal rank with his own, so that the
field presented the appearance of a multitude of private combats. In
the lull of regular warfare he sought solitary adventures for gaining
renown, and often challenged his companions in arms to contest
with him the palm of greater glory. Writers aptly liken the mediæval
knights to the heroic chiefs of Arabia, and even of the American
Indians, to whom personal prowess is more than patriotism. Hallam
would choose as the finest representative of the chivalric spirit the
Greek Achilles, who could fight valiantly, or sulk in his tent regardless
of the cause, when his individual honor or right seemed to be
menaced.
The association of Chivalry with gallantry, though prompted by the
benevolent motive of helping the weak or paying homage to woman
as the embodiment of the pure and beautiful, did not always serve
these high purposes. The “love of God and the ladies,” enjoined as a
single duty, was often to the detriment of the religious part of the
obligation. The fair one who was championed in the tournament was
apt to be sought beyond the lists. The poetry of the Troubadours
shows how the purest and most delicate sentiment next to the
religious, the love of man for woman, became debauched by a
custom which flaunted amid the brutal scenes of the combat the
name of her whose glory is her modesty, and often made her virtue
the prize of the ring.
Doubtless the good knight felt that the altar of his consecration was
not high enough. Even his vow to defend the faith had, within the
bounds of Christendom, little field where it could be honored by
exploit of arms. To take his part in the miserable quarrels that were
chronic between rival popes, or in the wars of the imperial against
the prelatic powers, both professedly Christian, could not satisfy any
really religious desires he may have felt. The chivalric spirit thus
kindled the aspiration for an ideal which it could not furnish. If the
soldier of the cross must wear armor, he would find no satisfaction
unless he sheathed his sword in the flesh of the Infidels, whose
hordes were gathering beyond the borders of Christendom. The
institution of Chivalry thus prepared the way for the crusades, which
afforded a field for all its physical heroism, while at the same time
these great movements stimulated and gratified what to this
superstitious age was the deepest religious impulse.
CHAPTER IV.
THE FEUDAL SYSTEM—GENERAL PRINCIPLES—
INFLUENCE ON PEOPLE.

In accounting for the crusades we must consider the governmental


condition of Europe at the time. Under no other system than that of
feudalism would it have been possible to unify and mobilize the
masses for the great adventure. Had Europe then been dominated
by several great rulers, each with a nation at his control, as the case
has been in subsequent times, even the popes would have been
unable to combine the various forces in any enterprise that was not
purely spiritual. Just to the extent in which the separate nationalities
have developed their autonomy has the secular influence of the
Roman see been lessened. Kings and emperors, whenever they
have felt themselves strong enough to do so, have resented the
leadership of Rome in matters having temporal bearings.
Nor would the mutual jealousies of the rulers themselves have
allowed them to unite in any movement for the common glory, since
the most urgent calls have never been sufficient to unite them even
for the common defence, as is shown by the supineness of Catholic
Europe when, in the fifteenth century, the Turks crossed the
Marmora and assailed Constantinople.
But in the eleventh century there was no strong national government
in Europe; kingship and imperialism existed rather in name than in
such power as we are accustomed to associate with the words. At
the opening of the tenth century France was parcelled out into
twenty-nine petty states, each controlled by its feudal lord. Hugh
Capet (987-996) succeeded in temporarily combining under his
sceptre these fragments of Charlemagne’s estate; but his
successors were unable to perpetuate the common dominion. In the
year 1000 there were fifty-five great Frankish lords who were
independent of the nominal sovereign. Indeed, some of these nobles
exercised authority more weighty than that of the throne. Louis VI.
(1108) first succeeded in making his lordly vassals respect his
kingship, but his domain was small. “Île de France, properly so
called, and a part of Orléannais, pretty nearly the five departments of
the Seine, French Vexin, half the countship of Sens, and the
countship of Bourges—such was the whole of it. But this limited state
was as liable to agitation, and often as troublous and toilsome to
govern, as the very greatest of modern states. It was full of petty
lords, almost sovereign in their own estates, and sufficiently strong to
struggle against their kingly suzerain, who had, besides, all around
his domains several neighbors more powerful than himself in the
extent and population of their states” (Guizot).
In Spain much of the land was still held by the Moors. That which
had been wrested from them was divided among the Christian
heroes who conquered it, and who, though feudal rules were not
formally recognized, held it with an aristocratic pretension
commensurate with the leagues they shadowed with their swords.
In Germany, though imperialism had been established firmly by Otho
the Great, the throne was forced to continual compromise with the
ambition of its chief vassals, like the dukes of Saxony, Bavaria,
Swabia, and Franconia. A papal appeal to such magnates was
sufficient at any time to paralyze, or at least to neutralize, the
imperial authority.
The Norman holdings in the south of Italy, the independence of the
cities of Lombardy in the north, the claims of the German emperor
and of the popes to landed control, were typical of the divisions of
that unhappy peninsula.
Later than the age we are studying, Frederick Barbarossa (1152-90)
enjoined that “in every oath of fealty to an inferior lord the vassal’s
duty to the emperor should be expressly reserved.” But it was not so
elsewhere. When Henry II. (1154-89) and Richard I. (1189-99)
claimed lands in France, their French vassals never hesitated to
adhere to these English lords, nor “do they appear to have incurred
any blame on that account. St. Louis (1226-70) declared in his laws
that if ‘justice be refused by the king to one of his vassals, the vassal
may summon his own tenants, under penalty of forfeiting their fiefs,
to assist him in obtaining redress by arms’” (Hallam).
The extent to which the French barons were independent of the
throne will be evident from a glance at their privileges. They
possessed unchallenged:
(1) The right of coining money. In Hugh Capet’s time there were one
hundred and fifty independent mints in the realm.
(2) The right of waging private war. Every castle was a fortress,
always equipped as in a state of siege.
(3) Immunity from taxation. Except that the king was provided with
entertainment on his journeys, the crown had no revenue beyond
that coming from the personal estates of its occupant.
(4) Freedom from all legislative control. Law-making ceased with the
capitularies of Carloman in 882. The first renewal of the attempt at
general legislation was not until the time of Louis VIII. in 1223. Even
St. Louis declared in his establishments that the king could make no
laws for the territories of the barons without their consent.
(5) Exclusive right of original judicature.
But if such was the independence of the feud-holder in his relations
to the sovereign, those beneath him were in absolute dependence
upon their lord. This is seen in the following obligations of feudal
tenants to their superior:
(1) Reliefs: sums of money due from every one coming of age and
taking a fief by inheritance; fines upon alienation or change of tenant
ownership.
(2) Escheats: reversion to the lord of all property upon a tenant’s
dying without natural heirs, or upon any delinquency of service.
(3) Aids: contributions levied in special emergency, as the lord’s
expedition to the Holy Land, the marriage of his sister, eldest son, or
daughter, his paying a “relief” to his overlord, making his son a
knight, or redeeming his own person from captivity.
(4) Wardship of tenant during minority. This involved on the part of
the lord the right to select a husband for a female dependent, which
alliance could be declined only on payment of a fine equal to that
which any one desiring the woman could be induced to offer for her.
If the feudal system pressed so harshly upon those who were
themselves of high rank, it need not be said that the common people
were utterly crushed by this accumulation of graded despotisms,
whose whole weight rested ultimately on the lowest stratum. The
mass of the lowly was divided into three orders:
(1) Freemen possessing small tracts of allodial land, so called
because held by original occupancy and not yet merged in the larger
holdings. There were many freemen in the fifth and sixth centuries,
but in the tenth century nearly all the land of Europe had become
feudal. The freemen, whose possessions were small, soon found it
necessary to surrender land and liberty for the sake of protection by
some neighboring lord.
(2) Villains or serfs, who were attached to the land and transferable
with it on change of owners.
(3) Slaves. The degradation of the servile class was limitless, the
master having the right of life and death, entire use of the property
and wages of his people, and absolute disposal of them in marriage.
Slavery was abolished in France by Louis the Gross (1108-37) so far
as respected the inhabitants of cities; but it took nearly two centuries
more to accomplish the abolition of servitude throughout the
kingdom.
The cities were, indeed, rising to assert their communal, if not
manhood, rights. The communes, as they were called, demanded
and received privilege in certain places of electing any persons to
membership as citizens who were guaranteed absolute ownership of
property. But the communes were far from even suggesting anything
like the modern democratic systems, and were opposed by clergy
and nobility. “So that,” says Guizot, “security could hardly be
purchased, save at the price of liberty. Liberty was then so stormy
and so fearful that people conceived, if not a disgust for it, at any
rate a horror of it.” Men had not evolved the morality which could
make a commonwealth. Law was bound on men only by force. The
wall of the castle, grand and impressive as wealth could build it, or
only a rude addition to the natural rock, was the sole earthly object of
reverence. To the strong man came the weak, saying, “Let me be
yours; protect me and I will fight for you.”
It will be evident that under the feudal system patriotism, in the
modern sense of attachment to one’s national domain, can scarcely
be said to have existed. While we may not believe recent French
writers who assert that the love of their country as such was born
with the Revolution a hundred years ago, it is certain that the
mediæval attachment was no wider than to one’s immediate
neighborhood. The crusading Count of Flanders, on viewing the
desolate hills about Jerusalem, exclaimed, “I am astonished that
Jesus Christ could have lived in such a desert. I prefer my big castle
in my district of Arras.” The love of the peasant seems to have been
only for his familiar hills and vineyards, and his loyalty was limited by
the protecting hand of his lord.
Yet generous spirits could not remain forever so narrowly bounded in
their interests. Men were ready to hear the call to a wider range of
sympathies and actions. The summons for the crusades thus
furnished the lacking sentiment of patriotism; but it was a patriotism
that could not be bounded by the Rhine or the Danube, by the
Channel or the Pyrenees. Europe was country; Christendom was
fatherland.
At the same time the compactness of each feud, the close
interdependence of lord and vassal, furnished the condition for the
organization of bands of fighting men, ready to move at once, and to
continue the enterprise so long as the means of the superior should
hold out. There was needed to start the crusading armies no council
of parliament or alliance of nations, hazarded and delayed by the
variant policies of different courts. If the baron was inclined to obey
the call of his ghostly superior, the successor of St. Peter, his
retainers were ready to march. And the most brawling of the barons
was superstitious enough to think that the voice of the Pope might be
the voice of God. If he did not, his retainers did, and disobedience to
the papal will might cost him the obedience of those subject to him.
Besides, many of the feudal lords were themselves in clerical orders,
with their oath of fealty lying at the feet of the Holy Father.
Thus Europe, though divided into many factions, and, indeed,
because the factions were so many, was in a condition to be readily
united. We shall see in a subsequent chapter that it was in the
interest of the holy see to apply the spring which should combine
and set in motion these various communities as but parts of that
gigantic piece of ecclesiastical and military mechanism invented by
Hildebrand.
CHAPTER V.
THE IMPOVERISHED CONDITION OF EUROPE.

The once luxuriant civilization of Rome had been swept away by the
Northern invaders as completely as a freshet despoils the fields
when it not only destroys standing vegetation, but carries with the
débris the soil itself. The most primitive arts, those associated with
agriculture, were forgotten, and the rudiments of modern industries
were not thought of. Much of the once cultivated land had, as has
elsewhere been noted, reverted to native forest and marsh, and in
places was still being purchased by strangers on titles secured by
occupancy and first improvement, as now in the new territories of
America. But even nature’s pity for man was outraged; the bounty
she gave from half-tilled acres was despoiled by men themselves, as
hungry children snatch the morsels of charity from one another’s
hands. What was hoarded for personal possession became the spoil
of petty robbers, and what was left by the neighborhood marauder
was destroyed in the incessant baronial strife. To these devouring
forces must be added the desolating wars between the papal and
imperial powers, the conquest and reconquest of Spain by Moors
and Christians, and the despoiling of Saxon England by the
Normans. Throughout Europe, fields, cottages, castles, oftentimes
churches, were stripped by the vandalism which had seemingly
become a racial disposition. To this ordinary impoverished condition
was added the especial misery, about 1195, of several years’ failure
of crops. Famine stalked through France and middle Europe; villages
were depopulated. Cruel as they were, men grew weary of raiding
one another’s possessions when there was nothing to bring back but
wounds. Even hatred palled when unsupported by envy and cupidity.
The crusades gave promise of opening a new world to greed. The
stories that were told of Eastern riches grew, as repeated from
tongue to tongue, until fable seemed poor in comparison with what
was believed to be fact. All the wealth of antiquity was presumed to
be still stored in treasure-vaults, which the magic key of the cross
would unlock. The impoverished baron might exchange his half-
ruined castle for some splendid estate beyond the Ægean, and the
vulgar crowd, if they did not find Jerusalem paved with gold like the
heavenly city, would assuredly tread the veins of rich mines or rest
among the flowers of an earthly paradise. The Mohammedan’s
expectation of a sensual heaven after death was matched by the
Christian’s anticipation of what awaited him while still in life.
They who were uninfluenced by this prospect may have seized the
more warrantable hope of opening profitable traffic with the Orient.
The maritime cities of Italy had for a long time harvested great gains
in the eastern Mediterranean, in spite of the Moslem interruptions of
commerce. Would not a tide of wealth pour westward if only the
swords of the Christians could hew down its barriers?
The church piously, but none the less shrewdly, stimulated the sense
of economy or greed by securing exemption from taxation to all who
should enlist, and putting a corresponding burden of excise upon
those who remained at home, whose estates were assessed to pay
the expenses of the absent. The householder who found it difficult to
save his possessions while keeping personal guard over them was
assured that all his family and effects would be under the watchful
protection of the church, with anathemas already forged against any
who should molest them. If one were without means he might borrow
to the limit of his zeal, with exemption from interest. It was
understood that the Jews were still under necessity of paying back
the thirty pieces of silver with which they had bought the Christians’
Lord, the interest on which, compounded through the centuries, was
now equal in amount to all there might be in the vaults of this
accursed race.
When we remember the wars of modern times which have originated
in the cupidity of men, we are not surprised that the same
disposition, inflamed by the sense of dire need at home and the
vision of untold treasures outre mer, with heavenly rewards beyond
the sky, should have led to the same result in an age that knew
almost nothing of the arts of peace.
CHAPTER VI.
THE PAPAL POLICY—DEMORALIZATION OF THE
WORLD AND THE CHURCH—HILDEBRAND’S
PURPOSE INHERITED BY HIS SUCCESSORS.

We shall fail to appreciate the inception of the crusades if we


overlook the influence of the papal policy in the middle ages. These
movements of Europe against Asia, being under the direct patronage
of the popes, facilitated the plans of Rome to consolidate and
universalize the ecclesiastical empire. To understand this policy we
must recall the condition of the church in its relation to popular life
and the secular powers.
We have referred to the fact that the year 1000 had been looked
forward to as that which should mark the end of the world. So
common was the expectation of this termination of human affairs that
many charters, which have been preserved from this period, begin
with the words, “As the world is now drawing to its close.” When,
however, the fatal day passed without any perceptible shock to the
universe, the popular credulity added the thirty-three years of the life
of our Lord to the calculation, and prolonged the gruesome
foreboding. But if the chronological interpretation of the prophecy of
the Book of Revelation was a mistaken one, there was not wanting
an apparent fulfilment of the descriptive prediction, “Satan shall be
loosed out of his prison.” The falsity and viciousness of men certainly
took on fiendish proportions.
The worst feature of the general demoralization was that the
millennial fear had driven all sorts of men into church orders. The
priesthood and monasteries were crowded with wretched characters,
whose imagined immunity in their sacred refuges gave license to
their carnal vices. The clergy were no longer the shepherds, but the
bell-wethers of the wayward flock. Priests lived in open concubinage.
When Hildebrand, previous to his elevation to the Papacy, took
charge of the monastery of St. Paul in Rome, his first work was to
drive out the cattle that were stabled in the basilica, and the
prostitutes who served the tables of the monks. Courtesans reigned
even in the palaces of the popes with more effrontery than in the
courts of the secular princes. The offspring of such creatures as the
infamous Theodora, and of her daughters Theodora and Marozia,
had, in the tenth century, purchased the tiara with their vices. In
those days the papal staff was wrenched by violence from the hands
that held it with more frequency than the old Roman sceptre had
been stolen in the worst days of the empire. It may well be credited
that men began to pray again to pagan deities in sheer despondency
under the darkness which veiled the Christian truth. The surviving
religious sentiment was voiced in the solemn utterance of the
Council of Rheims, which declared that the church was “ruled by
monsters of iniquity, wanting in all culture, whether sacred or
profane.”
If the tenth century closed with a gleam of hope in the elevation of
Gregory V. (996-999) and Sylvester II. (999-1003), it was quickly
remembered that the learning of the latter had been acquired among
the Saracens; and his biographer attributed his attainments to magic
and undue familiarity with the fiends in hell.
In the early part of the eleventh century the papal chair was filled
with the nominees of politicians, and from 1033 to 1045 disgraced by
Benedict IX., who at the age of twelve was selected to pose as the
Vicegerent of God. The lowest vices and caprices of unconscionable
youth were enthroned in the place that was most sacred in the
thoughts of men. One of his successors, Victor III. (1086-87), said of
Benedict that he led a life so shameful, so foul and execrable, that it
made one shudder to describe it. A man of such grovelling appetites
naturally wearied with even the slight usages of decency which had
come to be regarded as necessary in the papal palace; and after
twelve years of irksome attempt to support its lessened dignity, he
sold his tiara to Gregory VI. An unknown writer, about the middle of
the eleventh century, attempting a review of the passing age,
exclaimed, “Everything is degenerate and all is lost. Faith has
disappeared. The world has grown old and must soon cease
altogether.”
As the debasement of the church could go no lower, a reaction was
natural and inevitable, if virtue was not altogether decayed at the
roots. The sentiment of human decency reasserted itself, and, since
there was no power at Rome to inaugurate reform, an appeal was
made to the German emperor. Henry III., in response to the call,
deposed by force three rival claimants to the papal throne, and
secured the ascendency of a line of German popes. It was not
without the suspicion of poison that two of them died after brief
power: Clement II. within the year, and Damasus II. in twenty-three
days.
With Leo IX. (1049) came a better era. The year 1033, the ultimate
date set by the prophecy-mongers for the end of the world, being
clearly past, and men becoming again possessed of hope in the
continuance of mundane affairs, the best spirits dared to labor for the
renovation of society, that the earth thus saved as by fire might
become indeed “a new earth, wherein dwelleth righteousness.”
From this time the commanding genius and pure purpose of
Hildebrand guided, if he did not select, the occupants of the seat of
St. Peter, until, in 1073, the great counsellor himself assumed the
sacred sceptre. History, while it severely condemns the methods by
which Hildebrand sought to attain his ends, credits him with rigid
honesty and devotion to what he believed to be the will of Heaven.
While it writes into his epitaph the charge of most inordinate
ambition, it does not erase from it the record of his utterance as he
lay dying, a fugitive at Salerno: “I have loved righteousness and
hated iniquity; therefore I die in exile.”
The religious degradation of Christendom afflicted the soul of this
truly great man; but whence could come reform? The age was too
far gone in its demoralization to wait for recuperation through the
slow process of education. Society could not endure another
generation of its own putridity. The secular powers were utterly
impotent to cope with the gigantic evils that were abroad in every
land. Even had they possessed the disposition to champion the
virtues, such sovereigns as the King of France, the Emperor of
Germany, the new Norman King of England, were altogether
engrossed in holding their precarious crowns, surrounded as they
were by a multitude of feudal lords, some of whom could collect in
their own names a larger force than that which would rise to defend
the throne.
To Hildebrand but one course seemed open, a desperate one,
whose hazard showed the audacity of the genius that conceived it. It
was nothing less than to declare the Papacy a world monarchy, and
to force universal reform by the combined power of the secular and
spiritual sceptre held in his own hand. In his bull against the Emperor
Henry IV. he used these words: “Come now, I pray thee, O most holy
Father, and ye princes [St. Peter and St. Paul], that all the world may
know that if ye are able to bind and loose in heaven, ye are able on
earth to take away, or to give to each according to his merits,
empires, kingdoms, duchies, marquisates, counties, and the
possessions of all men.... If ye judge in spiritual affairs, how great
must be your power in secular! and if ye are to judge angels, who
rule over proud princes, what may ye not do to these their servants!
Let kings, then, and all the princes of the world learn what ye are and
how great is your power, and fear to treat with disrespect the
mandates of the church.”
To practicalize this enormous claim, the Pope made two demands,
which threw Europe into a state of turmoil, (1) He ordered the
renunciation of all investitures of religious office by secular
potentates. The clergy held of the empire cities, duchies, entire
provinces, rights of levying taxes, coinage, etc., amounting to one
half of all property. The sees thus held Hildebrand declared to be
vacated until their occupants should again receive them from his
hand under pledge of absolute obedience to the papal, as opposed
to the imperial, authority. By this stroke the Pope would gather to
himself the practical control of all countries. (2) Hildebrand forbade
the marriage of the clergy—a custom wide-spread at the time—and
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.

Let us accompany you on the journey of exploring knowledge and


personal growth!

ebookmass.com

You might also like