Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

create binary image #41

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions mask_the_face.py
Original file line number Diff line number Diff line change
Expand Up @@ -123,8 +123,11 @@
image_path = path + "/" + f

write_path = path + "_masked"
bin_path = path + "_bin"
if not os.path.isdir(write_path):
os.makedirs(write_path)
if not os.path.isdir(bin_path):
os.makedirs(bin_path)

if is_image(image_path):
# Proceed if file is image
Expand All @@ -146,17 +149,31 @@
+ "."
+ split_path[1]
)
w_path2 = (
bin_path
+ "/"
+ split_path[0]
+ "_"
+ mask[i]
+ "."
+ split_path[1]
)
img = masked_image[i]
cv2.imwrite(w_path, img)
img2 = mask_binary_array[i]
cv2.imwrite(w_path2, img2)

print_orderly("Masking image directories", 60)

# Process directories withing the path provided
for d in tqdm(dirs):
dir_path = args.path + "/" + d
dir_write_path = args.write_path + "/" + d
bin_path = args.write_path + "_bin/" + d
if not os.path.isdir(dir_write_path):
os.makedirs(dir_write_path)
if not os.path.isdir(bin_path):
os.makedirs(bin_path)
_, _, files = os.walk(dir_path).__next__()

# Process each files within subdirectory
Expand All @@ -182,10 +199,21 @@
+ "."
+ split_path[1]
)
w_path2 = (
bin_path
+ "/"
+ split_path[0]
+ "_"
+ mask[i]
+ "."
+ split_path[1]
)
w_path_original = write_path + "/" + f
img = masked_image[i]
img2 = mask_binary[i]
# Write the masked image
cv2.imwrite(w_path, img)
cv2.imwrite(w_path2, img2)
if args.write_original_image:
# Write the original image
cv2.imwrite(w_path_original, original_image)
Expand Down
41 changes: 4 additions & 37 deletions readme.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
# MaskTheFace - Convert face dataset to masked dataset
![cover_photo](images/MaskTheFace.png)

## What is MaskTheFace?
MaskTheFace is computer vision-based script to mask faces in images. It uses a dlib based face landmarks detector to identify the face tilt and six key features of the face necessary for applying mask. Based on the face tilt, corresponding mask template is selected from the library of mask. The template mask is then transformed based on the six key features to fit perfectly on the face. The complete block diagram can be seen below. MaskTheFace provides a number of masks to select from. It is difficult to collect mask dataset under various conditions. MaskTheFace can be used to convert any existing face dataset to masked-face dataset. MaskTheFace identifies all the faces within an image, and applies the user selected masks to them taking into account various limitations such as face angle, mask fit, lighting conditions etc. A single image, or entire directory of images can be used as input to code.
![block_diagram](images/block_diag.png)
Expand Down Expand Up @@ -30,6 +29,9 @@ python mask_the_face.py --path <path-to-file-or-dir> --mask_type <type-of-mask>

# Example
python mask_the_face.py --path 'data/office.jpg' --mask_type 'N95' --verbose --write_original_image

# !REAL directory Example!
python mask_the_face.py --path ./img_align_celeba --color white --verbose
```
![cover_photo](images/run.png)
### Arguments
Expand Down Expand Up @@ -130,41 +132,6 @@ The downloaded dataset folder contains

---

# Example usage

## 1. Face recognition with masks
Face recognition trained to usual face images has proven to give good accuracy. In the recent ongoing outbreak of Covid19, people have been advised to use face masks. With the majority of people using face masks, the face recognition system fails to perform. Due to limited mask images, there is not enough masked data available to train a new system. MaskTheFace can be used to create masked data set from an unmasked dataset which is then used to either fine-tune an existing or train a new face recognition system.

### Example
The paper below uses MaskTheFace for the application of masked face recognition and reports an increase of ∼38% in the true positive rate for the Facenet system. They also test the accuracy of the re-trained system on the MFR2 dataset and report similar accuracy.
[https://arxiv.org/pdf/2008.11104.pdf](https://arxiv.org/pdf/2008.11104.pdf)



## 2. Monitoring if people are using masks
MaskTheFace generated datasets can be used to monitor if people are using face masks.

### Example
The detector above was trained on 2000 images (1000 mask, 1000 without mask) from the VGGface2 dataset. The masked images contained 4 different types of masks. A VGG16 network was trained on these images which achieved a 98.9% accuracy on the test dataset.

![cover_photo](images/mask_no_mask.png)

## 3. Classify masks
MaskTheFace generated dataset can be used to classify among masks using a deep network.

## Citation
If you find this repository useful, please use following citation
```
@misc{anwar2020masked,
title={Masked Face Recognition for Secure Authentication},
author={Aqeel Anwar and Arijit Raychowdhury},
year={2020},
eprint={2008.11104},
archivePrefix={arXiv},
primaryClass={cs.CV}
}
```


## License
This project is licensed under the MIT License - see the [LICENSE.md](LICENSE) file for details