Face Detection Report
Face Detection Report
Face Detection Report
Bachelor Of Technology
Submitted by:
Suyansh Saxena (191340101029)
Aman Rawat (191340101005)
Ritesh Mishra (191340101023)
Arun Kumar (191340101011)
Under the Supervision of
Mr. Ravindra Rawat (Assistant Professor)
JUNE,2023
CERTIFICATE
The undersigned certify that they have read and recommended to the Department of
Computer Science & Engineering for acceptance, a project report entitled “FACE
MASK DETECTION SYSTEM” submitted by Suyansh Saxena, Aman Rawat,
Ritesh Mishra, Arun Kumar in partial fulfilment for the degree of Bachelor of
Technology in Computer Science & Engineering.
Signature
External Examiner
i
ACKNOWLEDGEMENT
First and foremost, we would like to express our gratitude to our Mentor, Mr. Ravindra
Rawat who was a continual source of inspiration. He pushed us to think imaginatively
and urged us to do this homework without hesitation. His vast knowledge, extensive
experience, and professional competence enabled us to successfully accomplish this
project. This endeavour would not have been possible without his help and supervision.
We could not have asked for a finer mentor in our studies. This initiative would not have
been a success without the contributions of each and every individual. We were always
there to cheer each other on, and that is what kept us together until the end.
We’d like to thank The College Institute of Technology Gopeshwar for providing us
with the opportunity to work on the project (FACE MASK DETECTION SYSTEM).
Last but not least, we would like to express our gratitude to our family, siblings, and friends
for their invaluable assistance, and We are deeply grateful to everyone who has
contributed to the successful completion of this project.
ii
DECLARATION
We hereby declare that the Major Project Report entitled, “FACE MASK
DETECTION SYSTEM” Submitted for the Bachelor of Technology Degree in
Computer Science & Engineering. Is our original work under the guidance of Mr.
Ravindra Rawat, Assistant Professor Computer Science and Engineering, Institute of
Technology Gopeshwar. The project report has not formed the basis for the award of
any degree, associate fellowship or any other similar titles.
iii
ABSTRACT
The COVID-19 pandemic has emphasized the critical importance of wearing face
masks as a preventive measure to curb the spread of the virus. To ensure public safety
and compliance with face mask mandates, there is a growing need for efficient and
automated systems that can accurately detect individuals wearing or not wearing masks
in various settings such as public places, workplaces, and transportation hubs. This
abstract presents an overview of a face mask detection system, a technological solution
aimed at monitoring and enforcing face mask usage.
The proposed face mask detection system employs computer vision techniques and
deep learning algorithms to analyze images captured from surveillance cameras or other
sources. The system's primary objective is to detect faces and determine whether they
are wearing masks or not, automated enforcement of face mask policies.
To identify faces within the input images. Subsequently, the detected faces are
subjected to further analysis using convolutional neural networks (CNNs) to classify
them as either masked or unmasked. The CNN model is trained on a large dataset of
annotated images containing individuals with and without masks to achieve high
accuracy in face mask detection.
The face mask detection system offers several benefits, including, immediate alerts for
non-compliance, and the potential to reduce the burden on human monitoring
personnel. It can be deployed in a wide range of settings, such as airports, shopping
malls, schools, and hospitals, contributing to public health and safety efforts.
In conclusion, the face mask detection system described in this abstract represents an
automated approach to monitor and enforce face mask compliance. By leveraging
computer vision techniques and deep learning algorithms, the system enables accurate
and efficient detection of individuals wearing or not wearing masks. Its implementation
iv
can play a vital role in maintaining public health and safety, particularly during the
ongoing COVID-19 pandemi
TABLE OF CONTENTS
v
3.5 Convolutional Neural Network (CNN) .......................................................................... 21
3.5.1 Applications of convolutional neural networks ...................................................... 23
3.6 Module Used .................................................................................................................. 24
3.6.1 Operating System (OS) Libraries : Enhancing Software Development and System
Interaction ........................................................................................................................ 24
3.6.2 NumPy: Efficient Numerical Computing in Python ............................................... 27
3.6.3 OpenCV: Empowering Computer Vision Applications ......................................... 29
3.6.4 Matplotlib: Data Visualization Made Easy ............................................................. 31
3.6.5 PIL (Python Imaging Library) ................................................................................ 33
3.6.6 scikit-learn............................................................................................................... 36
3.6.7 TensorFlow ............................................................................................................. 42
3.6.8 Keras ....................................................................................................................... 46
3.6.9 Gradio ..................................................................................................................... 48
CHAPTER 04- MODEL IMPLEMENTATION ..................................................................... 51
4.1 Introduction .................................................................................................................... 51
4.2 Software requirement ..................................................................................................... 52
4.3 Model Implementation ................................................................................................... 53
4.3.1 Accuracy of model depend on ........................................................................... 55
4.3.2 CNN Model Implementation .................................................................................. 57
4.4 Uses ............................................................................................................................... 59
4.5 MODEL CODE ............................................................................................................. 60
4.6 Results ............................................................................................................................ 71
CHAPTER 06-CONCLUSION AND FUTURE WORK ........................................................ 73
6.1 Conclusion ..................................................................................................................... 73
6.2 FUTURE WORK ........................................................................................................... 74
REFERENCES ........................................................................................................................ 76
vi
LIST OF FIGURES
vii
CHAPTER 01: INTRODUCTION
The symptoms of COVID 19 are variable but often include fever, cough, headache,
fatigue, breathing difficulties, loss of smell, and loss of taste. Symptoms may begin one
to fourteen days after exposure to the virus. At least a third of people who are infected
do not develop noticeable symptoms. Of those who develop symptoms noticeable
enough to be classified as patients, most (81%) develop mild to moderate symptoms
(up to mild pneumonia), while 14% develop severe symptoms (dyspnea, hypoxia, or
more than 50% lung involvement on imaging), and 5% develop critical symptoms
(respiratory failure, shock, or multiorgan dysfunction). Older people are at a higher risk
of developing severe symptoms. Some people continue to experience a range of effects
(long COVID) for months after recovery, and damage to organs has been observed.
Multi-year studies are underway to further investigate the long-term effects of the
disease.
1.1 Symptoms
People who experience Long COVID most commonly report:
1
• Headache
• Sleep problems
• Dizziness when you stand up
• Pins-and-needles feelings
• Change in smell or taste
• Depression or anxiety
4) Other symptoms
• Joint or muscle pain
• Rash
• Changes in menstrual cycles
A total of 486,648 cases with 22,030 deaths have been confirmed globally as of March
25, 2020, and the World Health Organization has declared this condition a global health
emergency.1 According to the WHO Situation Report 113 published on May 12, 2020,
coronavirus now affects 210 countries and has affected more than 4,100,000 people,
with more than 283,000 people dying.
One of the health protocols, especially masks, can prevent transmission; stated in
research6 the use of masks by the community is an effort to reduce the risk of infection
caused by pathogens that stick to the hands and face and fly in the air. This is also in
line with the statement8 that the use of face masks is designated as a non-
pharmaceutical intervention that has the potential to be very effective in inhibiting the
spread of COVID-19.
Face masks have played an important role in protecting health workers and the general
public by reducing the incidence of infection through airborne transmission. Face
masks worn by patients can reduce the release of virus-carrying droplets into the open
air and the inhalation of virus-carrying droplets from the open air.
2
Compliance with the use of masks is highly dependent on public awareness to protect
themselves and others. Public awareness of using masks in public is still very lacking,
this is due to the general perception that evidence to support the use of masks still lacks,
especially for the general public in the community. Many factors, including
unfamiliarity, impracticality cause of non-compliance with the use of masks,
must be purchased.
The proposed face mask detection system employs computer vision techniques and
deep learning algorithms to analyze images captured from surveillance cameras or other
sources. The system's primary objective is to detect faces and determine whether they
are wearing masks or not, automated enforcement of face mask policies.
1.4 Advantages
A face mask detection system offers several advantages, particularly in the context of
public health and safety. Here are some key advantages of a face mask detection system:
• Health and Safety: The primary advantage of a face mask detection system is
its ability to promote public health and safety. By automatically detecting
whether individuals are wearing face masks or not, the system can help enforce
mask-wearing guidelines in various settings such as public transport, hospitals,
schools, workplaces, and retail environments. This promotes a healthier and
safer environment for everyone, reducing the risk of viral transmission.
• Compliance and Enforcement: Face mask detection systems provide an
automated and efficient means of enforcing face mask policies. The system can
identify non-compliant individuals and trigger appropriate actions, such as
issuing warnings or alerts to the concerned individuals or notifying security
3
personnel to intervene. This helps ensure consistent compliance with face mask
regulations, reducing the burden on human resources and increasing overall
effectiveness.
• Scalability: Face mask detection systems can be easily scaled to cover large
areas or multiple locations simultaneously. Whether it's a single building, an
entire campus, or a city-wide implementation, the system can be deployed
across various sites with minimal effort. This scalability makes it suitable for a
wide range of applications, from small businesses to large public spaces.
• Cost-effectiveness: In the long run, implementing a face mask detection system
can be cost-effective. By automating the monitoring and enforcement of face
mask policies, the system reduces the need for constant manual oversight and
supervision. This saves both time and resources, allowing organizations to
allocate their personnel more efficiently to other critical tasks.
• Data Collection and Analysis: Face mask detection systems can collect data
on mask compliance rates, including the number of individuals wearing masks
and the frequency and duration of non-compliance incidents. This data can be
analyzed to identify trends, patterns, and areas of improvement. Insights gained
from the system's data can help organizations make informed decisions
regarding mask policies and adjust their strategies accordingly.
• Public Perception and Trust: The implementation of a face mask detection
system can boost public perception and instill trust in the safety measures
implemented by an organization or institution. By demonstrating a commitment
to public health and safety, organizations can enhance their reputation and
create a sense of security among employees, customers, and the general public.
It is important to note that face mask detection systems are a tool to aid in mask
compliance, and they should be used in conjunction with other preventive measures,
such as vaccination, social distancing, and regular hand hygiene, to effectively mitigate
the spread of infectious diseases.
4
• Accurate detection: The system should have a high accuracy in detecting
whether a person is wearing a face mask or not. It should be able to distinguish
between different types of masks, such as cloth masks, surgical masks, or
N95 respirators.
• Robust performance: The system should be able to handle various challenging
conditions, such as different lighting conditions, camera angles, and occlusions
(e.g., partial face coverage). It should be designed to work effectively in diverse
environments, including indoor and outdoor settings.
• Scalability and adaptability: The system should be scalable to handle large-
scale deployments, such as monitoring crowded public spaces or transportation
hubs. It should also be adaptable to different surveillance systems and
platforms, enabling easy integration into existing infrastructure.
• User-friendly interface: The system should provide a user-friendly interface
for administrators or operators to monitor and manage the detection process.
This may include visualizations, notifications, and reporting features to track
mask compliance trends over time.
• Privacy considerations: The development of the system should prioritize
privacy concerns and adhere to data protection regulations. It should be
designed to respect individuals' privacy while performing the necessary mask
detection tasks.
• Support public health measures: By accurately detecting face masks, the
system aims to support public health measures and guidelines that recommend
or require mask-wearing in specific situations. It can help in mitigating the
spread of infectious diseases and protecting the health and safety of individuals.
5
CHAPTER 02 : WORKING METHODOLOGY
2.1 Flow Chart
In order to predict whether a person has put on a mask, the model requires learning
from a well-curated dataset, as discussed later in this section. The model uses
Convolution Neural Network layers (CNN) as its backbone architecture to create
different layers. Along with this, libraries such as OpenCV, Keras, and Streamlit are
6
also used. The proposed model is designed in three phases: Data pre-processing, CNN
model training and Applying face mask detector.
Training: Here we’ll focus on loading our face mask detection dataset from disk,
training a model (using Keras/TensorFlow) on this dataset, and then serializing the face
mask detector to disk
Deployment: Once the face mask detector is trained, we can then move on to loading
the mask detector, performing face detection, and then classifying each face as
with_mask or without_mask.
we split our data into the training set which will contain the images on which the CNN
model will be trained and the test set with the images on which our model will be tested.
In this, we take split_size =0.8, which means that 80% of the total images will go to the
training set and the remaining 20% of the images will go to the test set.
we build our Sequential CNN model with various layers such as Conv2D,
MaxPooling2D, Flatten, Dropout and Dense. In the last Dense layer, we use the
‘softmax’ function to output a vector that gives the probability of each of
the two classes.
The code you provided is used to compile a model in a machine learning framework,
using the compile function. Here's an explanation of the different arguments:
7
After compiling the model with these settings, it is ready to be trained using the training
data and the specified optimizer and loss function. During training, the model will aim
to minimize the specified loss function and maximize the accuracy metric.
The first phase is the training phase. This stage is initiated with the collection of the
dataset. One of the most crucial steps is to have a good quantity and quality of data.
One can prepare the dataset or use already existing datasets from the various available
sources. If preparing yourself, the size of data could be increased by using techniques
like data augmentation. Also, the data has to be cleaned before use because it plays a
significant role in building a model. Various Steps involved in data cleaning. After
obtaining a good quality dataset, the model is selected under the system’s demands and
trained on the chosen dataset. Multiple techniques could be used to accomplish the
target. By acquiring the most suitable trained model, the first phase comes to an end. In
the subsequent step, the frames from the images are used as input to the trained model.
8
2.3 Software Requirement
SOFTWARE’S FUNCTION
NAME
• Free of cost: Google Colab is available for free to anyone with a Google
account. It provides a significant amount of computational resources,
including CPU, GPU, and even TPU (Tensor Processing Unit) for machine
learning tasks.
• Collaboration and sharing: Colab allows multiple users to collaborate on
the same notebook, making it easy to work on projects together. Notebooks
can also be shared with others, either by granting them access or by
providing a direct link.
9
• Code execution environment: Colab provides a Python environment
where you can write, execute, and modify code in cells. Each cell can be
executed independently, allowing you to run specific sections of code or
execute them sequentially.
• Markdown support: Colab supports Markdown, which means you can add
formatted text, images, equations, and even HTML elements to your
notebooks to create rich documentation.
• Integration with Google Drive: Colab seamlessly integrates with Google
Drive, allowing you to import and export data from your Drive storage,
including datasets, files, and other resources.
• Library and package availability: Colab comes pre-installed with many
popular Python libraries and packages for data analysis, machine learning,
and scientific computing. Additionally, you can install additional packages
using pip or conda commands.
• Hardware acceleration: Colab provides GPU and TPU resources, which
can significantly speed up computations, especially for machine learning
and deep learning tasks.
2.3.2 Kaggle
Kaggle is a renowned online platform that provides a collaborative environment
for data scientists, machine learning practitioners, and data enthusiasts to
explore, analyze, and solve real-world data challenges. This report delves into
the key features, advantages, and opportunities offered by Kaggle, highlighting
its significant role in advancing the field of data science.
Figure 4:Kaggle
10
2.3.3 Google Drive
Google Drive is a widely used cloud storage and collaboration platform
provided by Google. It offers a range of features and functionalities that enable
users to store, organize, and share files and collaborate seamlessly. This report
provides an overview of Google Drive, highlighting its key features,
advantages, and applications.
• Cloud Storage: Google Drive provides users with ample cloud storage
space to store files, documents, images, videos, and more. Users can
access their files from any device with an internet connection,
eliminating the need for physical storage devices and enabling easy file
management and accessibility.
• File Organization: Google Drive allows users to create folders and
subfolders to organize their files in a structured manner. This
hierarchical organization system facilitates efficient file management,
ensuring that files are easily searchable and accessible.
• File Sharing and Collaboration: One of the significant advantages of
Google Drive is its seamless file sharing and collaboration capabilities.
Users can easily share files and folders with specific individuals or
groups, granting various permissions such as view-only, edit, or
comment. Collaborators can work together in real-time, making
simultaneous edits and additions, facilitating efficient teamwork and
enhancing productivity.
• Integration with Productivity Tools: Google Drive seamlessly
integrates with other Google productivity tools such as Google Docs,
Google Sheets, and Google Slides. This integration enables users to
create, edit, and store documents, spreadsheets, and presentations
11
directly in Google Drive, fostering a cohesive and streamlined
workflow.
• Offline Access and Synchronization: Google Drive offers offline
access, allowing users to access and edit files even without an internet
connection. Changes made offline are automatically synchronized when
the user reconnects to the internet, ensuring seamless collaboration and
uninterrupted productivity.
• Accessibility and Cross-Platform Compatibility: Google Drive can
be accessed from any device with an internet connection, including
computers, laptops, tablets, and smartphones. It is compatible with
various operating systems such as Windows, macOS, Android, and iOS,
enabling users to access their files across different devices and
platforms.
• Collaborative Work Environment: The collaborative nature of
Google Drive enhances teamwork and simplifies communication among
team members. Real-time collaboration, comments, and version control
facilitate efficient collaboration, allowing teams to work together
seamlessly, irrespective of geographical locations.
• Version Control: Google Drive automatically saves versions of files,
ensuring that users can access previous versions and track changes made
over time. This feature is particularly useful for collaborative projects,
as it allows users to revert to earlier versions if needed and keeps a
record of all modifications made to the file.
• Security and Data Backup: Google Drive offers robust security
measures, including encryption and multi-factor authentication,
ensuring the privacy and protection of users' files. Additionally, Google
Drive provides automated backups, protecting files against accidental
deletion or loss due to device failures.
• Cost-Effectiveness: Google Drive provides a generous amount of free
storage space, and additional storage can be purchased at affordable
rates. This cost-effective solution eliminates the need for expensive
physical storage devices and allows users to scale their storage needs
according to their requirements.
12
CHAPTER 03 : MODEL IMPLEMENTATION
3.1 Python
Python is a high-level, general-purpose and a very popular programming language.
Python programming language (latest Python 3) is being used in web development,
Machine Learning applications, along with all cutting edge technology in Software
Industry. Python Programming Language is very well suited for Beginners, also for
experienced programmers with other programming languages like C++ and Java.
13
• Text processing and many more…
14
Figure 7:Key Concept of AI
3.2.2 Applications of AI
• Healthcare: AI is revolutionizing healthcare by improving diagnostics,
drug discovery, personalized medicine, and patient care. It enables the
analysis of large medical datasets, assists in disease diagnosis, and facilitates
precision medicine approaches
• Finance and Banking: AI is transforming the finance and banking industry
through applications like fraud detection, algorithmic trading, risk
assessment, and personalized financial recommendations. Machine learning
algorithms analyze vast amounts of financial data, identify patterns, and
make data-driven decisions.
• Transportation and Autonomous Vehicles: AI is paving the way for
autonomous vehicles, optimizing transportation systems, and improving
safety. Self-driving cars utilize computer vision, sensor fusion, and ML
algorithms to perceive the environment, make decisions, and navigate
autonomously.
• Manufacturing and Industry 4.0: AI-driven automation and robotics are
revolutionizing manufacturing processes, enabling increased efficiency,
quality control, predictive maintenance, and supply chain optimization. AI-
15
powered systems optimize production lines, monitor equipment health, and
enable intelligent decision-making.
• Customer Service and Chatbots: AI-powered chatbots and virtual
assistants enhance customer service by providing 24/7 support, answering
queries, and resolving common issues. Natural Language Processing allows
chatbots to understand and respond to human language, providing
personalized and efficient customer experiences.
16
accurately. As input data is fed into the model, the model adjusts its weights
until it has been fitted appropriately. This occurs as part of the cross validation
process to ensure that the model avoids overfitting or underfitting. Supervised
learning helps organizations solve a variety of real-world problems at scale,
such as classifying spam in a separate folder from your inbox. Some methods
used in supervised learning include neural networks, naïve bayes, linear
regression, logistic regression, random forest, and support vector machine
(SVM).
3.3.2 Unsupervised machine learning
Unsupervised learning, also known as unsupervised machine learning, uses
machine learning algorithms to analyze and cluster unlabeled datasets. These
algorithms discover hidden patterns or data groupings without the need for
human intervention. This method’s ability to discover similarities and
differences in information make it ideal for exploratory data analysis, cross-
selling strategies, customer segmentation, and image and pattern recognition.
It’s also used to reduce the number of features in a model through the process
of dimensionality reduction. Principal component analysis (PCA) and singular
value decomposition (SVD) are two common approaches for this. Other
algorithms used in unsupervised learning include neural networks, k-means
clustering, and probabilistic clustering methods.
• Neural networks: Neural networks simulate the way the human brain
works, with a huge number of linked processing nodes. Neural networks
are good at recognizing patterns and play an important role in
17
applications including natural language translation, image recognition,
speech recognition, and image creation.
• Linear regression: This algorithm is used to predict numerical values,
based on a linear relationship between different values. For example, the
technique could be used to predict house prices based on historical data
for the area.
• Logistic regression: This supervised learning algorithm makes
predictions for categorical response variables, such as“yes/no” answers
to questions. It can be used for applications such as classifying spam and
quality control on a production line.
• Clustering: Using unsupervised learning, clustering algorithms can
identify patterns in data so that it can be grouped. Computers can help
data scientists by identifying differences between data items that
humans have overlooked.
• Decision trees: Decision trees can be used for both predicting numerical
values (regression) and classifying data into categories. Decision trees
use a branching sequence of linked decisions that can be represented
with a tree diagram. One of the advantages of decision trees is that they
are easy to validate and audit, unlike the black box of the neural network.
• Random forests: In a random forest, the machine learning algorithm
predicts a value or category by combining the results from a number
of decision trees.
18
Figure 9:Biological Neuron
Artificial neural networks (ANNs) are comprised of a node layers, containing an input
layer, one or more hidden layers, and an output layer. Each node, or artificial neuron,
connects to another and has an associated weight and threshold. If the output of any
individual node is above the specified threshold value, that node is activated, sending
data to the next layer of the network. Otherwise, no data is passed along to the next
layer of the network.
Neural networks rely on training data to learn and improve their accuracy over time.
However, once these learning algorithms are fine-tuned for accuracy, they are powerful
tools in computer science and artificial intelligence, allowing us to classify and cluster
data at a high velocity. Tasks in speech recognition or image recognition can take
19
minutes versus hours when compared to the manual identification by human experts.
One of the most well-known neural networks is Google’s search algorithm.
Think of each individual node as its own linear regression model, composed of input
data, weights, a bias (or threshold), and an output. The formula would look something
like this:
Once an input layer is determined, weights are assigned. These weights help determine
the importance of any given variable, with larger ones contributing more significantly
to the output compared to other inputs. All inputs are then multiplied by their respective
weights and then summed. Afterward, the output is passed through an activation
function, which determines the output. If that output exceeds a given threshold, it
“fires” (or activates) the node, passing data to the next layer in the network. This results
in the output of one node becoming in the input of the next node. This process of passing
data from one layer to the next layer defines this neural network as a
feedforward network.
20
3.5 Convolutional Neural Network (CNN)
A convolutional neural network (CNN or convnet) is a subset of machine learning. It is
one of the various types of artificial neural networks which are used for different
applications and data types. A CNN is a kind of network architecture for deep learning
algorithms and is specifically used for image recognition and tasks that involve the
processing of pixel data.
There are other types of neural networks in deep learning, but for identifying and
recognizing objects, CNNs are the network architecture of choice. This makes them
highly suitable for computer vision (CV) tasks and for applications where object
recognition is vital, such as self-driving cars and facial recognition.
Artificial neural networks (ANNs) are a core element of deep learning algorithms. One
type of an ANN is a recurrent neural network (RNN) that uses sequential or time series
data as input. It is suitable for applications involving natural language processing
(NLP), language translation, speech recognition and image captioning.
The CNN is another type of neural network that can uncover key information in both
time series and image data. For this reason, it is highly valuable for image-related tasks,
such as image recognition, object classification and pattern recognition. To identify
patterns within an image, a CNN leverages principles from linear algebra, such as
matrix multiplication. CNNs can also classify audio and signal data.
21
A CNN's architecture is analogous to the connectivity pattern of the human brain. Just
like the brain consists of billions of neurons, CNNs also have neurons arranged in a
specific way. In fact, a CNN's neurons are arranged like the brain's frontal lobe, the area
responsible for processing visual stimuli. This arrangement ensures that the entire
visual field is covered, thus avoiding the piecemeal image processing problem of
traditional neural networks, which must be fed images in reduced-resolution pieces.
Compared to the older networks, a CNN delivers better performance with image inputs,
and also with speech or audio signal inputs.
A deep learning CNN consists of three layers: a convolutional layer, a pooling layer
and a fully connected (FC) layer. The convolutional layer is the first layer while the FC
layer is the last.
From the convolutional layer to the FC layer, the complexity of the CNN increases. It
is this increasing complexity that allows the CNN to successively identify larger
portions and more complex features of an image until it finally identifies the object in
its entirety.
Over multiple iterations: the kernel sweeps over the entire image. After each iteration
a dot product is calculated between the input pixels and the filter. The final output from
the series of dots is known as a feature map or convolved feature. Ultimately, the image
is converted into numerical values in this layer, which allows the CNN to interpret the
image and extract relevant patterns from it.
Pooling layer: Like the convolutional layer, the pooling layer also sweeps a kernel or
filter across the input image. But unlike the convolutional layer, the pooling layer
reduces the number of parameters in the input and also results in some information loss.
On the positive side, this layer reduces complexity and improves the efficiency of the
CNN.
22
Fully connected layer: The FC layer is where image classification happens in the CNN
based on the features extracted in the previous layers. Here, fully connected means that
all the inputs or nodes from one layer are connected to every activation unit or node of
the next layer.
All the layers in the CNN are not fully connected because it would result in an
unnecessarily dense network. It also would increase losses and affect the output quality,
and it would be computationally expensive.
A CNN can have multiple layers, each of which learns to detect the different features
of an input image. A filter or kernel is applied to each image to produce an output that
gets progressively better and more detailed after each layer. In the lower layers, the
filters can start as simple features.
At each successive layer, the filters increase in complexity to check and identify
features that uniquely represent the input object. Thus, the output of each convolved
image -- the partially recognized image after each layer -- becomes the input for the
next layer. In the last layer, which is an FC layer, the CNN recognizes the image or the
object it represents.
With convolution, the input image goes through a set of these filters. As each filter
activates certain features from the image, it does its work and passes on its output to
the filter in the next layer. Each layer learns to identify different features and the
operations end up being repeated for dozens, hundreds or even thousands of layers.
Finally, all the image data progressing through the CNN's multiple layers allow the
CNN to identify the entire object.
The most common applications of CV and CNNs are used in fields such as the
following:
23
Healthcare : CNNs can examine thousands of visual reports to detect any
anomalous conditions in patients, such as the presence of malignant cancer cells.
Social media : Social media platforms use CNNs to identify people in a user's
photograph and help the user tag their friends.
Audio processing for virtual assistants : CNNs in virtual assistants learn and
detect user-spoken keywords and process the input to guide their actions and
respond to the user.
24
Key Features of OS Libraries
25
Benefits and Applications of OS Libraries
Examples of OS Libraries
26
system operations, networking, device interaction, and graphical user
interface (GUI) development.
• Libc : libc is the C library that provides fundamental OS functions and
interfaces for the C programming language. It includes functions for
memory management, file operations, string manipulation, and system
calls, making it a crucial component in many software applications.
Figure 13:NumPy
27
eigenvalue decomposition), and more. NumPy's optimized
implementation ensures fast and accurate computations.
• Broadcasting : NumPy's broadcasting allows for performing element-
wise operations on arrays of different shapes and sizes. Broadcasting
eliminates the need for explicit loops, enabling concise and efficient
code. This feature simplifies operations like array addition, subtraction,
multiplication, and division, even when the arrays have different shapes.
• Integration with Python Ecosystem : NumPy seamlessly integrates
with other popular scientific computing libraries in Python, such as
SciPy, pandas, and Matplotlib. This integration enables a complete data
analysis and visualization workflow, providing a comprehensive toolkit
for scientific computing and data exploration.
• Performance Optimization : NumPy's underlying implementation is
written in C, which makes it significantly faster compared to pure
Python implementations. It leverages vectorized operations and
optimized algorithms, resulting in improved computational efficiency.
NumPy also provides options for parallel execution and integration with
other low-level libraries for further performance gains.
28
• Simulation and Modeling : NumPy is valuable in simulation and
modeling tasks. Its efficient numerical operations and array processing
capabilities allow for the implementation of complex mathematical
models and simulations. NumPy's integration with visualization
libraries like Matplotlib aids in the analysis and visualization of
simulation results.
• Signal and Image Processing : NumPy's array operations and Fourier
transforms are essential for signal and image processing tasks. It enables
tasks like noise reduction, filtering, edge detection, and image
manipulation. NumPy's ability to handle multidimensional arrays
efficiently makes it a versatile tool for working with image and audio
data.
Figure 14:OpenCV
29
extraction. OpenCV's extensive collection of algorithms enables
developers to manipulate images and videos efficiently.
• Object Detection and Tracking : OpenCV provides pre-trained models
and algorithms for object detection and tracking. It allows developers to
detect and track objects in real-time video streams or static images.
OpenCV's object detection capabilities have applications in various
domains, including surveillance, robotics, and autonomous systems.
• Feature Detection and Extraction : OpenCV offers algorithms for
detecting and extracting features from images, such as corners, edges,
and keypoints. These features serve as key components for tasks like
image matching, image recognition, and 3D reconstruction.
• Camera Calibration and Stereo Vision : OpenCV provides tools for
camera calibration and stereo vision, allowing developers to calibrate
cameras, estimate camera parameters, and perform stereo depth
perception. These features are essential for applications such as
augmented reality, 3D modeling, and depth estimation.
• Machine Learning Integration : OpenCV seamlessly integrates with
popular machine learning frameworks like TensorFlow and PyTorch.
This integration enables developers to combine the power of deep
learning with OpenCV's computer vision capabilities, facilitating tasks
such as image classification, object detection, and semantic
segmentation.
Applications of OpenCV
30
• Augmented Reality (AR) : OpenCV provides the necessary tools for
developing augmented reality applications. It enables the overlay of
virtual objects on real-world scenes, aligning virtual and real-world
coordinates, and performing real-time tracking and rendering.
• Medical Imaging : OpenCV finds applications in medical imaging,
assisting in tasks such as image segmentation, tumor detection, and
image enhancement. It aids healthcare professionals in accurate
diagnosis, treatment planning, and research.
• Industrial Automation and Quality Control : OpenCV is utilized in
industrial automation and quality control systems. It enables the
inspection and analysis of products, detection of defects, and quality
assurance. OpenCV's algorithms help ensure consistency, accuracy, and
efficiency in manufacturing processes.
Advancements in OpenCV
31
explores the key features, benefits, and applications of Matplotlib, highlighting
its significance in data visualization and analysis.
Figure 15:Matplotlib
32
• Integration with NumPy and Pandas : Matplotlib seamlessly
integrates with other scientific computing libraries like NumPy and
Pandas. It can directly plot data stored in NumPy arrays or Pandas
DataFrames, simplifying the process of generating visualizations from
numerical data. This integration enhances the interoperability of
Matplotlib with data analysis workflows.
33
PIL project and added Python3.x support to it. Pillow was announced as a
replacement for PIL for future usage. Pillow supports a large number of image
file formats including BMP, PNG, JPEG, and TIFF. The library encourages
adding support for newer formats in the library by creating new file decoders.
Figure 16:PIL
This module is not preloaded with Python. So to install it execute the following
command in the command-line:
img = Image.open(r"test.png")
Displaying the image using show(): This method is used to display the image.
For displaying the image Pillow first converts the image to a .png format (on
Windows OS) and stores it in a temporary buffer and then displays it. Therefore,
due to the conversion of the image format to .png some properties of the original
image file format might be lost (like animation). Therefore, it is advised to use
this method only for test purposes.
img = Image.open(r"test.png")
34
img.show()
Getting the size of the image: This attribute provides the size of the image. It
returns a tuple that contains width and height.
Example:
img = Image.open(r"test.png")
print(img.size)
Getting the format of the image: This method returns the format of the image
file.
img = Image.open(r"test.png")
print(img.format)
Rotating an image using rotate(): After rotating the image, the sections of the
image having no pixel values are filled with black (for non-alpha images) and
with completely transparent pixels (for images supporting transparency)
Example:
angle = 40
img = Image.open(r"test.png")
r_img = img.rotate(angle)
35
Example:
img = Image.open(r"test.png")
r_img = img.resize(size)
r_img.show()
3.6.6 scikit-learn
scikit-learn is an open-source Python library that implements a range of machine
learning, pre-processing, cross-validation, and visualization algorithms using a
unified interface.
Figure 17:scikit-learn
Scikit-learn requires:
NumPy
36
Before installing scikit-learn, ensure that you have NumPy and SciPy installed.
Once you have a working installation of NumPy and SciPy, the easiest way to
install scikit-learn is using pip:
Features: (also known as predictors, inputs, or attributes) they are simply the
variables of our data. They can be more than one and hence represented by a
feature matrix (‘X’ is a common notation to represent feature matrix). A list of
all the feature names is termed feature names.
Response: (also known as the target, label, or output) This is the output variable
depending on the feature variables. We generally have a single response column
and it is represented by a response vector (‘y’ is a common notation to represent
response vector). All the possible values taken by a response vector are
termed target names.
iris = load_iris()
X = iris.data
y = iris.target
feature_names = iris.feature_names
target_names = iris.target_names
37
# printing features and target names of our dataset
Loading external dataset: Now, consider the case when we want to load an
external dataset. For this purpose, we can use the pandas library for easily
loading and manipulating datasets.
Splitting the dataset : One important aspect of all machine learning models is
to determine their accuracy. Now, in order to determine their accuracy, one can
train the model using the given dataset and then predict the response values for
the same dataset using that model and hence, find the accuracy of the model.
38
Maximizing training accuracy rewards overly complex models that won’t
necessarily generalize our model.
A better option is to split our data into two parts: the first one for training our
machine learning model, and the second one for testing our model.
To summarize :
Split the dataset into two pieces : a training set and a testing set.
Test the model on the testing set, and evaluate how well our model did.
The model can be trained and tested on different data than the one used for
training.
Response values are known for the test dataset, hence predictions can be
evaluated
iris = load_iris()
X = iris.data
y = iris.target
39
from sklearn.model_selection import train_test_split
print(X_train.shape)
print(X_test.shape)
print(y_train.shape)
print(y_test.shape)
X, y: These are the feature matrix and response vector which need to be split.
test_size: It is the ratio of test data to the given data. For example, setting
test_size = 0.4 for 150 rows of X produces test data of 150 x 0.4 = 60 rows.
Training the model : Now, it’s time to train some prediction models using our
dataset. Scikit-learn provides a wide range of machine learning algorithms that
have a unified/consistent interface for fitting, predicting accuracy, etc.
iris = load_iris()
40
X = iris.data
y = iris.target
knn = KNeighborsClassifier(n_neighbors=3)
knn.fit(X_train, y_train)
y_pred = knn.predict(X_test)
preds = knn.predict(sample)
print("Predictions:", pred_species)
41
• Consistent interface to machine learning models
• Provides many tuning parameters but with sensible defaults
• Exceptional documentation
• Rich set of functionality for companion tasks
• Active community for development and support
3.6.7 TensorFlow
TensorFlow is a free and open-source software library for machine learning and
artificial intelligence. It can be used across a range of tasks but has a particular
focus on training and inference of deep neural networks.
TensorFlow was developed by the Google Brain team for internal Google use
in research and production. The initial version was released under the Apache
License 2.0 in 2015. Google released the updated version of TensorFlow, named
TensorFlow 2.0, in September 2019.
Features
42
• Eager execution : TensorFlow includes an “eager execution” mode,
which means that operations are evaluated immediately as opposed to
being added to a computational graph which is executed later. Code
executed eagerly can be examined step-by step-through a debugger,
since data is augmented at each line of code rather than later in a
computational graph. This execution paradigm is considered to be easier
to debug because of its step by step transparency.
• Distribute : In both eager and graph executions, TensorFlow provides
an API for distributing computation across multiple devices with various
distribution strategies. This distributed computing can often speed up
the execution of training and evaluating of TensorFlow models and is a
common practice in the field of AI.
• Losses : To train and assess models, TensorFlow provides a set of loss
functions (also known as cost functions).Some popular examples
include mean squared error (MSE) and binary cross entropy
(BCE).These loss functions compute the “error” or “difference”
between a model's output and the expected output (more broadly, the
difference between two tensors). For different datasets and models,
different losses are used to prioritize certain aspects of performance.
• Metrics : In order to assess the performance of machine learning
models, TensorFlow gives API access to commonly used metrics.
Examples include various accuracy metrics (binary, categorical, sparse
categorical) along with other metrics such as Precision, Recall, and
Intersection-over-Union (IoU).
• TF.nn : TensorFlow.nn is a module for executing primitive neural
network operations on models. Some of these operations include
variations of convolutions (1/2/3D, Atrous, depthwise), activation
functions (Softmax, RELU, GELU, Sigmoid, etc.) and their variations,
and other Tensor operations (max-pooling, bias-add, etc.).
• Optimizers : TensorFlow offers a set of optimizers for training neural
networks, including ADAM, ADAGRAD, and Stochastic Gradient
Descent (SGD). When training a model, different optimizers offer
43
different modes of parameter tuning, often affecting a model's
convergence and performance.
TFLite : TensorFlow Lite has APIs for mobile apps or embedded devices to
generate and deploy TensorFlow models. These models are compressed and
optimized in order to be more efficient and have a higher performance on
smaller capacity devices.
TensorFlow Lite uses FlatBuffers as the data serialization format for network
models, eschewing the Protocol Buffers format used by standard TensorFlow
models.
Integrations
44
Numpy : Numpy is one of the most popular Python data libraries, and
TensorFlow offers integration and compatibility with its data structures.Numpy
NDarrays, the library's native datatype, are automatically converted to
TensorFlow Tensors in TF operations; the same is also true vice versa. This
allows for the two libraries to work in unison without requiring the user to write
explicit data conversions. Moreover, the integration extends to memory
optimization by having TF Tensors share the underlying memory
representations of Numpy NDarrays whenever possible.
45
• pmap: SPMD programming
Applications
3.6.8 Keras
Keras is a high-level neural networks library written in Python. It is designed to
be user-friendly, modular, and extensible, allowing developers to build and
experiment with deep learning models easily.
Keras provides a simple and intuitive interface for creating and training neural
networks. It supports both convolutional networks, commonly used in computer
46
vision tasks, and recurrent networks, suitable for sequential data processing.
Keras also allows you to define custom network architectures and implement
complex models by stacking layers together.
Figure 19:Keras
One of the advantages of Keras is its ability to work with different backend
frameworks such as TensorFlow, Microsoft Cognitive Toolkit (CNTK), or
Theano. This means that you can use Keras as a front-end interface to build your
models while leveraging the computational power and optimization capabilities
of these backends.
Here's a simple example that demonstrates how to create a basic neural network
using Keras with TensorFlow as the backend:
import tensorflow as tf
model = keras.Sequential([
keras.layers.Dense(64, activation='relu'),
keras.layers.Dense(10, activation='softmax')
])
model.compile(optimizer='adam',
loss='categorical_crossentropy',
47
metrics=['accuracy'])
In this example, we create a sequential model with three layers: two dense (fully
connected) layers with ReLU activation, and a final dense layer with softmax
activation for multi-class classification. We compile the model with an
optimizer, loss function, and evaluation metrics. Then, we train the model using
training data (x_train and y_train) and evaluate its performance on test data
(x_test and y_test).
3.6.9 Gradio
Gradio is a Python library that allows you to quickly build and deploy
customizable user interfaces for machine learning models. It provides a simple
way to create web-based UIs or desktop applications to interact with your
models, without requiring extensive web development knowledge.
Figure 20:Gradio
With Gradio, you can create interactive interfaces that enable users to input data,
visualize predictions, and explore the behavior of your models. It supports a
variety of input types, such as text, images, audio, and video, making it suitable
for a wide range of machine learning tasks.
Here's a basic example that demonstrates how to use Gradio to create a simple
UI for a image classification model:
48
import gradio as gr
import tensorflow as tf
import numpy as np
model = tf.keras.applications.MobileNetV2()
def classify_image(image):
image = tf.keras.applications.mobilenet_v2.preprocess_input(image)
# Make predictions
top_class_index = np.argmax(predictions)
image_input = gr.inputs.Image()
label_output = gr.outputs.Label()
interface=gr.Interface(fn=classify_image,inputs=image_input,
outputs=label_output)
interface.launch()
49
In this example, we first load a pre-trained MobileNetV2 model from
TensorFlow's keras.applications module. Then, we define a function
classify_image that takes an input image, preprocesses it, makes predictions
using the model, and returns the predicted class label.
Gradio provides various input and output types, including text, number,
checkbox, slider, and more. You can customize the UI components, layout, and
styling to suit your needs. Additionally, Gradio supports integration with other
Python libraries and frameworks, such as TensorFlow, PyTorch, and Flask,
allowing you to build more complex applications.
50
CHAPTER 04- MODEL IMPLEMENTATION
4.1 Introduction
Face mask detection systems are an innovative technological solution that plays
a crucial role in promoting public health and safety. These systems leverage
computer vision and deep learning algorithms to identify whether individuals
are wearing face masks or not. With the ongoing global pandemic and the
importance of mask-wearing in preventing the spread of contagious diseases,
face mask detection systems have emerged as a valuable tool in enforcing mask-
wearing policies and mitigating the risk of transmission.The primary objective
of a face mask detection system is to analyze images or video streams and
determine whether a person's face is covered with a mask or not. By leveraging
advanced algorithms, the system can accurately identify and differentiate
between masked and unmasked faces. It achieves this by detecting and
localizing faces in the input data and analyzing the presence or absence of a
mask on each detected face.The face mask detection system can be deployed in
various settings such as airports, train stations, shopping malls, healthcare
facilities, educational institutions, and workplaces. By continuously monitoring
individuals' compliance with mask-wearing policies, these systems help
authorities enforce regulations and maintain a safer environment for
everyone.Implementing a face mask detection system involves training a
Convolutional Neural Network (CNN) model using a labeled dataset of images
containing masked and unmasked faces. The model learns to extract relevant
features from the input images and classify them into the respective categories.
Through an iterative training process, the model becomes adept at
distinguishing between faces with masks and those without.Once the model is
trained, it can be integrated into real-time applications that process video
streams or images in real-time. The system can identify and mark faces without
masks, allowing authorities or responsible personnel to take appropriate action,
such as issuing warnings or denying access.Face mask detection systems have
proven to be effective in enhancing public health measures and ensuring
compliance with mask-wearing guidelines. They provide an automated and
reliable solution for monitoring mask usage, reducing the reliance on manual
interventions, and minimizing the risk of virus transmission in public
51
spaces.Overall, face mask detection systems are a valuable technological
advancement that contributes to maintaining public health and safety. By
leveraging computer vision and deep learning techniques, these systems provide
an efficient and scalable solution for monitoring and enforcing mask-wearing
policies, ultimately helping to curb the spread of infectious diseases.
52
Alternatively, you can use IDEs like PyCharm, Spyder, or Visual Studio
Code for developing and debugging your face mask detection system.
• GPU Support (Optional): If you are working with large datasets or complex
CNN models, using a GPU (Graphics Processing Unit) can significantly
speed up the training and inference processes. Deep learning frameworks
like TensorFlow and PyTorch provide GPU support for accelerated
computation. Ensure that your system has compatible GPU drivers and
libraries installed.
• Additional Libraries and Tools: Depending on the specific requirements of
your face mask detection system, you may need to use other libraries and
tools. For example, scikit-learn can be useful for evaluation and metrics
calculations, matplotlib or seaborn for data visualization, and Flask or
Django for developing a web-based or API-based interface for your system.
It's important to note that the software requirements may vary based on your
specific implementation approach, framework preferences, and deployment
environment. It's always beneficial to research and select the tools and libraries
that best suit your project needs.
53
include fully connected layers to perform classification based on the
extracted features.
• Model Training: Train the CNN model on the training dataset. Define the
loss function, such as binary cross-entropy, and choose an optimizer, like
Adam or Stochastic Gradient Descent (SGD), to update the model's
parameters during training. Iterate over the training dataset, feeding the
images into the model, calculating the loss, and updating the weights using
backpropagation.
• Model Evaluation: Evaluate the trained model's performance on the testing
dataset. Calculate metrics such as accuracy, precision, recall, and F1 score
to assess how well the model generalizes to unseen data. This step helps
determine the model's effectiveness in detecting face masks.
• Fine-Tuning and Hyperparameter Tuning: Fine-tune the CNN model by
adjusting hyperparameters like learning rate, batch size, and number of
layers or filters. Use techniques such as grid search or random search to find
optimal hyperparameter values that improve the model's performance.
• Deployment and Integration: Once the model is trained and achieves
satisfactory performance, you can deploy it for face mask detection.
Integrate the model with a face detection algorithm to locate and extract
faces from images or video streams. Apply the trained CNN model to
classify each extracted face as wearing a mask or not. You can use
frameworks like OpenCV or libraries like Dlib for face detection.
• Real-Time Application: For real-time face mask detection, implement the
model and integration in a system that continuously processes video
streams. This can be achieved by capturing video frames, applying face
detection, and passing the detected faces through the trained CNN model for
mask detection. Display the results, such as bounding boxes around faces
and mask/no-mask labels, in real-time.
54
4.3.1 Accuracy of model depend on
The accuracy of a CNN model used in a face mask detection system can vary
depending on various factors, including the quality and diversity of the dataset,
model architecture, hyperparameter tuning, and the complexity of the task at
hand. Generally, the aim is to achieve high accuracy in correctly identifying
whether a person is wearing a face mask or not.The reported accuracy of CNN
models for face mask detection typically ranges from 90% to over 95%.
However, it's important to note that accuracy alone may not provide a complete
picture of the model's performance. It's essential to consider other evaluation
metrics such as precision, recall, and F1 score to assess the model's effectiveness
in correctly identifying both masked and unmasked faces and avoiding false
positives or false negatives.
To improve the accuracy of a face mask detection model, several techniques can
be employed:
55
use techniques like grid search or random search to find the optimal
combination of hyperparameters.
• Transfer Learning: Take advantage of pre-trained CNN models on large-
scale image datasets, such as ImageNet. Transfer learning involves using
the learned features from the pre-trained model as a starting point for
training the face mask detection model. Fine-tuning the pre-trained model
can often improve accuracy with a smaller amount of data.
• Regularization Techniques: Apply regularization techniques such as
dropout or weight decay to prevent overfitting and improve generalization.
These techniques help the model generalize well to unseen data, leading to
improved accuracy.
• Model Ensemble: Combine multiple trained models or predictions to create
an ensemble model. Ensemble methods, such as majority voting or
averaging, can help improve accuracy by leveraging diverse perspectives
from different models.
56
the input images.During the training phase, mathematical optimization
algorithms such as gradient descent and its variants (e.g., Adam, RMSprop) are
used to optimize the network's parameters (weights and biases). The
optimization process aims to minimize the difference between predicted outputs
and ground truth labels, often using a loss function such as cross-entropy or
mean squared error.Once the model is trained, it can be used for classification,
i.e., determining whether a given image contains a person wearing a mask or
not. Classification algorithms, such as softmax or sigmoid functions, assign
probabilities to each class (mask or no mask) based on the learned features and
model parameters. Thresholding techniques are used to convert probability
scores into binary decisions. By selecting an appropriate threshold value, the
system can decide whether the detected probability of mask presence is above
or below the threshold, thereby classifying the image according . Various
mathematical evaluation metrics are employed to assess the performance of face
mask detection systems, such as accuracy, precision, recall, F1 score, and
receiver operating characteristic (ROC) curve analysis. These metrics help
quantify the system's effectiveness in correctly identifying masked and
unmasked faces.
57
then fed into one or more fully connected layers, leading to the final
output layer that predicts the presence or absence of a mask.
• Model Compilation: Configure the CNN model by specifying the
optimizer, loss function, and evaluation metrics. Common choices
include using the Adam optimizer, binary cross-entropy loss function,
and accuracy as the evaluation metric.
• Model Training: Train the CNN model on the training dataset. During
training, the model adjusts its parameters to minimize the loss between
predicted and true labels. This is achieved by forward propagation,
computing gradients through backpropagation, and updating the model's
weights using an optimization algorithm.
• Model Evaluation: Evaluate the trained model's performance on the
testing dataset. Compute various evaluation metrics, such as accuracy,
precision, recall, and F1 score, to assess how well the model generalizes
to unseen data.
• Fine-Tuning and Hyperparameter Tuning: Fine-tune the CNN model by
adjusting its hyperparameters, such as learning rate, batch size, and
number of layers or filters. This can be done through a systematic search
or using techniques like grid search or random search to find optimal
hyperparameter values that improve the model's performance.
• Deployment and Integration: Once the CNN model is trained and
achieves satisfactory performance, it can be deployed for face mask
detection. Integration with a face detection algorithm may be required
to localize and extract faces from input images or video streams. The
trained CNN model can then be used to classify each extracted face as
either wearing a mask or not.It is important to note that the
implementation details may vary depending on the specific deep
learning framework or library being used (e.g., TensorFlow, PyTorch).
These frameworks provide APIs and tools to facilitate the development
and training of CNN models for face mask detection.
• Top of Form : It is important to note that the implementation details may
vary depending on the specific deep learning framework or library being
used (e.g., TensorFlow, PyTorch). These frameworks provide APIs and
58
tools to facilitate the development and training of CNN models for face
mask detection.
4.4 Uses
Face mask detection systems have numerous applications and can be used in various
settings to promote mask-wearing compliance and enhance public health and safety.
Here are some specific use cases:
• Public Spaces: Face mask detection systems can be deployed in public spaces
such as airports, train stations, bus terminals, shopping malls, and stadiums. By
monitoring individuals entering these spaces, the system can identify those not
wearing masks and provide real-time alerts or deny access if necessary. This
helps enforce mask-wearing policies and reduces the risk of virus transmission
in crowded areas.
• Healthcare Facilities: Face mask detection systems can be utilized in hospitals,
clinics, and other healthcare settings. These systems can help ensure that both
healthcare providers and visitors adhere to mask-wearing guidelines,
minimizing the spread of infections within the facility. Additionally, the
systems can be integrated with access control systems to regulate entry and
maintain a safe environment for patients and staff.
• Educational Institutions: Face mask detection systems can be employed in
schools, colleges, and universities. By monitoring students, teachers, and
visitors, the system can identify individuals without masks and trigger
appropriate actions, such as sending alerts to the staff or denying access to
classrooms. This helps create a safer learning environment and reduces the risk
of outbreaks.
• Workplace Safety: Many workplaces, such as offices, factories, and
construction sites, can benefit from face mask detection systems. These systems
can ensure that employees and visitors follow mask-wearing protocols,
enhancing workplace safety and minimizing the spread of infectious diseases
within the premises.
• Transportation: Face mask detection systems can be integrated into public
transportation systems, including buses, trains, and subways. By monitoring
59
passengers entering the vehicles, the system can identify individuals without
masks and alert the transportation authorities or staff. This helps maintain a safer
environment for both passengers and employees.
• Law Enforcement: Face mask detection systems can aid law enforcement
agencies in monitoring compliance with mask-wearing regulations during
public events, protests, or demonstrations. By identifying individuals without
masks, authorities can take appropriate actions to enforce public health
guidelines and maintain public safety.
• Retail and Hospitality: Face mask detection systems can be implemented in
retail stores, restaurants, and hotels to ensure that customers and employees
wear masks while inside the premises. This not only promotes customer
confidence but also protects the staff and other patrons from potential virus
transmission.
• These are just a few examples of how face mask detection systems can be used
to enhance public health and safety in various settings. As the technology
continues to evolve, the applications and impact of these systems are expected
to expand, contributing to a healthier and safer society.
drive.mount('/content/drive/')
import os
import numpy as np
import cv2
with_mask_files = os.listdir('/content/drive/MyDrive/Project/data/with_mask')
print(with_mask_files[0:5])
print(with_mask_files[-5:])
60
without_mask_files =
os.listdir('/content/drive/MyDrive/Project/data/without_mask')
print(without_mask_files[0:5])
print(without_mask_files[-5:])
with_mask_labels = [1]*3725
without_mask_labels = [0]*3828
print(with_mask_labels[0:5])
print(without_mask_labels[0:5])
print(len(labels))
print(labels[0:5])
print(labels[-5:])
Img=mpimg.imread('/content/drive/MyDrive/Project/data/with_mask/with_ma
sk_865.jpg')
imgplot = plt.imshow(img)
plt.show()
61
Figure 21:Cell output 1
img=mpimg.imread('/content/drive/MyDrive/Project/data/without_mask/witho
ut_mask_640.jpg')
imgplot = plt.imshow(img)
plt.show()
62
Figure 22:Cell output 2
with_mask_path = '/content/drive/MyDrive/Project/data/with_mask/'
data = []
image = image.resize((128,128))
image = image.convert('RGB')
image = np.array(image)
data.append(image)
without_mask_path = '/content/drive/MyDrive/Project/data/without_mask/'
image = image.resize((128,128))
63
image = image.convert('RGB')
image = np.array(image)
data.append(image)
type(data)
len(data)
data[0]
type(data[0])
data[0].shape
X = np.array(data)
Y = np.array(labels)
type(X)
type(Y)
print(X.shape)
print(Y.shape)
64
print(Y)
X_train_scaled = X_train/255
X_test_scaled = X_test/255
X_train[0]
X_train_scaled[0]
65
Figure 25:Cell output 5
build cnn
import tensorflow as tf
num_of_classes = 2
model = keras.Sequential()
model.add(keras.layers.MaxPooling2D(pool_size=(2,2)))
model.add(keras.layers.MaxPooling2D(pool_size=(2,2)))
model.add(keras.layers.Flatten())
model.add(keras.layers.Dense(128, activation='relu'))
model.add(keras.layers.Dropout(0.5))
model.add(keras.layers.Dense(64, activation='relu'))
model.add(keras.layers.Dropout(0.5))
model.add(keras.layers.Dense(num_of_classes, activation='sigmoid'))
66
model.compile(optimizer='adam',
loss='sparse_categorical_crossentropy',
metrics=['acc'])
model evaluation
h = history
plt.legend()
plt.show()
plt.legend()
plt.show()
67
Figure 27:Cell output 7
predictive sys
input_image = cv2.imread(input_image_path)
cv2_imshow(input_image)
input_image_scaled = input_image_resized/255
68
input_image_reshaped = np.reshape(input_image_scaled, [1,128,128,3])
input_prediction = model.predict(input_image_reshaped)
print(input_prediction)
input_pred_label = np.argmax(input_prediction)
print(input_pred_label)
if input_pred_label == 1:
else:
interface code
drive.mount('/content/drive/')
import gradio as gr
import cv2
import numpy as np
69
model =
keras.models.load_model('/content/drive/MyDrive/Project/mask_detection_m
odel.h5')
def predict_with_mask(image):
prediction = model.predict(image_reshaped)
label = np.argmax(prediction)
if label == 1:
else:
iface = gr.Interface(
fn=predict_with_mask,
inputs="image",
outputs="text",
title="Mask Detection",
allow_flagging=False,
flagging_dir="flagged_images/",
iface.launch()
70
Figure 29:Cell output 9
4.6 Results
We have completed our project with accuracy of 92.19% by using CNN model.
71
Figure 31:Result Output no. 2
72
CHAPTER 06-CONCLUSION AND FUTURE WORK
6.1 Conclusion
In conclusion, face mask detection systems have emerged as a valuable technology in
the context of public health and safety. These systems leverage computer vision
algorithms to identify whether individuals are wearing masks or not, helping enforce
mask-wearing policies and mitigate the spread of contagious diseases.The future scope
of face mask detection systems is promising. Advancements in accuracy, real-time
monitoring, and integration with other systems such as access control and surveillance
can enhance their effectiveness. Additionally, the ability to provide mask usage
analytics and adapt to new variants or future pandemics adds further value.By
incorporating face mask detection systems into various settings such as airports, train
stations, shopping malls, and schools, authorities can ensure compliance with mask-
wearing policies and respond promptly to violations. The integration of these systems
with mobile applications and wearable devices can also promote personal compliance
and provide valuable health-related information.Ultimately, face mask detection
systems have the potential to contribute significantly to public health initiatives,
improve safety in public spaces, and assist in containing the spread of infectious
diseases. As technology continues to advance, these systems will likely play an
essential role in safeguarding public health in the future.Face mask detection systems
have numerous applications and can be used in various settings to promote mask-
wearing compliance and enhance public health and safety. Face mask detection systems
can be deployed in public spaces such as airports, train stations, bus terminals, shopping
malls, and stadiums. By monitoring individuals entering these spaces, the system can
identify those not wearing masks and provide real-time alerts or deny access if
necessary. This helps enforce mask-wearing policies and reduces the risk of virus
transmission in crowded areas.Face mask detection systems can be utilized in hospitals,
clinics, and other healthcare settings. These systems can help ensure that both
healthcare providers and visitors adhere to mask-wearing guidelines, minimizing the
spread of infections within the facility. Additionally, the systems can be integrated with
access control systems to regulate entry and maintain a safe environment for patients
and staff.Face mask detection systems can be employed in schools, colleges, and
universities. By monitoring students, teachers, and visitors, the system can identify
individuals without masks and trigger appropriate actions, such as sending alerts to the
73
staff or denying access to classrooms. This helps create a safer learning environment
and reduces the risk of outbreaks.Many workplaces, such as offices, factories, and
construction sites, can benefit from face mask detection systems. These systems can
ensure that employees and visitors follow mask-wearing protocols, enhancing
workplace safety and minimizing the spread of infectious diseases within the
premises.Face mask detection systems can be integrated into public transportation
systems, including buses, trains, and subways. By monitoring passengers entering the
vehicles, the system can identify individuals without masks and alert the transportation
authorities or staff. This helps maintain a safer environment for both passengers and
employees.Face mask detection systems can aid law enforcement agencies in
monitoring compliance with mask-wearing regulations during public events, protests,
or demonstrations. By identifying individuals without masks, authorities can take
appropriate actions to enforce public health guidelines and maintain public safety.Face
mask detection systems can be implemented in retail stores, restaurants, and hotels to
ensure that customers and employees wear masks while inside the premises. This not
only promotes customer confidence but also protects the staff and other patrons from
potential virus transmission.These are just a few examples of how face mask detection
systems can be used to enhance public health and safety in various settings. As the
technology continues to evolve, the applications and impact of these systems are
expected to expand, contributing to a healthier and safer society.
74
malls, and schools. By constantly monitoring mask compliance, authorities can
respond quickly to any violations and take appropriate actions.
3. Integration with Access Control Systems: Face mask detection systems can be
integrated with access control systems to regulate entry into certain premises or
areas. For example, if a person is not wearing a mask, the system can deny
access to a building or trigger an alarm for security personnel to intervene. This
integration can help enforce mask-wearing policies effectively.
4. Mask Usage Analytics: Face mask detection systems can provide valuable data
on mask usage patterns and compliance rates. By analyzing this data, authorities
and organizations can gain insights into areas where compliance is low and take
targeted measures to improve it. This information can also be used for research
purposes, public health initiatives, and policy-making.
5. Integration with Surveillance Systems: Face mask detection systems can be
integrated with existing surveillance systems, such as CCTV cameras. This
integration can enable automated monitoring of mask compliance in various
public spaces. It can also assist in contact tracing efforts during outbreaks or in
identifying individuals who have violated mask-wearing protocols.
6. Mobile Applications and Wearable Devices: Face mask detection systems can
be adapted for use in mobile applications and wearable devices. These
technologies can provide individuals with personal feedback on their mask
usage, reminding them to wear masks in appropriate situations. They can also
help track personal compliance and provide health-related information and
guidelines.
7. Adaptation to New Variants or Future Pandemics: Face mask detection systems
can be updated and trained to recognize different types of masks, including new
designs or materials used in response to emerging variants or future pandemics.
This adaptability ensures that the system remains relevant and effective in
various circumstances.
8. Overall, the future scope of face mask detection systems is likely to encompass
advancements in accuracy, real-time monitoring, integration with access control
and surveillance systems, data analytics, mobile applications, wearable devices,
and adaptability to changing circumstances. These developments can
significantly contribute to public health efforts, improve safety in public spaces,
and aid in containing the spread of contagious diseases.
75
REFERENCES
1. https://keras.io/about/
2. https://www.tensorflow.org/learn
3. https://www.google.com/amp/s/www.geeksforgeeks.org/python-pillow-a-fork-
of-pil/amp/
4. https://scikit-learn.org/stable/
5. https://colab.research.google.com/
6. https://www.google.com/intl/en_in/drive/
7. https://en.m.wikipedia.org/wiki/COVID-19
8. https://www.google.com/amp/s/www.geeksforgeeks.org/convolutional-neural-
network-cnn-in-machine-learning/amp/
9. https://en.m.wikipedia.org/wiki/Artificial_intelligence
10. https://www.google.com/amp/s/www.geeksforgeeks.org/introduction-deep-
learning/amp/
11. https://en.m.wikipedia.org/wiki/Neural_network
12. https://opencv.org/
13. https://www.python.org/
14. https://www.investopedia.com/terms/n/neuralnetwork.asp
15. https://www.google.com/amp/s/www.geeksforgeeks.org/python-introduction-
matplotlib/amp/
16. https://gradio.app/
17. https://www.w3schools.com/python/matplotlib_pyplot.asp
18. https://www.techtarget.com/searchenterpriseai/definition/deep-learning-deep-
neural-network
19. https://www.analyticsvidhya.com/blog/2022/01/introduction-to-neural-
networks/
20. https://www.google.com/amp/s/www.geeksforgeeks.org/opencv-
overview/amp/
76