• Stars
    star
    252
  • Rank 161,312 (Top 4 %)
  • Language
    Kotlin
  • License
    Apache License 2.0
  • Created over 4 years ago
  • Updated 4 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Face Recognition using the FaceNet model and MLKit on Android.

Face Recognition and Classification With FaceNet On Android

Store images of people who you would like to recognize and the app, using these images, will classify those people. We don't need to modify the app/retrain any ML model to add more people ( subjects ) for classification

repo_banner

Message from the developer,

You may also like my latest project -> Age + Gender Estimation in Android with TensorFlow. I'm open for freelancing in Android + ML projects as well as Technical Blogging. You may send me an email/message on Google Chat at [email protected].

Features

  • Asynchronous processing with Kotlin Coroutines
  • Quick labelling of faces with C++ computation (See native branch)
  • Use of latest Android development practices with configurable camera facing, GPU usage and mask detection.

Working of the app

If you're ML developer, you might have heard about FaceNet, Google's state-of-the-art model for generating face embeddings. In this
project, we'll use the FaceNet model on Android and generate embeddings ( fixed size vectors ) which hold information of the face.

The accuracy of the face detection system ( with FaceNet ) may not have a considerable accuracy. Make sure you explore other options as well while considering your app's production.

FaceNet

Working of the FaceNet model

So, the aim of the FaceNet model is to generate a 128 dimensional vector of a given face. It takes in an 160 * 160 RGB image and
outputs an array with 128 elements. How is it going to help us in our face recognition project?
Well, the FaceNet model generates similar face vectors for similar faces. Here, by the term "similar", we mean
the vectors which point out in the same direction. In this app, we'll generate two such vectors and use a suitable metric to compare them ( either L2norm or cosine similarity ). The one which is the closest will form our desired output.

You can download the FaceNet Keras .h5 file from this repo and TFLite model from the assets folder.

Usage

Intended File Structure

So, an user can store images in his/her device in a specific folder. If, for instance, the user wants the app to recognize
two people namely "Rahul" and "Neeta". So the user needs to store the images by creating two directories namely "Rahul" and "Neeta"
and store their images inside of these directories. For instance, the file structure for the working example ( as shown above in the GIF ),

Intended File Structure

The app will then process these images and classify these people thereafter. For face recognition, Firebase MLKit is used which
fetches bounding boxes for all the faces present in the camera frame.

For better performance, we recommend developers to use more images of the subjects, they need to recognize.

Working

Sample Prediction

The app's working is described in the steps below:

  1. Scan the images folder present in the internal storage. Next, parse all the images present within images folder and store
    the names of sub directories within images. For every image, collect bounding box coordinates ( as a Rect ) using MLKit. Crop the face from the image ( the one which was collected from user's storage ) using the bounding box coordinates.

  2. Finally, we have a list of cropped Bitmap of the faces present in the images. Next, feed the cropped Bitmap to the FaceNet
    model and get the embeddings ( as FloatArray ). Now, we create a HashMap<String,FloatArray> object where we store the names of
    the sub directories as keys and the embeddings as their corresponding values.

See MainActivity.kt and FileReader.kt for the code.

The above procedure is carried out only on the app's startup. The steps below will execute on each camera frame.

  1. Using androidx.camera.core.ImageAnalysis, we construct a FrameAnalyser class which processes the camera frames. Now, for a
    given frame, we first get the bounding box coordinates ( as a Rect ) of all the faces present in the frame. Crop the face from
    the frame using these boxes.
  2. Feed the cropped faces to the FaceNet model to generate embeddings for them. We compare the embedding with a suitable metric and form clusters for each user. We compute the average score for each cluster. The cluster with the best score is our output. The final output is then stored as a Prediction and passed to the BoundingBoxOverlay which draws boxes and
    text.
  3. For multiple images for a single user, we compute the score for each image. An average score is computed for each group. The group with the best score is chosen as the output. See FrameAnalyser.kt.
images ->  
    Rahul -> 
         image_rahul_1.png -> score=0.6 --- | average = 0.65 --- |
         image_rahul_2.png -> score=0.5 ----|                    | --- output -> "Rahul"
    Neeta ->                                                     |
         image_neeta_1.png -> score=0.4 --- | average = 0.35 --- |
         image_neeta_2.png -> score=0.3 ----|             

See FaceNetModel.kt and FrameAnalyser.kt for the code.

Limitations

Predictions may go wrong as FaceNet does not always produce similar embeddings for the same person. Consider the accuracy of the FaceNet model while using it in your apps. In that case, you may learn to use the FaceNetModel class separating for using FaceNet in some other tasks.

Important Resources

More Repositories

1

Age-Gender_Estimation_TF-Android

Age + Gender Estimation on Android with TensorFlow Lite
Kotlin
57
star
2

Android-Document-QA

A custom RAG pipeline for multi-document QA from PDF/DOCX documents, in Android
Kotlin
38
star
3

Depth-Anything-Android

An Android app running inference on Depth-Anything and Depth-Anything-V2
Kotlin
29
star
4

OnDevice-Face-Recognition-Android

On-device customizable face recognition in Android with FaceNet and an embedded vector database
Kotlin
26
star
5

Text2Summary-Android

A library for Text Summarization on Android applications.
Kotlin
25
star
6

Realtime_MiDaS_Depth_Estimation_Android

Monocular Depth Estimation using MiDaS in Android
Kotlin
23
star
7

Hand_Detection_TFLite_Android

Hand Detection using TFLite in Android. Uses Victor Dibia's model checkpoints.
Kotlin
19
star
8

Sentence-Embeddings-Android

Embeddings from sentence-transformers in Android! Supports all-MiniLM-L6-V2, bge-small-en, snowflake-arctic models and more
Kotlin
17
star
9

Segment-Anything-Android

An Android app running inference on Meta's Segment-Anything (SAM) and SAM v2
Kotlin
15
star
10

glove-android

Power of GloVe word embeddings in Android
Kotlin
14
star
11

Dataset_Archives

A Repository with datasets in zipped format. ( Hosted on GitHub for use with IPython notebooks )
14
star
12

Scikit_Learn_Android_Demo

An Android app that runs a scikit-learn model converted to the ONNX format
Kotlin
13
star
13

Spam_Classification_Android_Demo

An Android Project to demonstrate the use of a TensorFlow Lite model to classify spam messages.
Kotlin
12
star
14

MLKit_Selfie_Segmentation_Android

Selfie Segmentation in Android with MLKit APIs
Kotlin
12
star
15

tfidf-summarizer.rs

Simple, efficient and cross-platform TFIDF-based text summarizer in Rust
Rust
8
star
16

AirDrawing_with_Mediapipe_Android

A fun demo for the MediaPipe Hands solution
Kotlin
7
star
17

Google_Colab_Notebooks

Google Colab notebooks - tutorials, guides on ML topics
Jupyter Notebook
7
star
18

Sarcasm_Detection_NLP_TensorFlow

A TensorFlow project to detect sarcasm in tweets using the power of GloVe embeddings and Convolution layers.
Python
7
star
19

Decision_Tree_On_Android

Decision Trees and Random Forests made with pure Kotlin!
Kotlin
6
star
20

Spam_Classification_TF

A TensorFlow Project which detects spam messages and uses this capability in Android apps.
Python
6
star
21

Simple_Document_Scanner_Android

A simple document scanner app for Android available in two options - API-based and on-device inference
Kotlin
5
star
22

Sarcaso_for_Android

Sarcasm Detection app for Android.
Kotlin
5
star
23

text-predictor-android

Next word prediction and word auto-complete for Android - with the power of Rust 🚀
Kotlin
5
star
24

Skinly_for_Melanoma

An Android application which can detect potential cases of Melanoma with just a picture of the skin. It is currently in research stage.
Java
5
star
25

TF_ESRGAN_Desktop_App

A desktop app built with Qt (PySide6) for super resolution with ESRGANs in TensorFlow
Python
5
star
26

postagger.rs

NLTK inspired Parts-of-Speech Tagger (Perceptron Tagger) in Rust
Rust
4
star
27

PICT_Coursework

Programs for all assignments included in the Computer Engineering course of SPPU
4
star
28

Linear_Regression_with_Kotlin_Android

A multiple linear regression model built in Kotlin for Android
Kotlin
3
star
29

Multimodal_Summarization

Jupyter Notebook
3
star
30

GaussianNaiveBayes_Android_App

A simple implementation of the Gaussian Naive Bayes algorithm in Android ( with Kotlin ).
Kotlin
3
star
31

CLIP-Android

Inference on OpenAI's CLIP model in Android using clip.cpp
Kotlin
3
star
32

WASM-JS-Algorithms

Implementation of various algorithms in WebAssembly and vanilla Javascript
JavaScript
3
star
33

language-modelling-with-pytorch

Using the Transformer model to generate English sentences, given a prompt - in PyTorch
Python
2
star
34

Bayes_Text_Classifier_with_Kotlin

A basic Bayes Text Classifier written in Kotlin and can be used for classification purposes on Android.
Kotlin
1
star
35

Blind_Camera_Detection_Android

An Android app to check if the camera has been blinded by use of a sticker/obstruction.
Kotlin
1
star
36

glove.c

Simple, cross-platform port of GloVe embeddings, written in C
C
1
star
37

NMT_With_Attention_Eng-Marathi

Neural Machine Translation ( from English to Marathi ) with Attention Mechanism
Python
1
star
38

shubham0204

1
star
39

TensorFlow_Model_Deployment_Heroku_Android

Deploying TF models on Heroku and then using them on an Android device
Kotlin
1
star
40

QLearning_With_FrozenLakeEnv_Android

Explore Q Learning with the Frozen Lake Environment 🥶 in Android.
Kotlin
1
star
41

Feedforward_Neural_Network_Kotlin

Feed-Forward Neural Networks implemented in Kotlin.
Kotlin
1
star
42

full-text-search

Full Text Search built for PDFs, DOCX using Inverted Index in Java
Java
1
star