Face Mask Detection
Face Mask Detection
Face Mask Detection
FIRST SYNOPSIS ON
‘PYTHON WITH MACHINE LEARNING’
FOR THE PARTIAL FULFILLMENT OF THE
REQUIREMENT FOR THE AWARD OF THE DEGREE
OF
‘BACHELOR OF TECHNOLOGY
COMPUTER SCIENCE AND ENGINEERING
SESSION
2017-2021’
PROJECT REPORT ON
• INTRODUCTION TO PYTHON
• PYTHON FEATURES
➢ Project Description
• PACKAGES USED
➢ cv2
➢ argparse
➢ numpy
➢ imutils
➢ tensorflow/keras
7. Screenshots
COMPANY PROFILE
EXCELLENCE TECHNOLOGY ( ET) is India based leading strategic IT Company offering integrated IT
solutions with the vision to provide Excellence in software solution. We at
EXCELLENCE 4 TECHNOLOGY bring innovative ideas and
PHILOSOPHY
✓ To equip a local team with a strong knowledge of international best practices and
international expert support to provide practical advisories in the best interests of
our clients
SERVICES AVAILABLE:
With the EXCELLENCE TECHNOLOGY experience the incredible services such as agile software
development and the problems related to outsourcing. We comprise of the team of
experienced and professional members who with their skills efficiently get the job done and
innovatively help you to transform your ideas into the successful business.
COMPANY’S CLIENTS
INTRODUCTION TO PYTHON
• Overview
• Evolution of technology
Python was developed by Guido van Rossum in the late eighties and early nineties at the
National Research Institute for Mathematics and Computer Science in the Netherlands.
Python is derived from many other languages, including ABC, Modula-3, C, C++, Algol-68,
SmallTalk, and Unix shell and other scripting languages.
Python is copyrighted. Like Perl, Python source code is now available under the GNU General
Public License (GPL).
Python is now maintained by a core development team at the institute, although Guido van
Rossum still holds a vital role in directing its progress.
• Python Features:
Python's features include −
• Easy-to-learn − Python has few keywords, simple structure, and a clearly defined
syntax. This allows the student to pick up the language quickly.
• Easy-to-read − Python code is more clearly defined and visible to the eyes.
• Easy-to-maintain − Python's source code is fairly easy-to-maintain.
• A broad standard library − Python's bulk of the library is very portable and cross-
platform compatible on UNIX, Windows, and Macintosh.
• Interactive Mode − Python has support for an interactive mode which allows
interactive testing and debugging of snippets of code.
• Portable − Python can run on a wide variety of hardware platforms and has the same
interface on all platforms.
• Extendable − You can add low-level modules to the Python interpreter. These
modules enable programmers to add to or customize their tools to be more efficient.
• GUI Programming − Python supports GUI applications that can be created and ported
to many system calls, libraries and windows systems, such as Windows MFC,
Macintosh, and the X Window system of Unix.
• Scalable − Python provides a better structure and support for large programs than
shell scripting.
Apart from the above-mentioned features, Python has a big list of good features, few are
listed below −
• It provides very high-level dynamic data types and supports dynamic type checking.
INTRODUCTION TO PROJECT
Name of the Project: Face Mask Detection
Objective of the Project
The corona virus COVID-19 pandemic is causing a global health crisis so the effective
protection methods are wearing a face mask in public areas according to the World Health
Organization (WHO). The COVID-19 pandemic forced government’s across the world to
impose lockdowns to prevent virus transmissions. Reports indicate that wearing facemasks
while at work clearly reduces the risk of transmission. An efficient and economic approach of
using AI to create a safe environment in a manufacturing setup. A hybrid model using deep
and classical machine learning for face mask detection will be presented. A face mask
detection dataset consists of with mask and without mask images , we are going to use
OpenCV to do real-time face detection from a live stream via our webcam. We will use the
dataset to build a COVID-19 face mask detector with computer vision using Python, OpenCV,
and Tensor Flow and Keras. Our goal is to identify whether the person on image/video
stream is wearing a face mask or not with the help of computer vision and deep learning.
The Dataset
For this python project, we’ll use the Adience dataset; the dataset is available in the
public domai. This dataset serves as a benchmark for face photos and is inclusive of
various real-world imaging conditions like noise, lighting, pose, and appearance. The
images have been collected from Flickr albums and distributed under the Creative
Commons (CC) license. It has a total of 6,000 photos of 3,000 subjects in mask
accuracy and is about 1GB in size. The models we will use have been trained on this
dataset.
In this python project, we implemented a CNN to detect mask and accuracy of mask from a single
picture of a face.
Prerequisites
You’ll need to install OpenCV (cv2) to be able to run this project. You can do this with pip-
Other packages you’ll be needing are math and argparse, but those come as part of the standard
Python library.
What is OpenCV?
OpenCV is short for Open Source Computer Vision. Intuitively by the name, it is an open-source
Computer Vision and Machine Learning library. This library is capable of processing real-time image
and video while also boasting analytical capabilities. It supports the Deep Learning frameworks
TensorFlow, Caffe, and PyTorch.
A Convolutional Neural Network (ConvNet/CNN) is a Deep Learning algorithm which can take in an
input image, assign importance (learnable weights and biases) to various aspects/objects in the
image and be able to differentiate one from the other. The pre-processing required in a ConvNet is
much lower as compared to other classification algorithms. While in primitive methods filters are
hand-engineered, with enough training, ConvNets have the ability to learn these
filters/characteristics. The kernel is just like a small window sliding over the large window in order to
extract the spatial features and in the end, we get feature maps.
The architecture of a ConvNet is analogous to that of the connectivity pattern of
Neurons in the Human Brain and was inspired by the organization of the Visual Cortex.
Individual neurons respond to stimuli only in a restricted region of the visual field known as
the Receptive Field. A collection of such fields overlaps to cover the entire visual area.
Deep learning is an artificial intelligence (AI) function that imitates the workings of the
human brain in processing data and creating patterns for use in decision making. Deep
learning is a subset of machine learning in artificial intelligence that has networks capable
of learning unsupervised from data that is unstructured or unlabelled. Also known as deep
neural learning or deep neural network.
Deep learning has evolved hand-in-hand with the digital era, which has brought about an
explosion of data in all forms and from every region of the world. This data, known simply
as big data, is drawn from sources like social media, internet search engines, e-commerce
platforms, and online cinemas, among others. This enormous amount of data is readily
accessible and can be shared through fintech applications like cloud computing.
However, the data, which normally is unstructured, is so vast that it could take decades
for humans to comprehend it and extract relevant information.
2. The Dataset:
For this python project, we’ll use the Adience dataset; the dataset is available in the
public domain. This dataset serves as a benchmark for face photos and is inclusive of
various real-world imaging conditions like noise, lighting, pose, and appearance. The
images have been collected from Flickr albums and distributed under the Creative
Commons (CC) license. It has a total of 26,580 photos of 2,284 subjects in eight age
ranges (as mentioned above) and is about 1GB in size. The models we will use have
been trained on this dataset.
3. Argparse :
The argparse module makes it easy to write user-friendly command-line interfaces. The program
defines what arguments it requires, and argparse will figure out how to parse those out of sys.argv.
The argparse module also automatically generates help and usage messages and issues errors when
users give the program invalid arguments.
Changed in version 3.8: In previous versions, allow_abbrev also disabled grouping of short
flags such as -vv to mean -v -v.
Numeric, the ancestor of NumPy, was developed by Jim Hugunin. Another package
Numarray was also developed, having some additional functionalities. In 2005, Travis
Oliphant created NumPy package by incorporating the features of Numarray into Numeric
package. There are many contributors to this open source project.
NumPy is often used along with packages like SciPy (Scientific Python) and Mat−plotlib
(plotting library). This combination is widely used as a replacement for MatLab, a popular
platform for technical computing. However, Python alternative to MatLab is now seen as a
more modern and complete programming language.
5. Matplot: Matplot library is a python library used to create 2D graphs and plots by using
python scripts. It has a module named pyplot which makes things easy for plotting by providing
feature to control line styles, font properties, formatting axes etc. It supports a very wide variety
of graphs and plots namely - histogram, bar charts, power spectra, error charts etc. It is used
along with NumPy to provide an environment that is an effective open source alternative for
MatLab. It can also be used with graphics toolkits like PyQt and wxPython.
Types of Plots:
There are various plots which can be created using python matplotlib.
There are several toolkits which are available that extend python matplotlib functionality. Some
of them are separate downloads, others can be shipped with the matplotlib source code but
have external dependencies.
Basemap: It is a map plotting toolkit with various map projections, coastlines and political
boundaries.
Excel tools: Matplotlib provides utilities for exchanging data with Microsoft Excel.
Natgrid: It is an interface to the natgrid library for irregular gridding of the spaced data.
The most important object defined in NumPy is an N-dimensional array type called Ndarray. It
describes the collection of items of the same type. Items in the collection can be accessed using a
zero-based index.
Every item in an Ndarray takes the same size of block in the memory. Each element in Ndarray is an
object of data-type object (called dtype).
Fig: Relationship between ndarray, data type object (dtype) and array scalar type
An instance of ndarray class can be constructed by different array creation routines described later
in the tutorial. The basic ndarray is created using an array function in NumPy as follows:
NumPy. Array
It creates an ndarray from any object exposing array interface, or from any method that returns an
array.
NumPy. Array (object, dtype = None, copy = True, order = None, subok = False, ndmin = 0)
Imutils:
Before we continue to the code we need install imutils.
Imutils are a series of convenience functions to make basic
image processing functions such as translation, rotation,
resizing, skeletonization, and displaying Matplotlib images
easier with OpenCV and both Python 2.7 and Python 3.
For installing:
matplotlib.pyplot is a collection of command style functions that make matplotlib work like
MATLAB. Each pyplot function makes some change to a figure: e.g., creates a figure, creates a
plotting area in a figure, plots some lines in a plotting area, decorates the plot with labels, etc.
6. Pandas: Pandas is an opensource Python package that is most widely used for data
science/data analysis and machine learning tasks. It is built on top of another package
named Numpy, which provides support for multi-dimensional arrays. As one of the most
popular data wrangling packages, Pandas works well with many other data science modules
inside the Python ecosystem, and is typically included in every Python distribution, from
those that come with your operating system to commercial vendor distributions like Active
State’s ActivePython.
• Fast and efficient Data Frame object with default and customized indexing.
• Tools for loading data into in-memory data objects from different file formats.
• Data alignment and integrated handling of missing data.
• Reshaping and pivoting of date sets.
• Label-based slicing, indexing and subsetting of large data sets.
• Columns from a data structure can be deleted or inserted.
• Group by data for aggregation and transformations.
• High performance merging and joining of data.
• Time Series functionality.
• Series
• Data Frame
• Panel
These data structures are built on top of Numpy array, which means they are fast.
The best way to think of these data structures is that the higher dimensional data structure is a
container of its lower dimensional data structure. For example, DataFrame is a container of Series,
Panel is a container of DataFrame.
Dictionary of Series can be passed to form a DataFrame. The resultant index is the union of all the
series indexes passed.
Example:
import pandas as pd
tensorflow/keras:
o KERAS:
Many users and data scientists, us included, like using Keras because it makes TensorFlow
much easier to navigate—which means you’re far less prone to make models that offer the
wrong conclusions.
Keras builds and trains neural networks, but it is user friendly and modular, so you can
experiment more easily with deep neural networks. Keras is a great option for anything from
fast prototyping to state-of-the-art research to production. The key advantages of using Keras,
particularly over TensorFlow, include:
Ease of use. The simple, consistent UX in Keras is optimized for use cases, so you
get clear, actionable feedback for most errors.
Modular composition. Keras models connect configurable building blocks, with few
restrictions.
Highly flexible and extendable. You can write custom blocks for new research and
create new layers, loss functions, metrics, and whole models.
So here, we use Keras because it offers something unique in machine learning i.e
single API that works across several ML frameworks to make that work easier.
From there, we’ll discuss our deep learning-based mask detection model
and then learn how to use the model for both:
HOG + Linear SVM models are more accurate than Haar cascades
but are slower. They also aren’t as tolerant with occlusion (i.e., not all
of the face visible) or viewpoint changes (i.e., different views of the
face)
Deep learning-based face detectors are the most robust and will give
you the best accuracy, but require even more computational
resources than both Haar cascades and HOG + Linear SVMs
When choosing a face detector for your application, take the time to
consider your project requirements — is speed or accuracy more important
for your use case? I also recommend running a few experiments with each
of the face detectors so you can let the empirical results guide your
decisions.
# USAGE
# python detect_mask_video.py
import numpy as np
import argparse
import imutils
import time
import cv2
import os
# from it
(h, w) = frame.shape[:2]
# pass the blob through the network and obtain the face detections
faceNet.setInput(blob)
detections = faceNet.forward()
faces = []
locs = []
preds = []
# the detection
confidence = detections[0, 0, i, 2]
# the object
# the frame
face = img_to_array(face)
face = preprocess_input(face)
# lists
faces.append(face)
if len(faces) > 0:
# locations
ap = argparse.ArgumentParser()
default="face_detector",
help="path to face detector model directory")
default="mask_detector.model",
args = vars(ap.parse_args())
weightsPath = os.path.sep.join([args["face"],
"res10_300x300_ssd_iter_140000.caffemodel"])
maskNet = load_model(args["model"])
# initialize the video stream and allow the camera sensor to warm up
vs = VideoStream(src=0).start()
time.sleep(2.0)
# grab the frame from the threaded video stream and resize it
frame = vs.read()
# locations
# frame
cv2.imshow("Frame", frame)
if key == ord("q"):
break
# do a bit of cleanup
cv2.destroyAllWindows()
vs.stop()
Screenshots:
Conclusion
This project presents a system for a smart city to reduce the spread of
coronavirus by informing the authority about the person who is not wearing
a facial mask that is a precautionary measure of COVID-19. The motive of
the work comes from the people disobeying the rules that are mandatory to
stop the spread of coronavirus. The system contains a face mask detection
architecture where a deep learning algorithm is used to detect the mask on
the face. To train the model, labeled image data are used where the images
were facial images with masks and without a mask. The proposed system
detects a face mask with an accuracy of 98.7%. The decision of the
classification network is transferred to the corresponding authority. The
system proposed in this study will act as a valuable tool to strictly impose
the use of a facial mask in public places for all people.
FUTURE SCOPE
The developed system faces difficulties in classifying faces covered by hands since it
almost looks like the person wearing a mask. While any person without a face mask is
traveling on any vehicle, the system cannot locate that person correctly. For a very
densely populated area, distinguishing the face of each person is very difficult. For this
type of scenario, identifying people without face mask would be very difficult for our
proposed system. In order to get the best result out of this system, the city must have a
large number of CCTV cameras to monitor the whole city as well as dedicated manpower
to enforce proper laws on the violators. Since the information about the violator is sent
via SMS, the system fails when there is a problem in the network.
The proposed system mainly detects the face mask and informs the corresponding
authority with the location of a person not wearing a mask. Based on this, the authority
has to send their personnel to find out the person and take necessary actions. But this
manual scenario can be automated by using drones and robot technology [22], [23] to
take action instantly. Furthermore, people near to the person not wearing a mask may be
alerted by an alarm signal on that location, and displaying the violators face in a LED
screen to maintain a safe distance from the person would be a further study.