{"id":13856795,"url":"https://github.com/ternaus/facemask_detection","last_synced_at":"2025-06-29T14:32:30.620Z","repository":{"id":57428121,"uuid":"283388153","full_name":"ternaus/facemask_detection","owner":"ternaus","description":"Detection masks on faces.","archived":false,"fork":false,"pushed_at":"2020-08-14T00:51:03.000Z","size":32,"stargazers_count":29,"open_issues_count":1,"forks_count":5,"subscribers_count":4,"default_branch":"master","last_synced_at":"2025-06-10T20:50:10.549Z","etag":null,"topics":["classification","image-processing","mask-detection","python","pytorch"],"latest_commit_sha":null,"homepage":"","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/ternaus.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":".github/FUNDING.yml","license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null},"funding":{"github":null,"patreon":"ternaus","open_collective":null,"ko_fi":null,"tidelift":null,"community_bridge":null,"liberapay":null,"issuehunt":null,"otechie":null,"custom":null}},"created_at":"2020-07-29T03:26:45.000Z","updated_at":"2022-12-21T15:00:59.000Z","dependencies_parsed_at":"2022-09-02T15:31:14.287Z","dependency_job_id":null,"html_url":"https://github.com/ternaus/facemask_detection","commit_stats":null,"previous_names":[],"tags_count":5,"template":false,"template_full_name":null,"purl":"pkg:github/ternaus/facemask_detection","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ternaus%2Ffacemask_detection","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ternaus%2Ffacemask_detection/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ternaus%2Ffacemask_detection/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ternaus%2Ffacemask_detection/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/ternaus","download_url":"https://codeload.github.com/ternaus/facemask_detection/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ternaus%2Ffacemask_detection/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":262609044,"owners_count":23336621,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["classification","image-processing","mask-detection","python","pytorch"],"created_at":"2024-08-05T03:01:13.697Z","updated_at":"2025-06-29T14:32:30.590Z","avatar_url":"https://github.com/ternaus.png","language":"Python","funding_links":["https://patreon.com/ternaus"],"categories":["Python"],"sub_categories":[],"readme":"# Facemask detection\n\nIt could be confusing, but the model in this library perform classifications of the images.\nIt takes image as an input and outputs probability of person in the image wearing a mask.\n\nHence in order to get expected results the model should be combined with face detector, for example from\nhttps://github.com/ternaus/retinaface.\n\n[Example on how to combine face detector with mask detector](https://colab.research.google.com/drive/13Ktsrx164eQHfDmYLyMCoI-Kq0gC5Kg1?usp=sharing)\n\n[WebApp with face detector and mask classifier](https://facemaskd.herokuapp.com/)\n\n![https://habrastorage.org/webt/b_/ja/ww/b_jawwxndpkdl2pjlxlcxvars6m.png](https://habrastorage.org/webt/b_/ja/ww/b_jawwxndpkdl2pjlxlcxvars6m.png)\n\n# Installation\n`pip install -U facemask_detection`\n\n# Use\n```python\nimport albumentations as A\nimport torch\nfrom facemask_detection.pre_trained_models import get_model\n\nmodel = get_model(\"tf_efficientnet_b0_ns_2020-07-29\")\nmodel.eval()\n\ntransform = A.Compose([A.SmallestMaxSize(max_size=256, p=1),\n                       A.CenterCrop(height=224, width=224, p=1),\n                       A.Normalize(p=1)])\n```\n`image = \u003cnumpy array with the shape (height, width, 3)\u003e`\n\n```python\n\ntransformed_image = transform(image=image)['image']\n\ninput = torch.from_numpy(np.transpose(transformed_image, (2, 0, 1))).unsqueeze(0)\n\nprint(\"Probability of the mask on the face = \", model(input)[0].item())\n```\n\n* Jupyter notebook with the example: [![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/drive/1VkSK5MKIuGPIA31KJpGiFe_FafYC4xfD?usp=sharing)\n* Jupyter notebook with the example on how to combine face detector with mask detector: [![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/drive/13Ktsrx164eQHfDmYLyMCoI-Kq0gC5Kg1?usp=sharing)\n## Train set\n\nTrain dataset was composed from the data:\n\n### No mask:\n*  [VGGFace2](http://www.robots.ox.ac.uk/~vgg/data/vgg_face2/)\n\n### Mask:\n*  [https://www.kaggle.com/andrewmvd/face-mask-detection](https://www.kaggle.com/andrewmvd/face-mask-detection)\n*  [https://www.kaggle.com/alexandralorenzo/maskdetection](https://www.kaggle.com/alexandralorenzo/maskdetection)\n*  [https://github.com/X-zhangyang/Real-World-Masked-Face-Dataset](https://github.com/X-zhangyang/Real-World-Masked-Face-Dataset)\n*  [https://humansintheloop.org/medical-mask-dataset](https://humansintheloop.org/medical-mask-dataset)\n\n\n# Trainining\n\nDefine config, similar to [facemask_detection_configs/2020-07-29.yaml](facemask_detection_configs/2020-07-29.yaml).\n\nRun\n\n```bash\npython facemask_detection/train.py -c \u003cconfig\u003e\n```\n\nInference\n\n```bash\npython -m torch.distributed.launch --nproc_per_node=1 facemask_detection/inference.py -h\nusage: inference.py [-h] -i INPUT_PATH -c CONFIG_PATH -o OUTPUT_PATH\n                    [-b BATCH_SIZE] [-j NUM_WORKERS] -w WEIGHT_PATH\n                    [--world_size WORLD_SIZE] [--local_rank LOCAL_RANK]\n                    [--fp16]\n\noptional arguments:\n  -h, --help            show this help message and exit\n  -i INPUT_PATH, --input_path INPUT_PATH\n                        Path with images.\n  -c CONFIG_PATH, --config_path CONFIG_PATH\n                        Path to config.\n  -o OUTPUT_PATH, --output_path OUTPUT_PATH\n                        Path to save jsons.\n  -b BATCH_SIZE, --batch_size BATCH_SIZE\n                        batch_size\n  -j NUM_WORKERS, --num_workers NUM_WORKERS\n                        num_workers\n  -w WEIGHT_PATH, --weight_path WEIGHT_PATH\n                        Path to weights.\n  --world_size WORLD_SIZE\n                        number of nodes for distributed training\n  --local_rank LOCAL_RANK\n                        node rank for distributed training\n  --fp16                Use fp6\n```\n\nExample:\n\n```\npython -m torch.distributed.launch --nproc_per_node=\u003cnum_gpu\u003e facemask_detection/inference.py \\\n                                   -i \u003cinput_path\u003e \\\n                                   -w \u003cpath to weights\u003e \\\n                                   -o \u003cpath to the output_csv\u003e \\\n                                   -c \u003cpath to config\u003e\n                                   -b \u003cbatch size\u003e\n```\n\nWeb https://facemaskd.herokuapp.com/\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fternaus%2Ffacemask_detection","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fternaus%2Ffacemask_detection","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fternaus%2Ffacemask_detection/lists"}