Mask Detector using TensorFlow Lite

angga kusumandaru
3 min readJul 25, 2020

It can use Raspberry Pi or Macbook Pro and send into slack, even use 4 USD webcam

Photo by Adam Nieścioruk on Unsplash

Since some people don’t have sense of concern to use facemask on public area. Our office have regulation to assign some people to be watcher and take a record of image people which stubborn or forget. Furthermore it will took my time to watch surrounding and keep eye of my friend.

Because of that Isearch and found repository of Facemask detection and give some improvement to save image when some frame was detected and send into slack.

In Indonesia, you can bought equipment here:

This program is consist of several part:

Load Image Model

sess, graph = load_tf_model('models/face_mask_detection.pb')

Send image to tensorflow detection (Receive an image array and run inference). It have detection inference with several params:

  • image: 3D numpy array of image
  • conf_thresh: the min threshold of classification probabity.
  • iou_thresh: the IOU threshold of NMS
  • target_shape: the model input size.
  • draw_result: whether to draw bounding box to the image.
  • show_result: whether to display the image.
y_bboxes_output, y_cls_output = tf_inference(sess, graph, image_exp)

y_bboxes_output will return box detection coordinate

y_cls_output will return score of confidence image have using proper mask or not

  • set label
id2class = {0: 'Mask', 1: 'NoMask'}
  • write image
cv2.imwrite(filename_path, cv2.cvtColor(image, cv2.COLOR_RGB2BGR))
  • send to slack
requests.post('https://slack.com/api/files.upload',{'token': slack_token,'filename': file_name,'channels': slack_channel,'filetype': file_type,'initial_comment': text,'title': title},files = { 'file': file_bytes }).json()

To running follow these step:

  • git clone these repository
  • create virtual environment -> virtual env facemask
  • activate virtual environemnt -> source facemask/bin/activate
  • copy env -> cp .env.staging .env
  • update env value with slack channel and slack token
  • install requirements depends on raspberry or mac -> pip3 install -r requirements_raspberry.txt
  • run application -> python3 tensorflow_infer.py — img-mode 0 — video-path 0

Example image catch from running raspberry video webcam.

People which using masked unproperly or not using mask. Capture with confidence score

--

--