[Huawei Developers] Sherlock F:
More information like this, you can visit HUAWEI Developer Forum​

Introduction

This article covers, how to integrate React Native HMS ML Kit to a React Native application.

React Native Hms ML Kit supports services listed below

· Text Related Services

· Language Related Services

· Image Related Services

· Face/Body Related Services

There are several number of uses cases of these services, you can combine them or just use them to create different functionalities in your app. For basic understanding, please read uses cases from here.

Github: https://github.com/HMS-Core/hms-react-native-plugin/tree/master/react-native-hms-ml

Prerequisites

Step 1

Prepare your development environment using this guide.

After reading this guide you should have React Native Development Environment setted up, Hms Core (APK) installed and Android Sdk installed.

Step 2

Configure your app information in App Gallery by following this guide.

After reading this guide you should have a Huawei Developer Account, an App Gallery app, a keystore file and enabled ml kit service from AppGallery.

Integrating React Native Hms ML Kit

Warning : Please make sure that, prerequisites part successfully completed.

Step 1

npm i @hmscore/react-native-hms-ml

Step 2

Open build.gradle file in project-dir > android folder.

Go to buildscript > repositories and allprojects > repositories, and configure the Maven repository address.

buildscript {
repositories {
google()
jcenter()
maven {url 'https://developer.huawei.com/repo/'}
}
}
allprojects {
repositories {
google()
jcenter()
maven {url 'https://developer.huawei.com/repo/'}
}
}

Go to buildscript > dependencies and add dependency configurations.

buildscript {
dependencies {
classpath 'com.huawei.agconnect:agcp:1.2.1.301'
}
}

Step 3

Open build.gradle file which is located under project.dir > android > app directory.

Add the AppGallery Connect plug-in dependency to the file header.

apply plugin: 'com.huawei.agconnect'

The apply plugin: ‘com.huawei.agconnect’ configuration must be added after the apply plugin: ‘com.android.application’ configuration.

The minimum Android API level (minSdkVersion) required for ML Kit is 19.

Configure build dependencies of your project.

dependencies {
...
implementation 'com.huawei.agconnect:agconnect-core:1.0.0.301'
}

Now you can use React Native Hms ML and import modules like below code.

import {<module_name>} from '@hmscore/react-native-hms-ml';

Lets Create An Application

We have already created an application in prerequisites section.

Our app will be about recognizing text in images and converting it to speech. So, we will use HmsTextRecognitionLocal, HmsFrame and HmsTextToSpeech modules. We will select images by using react-native-image-picker, so don’t forget to install it.

Note That : Before running this code snippet please check for your app permissions.

Step 1

We need to add some settings before using react-native-image-picker to AndroidManifest.xml.

<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />

<application
...
android:requestLegacyExternalStorage="true">

Step 2

import React from 'react';
import { Text, View, ScrollView, TextInput, TouchableOpacity, StyleSheet } from 'react-native';
import { HmsTextRecognitionLocal, HmsFrame, HmsTextToSpeech, NativeEventEmitter } from '@hmscore/react-native-hms-ml';
import ImagePicker from 'react-native-image-picker';

const options = {
title: 'Choose Method',
storageOptions: {
skipBackup: true,
path: 'images',
},
};

const styles = StyleSheet.create({
bg: { backgroundColor: '#eee' },
customEditBox: {
height: 450,
borderColor: 'gray',
borderWidth: 2,
width: "95%",
alignSelf: "center",
marginTop: 10,
backgroundColor: "#fff",
color: "#000"
},
buttonTts: {
width: '95%',
height: 70,
alignSelf: "center",
marginTop: 35,
},
startButton: {
paddingTop: 10,
paddingBottom: 10,
backgroundColor: 'white',
borderRadius: 10,
borderWidth: 1,
borderColor: '#888',
backgroundColor: '#42aaf5',
},
startButtonLabel: {
fontWeight: 'bold',
color: '#fff',
textAlign: 'center',
paddingLeft: 10,
paddingRight: 10,
},
});

export default class App extends React.Component {

// create your states
// for keeping imageUri and recognition result
constructor(props) {
super(props);
this.state = {
imageUri: '',
result: '',
};
}

// this is a key function in Ml Kit
// It sets the frame for you and keeps it until you set a new one
async setMLFrame() {
try {
var result = await HmsFrame.fromBitmap(this.state.imageUri);
console.log(result);
} catch (e) {
console.error(e);
}
}

// this creates text recognition settings by default options given below
// languageCode : default is "rm"
// OCRMode : default is OCR_DETECT_MODE
async createTextSettings() {
try {
var result = await HmsTextRecognitionLocal.create({});
console.log(result);
} catch (e) {
console.error(e);
}
}

// this function calls analyze function and sets the results to state
// The parameter false means we don't want a block result
// If you want to see results as blocks you can set it to true
async analyze() {
try {
var result = await HmsTextRecognitionLocal.analyze(false);
this.setState({ result: result });
} catch (e) {
console.error(e);
}
}

// this function calls close function to stop recognizer
async close() {
try {
var result = await HmsTextRecognitionLocal.close();
console.log(result);
} catch (e) {
console.error(e);
}
}

// standart image picker operation
// sets imageUri to state
// calls startAnalyze function
showImagePicker() {
ImagePicker.showImagePicker(options, (response) => {

if (response.didCancel) {
console.log('User cancelled image picker');
} else if (response.error) {
console.log('ImagePicker Error: ', response.error);
} else {
this.setState({
imageUri: response.uri,
});

this.startAnalyze();
}
});
}

// configure tts engine by giving custom parameters
async configuration() {
try {
var result = await HmsTextToSpeech.configure({
"volume": 1.0,
"speed": 1.0,
"language": HmsTextToSpeech.TTS_EN_US,
"person": HmsTextToSpeech.TTS_SPEAKER_FEMALE_EN
});
console.log(result);
} catch (e) {
console.error(e);
}
}

// create Tts engine by call
async engineCreation() {
try {
var result = await HmsTextToSpeech.createEngine();
console.log(result);
} catch (e) {
console.error(e);
}
}

// set Tts callback
async callback() {
try {
var result = await HmsTextToSpeech.setTtsCallback();
console.log(result);
} catch (e) {
console.error(e);
}
}

// start speech
async speak(word) {
try {
var result = await HmsTextToSpeech.speak(word, HmsTextToSpeech.QUEUE_FLUSH);
console.log(result);

} catch (e) {
console.error(e);
}
}

// stop engine
async stop() {
try {
var result = await HmsTextToSpeech.stop();
console.log(result);

} catch (e) {
console.error(e);
}
}

// manage functions in order
startAnalyze() {
this.setState({
result: 'processing...',
}, () => {
this.createTextSettings()
.then(() => this.setMLFrame())
.then(() => this.analyze())
.then(() => this.close())
.then(() => this.configuration())
.then(() => this.engineCreation())
.then(() => this.callback())
.then(() => this.speak(this.state.result));
});
}

render() {
return (
<ScrollView style={styles.bg}>

<TextInput
style={styles.customEditBox}
value={this.state.result}
placeholder="Text Recognition Result"
multiline={true}
editable={false}
/>

<View style={styles.buttonTts}>
<TouchableOpacity
style={styles.startButton}
onPress={this.showImagePicker.bind(this)}
underlayColor="#fff">
<Text style={styles.startButtonLabel}> Start Analyze </Text>
</TouchableOpacity>
</View>

</ScrollView>
);
}
}

Test the App

· First write “Hello World” on a blank paper.

· Then run the application.

· Press “Start Analyze” button and take photo of your paper.

· Wait for the result.

· Here it comes. You will see “Hello World” on screen and you will hear it from your phone.

[IMG="https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/006/560/358/2640852000006560358.20200922123247.66492055960463980591930062627657:50511012055954:2800:95297A00C71E629AF502BD6E8581DF52D2CCC0415A46098FBE71501337382424.png"]https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/006/560/358/2640852000006560358.20200922123247.66492055960463980591930062627657:50511012055954:2800:95297A00C71E629AF502BD6E8581DF52D2CCC0415A46098FBE71501337382424.png[/IMG]

Conclusion

In this article, we integrated and used React Native Hms ML Kit to our application.

From: https://medium.com/huawei-developers/react-native-hms-ml-kit-installation-and-example-242dc83e0941