Hello friends, and welcome to Obsidian Soft.
Today, I will teach you how to make a Face Recognition App in MIT App Inventor.
So, let’s begin. First of all, we need to make a free account in Face++
https://www.faceplusplus.com/face-comparing/
Now, remember that this free account has a limited usage plan, which is enough for experimentation and some fun. The goal today is to show you a practical MIT App Inventor Project with API integration. If you want to use it professionally and make a student or employee attendance system with more usage, you will have to get a proper billing plan on Face++.
Open https://www.faceplusplus.com/face-comparing/ and click the Start Free buton.

Click on the Sign up for Face++ button.

Fill up the text boxes with the required information such as user name, email and password.

You will also have to provide a phone number for account security. After confirming, click on the Get API Key button.

Copy the API Key and the API Secret and paste them into Notepad/TextEdit, as we will need them later in MIT App Inventor.

Now, we need two extensions for this app.
We need an extension for converting images to base64:
http://kio4.com/appinventor/277_extension_imagen_string.htm
Choose the one that says: com.KIO4_Base64.aix and download it.
We also need an extension for resizing the images to reduce their size, as Face++ has a 2MB limit on the image size.
https://puravidaapps.com/image.php
Download the .aix file.
Now, open up MIT App Inventor. And go down on the left side and under extensions, upload the two extensions and add them to the project by dragging them onto the viewer.
Then, in screen1’s properties, make align horizontal and align vertical both center, and make screen orientation portrait.
Drag and drop an image component from the User Interface palette onto the viewer.
Now, add horizontal arrangement from the Layout palette below it.
In its properties, make align horizontal and align vertical both center.
Make height 10% and width fill parent.
Drag and drop a Textbox from the User Interface palette inside this arrangement and rename it to nameTxt.
Then, put a button on its right. Rename it to registerBtn. Make the background color green, font bold, font size 20, and change the text to: Register
Select this button. Duplicate it by pressing Ctrl+C & Ctrl+V on the keyboard for Windows and Command+C & Command+V on the keyboard for macOS.
Rename to checkBtn. Change the text to Check Photo.
Add a label below this checkBtn. Rename to resultLbl. Make the font bold, font size 22, and remove all text from it.
Add a Web component from Connectivity for calling the Face++ API.
Add a TinyDB component from Storage for storing our registered photos.
Add a Camera from Media for taking pictures.
Add a notifier from the user interface for showing messages.
The screen design is done, so let’s go to the blocks section.
We need some global variables:
We need a list for storing the registered photos. We need a variable for differentiating between the registering photos and comparing photos scenarios. We need a variable for storing the captured photo for comparison and an index for knowing where we are in the list while comparing a photo with registered photos.

We also need global variables for our api key and secret. Make sure you provide valid keys and secrets.

When the screen is initialized, we need to load already registered photos, so load data from tinyDB in the screen’s initialize event.

The register photo button just sets the isRegister variable to true and calls the camera’s take picture procedure.

The check photo button sets the isRegister variable to false and calls the camera’s take picture procedure.

Before we handle the camera’s after picture event, make an empty procedure called “checkNextPerson.”

Then, in the camera’s after picture event, we first set the image component on the screen to the captured photo, and we also resize it. Resizing is important as we cannot send huge photos to Face++. There is a 2MB limit.

After this, we need to differentiate between registering and comparing using the isRegister boolean variable, so get an if/else block from Control.
If isRegister is true, we join the entered name, “|”, and getImage (this is the path to the image on your device), and we add this item to the registeredPhotos list. Since the list has changed, we need to store it back in tinyDB.

In the else scenario, we store the capturedPhoto in the global variable that we made for this purpose. We inform the user using the result label that we are going to start checking faces. We make the currentIndex 1, and we call the checkNextPerson procedure. Don’t worry, we still have to work on this procedure 🙂.

So, the final camera. Afterpicture event looks like this:

Now, make an empty procedure called “compareFaces” that will actually call the Face++ API, but we will work on it later.

First, we will work on the checkNextPerson procedure that we had left empty earlier.
We first check that we haven’t checked all the registered photos. We can do this by checking that the currentIndex is still less than or equal to the length of the registered photos list. If it is still less than, then we call our compareFaces procedure. In the else, we just tell the user using the resultLbl that the person wasn’t recognized.

Now, we work on the compareFaces procedure to call the Face++ API.
We first set the web’s URL for calling the API. The URL is
https://api-us.faceplusplus.com/facepp/v3/compare
The URL has to be exactly the same.

Then we have to call the web’s postText procedure, but the setting of the text is a bit lengthy, so do it carefully with me.

We need a join block with 8 slots, and the first 4 slots have info related to API key and secrets. Make sure that you get the same spellings.
- “api_key=”
- The value inside the API key global variable
- “&api_secret=”
- The value inside the API secret global variable
- The fifth slot contains “&image_base64_1=”

- The sixth slot contains the encoded captured photo to base64, so do it carefully.

- The seventh slot contains “&image_base64_2=”

- And the last slot has to be done even more carefully, as we need to extract the image path from the registered photos for that current index and then encode it to base64.

So the final compareFaces looks like this:

If you have seen my previous tutorials, you may already know that when we call Web’s postText procedure, Web’s gotText event is triggered when a response is received from the server.
We first check that responseCode was 200, as 200 means success. In else, we use the notifier to see the error. For professional apps, I will recommend decoding the response content to get the actual error as showing raw error is not a very professional approach but for the sake of simplicity, I am going for this approach. You can check out the json returned in case of error here: https://console.faceplusplus.com/documents/5679308

In case of success, we decode the responseContent into a local variable jsonData.
JsonData is a dictionary like this(https://console.faceplusplus.com/documents/5679308) :
{
“time_used”: 473,
“confidence”: 96.46,
“thresholds”: {
“1e-3”: 65.3,
“1e-5”: 76.5,
“1e-4”: 71.8
},
“request_id”: “1469761507,07174361-027c-46e1-811f-ba0909760b18”
}
We are interested in the key “confidence” and its value. If confidence is greater than 80, then we tell the user that face recognized with the person’s name; otherwise, we add 1 to currentIndex and call the checkNextPerson procedure.

And we are done making this exciting face recognition app in MIT App Inventor. Make it today and impress your friends and teachers.
You can also watch the video tutorials for this Face Recognition App in MIT App Inventor on my YouTube channel for step by step instructions.
Please like my videos and share it with your friends and family. Also, subscribe to my channel and press the bell icon so you don’t miss any of the great projects I have planned for you.
https://www.youtube.com/c/obsidiansofteducation
Please like my Facebook page and Instagram page for more educational resources and tips.
Facebook: https://www.facebook.com/ObsidianSoft/
Instagram: https://www.instagram.com/obsidiansoftapps/
Pinterest: https://www.pinterest.com/obsidiansoft/
For links to free educational apps, have a look at the educational apps page
