Skip to main content

Facemask detection

It could be confusing, but the model in this library perform classifications of the images. It takes image as an input and outputs probability of person in the image wearing a mask.

Hence in order to get expected results the model should be combined with face detector, for example from https://github.com/ternaus/retinaface.

Example on how to combine face detector with mask detector

https://habrastorage.org/webt/b_/ja/ww/b_jawwxndpkdl2pjlxlcxvars6m.png

Installation

pip install -U facemask_detection

Use

import albumentations as A
import torch
from facemask_detection.pre_trained_models import get_model

model = get_model("tf_efficientnet_b0_ns_2020-07-29")
model.eval()

transform = A.Compose([A.SmallestMaxSize(max_size=256, p=1),
                       A.CenterCrop(height=224, width=224, p=1),
                       A.Normalize(p=1)])

image = <numpy array with the shape (height, width, 3)>

transformed_image = transform(image=image)['image']

input = torch.from_numpy(np.transpose(transformed_image, (2, 0, 1))).unsqueeze(0)

print("Probability of the mask on the face = ", model(input)[0].item())
  • Jupyter notebook with the example: Open In Colab
  • Jupyter notebook with the example on how to combine face detector with mask detector: Open In Colab

Train set

Train dataset was composed from the data:

No mask:

Mask:

Trainining

Define config, similar to facemask_detection_configs/2020-07-29.yaml.

Run

python facemask_detection/train.py -c <config>

Inference

python -m torch.distributed.launch --nproc_per_node=1 facemask_detection/inference.py -h
usage: inference.py [-h] -i INPUT_PATH -c CONFIG_PATH -o OUTPUT_PATH
                    [-b BATCH_SIZE] [-j NUM_WORKERS] -w WEIGHT_PATH
                    [--world_size WORLD_SIZE] [--local_rank LOCAL_RANK]
                    [--fp16]

optional arguments:
  -h, --help            show this help message and exit
  -i INPUT_PATH, --input_path INPUT_PATH
                        Path with images.
  -c CONFIG_PATH, --config_path CONFIG_PATH
                        Path to config.
  -o OUTPUT_PATH, --output_path OUTPUT_PATH
                        Path to save jsons.
  -b BATCH_SIZE, --batch_size BATCH_SIZE
                        batch_size
  -j NUM_WORKERS, --num_workers NUM_WORKERS
                        num_workers
  -w WEIGHT_PATH, --weight_path WEIGHT_PATH
                        Path to weights.
  --world_size WORLD_SIZE
                        number of nodes for distributed training
  --local_rank LOCAL_RANK
                        node rank for distributed training
  --fp16                Use fp6

Example:

python -m torch.distributed.launch --nproc_per_node=<num_gpu> facemask_detection/inference.py \
                                   -i <input_path> \
                                   -w <path to weights> \
                                   -o <path to the output_csv> \
                                   -c <path to config>
                                   -b <batch size>

Metadata

Release files for facemask-detection 0.0.4

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for facemask-detection 0.0.4
File Size Uploaded
facemask_detection-0.0.4.tar.gz 7.8 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for facemask-detection 0.0.4
File Interpreter ABI Platform
facemask_detection-0.0.4-py2.py3-none-any.whl Python 3, Python 2 none any Details

Total release size: 16.9 kB

Release files / facemask_detection-0.0.4.tar.gz

Download URL facemask_detection-0.0.4.tar.gz
Size 7.8 kB
Tags Source
SHA-256 checksum
How to use checksums
20419911bbe440683028e18c9c34a324751d160c287632ca745708f02816b55f
BLAKE2b-256 checksum
How to use checksums
dfe4dd070ae10232c907bd6321e176b0e1b0966a868e1a73ab434ec556e8c9de
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.1.1 pkginfo/1.5.0.1 requests/2.23.0 setuptools/47.3.1 requests-toolbelt/0.9.1 tqdm/4.46.1 CPython/3.7.3

Release files / facemask_detection-0.0.4-py2.py3-none-any.whl

Download URL facemask_detection-0.0.4-py2.py3-none-any.whl
Size 9.1 kB
Tags Python 2 Python 3
SHA-256 checksum
How to use checksums
0a41cde2ffcfc1e2eedd1a0bad954d686e315181d61c93d4140f25b701a4dd5d
BLAKE2b-256 checksum
How to use checksums
6d8cfabe04a674984346aeb83991b5a46890615dece60a2c7ec7d453ca22a2cc
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.1.1 pkginfo/1.5.0.1 requests/2.23.0 setuptools/47.3.1 requests-toolbelt/0.9.1 tqdm/4.46.1 CPython/3.7.3

Release history Release notifications | RSS feed

This release

0.0.4 This release

2 release files

0.0.3

2 release files

0.0.2

2 release files

0.0.1

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page