[Huawei Developers] John Wo:
In my previous articles, we learn how to integrate Huawei ML Kit Text-related, Language-related and Image-related services. Today we are going to explore Face/Body-related service of Huawei machine learning kit.

Introduction

Huawei ML kit Face/Body-related service is used for detecting the shapes and features of our face, hand and body which includes facial expression, age, gender, hand points and many more. Use this service by creating an application which beautify user face when they are in video call or astrology & horoscope application by predicting future using hand key point detection service.

Face/Body-related services

As of now there is only one Cordova plugin developed for Face/Body- related services and that is Face Detection. Here we will use this service to showcase the power HMS ML Kit in Ionic application.

Demo

[IMG="https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/001/647/156/2640091000001647156.20201022142407.04740607516516303113904749769757:50511105012215:2800:73A5C4C58DD467FD17D789422EF29732D9DCD5260100C3B869D084D105917E0F.gif"]https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/001/647/156/2640091000001647156.20201022142407.04740607516516303113904749769757:50511105012215:2800:73A5C4C58DD467FD17D789422EF29732D9DCD5260100C3B869D084D105917E0F.gif[/IMG]

Prerequisite

1) Must have a Huawei Developer Account.

2) Must have a Huawei phone with HMS 4.0.0.300 or later.

3) Must have a laptop or desktop with Android Studio, Jdk 1.8, SDK platform 26 and Gradle 4.6 installed.

4) Must install node in the system

5) Must install Ionic in the system using below command:

npm install -g [MENTION=1329689]iONiC[/MENTION]/cli

Things need to be done

1) Generating a Signing Certificate Fingerprint. For generating the SHA key, refer this article.

2) Create an app in the Huawei AppGallery connect and enable ML Kit in Manage API section.

3) Provide the SHA Key in App Information Section.

4) Provide storage location.

5) Download the agconnect-services.json and store it somewhere on our computer.

6) Create a blank Ionic Project using below command:

ionic start Your_Application_Name blank --type=angular

7) Download the Cordova Ml Kit Plugin. Run the following command in the root directory of your Ionic project to install it through npm.

npm install <CORDOVA_MLKIT_PLUGIN_PATH>

8) If you want full Ionic support with code completion etc., install [MENTION=1329689]iONiC[/MENTION]-native/core in your project.

npm install [MENTION=1329689]iONiC[/MENTION]-native/core --save-dev

9) Run the following command to copy the "ionic/dist/hms-ml" folder from library to "node_modules [MENTION=1329689]iONiC[/MENTION]-native" folder under your Ionic project.

cp node_modules/@hmscore/cordova-plugin-hms-ml/ionic/dist/hms-ml node_modules [MENTION=1329689]iONiC[/MENTION]-native/ -r

10) Run the following command to compile the project:

ionic build

npx cap init [appName] [appId]

Where appName is the name of your app, and appId is package_name in your agconnect-services.json file (example: com.example.app).

11) Run the following command to add android platform to your project:

ionic capacitor add android

12) Make sure your project has a build.gradle file with a maven repository address and agconnect service dependencies as shown below:

[IMG="https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/001/647/156/2640091000001647156.20201016091709.57709094569223736910940757698934:50511105012215:2800:A2C09CE8B93A6FFB1487F86FD79E7F0A3033FEE2AD64C67B90B31BA952B2E108.png"]https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/001/647/156/2640091000001647156.20201016091709.57709094569223736910940757698934:50511105012215:2800:A2C09CE8B93A6FFB1487F86FD79E7F0A3033FEE2AD64C67B90B31BA952B2E108.png[/IMG]

13) Add the Signing certificate configuration to the build.gradle file in the app directory as show below:

[IMG="https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/001/647/156/2640091000001647156.20201016092004.39144510248924744114409179507543:50511105012215:2800:B5CFDA9C0ACD19248B6E1DC069DAC5B8E6C4737D169F4DEE007FCF6920DC0352.png"]https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/001/647/156/2640091000001647156.20201016092004.39144510248924744114409179507543:50511105012215:2800:B5CFDA9C0ACD19248B6E1DC069DAC5B8E6C4737D169F4DEE007FCF6920DC0352.png[/IMG]

14) Add plugin to the build.gradle file in the app directory as show below:

[IMG="https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/001/647/156/2640091000001647156.20201016092111.01691661891330532712549802523795:50511105023337:2800:1462E70CD573EAB9035187156E4335AB7CEB67F001C1CC61E73C0E8DCC445F85.png"]https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/001/647/156/2640091000001647156.20201016092111.01691661891330532712549802523795:50511105023337:2800:1462E70CD573EAB9035187156E4335AB7CEB67F001C1CC61E73C0E8DCC445F85.png[/IMG]

15) Add ads service implementation into to dependencies section of build.gradle file in the app directory as show below:

[IMG="https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/001/647/156/2640091000001647156.20201016092213.20118207975368829362630934618278:50511105012215:2800:321B2D5841B61612CBEB3C1807F9891FCD3A55248BDC8CC4D5B008F11E533AB3.png"]https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/001/647/156/2640091000001647156.20201016092213.20118207975368829362630934618278:50511105012215:2800:321B2D5841B61612CBEB3C1807F9891FCD3A55248BDC8CC4D5B008F11E533AB3.png[/IMG]

16) Add agconnect-services.json and signing certificate jks file to the app directory in your Android project as show below:

[IMG="https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/001/647/156/2640091000001647156.20201016092405.61953153049624786610761632438087:50511105012215:2800:C44A829C4BE78EF6C5178011E10F577A6A05A46AFD7A283888AC8C6D616020B0.png"]https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/001/647/156/2640091000001647156.20201016092405.61953153049624786610761632438087:50511105012215:2800:C44A829C4BE78EF6C5178011E10F577A6A05A46AFD7A283888AC8C6D616020B0.png[/IMG]

17) To update dependencies, and copy any web assets to your project, run the following code:

npx capacitor sync

Let's Code

Install FileChooser

We need to install FileChooser to select a file or in this case an image, returns a file URI. Run the following command on visual studio terminal:

npm install cordova-plugin-filechooser
npm install [MENTION=1329689]iONiC[/MENTION]-native/file-chooser
ionic cap sync

Android Permission

We need android permission in order to allow our users to give their permission to the application like camera permission, storage permission etc. Run the following command on visual studio terminal:

npm install cordova-plugin-android-permissions
npm install [MENTION=1329689]iONiC[/MENTION]-native/android-permissions
ionic cap sync

Avoid No Provider Error

To avoid No Provider Error, we need to add FileChooser, Android Permission and HMS ML kit in app.module.ts providers section and also make sure to import the same as shown below:

import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { RouteReuseStrategy } from '@angular/router';

import { IonicModule, IonicRouteStrategy } from [MENTION=1329689]iONiC[/MENTION]/angular';
import { SplashScreen } from [MENTION=1329689]iONiC[/MENTION]-native/splash-screen/ngx';
import { StatusBar } from [MENTION=1329689]iONiC[/MENTION]-native/status-bar/ngx';

import { AppComponent } from './app.component';
import { AppRoutingModule } from './app-routing.module';
import { AndroidPermissions } from [MENTION=1329689]iONiC[/MENTION]-native/android-permissions/ngx';
import { FileChooser } from [MENTION=1329689]iONiC[/MENTION]-native/file-chooser/ngx';
import { HMSMLKit } from [MENTION=1329689]iONiC[/MENTION]-native/hms-ml/ngx'
import { FilePath } from [MENTION=1329689]iONiC[/MENTION]-native/file-path/ngx';

@NgModule({
declarations: [AppComponent],
entryComponents: [],
imports: [BrowserModule, IonicModule.forRoot(), AppRoutingModule],
providers: [
StatusBar,
SplashScreen,
AndroidPermissions,
FileChooser,
HMSMLKit,
FilePath,
{ provide: RouteReuseStrategy, useClass: IonicRouteStrategy }
],
bootstrap: [AppComponent]
})
export class AppModule {}

More details about this, you can check https://forums.developer.huawei.com/forumPortal/en/topicview?tid=0202387591680950486&fid=0101187876626530001