How a Programmer Developed a Perfect Flower Recognition App
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.
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.
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.
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.
Development Process
1. Create and configure an on-cloud image classification analyzer.
Create a class for the image classification analyzer.
In the class, use the custom class (MLRemoteClassificationAnalyzerSetting) to create an analyzer, set relevant parameters, and configure the handler.
2. Call asyncAnalyseFrame to process the image.
Asynchronously classify the input MLFrame object.
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.
If recognition fails, handle the error and check the failure reason in the log.
4. Release resources when recognition ends.
When recognition ends, stop the analyzer, release detection resources, and override the stop() method in RemoteImageClassificationTransactor.