Using FaceNet For On-Device Face Recognition With Android

栏目: IT技术 · 发布时间: 4年前

内容简介:The FaceNet Keras model is available onTo implement a live Camera feed, we use CameraX. I have used the code available in theAll our classification code will come in the

1. Convert the Keras model to a TFLite model

The FaceNet Keras model is available on nyoki-mtl/keras-facenet repo. After downloading the .h5 model, we’ll use the tf.lite.TFLiteConverter API to convert our Keras model to a TFLite model.

Converting a Keras model to TFLite.

2. Setting up a Preview and ImageAnalyser using CameraX

To implement a live Camera feed, we use CameraX. I have used the code available in the official docs . Next, we create a FrameAnalyser class which implements ImageAnalysis class, which will help us retrieve camera frames and run inference on them.

Setting up FrameAnalyser class.

All our classification code will come in the analyze method. First, using Firebase MLKit, we’ll get bounding boxes for all faces present in the camera frame ( a Bitmap object ). We’ll create a FirebaseVisionFaceDetector which runs the face detection model on an FirebaseVisionInputImage object.

Implementing the FirebaseVisionFaceDetector.

3. Producing Face Embeddings using FaceNet and Comparing them.

First, we’ll produce face embeddings using our FaceNet model. Before, we’ll create a helper class for handling the FaceNet model. This helper class will,

  1. Crop the given camera frame using the bounding box ( as Rect ) which we got from Firebase MLKit.
  2. Transform this cropped image from a Bitmap to a ByteBuffer with normalized pixel values.
  3. Finally, feed the ByteBuffer to our FaceNet model using the Interpreter class provided by TF Lite Android library.

In the snippet below, see the getFaceEmbedding() method which encapsulates all the above steps.

Implementing a helper class for FaceNet

Now, we have a class that would return us the 128-dimensional embedding for all faces present in the given image. We come back to a FrameAnalyser ‘s analyze() method. Using the helper class which just created, we’ll produce face embeddings and compare each of them with a set of embeddings that we already have.

Before that, we need to get the set of predefined embeddings, right? These embeddings will refer to the people whom we need to recognize. So, the app will read the images folder present in the internal storage of the user’s device. If the user wants to recognize, two users, namely Rahul and Neeta , then he/she needs to create two separate directories within the images folder. Then he/she has to place an image of Rahul and Neeta in their respective sub-directories.

images ->
   rahul ->
      image_rahul.png   neeta ->
      image_neeta.png

Our aim to read these images and produce a HashMap<String,FloatArray> object where the key ( String )will the subject’s name like Rahul or Neeta and the value ( FloatArray ) will the corresponding face embedding. You’ll get an idea of the process like studying the code below.

Reading and generating embeddings for images present in device storage.

We’ll compare the embeddings using the cosine similarity metrics which will return a similarity score in the interval [ -1 , 1 ] .

cosine of an angle made by two vectors.
Comparing face embeddings.

The predictions array is then supplied to the boundingBoxOverlay class which draws the bounding boxes and also displays the label. In the BoundingBoxOverlay.kt class,

Displaying the bounding boxes and labels.

The Results

Using the app, I have tried to recognize the faces of Jeff Bezos and Elon Musk,

A glimpse of the output.

Also, I had stored in the images in my internal storage as such,

The file structure.

The End

I hope you liked the story. I have included any APK in the GitHub repo so that you can try the app on your device. Thanks for reading!


以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

世界是平的

世界是平的

[美] 托马斯·弗里德曼 / 何帆、肖莹莹、郝正非 / 湖南科学技术出版社 / 2006-11 / 56.00元

当学者们讨论世界这20年发展的历史,并把目光聚集在2000年到2004年3月这一段时间时,他们将说些什么?9·11恐怖袭击还是伊拉克战争?或者,他们将讨论:科技的汇集与传播使得印度、中国和许多发展中国家成为世界商品和服务产品供给链上的一员,从而为世界大的发展中国家中的中产阶级带来了大量的财富,使这两个国家在全球化浪潮中占据更有利的位置?随着世界变得平坦,我们必须以更快的速度前进,才能在竞争中赢得胜......一起来看看 《世界是平的》 这本书的介绍吧!

在线进制转换器
在线进制转换器

各进制数互转换器

UNIX 时间戳转换
UNIX 时间戳转换

UNIX 时间戳转换

RGB CMYK 转换工具
RGB CMYK 转换工具

RGB CMYK 互转工具