Git Product home page Git Product logo

pi-mask-detection's Introduction

Pi Mask Detection

This code runs a face detection models as well as a binary classifier mask/no mask on camera footage from a raspberry Pi to detect whether a person is wearing or not their mask.

For each event - i.e., someone not wearing their mask - we store an event in a local SQLite database.

My alertDispatcher project proposes a service to publish those events to a Kafka topic.

Installation instruction on the Pi

Prerequisite:

Hardware

  • A Raspberry Pi 4 model B with 4Go of RAM and at least 2Go storage available on the SD card (untested on 2Go RAM models but should work) - required.
  • A Camera compatible wth the Pi (I used the Pi Camera Module V2) - required.
  • A Coral USB accelerator to speed up model inference on your Pi - nice to have but not mandatory.
  • A fan and heat dissipating components are highly recommended to avoid overheating (my ventilated Pi in its protective case never goes above 38°C while running the inference code and the alertDispatcher).

Software

  • You have setup your Pi with an operating system (I used NOOBS).
  • Your Pi has python 3.7 installed. Run `which python3` -V to see which version of python3 you have.
    sudo add-apt-repository ppa:deadsnakes/ppa
    sudo apt-get update
    sudo apt-get install python3.7
  • Install SQLite 3 on your Pi.
    sudo apt-get update
    sudo apt-get upgrade
    sudo apt-get install sqlite3

Then you can create the database using the command sqlite alert.db. Create the alert table using the SQL commands from the schema.sql file of this project.

Install steps

  1. Create a virtual env in this project folder, let's call it venv and activate it:
python3 -m venv venv
source venv/bin/activate
  1. Install dependencies, if on your development machine, use dev-requirements.txt instead of requirements.txt since requirements.txt requires the pi camera module.
pip install -r requirements.txt
  1. For TensorFlow Lite (TF Lite) runtime on your Pi, check out Tensorflow Quickstart for the latest instructions. Running the following command worked for me to install the TF Lite runtime.
pip install https://dl.google.com/coral/python/tflite_runtime-2.1.0.post1-cp37-cp37m-linux_armv7l.whl
  1. Install lib for Coral USB accelerator following the getting started doc of Coral

Running unit tests

Use pytest, cd to the test folder and run pytest:

cd tests && pytest serialization.py

Running the project

Running the detect_mask.py script will start capture the video feed from the Pi camera and capture alerts.

python detect_mask.py

Machine learning models used

I provide you with two pretrained models, a widely available face detection model (ssd_mobilenet_v2) and a binary classifier mask/no mask I trained. (See under the models/ folder. They are optimized to run on the Coral USB edge TPU accelerator.

You can retrain you own classification model using your coral device, see Coral - retrain classification

Training process for mak/no mask binary classifier

For the mask/no mask dataset, I used the one provided on the Face Mask Detection project

For the training, I ued Google AutoML Vision to generate a model compatible with my Pi and USB accelerator, detailed example here

Related projects

This repository hosts the code for the first part of the project; detecting the events at the edge. It can be completed by the following projects.

  • alertDispatcher a Go module designed to run at the edge, especially a Raspberry Pi 4 B with 4Go of RAM. The alertDispatcher polls the local SQLite event store and publishes them to a Kafka topic.

  • alertIngress an Go module designed to run on a server, consuming from a Kafka topic where edge devices pushes their events. Each event consumed by the alert Ingress are archived in PostgresSQL and pushed to a notification service.

  • notifyMask a Go module designed to run on a server, sending email notification to a system administrator when an event occurs.

pi-mask-detection's People

Contributors

dependabot[bot] avatar fpaupier avatar

Stargazers

 avatar  avatar

Forkers

getcontrol

pi-mask-detection's Issues

Syntax Errors

For some reason I keep getting syntax errors when I run detect_mask.py on my Raspberry Pi 4 running Python3 , specifically around any code where there's colons in this format :

deployment: dict = operational_config["deployment"]

(vs the previous format in the line before
mask_threshold = operational_config["models"]["mask_classifier"]["threshold"]

as well as all the print statements where there's variables inside parentheses:
print(f"{len(faces)} Face(s) detected")

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.