[Huawei Developers] Freemind R:
Introduction

Machine learning is an application of artificial intelligence that provides systems the ability to automatically learn and improve from experience without being explicitly programmed. Machine learning focuses on the development of computer programs that can access data and use it learn.

HMS ML Kit allow to support diverse artificial intelligence applications throughout a wide range of industries.

HMS ML Kit Services

1. Text-related Services

2. Language/Voice-related Services

3. Image-related Services

4. Face/Body-related Services

5. Custom Model

In this article we will focus on text related services provided by HMS ML Kit.

Ionic Framework

Ionic Framework is an open source UI toolkit for building performant, high-quality mobile and desktop apps using web technologies such as HTML, CSS, and JavaScript with integrations for popular frameworks like Angular and React.

Think of Ionic as the front-end UI framework that handles all of the look and feel and UI interactions your app needs in order to be compelling. Unlike a responsive framework, Ionic comes with very native-styled mobile UI elements and layouts that you should get with a native SDK on Android or iOS but didn’t really exist before on the web.

Since Ionic is an HTML5 framework, it needs a native wrapper like Cordova or Capacitor in order to run as a native app.

Here we will use Ionic framework with Angular and Capacitor as native wrapper.

Text-related Services

There are four type of services which comes under text related service:

1. Text Recognition

2. Document Recognition

3. Bank Card Recognition

4. General Card Recognition

Here we will use three services that is Text Recognition, Bank Card Recognition and General Card Recognition 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.20201001064135.12427194042418868494648710553534:50511021015633:2800:E52A0931DC3AAF1C2B563D29431DBD1BE257A934CCD3CFF4EE9D7FDC2F57DEB4.gif"]https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/001/647/156/2640091000001647156.20201001064135.12427194042418868494648710553534:50511021015633:2800:E52A0931DC3AAF1C2B563D29431DBD1BE257A934CCD3CFF4EE9D7FDC2F57DEB4.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 (ex: 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.20201001064915.17192757088576512013650482793418:50511021015633:2800:B89E2182453C9FBBE2711DEC5025858A175D94E1498C3AFB05CFEA53D3FBFCC1.png"]https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/001/647/156/2640091000001647156.20201001064915.17192757088576512013650482793418:50511021015633:2800:B89E2182453C9FBBE2711DEC5025858A175D94E1498C3AFB05CFEA53D3FBFCC1.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.20201001065022.67804462775037186697672005866201:50511021015633:2800:78FD89DFC1A3E7E43B7E1420E87E194572BABC36F0ADBB858329F37C9B730386.png"]https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/001/647/156/2640091000001647156.20201001065022.67804462775037186697672005866201:50511021015633:2800:78FD89DFC1A3E7E43B7E1420E87E194572BABC36F0ADBB858329F37C9B730386.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.20201001065144.33649357369592044628346407869454:50511021015633:2800:6CDB90CA545299B3D3DC709562F47BE0BE1CAA2D9B4B0EEBD744FA2FFD0BFC9A.png"]https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/001/647/156/2640091000001647156.20201001065144.33649357369592044628346407869454:50511021015633:2800:6CDB90CA545299B3D3DC709562F47BE0BE1CAA2D9B4B0EEBD744FA2FFD0BFC9A.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.20201001065217.79115297238455642868975925038989:50511021015633:2800:8028152E150A140F7A5BB5D52957C93995EDD46346116418CA22BA352B97B23A.png"]https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/001/647/156/2640091000001647156.20201001065217.79115297238455642868975925038989:50511021015633:2800:8028152E150A140F7A5BB5D52957C93995EDD46346116418CA22BA352B97B23A.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.20201001065301.00242564479130146670656693204737:50511021015633:2800:BBC6ADB81AA3BCC48D6D86AEAF4D45EAAE1BBD81C161D8EE4C27427C33AEB984.png"]https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/001/647/156/2640091000001647156.20201001065301.00242564479130146670656693204737:50511021015633:2800:BBC6ADB81AA3BCC48D6D86AEAF4D45EAAE1BBD81C161D8EE4C27427C33AEB984.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 user 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 {}

For more information, you can visit https://forums.developer.huawei.com/forumPortal/en/topicview?tid=0202369186369300340&fid=0101187876626530001