[Huawei Developers] shikkerimath:
Preface

Free Translation is a real-time translation app that provides a range of services, including speech recognition, text translation, and text-to-speech (TTS).

Developing an AI app like Free Translation tends to require complex machine learning know-how, but integrating ML Kit makes the development quick and easy.

Use Scenarios

Free Translation is equipped to handle a wide range of user needs, for example translating content at work, assisting during travel in a foreign country, or helping with communicating with a foreign friend or learning a new language.

Development Preparations

1. Configure the Huawei Maven repository address.

2. Add build dependencies for the ML SDK.

Open the build.gradle file in the app directory of your project.

dependencies {
// Import the automatic speech recognition (ASR) plug-in.
implementation 'com.huawei.hms:ml-computer-voice-asr-plugin:2.0.3.300'
// Import the text translation SDK.
implementation 'com.huawei.hms:ml-computer-translate:2.0.4.300'
// Import the text translation algorithm package.
implementation 'com.huawei.hms:ml-computer-translate-model:2.0.4.300'
// Import the TTS SDK.
implementation 'com.huawei.hms:ml-computer-voice-tts:2.0.4.300'
// Import the bee voice package of on-device TTS.
implementation 'com.huawei.hms:ml-computer-voice-tts-model-bee:2.0.4.300'

For more details, please refer to Preparations.

Open the AndroidManifest.xml file in the main directory, and add the relevant permissions above the <application/> line.

<uses-permission android:name="android.permission.INTERNET" /> <!-- Accessing the Internet. -->

<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> <!-- Obtaining the network status. -->

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /><!-- Upgrading the algorithm version. -->

<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" /><!-- Obtaining the Wi-Fi status. -->

<uses-permission android:name="android.permission.RECORD_AUDIO" /><!-- Recording audio data by using the recorder. -->

Development Procedure

UI Design

Customize the app UI according to your needs, and based on activity_main.xml, the layout file.

Tap on START RECOGNITION to load the ASR module, which recognizes what the user says.

Tap on SYNTHETIC VOICE to load the TTS module, which reads out the resulting translation.

View attachment 5208365

Function Development

You can integrate an ASR plug-in to quickly integrate the ASR service.

public void startAsr(View view) {

// Use Intent for recognition settings.

Intent intent = new Intent(this, MLAsrCaptureActivity.class)

// Set the language that can be recognized to English. If this parameter is not set, English is recognized by default. Languages supported include the following: "zh-CN": Chinese; "en-US": English; "fr-FR": French; "de-DE": German; "it-IT": Italian.

.putExtra(MLAsrCaptureConstants.LANGUAGE, Constants.ASR_SOURCE[spinnerInput.getSelectedItemPosition()])

// Set whether to display the recognition result on the speech pickup UI. MLAsrCaptureConstants.FEATURE_ALLINONE: no; MLAsrCaptureConstants.FEATURE_WORDFLUX: yes.

.putExtra(MLAsrCaptureConstants.FEATURE, MLAsrCaptureConstants.FEATURE_WORDFLUX);

// 100: request code between the current activity and speech pickup UI activity. You can use this code to obtain the processing result of the speech pickup UI.

startActivityForResult(intent, 100);

}

@Override

protected void onActivityResult(int requestCode, int resultCode, Intent data) {

super.onActivityResult(requestCode, resultCode, data);

String text;

// 100: request code between the current activity and speech pickup UI activity, which is defined above.

if (requestCode == 100) {

switch (resultCode) {

// MLAsrCaptureConstants.ASR_SUCCESS: Recognition is successful.

case MLAsrCaptureConstants.ASR_SUCCESS:

if (data != null) {

Bundle bundle = data.getExtras();

// Obtain the text information recognized from speech.

if (bundle != null && bundle.containsKey(MLAsrCaptureConstants.ASR_RESULT)) {

text = bundle.getString(MLAsrCaptureConstants.ASR_RESULT);

// Process the recognized text information.

textViewInput.setText(text);

Translation.run(this, textViewOutput, spinnerInput.getSelectedItemPosition(),

spinnerOutput.getSelectedItemPosition(), text);

}

}

break;

}

}

}

Create the Translation class to use the text translation service.

Step 1 Define the public method, which decides whether to use real-time or on-device translation.

public static void run(Activity activity, TextView textView, int sourcePosition, inttargetPosition, String sourceText) {

Log.d(TAG, Constants.TRANSLATE[sourcePosition] + ", " + Constants.TRANSLATE[targetPosition] + ", " + sourceText);

if (isOffline) {

onDeviceTranslation(activity, textView, sourcePosition, targetPosition, sourceText);

} else {

realTimeTranslation(textView, sourcePosition, targetPosition, sourceText);

}

}

Step 2 Call the real-time or on-device translation method.

private static void realTimeTranslation(final TextView textView, int sourcePosition, final int targetPosition, String sourceText) {

Log.d(TAG, "realTimeTranslation");
}

private static void onDeviceTranslation(final Activity activity, final TextView textView, final int sourcePosition, final int targetPosition, final String sourceText) {

Set<String> result = MLTranslateLanguage.syncGetLocalAllLanguages();

Log.d(TAG, "Languages supported by on-device translation: " +Arrays.toString(result.toArray()));

}

Create the TTS class to use the text-to-speech service.

Step 1 Just as with Step 1 in Translation, define the public method, which decides whether to use real-time or on-device TTS.

public static void run(Activity activity, int targetPosition, String sourceText) {

Log.d(TAG, sourceText);

if (isNotAuto || sourceText.isEmpty()) {

return;

}

if (isOffline) {

if (0 == targetPosition) {

Toast.makeText(activity, ,

Toast.LENGTH_SHORT).show();

return;
}

offlineTts(activity, Constants.TTS_TARGET[targetPosition],

Constants.TTS_TARGET_SPEAKER_OFFLINE[targetPosition], sourceText);

} else {

onlineTts(Constants.TTS_TARGET[targetPosition], Constants.TTS_TARGET_SPEAKER[targetPosition], sourceText);

}

}

Step 2 Call the real-time or on-device TTS method.

private static void onlineTts(String language, String person, String sourceText) {

Log.d(TAG, language + ", " + person + ", " + sourceText);

}

private static void offlineTts(final Activity activity, String language, final String person, finalString sourceText) {

// Use customized parameter settings to create a TTS engine.

// For details about the speaker names, please refer to the Timbres section.

final MLTtsConfig mlTtsConfig = new MLTtsConfig().setLanguage(language)

.setPerson(person)

// Set the TTS mode to on-device mode. The default mode is real-time mode.

.setSynthesizeMode(MLTtsConstants.TTS_OFFLINE_MODE);

}

Final Effects

View attachment 5208367

More Information

To join in on developer discussion forums, go to Reddit.

To download the demo app and sample code, go to GitHub.

For solutions to integration-related issues, go to Stack Overflow.

More details