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

Introduction

Huawei ML kit image-related service is used for classifying an image into categories like food, people, objects etc., separate a human body from an image and much more. The scenario where we can use this service is by creating an ecommerce like application where we can take a picture, detect a category and use this category to show a list of similar category products. It will definitely help users to select any product from the list and buy the product on the spot. Sounds good right.

Image-related services

As of now there are five types of Cordova plugin created for image related services, as follows:

1) Image Classification

2) Object Detection and Tracking

3) Landmark Recognition

4) Image Segmentation

5) Product Visual Search

Here we will use three services that is Image Classification, Object Detection and Tracking and Image Segmentation 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.20201016090839.00828663067096561690973856026976:50511027012332:2800:1478DFE638DF99CC4DEABC29149D05EA65F2150C974444DAB6E2787A134B6701.gif"]https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/001/647/156/2640091000001647156.20201016090839.00828663067096561690973856026976:50511027012332:2800:1478DFE638DF99CC4DEABC29149D05EA65F2150C974444DAB6E2787A134B6701.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:50511027012332:2800:A81D0D32B5CDA52291637A57485CB230C9DD0346F0D52F09422D4A5061B576E4.png"]https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/001/647/156/2640091000001647156.20201016091709.57709094569223736910940757698934:50511027012332:2800:A81D0D32B5CDA52291637A57485CB230C9DD0346F0D52F09422D4A5061B576E4.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:50511027012332:2800:8E610AFD8A32D484C87F1C995A65591B8CA8029B574871427F5695B3FB755BDA.png"]https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/001/647/156/2640091000001647156.20201016092004.39144510248924744114409179507543:50511027012332:2800:8E610AFD8A32D484C87F1C995A65591B8CA8029B574871427F5695B3FB755BDA.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:50511027012332:2800:08CA13EFBE9EC0E47BA791934704BA14BCB87B0D0AF88DCDB7230309A0999C37.png"]https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/001/647/156/2640091000001647156.20201016092111.01691661891330532712549802523795:50511027012332:2800:08CA13EFBE9EC0E47BA791934704BA14BCB87B0D0AF88DCDB7230309A0999C37.png[/IMG]

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

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:50511027012332:2800:7BAF155FF9934C3A55897C82DD99122012D8FAC2A4E3009E1B47E56313068354.png"]https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/001/647/156/2640091000001647156.20201016092405.61953153049624786610761632438087:50511027012332:2800:7BAF155FF9934C3A55897C82DD99122012D8FAC2A4E3009E1B47E56313068354.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

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