How to Build a Face Mask Detector with Raspberry Pi

Face Mask Detector
(Image credit: Tom's Hardware)

One of the worst jobs in the world right now is being a greeter at a retail store who has to tell people to put on their face masks. Instead of making a human check for mask compliance, we can create a Raspberry Pi-powered mask detector that uses image recognition. Then unruly patrons can yell at a Raspberry Pi screen instead. 

In this article, we’ll show you how to set up a Raspberry Pi Face Mask Detection System and sound a buzzer when someone is not wearing their face mask. This project was inspired by a video of a mall in Asia where an entry gate could only be activated by a user wearing a face mask. 

Latest Videos FromTom's Hardware
Contributor

Caroline Dunn is a freelance writer for Tom's Hardware. Her expertise lies in covering Raspberry Pi projects, creating video tutorials, writing guides, and exploring other entertaining tech DIY initiatives.

  • Andrew_1313
    When i run, I got error :(

    loading face detector model...
    loading face mask detector model...
    Traceback (most recent call last):
    File "detect_mask_webcam.py", line 98, in <module>
    maskNet = load_model(args)
    File "/usr/local/lib/python3.7/dist-packages/tensorflow_core/python/keras/saving/save.py", line 146, in load_model
    return hdf5_format.load_model_from_hdf5(filepath, custom_objects, compile)
    File "/usr/local/lib/python3.7/dist-packages/tensorflow_core/python/keras/saving/hdf5_format.py", line 166, in load_model_from_hdf5
    model_config = json.loads(model_config.decode('utf-8'))
    AttributeError: 'str' object has no attribute 'decode'
    Reply
  • andreasc1
    it would be good to see if this project can run behind a mirror using the magic mirror project ;)
    Reply
  • kart94
    Andrew_1313 said:
    When i run, I got error :(

    loading face detector model...
    loading face mask detector model...
    Traceback (most recent call last):
    File "detect_mask_webcam.py", line 98, in <module>
    maskNet = load_model(args)
    File "/usr/local/lib/python3.7/dist-packages/tensorflow_core/python/keras/saving/save.py", line 146, in load_model
    return hdf5_format.load_model_from_hdf5(filepath, custom_objects, compile)
    File "/usr/local/lib/python3.7/dist-packages/tensorflow_core/python/keras/saving/hdf5_format.py", line 166, in load_model_from_hdf5
    model_config = json.loads(model_config.decode('utf-8'))
    AttributeError: 'str' object has no attribute 'decode'
    Hi Andrew,
    I too encountered the same error I found out its because of h5py version 3.0.0 , one of the solution I found on git was to remove .decode() from hdf5_format.py . I'll post the git link for your reference.
    https://github.com/tensorflow/tensorflow/issues/44467
    Reply
  • Andrew_1313
    kart94 said:
    Hi Andrew,
    I too encountered the same error I found out its because of h5py version 3.0.0 , one of the solution I found on git was to remove .decode() from hdf5_format.py . I'll post the git link for your reference.
    https://github.com/tensorflow/tensorflow/issues/44467
    Hi Kart94
    Its work now. I correct lines: 166, 180, 651, and 655 :) Thanks

    i use comands su and nano to edit hdf5_format.py.
    Example change line 166"
    model_config = json.loads(model_config)#.decode('utf-8'))
    "
    Reply
  • Obitech96
    hi there
    i tried this project with my new raspberri pi4 with 4 Gb of ram
    the video with mask detection is lagging too much! and it have a lot of delay....
    Some one can help me to improve the detection in less time? thanks a lot
    Marco
    Reply
  • rcsc484
    Hi, I have already removed the ".decode(UTF-8)" from hdf5_format.py, but it's still not working. When I try to run, it displays this:

    loading face detector model...
    loading face mask detector model...
    Traceback (most recent call last):
    File "detect_mask_webcam.py", line 98, in <module>
    maskNet = load_model(args)
    File "/usr/local/lib/python3.7/dist-packages/tensorflow_core/python/keras/saving/save.py", line 146, in load_model
    return hdf5_format.load_model_from_hdf5(filepath, custom_objects, compile)
    File "/usr/local/lib/python3.7/dist-packages/tensorflow_core/python/keras/saving/hdf5_format.py", line 169, in load_model_from_hdf5
    custom_objects=custom_objects)
    File "/usr/local/lib/python3.7/dist-packages/tensorflow_core/python/keras/saving/model_config.py", line 55, in model_from_config
    return deserialize(config, custom_objects=custom_objects)
    File "/usr/local/lib/python3.7/dist-packages/tensorflow_core/python/keras/layers/serialization.py", line 98, in deserialize
    layer_class_name = configTypeError: 'function' object is not subscriptable
    Reply
  • Obitech96
    you must only delete .decode(UTF-8) from hdf5_format.py in the line: lines: 166, 180, 651, and 655
    say me if it works and if the camera is lagging and if it have some delay
    Reply
  • seonho
    Hi. Thanks to you, I was able to make a good mask detection program. Thank you.
    I want to add one more thing.

    If you don't wear a mask, you'll sound a siren.
    What additional code should I add if I want to play a video from a file that recommends wearing a mask?
    Reply
  • ju34
    Mask detection
    I am getting about 1 fps, very slow...
    Any ideas?
    Reply
  • beatskreig
    When i am running the detect_mask_picam.py i am getting the following error

    loading face detector model...
    loading face mask detector model...
    Traceback (most recent call last):
    File "detect_mask_video.py", line 98, in <module>
    maskNet = load_model(args)
    File "/home/pi/mask_project/mask-env/lib/python3.7/site-packages/tensorflow_core/python/keras/saving/save.py", line 149, in load_model
    loader_impl.parse_saved_model(filepath)
    File "/home/pi/mask_project/mask-env/lib/python3.7/site-packages/tensorflow_core/python/saved_model/loader_impl.py", line 83, in parse_saved_model
    constants.SAVED_MODEL_FILENAME_PB))
    OSError: SavedModel file does not exist at: mask_detector.model/{saved_model.pbtxt|saved_model.pb}


    what to do please help
    Reply
  • ceplaska
    Smart facial Recognition systems support changes in a Person's appearance. Such systems can be very slow if you went all winter with a headdress, and then came without.
    Reply