최성환

Face Mask Detection MobileNetV2

Showing 1000 changed files with 349 additions and 0 deletions

Too many changes to show.

To preserve performance only 1000 of 1000+ files are displayed.

1 +# Python virtual environments
2 +venv
1 +# Contributor Covenant Code of Conduct
2 +
3 +## Our Pledge
4 +
5 +In the interest of fostering an open and welcoming environment, we as
6 +contributors and maintainers pledge to making participation in our project and
7 +our community a harassment-free experience for everyone, regardless of age, body
8 +size, disability, ethnicity, sex characteristics, gender identity and expression,
9 +level of experience, education, socio-economic status, nationality, personal
10 +appearance, race, religion, or sexual identity and orientation.
11 +
12 +## Our Standards
13 +
14 +Examples of behavior that contributes to creating a positive environment
15 +include:
16 +
17 +* Using welcoming and inclusive language
18 +* Being respectful of differing viewpoints and experiences
19 +* Gracefully accepting constructive criticism
20 +* Focusing on what is best for the community
21 +* Showing empathy towards other community members
22 +
23 +Examples of unacceptable behavior by participants include:
24 +
25 +* The use of sexualized language or imagery and unwelcome sexual attention or
26 + advances
27 +* Trolling, insulting/derogatory comments, and personal or political attacks
28 +* Public or private harassment
29 +* Publishing others' private information, such as a physical or electronic
30 + address, without explicit permission
31 +* Other conduct which could reasonably be considered inappropriate in a
32 + professional setting
33 +
34 +## Our Responsibilities
35 +
36 +Project maintainers are responsible for clarifying the standards of acceptable
37 +behavior and are expected to take appropriate and fair corrective action in
38 +response to any instances of unacceptable behavior.
39 +
40 +Project maintainers have the right and responsibility to remove, edit, or
41 +reject comments, commits, code, wiki edits, issues, and other contributions
42 +that are not aligned to this Code of Conduct, or to ban temporarily or
43 +permanently any contributor for other behaviors that they deem inappropriate,
44 +threatening, offensive, or harmful.
45 +
46 +## Scope
47 +
48 +This Code of Conduct applies both within project spaces and in public spaces
49 +when an individual is representing the project or its community. Examples of
50 +representing a project or community include using an official project e-mail
51 +address, posting via an official social media account, or acting as an appointed
52 +representative at an online or offline event. Representation of a project may be
53 +further defined and clarified by project maintainers.
54 +
55 +## Enforcement
56 +
57 +Instances of abusive, harassing, or otherwise unacceptable behavior may be
58 +reported by contacting the project team at chandrikadeb7@gmail.com. All
59 +complaints will be reviewed and investigated and will result in a response that
60 +is deemed necessary and appropriate to the circumstances. The project team is
61 +obligated to maintain confidentiality with regard to the reporter of an incident.
62 +Further details of specific enforcement policies may be posted separately.
63 +
64 +Project maintainers who do not follow or enforce the Code of Conduct in good
65 +faith may face temporary or permanent repercussions as determined by other
66 +members of the project's leadership.
67 +
68 +## Attribution
69 +
70 +This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
71 +available at https://www.contributor-covenant.org/version/1/4/code-of-conduct.html
72 +
73 +[homepage]: https://www.contributor-covenant.org
74 +
75 +For answers to common questions about this code of conduct, see
76 +https://www.contributor-covenant.org/faq
1 +MIT License
2 +
3 +Copyright (c) 2020 Chandrika Deb
4 +
5 +Permission is hereby granted, free of charge, to any person obtaining a copy
6 +of this software and associated documentation files (the "Software"), to deal
7 +in the Software without restriction, including without limitation the rights
8 +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 +copies of the Software, and to permit persons to whom the Software is
10 +furnished to do so, subject to the following conditions:
11 +
12 +The above copyright notice and this permission notice shall be included in all
13 +copies or substantial portions of the Software.
14 +
15 +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21 +SOFTWARE.
1 +<h1 align="center">Face Mask Detection</h1>
2 +
3 +<div align= "center">
4 + <h4>Face Mask Detection system built with OpenCV, Keras/TensorFlow using Deep Learning and Computer Vision concepts in order to detect face masks in static images as well as in real-time video streams.</h4>
5 +</div>
6 +
7 +&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
8 +![Python](https://img.shields.io/badge/python-v3.6+-blue.svg)
9 +[![contributions welcome](https://img.shields.io/badge/contributions-welcome-brightgreen.svg?style=flat)](https://github.com/chandrikadeb7/Face-Mask-Detection/issues)
10 +[![Forks](https://img.shields.io/github/forks/chandrikadeb7/Face-Mask-Detection.svg?logo=github)](https://github.com/chandrikadeb7/Face-Mask-Detection/network/members)
11 +[![Stargazers](https://img.shields.io/github/stars/chandrikadeb7/Face-Mask-Detection.svg?logo=github)](https://github.com/chandrikadeb7/Face-Mask-Detection/stargazers)
12 +[![Issues](https://img.shields.io/github/issues/chandrikadeb7/Face-Mask-Detection.svg?logo=github)](https://github.com/chandrikadeb7/Face-Mask-Detection/issues)
13 +[![MIT License](https://img.shields.io/github/license/chandrikadeb7/Face-Mask-Detection.svg?style=flat-square)](https://github.com/chandrikadeb7/Face-Mask-Detection/blob/master/LICENSE)
14 +[![LinkedIn](https://img.shields.io/badge/-LinkedIn-black.svg?style=flat-square&logo=linkedin&colorB=555)](https://www.linkedin.com/in/chandrika-deb/)
15 +
16 +
17 +&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
18 +![Live Demo](https://github.com/chandrikadeb7/Face-Mask-Detection/blob/master/Readme_images/Demo.gif)
19 +
20 +
21 +
22 +## :innocent: Motivation
23 +In the present scenario due to Covid-19, there is no efficient face mask detection applications which are now in high demand for transportation means, densely populated areas, residential districts, large-scale manufacturers and other enterprises to ensure safety. Also, the absence of large datasets of __‘with_mask’__ images has made this task more cumbersome and challenging.
24 +
25 +
26 +## :hourglass: Project Demo
27 +:movie_camera: [YouTube Demo Link](https://www.youtube.com/watch?v=AAkNyZlUae0)
28 +
29 +:computer: [Dev Link](https://dev.to/chandrikadeb7/face-mask-detection-my-major-project-3fj3)
30 +
31 +[![Already deployed version](https://raw.githubusercontent.com/vasantvohra/TrashNet/master/hr.svg)](https://face-mask--detection-app.herokuapp.com/)
32 +
33 +
34 +
35 +<p align="center"><img src="https://github.com/chandrikadeb7/Face-Mask-Detection/blob/master/Readme_images/Screen%20Shot%202020-05-14%20at%208.49.06%20PM.png" width="700" height="400"></p>
36 +
37 +
38 +## :warning: TechStack/framework used
39 +
40 +- [OpenCV](https://opencv.org/)
41 +- [Caffe-based face detector](https://caffe.berkeleyvision.org/)
42 +- [Keras](https://keras.io/)
43 +- [TensorFlow](https://www.tensorflow.org/)
44 +- [MobileNetV2](https://arxiv.org/abs/1801.04381)
45 +
46 +## :star: Features
47 +Our face mask detector didn't use any morphed masked images dataset. The model is accurate, and since we used the MobileNetV2 architecture, it’s also computationally efficient and thus making it easier to deploy the model to embedded systems (Raspberry Pi, Google Coral, etc.).
48 +
49 +This system can therefore be used in real-time applications which require face-mask detection for safety purposes due to the outbreak of Covid-19. This project can be integrated with embedded systems for application in airports, railway stations, offices, schools, and public places to ensure that public safety guidelines are followed.
50 +
51 +## :file_folder: Dataset
52 +The dataset used can be downloaded here - [Click to Download](https://drive.google.com/drive/folders/1XDte2DL2Mf_hw4NsmGst7QtYoU7sMBVG?usp=sharing)
53 +
54 +This dataset consists of __3835 images__ belonging to two classes:
55 +* __with_mask: 1916 images__
56 +* __without_mask: 1919 images__
57 +
58 +The images used were real images of faces wearing masks. The images were collected from the following sources:
59 +
60 +* __Bing Search API__ ([See Python script](https://github.com/chandrikadeb7/Face-Mask-Detection/blob/master/search.py))
61 +* __Kaggle datasets__
62 +* __RMFD dataset__ ([See here](https://github.com/X-zhangyang/Real-World-Masked-Face-Dataset))
63 +
64 +## :key: Prerequisites
65 +
66 +All the dependencies and required libraries are included in the file <code>requirements.txt</code> [See here](https://github.com/chandrikadeb7/Face-Mask-Detection/blob/master/requirements.txt)
67 +
68 +## 🚀&nbsp; Installation
69 +1. Clone the repo
70 +```
71 +$ git clone https://github.com/chandrikadeb7/Face-Mask-Detection.git
72 +```
73 +
74 +2. Change your directory to the cloned repo and create a Python virtual environment named 'test'
75 +```
76 +$ mkvirtualenv test
77 +```
78 +
79 +3. Now, run the following command in your Terminal/Command Prompt to install the libraries required
80 +```
81 +$ pip3 install -r requirements.txt
82 +```
83 +
84 +## :bulb: Working
85 +
86 +1. Open terminal. Go into the cloned project directory and type the following command:
87 +```
88 +$ python3 train_mask_detector.py --dataset dataset
89 +```
90 +
91 +2. To detect face masks in an image type the following command:
92 +```
93 +$ python3 detect_mask_image.py --image images/pic1.jpeg
94 +```
95 +
96 +3. To detect face masks in real-time video streams type the following command:
97 +```
98 +$ python3 detect_mask_video.py
99 +```
100 +## :key: Results
101 +
102 +#### Our model gave 93% accuracy for Face Mask Detection after training via <code>tensorflow-gpu==2.0.0</code>
103 +
104 +![](https://github.com/chandrikadeb7/Face-Mask-Detection/blob/master/Readme_images/Screenshot%202020-06-01%20at%209.48.27%20PM.png)
105 +
106 +#### We got the following accuracy/loss training curve plot
107 +![](https://github.com/chandrikadeb7/Face-Mask-Detection/blob/master/plot.png)
108 +
109 +## Streamlit app
110 +
111 +Face Mask Detector webapp using Tensorflow & Streamlit
112 +
113 +command
114 +```
115 +$ streamlit run app.py
116 +```
117 +## Images
118 +
119 +<p align="center">
120 + <img src="Readme_images/1.PNG">
121 +</p>
122 +<p align="center">Upload Images</p>
123 +
124 +<p align="center">
125 + <img src="Readme_images/2.PNG">
126 +</p>
127 +<p align="center">Results</p>
128 +
129 +## :clap: And it's done!
130 +Feel free to mail me for any doubts/query
131 +:email: chandrikadeb7@gmail.com
132 +
133 +## :handshake: Contribution
134 +Feel free to **file a new issue** with a respective title and description on the the [Face-Mask-Detection](https://github.com/chandrikadeb7/Face-Mask-Detection/issues) repository. If you already found a solution to your problem, **I would love to review your pull request**!
135 +
136 +## :heart: Owner
137 +Made with :heart:&nbsp; by [Chandrika Deb](https://github.com/chandrikadeb7)
138 +
139 +## :+1: Credits
140 +* [https://www.pyimagesearch.com/](https://www.pyimagesearch.com/)
141 +* [https://www.tensorflow.org/tutorials/images/transfer_learning](https://www.tensorflow.org/tutorials/images/transfer_learning)
142 +
143 +## :eyes: License
144 +MIT © [Chandrika Deb](https://github.com/chandrikadeb7/Face-Mask-Detection/blob/master/LICENSE)
1 +import streamlit as st
2 +from PIL import Image, ImageEnhance
3 +import numpy as np
4 +import cv2
5 +import os
6 +from tensorflow.keras.applications.mobilenet_v2 import preprocess_input
7 +from tensorflow.keras.preprocessing.image import img_to_array
8 +from tensorflow.keras.models import load_model
9 +import detect_mask_image
10 +
11 +
12 +def mask_image():
13 + global RGB_img
14 + # load our serialized face detector model from disk
15 + print("[INFO] loading face detector model...")
16 + prototxtPath = os.path.sep.join(["face_detector", "deploy.prototxt"])
17 + weightsPath = os.path.sep.join(["face_detector",
18 + "res10_300x300_ssd_iter_140000.caffemodel"])
19 + net = cv2.dnn.readNet(prototxtPath, weightsPath)
20 +
21 + # load the face mask detector model from disk
22 + print("[INFO] loading face mask detector model...")
23 + model = load_model("mask_detector.model")
24 +
25 + # load the input image from disk and grab the image spatial
26 + # dimensions
27 + image = cv2.imread("./images/out.jpg")
28 + (h, w) = image.shape[:2]
29 +
30 + # construct a blob from the image
31 + blob = cv2.dnn.blobFromImage(image, 1.0, (300, 300),
32 + (104.0, 177.0, 123.0))
33 +
34 + # pass the blob through the network and obtain the face detections
35 + print("[INFO] computing face detections...")
36 + net.setInput(blob)
37 + detections = net.forward()
38 +
39 + # loop over the detections
40 + for i in range(0, detections.shape[2]):
41 + # extract the confidence (i.e., probability) associated with
42 + # the detection
43 + confidence = detections[0, 0, i, 2]
44 +
45 + # filter out weak detections by ensuring the confidence is
46 + # greater than the minimum confidence
47 + if confidence > 0.5:
48 + # compute the (x, y)-coordinates of the bounding box for
49 + # the object
50 + box = detections[0, 0, i, 3:7] * np.array([w, h, w, h])
51 + (startX, startY, endX, endY) = box.astype("int")
52 +
53 + # ensure the bounding boxes fall within the dimensions of
54 + # the frame
55 + (startX, startY) = (max(0, startX), max(0, startY))
56 + (endX, endY) = (min(w - 1, endX), min(h - 1, endY))
57 +
58 + # extract the face ROI, convert it from BGR to RGB channel
59 + # ordering, resize it to 224x224, and preprocess it
60 + face = image[startY:endY, startX:endX]
61 + face = cv2.cvtColor(face, cv2.COLOR_BGR2RGB)
62 + face = cv2.resize(face, (224, 224))
63 + face = img_to_array(face)
64 + face = preprocess_input(face)
65 + face = np.expand_dims(face, axis=0)
66 +
67 + # pass the face through the model to determine if the face
68 + # has a mask or not
69 + (mask, withoutMask) = model.predict(face)[0]
70 +
71 + # determine the class label and color we'll use to draw
72 + # the bounding box and text
73 + label = "Mask" if mask > withoutMask else "No Mask"
74 + color = (0, 255, 0) if label == "Mask" else (0, 0, 255)
75 +
76 + # include the probability in the label
77 + label = "{}: {:.2f}%".format(label, max(mask, withoutMask) * 100)
78 +
79 + # display the label and bounding box rectangle on the output
80 + # frame
81 + cv2.putText(image, label, (startX, startY - 10),
82 + cv2.FONT_HERSHEY_SIMPLEX, 0.45, color, 2)
83 + cv2.rectangle(image, (startX, startY), (endX, endY), color, 2)
84 + RGB_img = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
85 +mask_image()
86 +
87 +def mask_detection():
88 + st.title("Face mask detection")
89 + activities = ["Image", "Webcam"]
90 + st.set_option('deprecation.showfileUploaderEncoding', False)
91 + choice = st.sidebar.selectbox("Mask Detection on?", activities)
92 +
93 + if choice == 'Image':
94 + st.subheader("Detection on image")
95 + image_file = st.file_uploader("Upload Image", type=['jpg']) # upload image
96 + if image_file is not None:
97 + our_image = Image.open(image_file) # making compatible to PIL
98 + im = our_image.save('./images/out.jpg')
99 + saved_image = st.image(image_file, caption='image uploaded successfully', use_column_width=True)
100 + if st.button('Process'):
101 + st.image(RGB_img, use_column_width=True)
102 +
103 + if choice == 'Webcam':
104 + st.subheader("Detection on webcam")
105 + st.text("This feature will be avilable soon")
106 +mask_detection()