[Huawei Developers] lokeshsuryan:
Spring is a great season for hiking, especially when flowers are in full bloom. One weekend, Jenny, John's girlfriend, a teacher, took her class for an outing in a park. John accompanied them to lend Jenny a hand.

John had prepared for a carefree outdoor outing, like those in his childhood, when he would run around on the grass — but it took a different turn. His outing turned out to be something like a Q&A session that was all about flowers: the students were amazed at John’s ability to recognize flowers, and repeatedly asked him what kind of flowers they encountered. Faced with their sincere questioning and adoring expression, John, despite not a flower expert, felt obliged to give the right answer even though he had to sneak to search for it on the Internet.

It occurred to John that there could be an easier way to answer these questions — using a handy app.

As a programmer with a knack for the market, he soon developed a flower recognition app that's capable of turning ordinary users into expert "botanists": to find out the name of a flower, all you need to do is using the app to take a picture of that flower, and it will swiftly provide you with the correct answer.

Demo

How to Implement

The flower recognition function can be created by using the image classification service in HUAWEI ML Kit. It classifies elements within images into intuitive categories to define image themes and usage scenarios. The service supports both on-device and on-cloud recognition modes, with the former recognizing over 400 categories of items, and the latter, 12,000 categories. It also allows for creating custom image classification models.

Preparations

1. Create an app in AppGallery Connect and configure the signing certificate fingerprint.

2. Configure the Huawei Maven repository address, and add the build dependency on the image classification service.

<p style="line-height: 1.5em;">dependencies{
// Import the basic SDK.
implementation 'com.huawei.hms:ml-computer-vision-classification:2.0.1.300'
// Import the image classification model package.
implementation 'com.huawei.hms:ml-computer-vision-image-classification-model:2.0.1.300'
}</p>

3. Automatically update the machine learning model.

Add the following statements to the AndroidManifest.xml file. After a user installs your app from HUAWEI AppGallery, the machine learning model will be automatically updated to the user's device.

<p style="line-height: 1.5em;"><manifest
...
<meta-data
android:name="com.huawei.hms.ml.DEPENDENCY"
android:value= "label"/>
...
</manifest></p>

4. Configure obfuscation scripts.

For details, please refer to the ML Kit Development Guide on HUAWEI Developers.

5. Declare permissions in the AndroidManifest.xml file.

To obtain images through the camera or album, you'll need to apply for relevant permissions in the file.

<p style="line-height: 1.5em;"> <uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-feature android:name="android.hardware.camera" />
<uses-feature android:name="android.hardware.camera.autofocus" /></p>

Development Process

1. Create and configure an on-cloud image classification analyzer.

Create a class for the image classification analyzer.

<p style="line-height: 1.5em;">public class RemoteImageClassificationTransactor extends BaseTransactor<List<MLImageClassification>>
</p>

In the class, use the custom class (MLRemoteClassificationAnalyzerSetting) to create an analyzer, set relevant parameters, and configure the handler.

<p style="line-height: 1.5em;">private final MLImageClassificationAnalyzer detector;

private Handler handler;MLRemoteClassificationAnalyzerSetting options = new MLRemoteClassificationAnalyzerSetting.Factory().setMinAcceptablePossibility(0f).create();
this.detector = MLAnalyzerFactory.getInstance().getRemoteImageClassificationAnalyzer(options);this.handler = handler;
</p>

2. Call asyncAnalyseFrame to process the image.

Asynchronously classify the input MLFrame object.

<p style="line-height: 1.5em;">@Override
protected Task<List<MLImageClassification>> detectInImage(MLFrame image) {
return this.detector.asyncAnalyseFrame(image);
}
</p>

3. Obtain the result of a successful classification.

Override the onSuccess method in RemoteImageClassificationTransactor to display the name of the recognized object in the image.

<p style="line-height: 1.5em;">@Override
protected void onSuccess(
Bitmap originalCameraImage,
List<MLImageClassification> classifications,
FrameMetadata frameMetadata,
GraphicOverlay graphicOverlay) {
graphicOverlay.clear();
this.handler.sendEmptyMessage(Constant.GET_DATA_SUCCESS);
List<String> classificationList = new ArrayList<>();
for (int i = 0; i < classifications.size(); ++i) {
MLImageClassification classification = classifications.get(i);
if (classification.getName() != null) {
classificationList.add(classification.getName());
}
}
RemoteImageClassificationGraphic remoteImageClassificationGraphic =
new RemoteImageClassificationGraphic(graphicOverlay, this.mContext, classificationList);
graphicOverlay.addGraphic(remoteImageClassificationGraphic);
graphicOverlay.postInvalidate();
}
</p>

If recognition fails, handle the error and check the failure reason in the log.

<p style="line-height: 1.5em;">@Override
protected void onFailure(Exception e) {
this.handler.sendEmptyMessage(Constant.GET_DATA_FAILED);
Log.e(RemoteImageClassificationTransactor.TAG, "Remote image classification detection failed: " + e.getMessage());
}
</p>

4. Release resources when recognition ends.

When recognition ends, stop the analyzer, release detection resources, and override the stop() method in RemoteImageClassificationTransactor.

<p style="line-height: 1.5em;">@Override
public void stop() {
super.stop();
try {
this.detector.stop();
} catch (IOException e) {
Log.e(RemoteImageClassificationTransactor.TAG,
"Exception thrown while trying to close remote image classification transactor" + e.getMessage());
}
}

</p>

For more details, you can go to:

For more details, you can go to:

l Our official website

l Our Development Documentation page, to find the documents you need

l Reddit to join our developer discussion

l GitHub to download demos and sample codes

l Stack Overflow to solve any integration problems

| Original Source