Giter VIP home page Giter VIP logo

qengineering / face-recognition-with-mask-jetson-nano Goto Github PK

View Code? Open in Web Editor NEW
35.0 3.0 5.0 29.53 MB

Recognize 2000+ faces on your Jetson Nano with additional mask detection, auto-fill and anti-spoofing

Home Page: https://qengineering.eu/deep-learning-examples-on-raspberry-32-64-os.html

License: BSD 3-Clause "New" or "Revised" License

C++ 100.00%
deep-learning face-recognition mask-detection face-detection face-mask-detection ncnn paddle-lite arcface retinaface mtcnn

face-recognition-with-mask-jetson-nano's Introduction

Recognize 2000+ faces + masks with your Jetson Nano.

output image

A fast face recognition and face recording running on a Jetson Nano with additional mask detector

output image

License

This C++ application recognizes a person from a database of more than 2000 faces. It is built for a Jetson Nano, but can easily be ported to other platforms.

First, the faces and their landmarks are detected by RetinaFace or MTCNN. The next stage checks if the person is wearing a mask. If not, the database is scanned with Arcface for the matching face. In the end, Face Anti Spoofing tests whether the person in front of the camera is real and not a mask or a cardboard photo.

If the face is not found in the database, it will be added automatically. A blur filter ensures only sharp faces in the database. One photo per person is sufficient, although more does not hurt.

Special made for a Jetson Nano see Q-engineering deep learning examples


Benchmark.

Model Jetson Nano 2015 MHz Jetson Nano 1479 MHz RPi 4 64-OS 1950 MHz RPi 4 64-OS 1500 MHz
MTCNN 11 mS 14 mS 22 mS 25 mS
RetinaFace 15 mS 19 mS 35 mS 37 mS
ArcFace +17 mS +21 mS +36 mS +40 mS
Mask +8 mS +10 mS +10 mS +12 mS
Spoofing +25 mS +37 mS +37 mS +45 mS

Dependencies.

April 4 2021: Adapted for ncnn version 20210322 or later

To run the application, you have to:

  • The Paddle Lite framework installed. Install Paddle
  • The Tencent ncnn framework installed. Install ncnn
  • Code::Blocks installed. ($ sudo apt-get install codeblocks)

Installing the app.

To extract and run the application in Code::Blocks
$ mkdir MyDir
$ cd MyDir
$ wget https://github.com/Qengineering/Face-Recognition-with-Mask-Jetson-Nano/archive/refs/heads/main.zip
$ unzip -j master.zip
Remove master.zip and README.md as they are no longer needed.
$ rm master.zip
$ rm README.md

Your MyDir folder must now look like this:
Graham Norton.jpg (example image)
FaceRecognition.cbp (code::blocks project file)
Norton_A.mp4 (movie with faces to load)
Norton_M.mp4 (movie to check)
img (database folder)
models (folder with used ncnn deep learning models)
src (C++ source files)
include (the C++ headers)


Running the app.

To run the application load the project file FaceRecognition.cbp in Code::Blocks.
First, we are going to fill the database with new faces. The database img initial holds one face, Graham.jpg.

output image

Check in main.cpp line 253. It must be cv::VideoCapture cap("Norton_A.mp4");

Compile and run the app. Movie Norton_A.mp4 will be played and new faces are stored in the database. In the end, you have the database filled as below.

output image

Next, alter the name of the movie in line 253 of main.cpp to Norton_2.mpg. Compile and run the application again. You will see that all the faces are correctly recognized. It can still happen that faces are added to the database due to strange angles or grimaces.


Database.

The application can easily contain more than 2000 faces. There are reports that ArcFace works flawlessly with over 5000 faces. With large databases, it is important to keep your face "natural". It means a front view photo with eyes open and mouth closed without a smile or other funny faces.
The database is filled "on the fly", as you have seen above. It is also possible to manually add a face to the databases. To do this, run the application from the command-line and enter the name of the image as an argument. For example ./FaceRecognition "Graham Norton.jpg" Note the quotation marks around the name if it has a space.

You can give the faces a corresponding name. By using a hash, you can associate multiple pictures with the same name.

output image

By the way, note the warp perspective of Graham Norton's face that we added via a command-line argument and the crop of the same photo already saved in the database. This is done by the ArcFace algorithms.

The blur filter prevents vague or imprecise faces from being added to the database. Below you see a few examples of faces we encounter in the database when de blur filter was switched off.

output image

Another safety measure is the orientation of the face. Only faces in front of the camera are added to the database. Faces "in profile" are often inaccurate in large databases.


Mask detection.

We use Paddle-Lite mask detection for face mask detection. We have previously applied this network in the Face-Mask-Detection-Jetson-Nano app. It's lightweight and fast. However, you will need to install Paddle-Lite on your Jetson Nano as an additional framework in addition to ncnn. Please note that PaddlePaddle has only trained its face mask detection network with white (or light-coloured) masks. Nowadays you see a lot of black masks or masks with fancy prints. Unfortunately, they are not recognized by this network


Code.

The application is written in C ++. The setup is flexible and easy to adapt to your own needs. See it as a skeleton which you can expand yourself. Some hints. In main .cpp at line 21 you see a few defines.

#define RETINA                  //comment if you want to use MtCNN landmark detection instead
#define RECOGNIZE_FACE
#define TEST_LIVING
#define TEST_MASK
#define AUTO_FILL_DATABASE
#define BLUR_FILTER_STRANGER
// some diagnostics
#define SHOW_LEGEND
#define SHOW_LANDMARKS

By commenting the line the define is switched off. For instance, if you do not want to incorporate the anti-spoofing test (saves you 37 mS), comment this line. The MtCNN face detection is switched on by turning RETINA off.
Another important point is that only one face is labelled. It is no problem to loop through all faces. However, they are usually too small to be recognized with great accuracy. Besides, your FPS will drop also. Note, the input image for the RetinaFace is 324 x 240 pixels. Larger pictures are resized to that format. ArcFace works with an input of 112 x 112 pixels. If you have a large input format, you could extract the faces at a larger scale from this image, once you have the coordinates from the RetinaFace network. Now, faces are to be recognized with much greater accuracy. Of course, there will be not much of an FPS left.


WebCam.

If you want to use a camera please alter line 253 in main.cpp to
cv::VideoCapture cap(0); //WebCam
If you want to run a movie please alter line 253 in main.cpp to
cv::VideoCapture cap("Norton_M.mp4"); //Movie


Papers.

MTCNN
RetinaFace
ArcFace
Anti spoofing


Thanks.

https://github.com/Tencent/ncnn
https://github.com/PaddlePaddle/PaddleHub
https://github.com/nihui
https://github.com/LicheeX/MTCNN-NCNN
https://github.com/XinghaoChen9/LiveFaceReco_RaspberryPi
https://github.com/deepinsight/insightface
https://github.com/minivision-ai/Silent-Face-Anti-Spoofing
https://github.com/Qengineering/Blur-detection-with-FFT-in-C

face-recognition-with-mask-jetson-nano's People

Contributors

qengineering avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

face-recognition-with-mask-jetson-nano's Issues

Video is slow on some cameras

Hi,
I tested it on two cameras :-

QHM495B supports 480P high-definition images and true colour images. Anti-flicker 50Hz, 60Hz or outdoor : 480P
&
Lapcam web camera HD 720P High resolution Video LWC-042  : 720P

On the first one which is 480p, the accuracy is low as the default exposure is low and video is coming dark. Hence its saying 'fake' most of the time.
While on the second one which is 720p, accuracy is really good, video is well lit but the video is slow compared to first camera.

Any reasons as to why this is happening ? And how to solve it.
Id definitely like to use second camera, but how to solve the slow video issue ? Is the video slow because the camera is 720p ?

Disabling face recognition until a condition is met

Sir,
Let's say I want to disable face detection and recognition until a certain condition is true( decided by another external code).
How can I achieve this.
At exactly what line do I need to put a boolean condition :
if true : execute Face detection and recognition ,else: do nothing.

Sometimes it doesnt recognize a person at all.

Keeps throwing "Fake"/"Stranger" even for a person with 3 photographs.
While sometimes it works. This happened when we took the photo in the same room, we tested this application for.
I cant understand why this erratic behavior, is it due to lightening of room or anything else

Cannot build with Code::Blocks in Jetson Nano

Tried to build in Jetson Nano on Ubuntu 18.04 and this error showed up:

g++: error: /usr/local/lib/paddle-lite/libpaddle_api_light_bundled.a: No such file or directory

I have paddle-lite for Armv8 installed, but there's only a single file inside the lib directory which is libpaddle_light_api_shared.so. As i know, the one with libpaddle_api_light_bundled.a is the x86 version

Playing a welcome audio whenever someone comes infront of camera (either person/stranger or recognized person)

Hi Sir,

Ive got a .wav file which Im playing whenever any person's Face is detected in camera. Please have a look at the playsound section of this snippet (main.cpp). The sound is playing but I think its slowing down the FaceRecognition also. Is system command slowing down the recognition/detection process ? Is there a faster /better way to play audio without slowing down the recognition/detection ?

#ifdef RECOGNIZE_FACE
        cv::String Str;
        cv::Scalar color;
        int  baseLine = 0;

        switch(obj.Color){
            case 0 : color = cv::Scalar(255, 255, 255); break;  //default white -> face ok
            case 1 : color = cv::Scalar( 80, 255, 255); break;  //yellow ->stranger
            case 2 : color = cv::Scalar(255, 237, 178); break;  //blue -> too tiny
            case 3 : color = cv::Scalar(127, 127, 255); break;  //red -> fake
            case 4 : color = cv::Scalar( 32,  32, 255); break;  //pure red -> mask
            default: color = cv::Scalar(255, 255, 255);
        }

        switch(obj.NameIndex){
            case -1: Str="Stranger"; break;
            case -2: Str="too tiny"; break;
            case -3: Str="Fake !";   break;
            case -4: Str="Mask !";   break;
            default: Str=NameFaces[obj.NameIndex];
        }
        cv::Size label_size = cv::getTextSize(Str, cv::FONT_HERSHEY_SIMPLEX, 0.6, 1, &baseLine);

        /////// ======== PLAY SOUND ============================ ///////////////
        // DETECT NAME & OUTPUT ON CONSOLE LOG
        cout << "NAME DETECTED AS \n\n " << Str << std::endl;

      //PLAY WELCOME SOUND   
          string str1 = "aplay ";
          str1 = str1 + "-Dhw:0,3 Welcome.wav" + " & ";
          const char *command = str1.c_str();
          system(command);
        } 

On the FaceMaskRecognition console im getting this error. I think whenever a face is detected audio is being called to play while the previous audio file hasnt finished playing. Is there a way to solve this ?
aplay : audio open error: device or resource busy

Installation on x86_64 linux

Hi Sir,

Is it possible to install this on normal linux desktop ? Since both paddle and ncnn can be installed on desktop also, hence I suppose it might work on desktop also , right ?

How to deal with 3800 face records

Sir my college has approx 3800 students and some 500 in staff. Will nano be able to handle this much load or shall we test on Jetson Xavier. Or if you can then please suggest an alternative hardware as we are not in college premise right now, but after two months we would like to demo this at our college by being well prepared in advance.

How to compile and run programs?

Hi!
I have successfully installed the environment needed to run the program on Jetson Nano, but I don't know how to compile this project to make it run successfully.

Mask detection isnt working

Hi buddy,

Awesome work, but I think mask detection isnt working at all. I tried with lots of different masks with 4 students in front of the cam but its always showing "OFF".
I think you might have forgotten to update/enable something in the github code.

How run run multiple models on a single video

Hi Sir,

You have 4 models ( RetinaFace, Mask, Arcface & AntiSpoofing ) running over single video.

1.] What exactly is this method of running multiple models known as?

2.] Is there any documentation/book on how to run multiple models at once ?

3.] Why Im asking is because I have a model for License Plate detection and another model for License Plate Recognition, and I want to combine both the models in same way as you have done. (First model tries to locate license plate in video -> If found -> Second Model reads the number plate)

How to Disable auto database fill ?

How can I disable the auto database fill feature.
If the face isnt found in the DB, I dont want to add it automatically.
How to do this ?

How to run with GPU

Thanks for great repo, I wonder if we can run with GPU or both? Thanks again

Issue with someone standing behind

Theres one small issue. When the algorithm detects a tiny face (personA) at a distance, and then suddenly if someone comes near the camera (personB), then personB is also recognized as "too tiny".
But when the personA from behind goes away, then personB is aptly recognized.

Face Recognition that wearing mask

Hi,

I really appreciate your work and great responsiveness of the issues.

Did you make any test that can we recognize people with mask ? I mean I have both phot in the DB bot maske and normal.

Can system recognize me with mask ? is there any tested accuracy ? Or we need a new model to recognize people wearing mask

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.