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

Original link: https://forums.developer.huawei.com/forumPortal/en/topicview?tid=0202351447289070169&fid=0101187876626530001

Automatic Speech Recognition

[IMG="https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/002/250/698/2640091000002250698.20200910181959.20702963561511182796108821434023:50510923021051:2800:CAE6B72C2A5E294A672E36AADFC013B6BDC0B9D50B0EFD5B26FAF4DFE51BDF92.gif"]https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/002/250/698/2640091000002250698.20200910181959.20702963561511182796108821434023:50510923021051:2800:CAE6B72C2A5E294A672E36AADFC013B6BDC0B9D50B0EFD5B26FAF4DFE51BDF92.gif[/IMG]

Service Introduction

Automatic speech recognition (ASR) can recognize speech not longer than 60 seconds and convert the input speech into text in real time. This service uses industry-leading deep learning technologies to achieve a recognition accuracy of over 95%. Currently, Mandarin Chinese (including Chinese-English bilingual speech), English, French, German, Spanish, and Italian can be recognized.

Precausions

Currently, ASR is available only on Huawei phones.

ASR depends on the on-cloud API for speech recognition. During commissioning and usage, ensure that the device can access the Internet.

Integrating the Automatic Speech Recognition Service

You can integrate the automatic speech recognition (ASR) service in either of the following modes:

Integrate the ASR plug-in

Integrate the ASR SDK

The SDK provides only the basic ASR services, and you need to develop the speech pickup UI by yourself.

The sample code for integration through SDK is as follows:

dependencies{
// Import the ASR SDK.
implementation 'com.huawei.hms:ml-computer-voice-asr:2.0.1.300'
}

Development Process

Before API developement, you need to create custom layout based on your requirements. Check the below code

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#fff">

<androidx.cardview.widget.CardView
android:id="@+id/card_listen"
android:visibility="gone"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:cardBackgroundColor="#f1f1f1"
app:cardElevation="5dp">

<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="@dimen/dimen_10">

<ImageView
android:id="@+id/img_close_listen"
android:layout_width="@dimen/dimen_50"
android:layout_height="@dimen/dimen_50"
android:padding="@dimen/dimen_10"
android:src="@drawable/close_pc" />

<TextView
android:id="@+id/txt_listening"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/img_close_listen"
android:layout_marginTop="@dimen/dimen_20"
android:text="Listening..."
android:textColor="#000"
android:textSize="18sp"
android:textStyle="bold" />

<ImageView
android:id="@+id/img_listening_mic"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:src="@drawable/listening_mic" />

<RelativeLayout
android:id="@+id/rl_try_again"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:visibility="gone"
android:layout_alignParentBottom="true"
android:layout_margin="@dimen/dimen_50">

<com.google.android.material.floatingactionbutton.FloatingActionButton
android:id="@+id/fab_mic"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:backgroundTint="#fff"
android:src="@drawable/mic_pc" />

<TextView
android:id="@+id/txt_tap_micro"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/fab_mic"
android:layout_centerHorizontal="true"
android:layout_marginTop="@dimen/dimen_20"
android:text="Tap microphone to try again"
android:textColor="#000"
android:textSize="12sp"
android:textStyle="bold" />
</RelativeLayout>
</RelativeLayout>
</androidx.cardview.widget.CardView>
</RelativeLayout>

1. Set the ApiKey

private void setApiKey() {
// AGConnectServicesConfig config = AGConnectServicesConfig.fromContext(getApplication());
// MLApplication.getInstance().setApiKey(config.getString("client/api_key"));
MLApplication.getInstance().setApiKey(getString(R.string.api_key));
}

2. Initialize ASR

private void initASR() {
speechRecognizer = MLAsrRecognizer.createAsrRecognizer(this);
speechRecognizer.setAsrListener(this);
}

3. Implement a speech recognition result listener callback

@Override
public void onResults(Bundle bundle) {
Log.e(TAG + "onResults", "onResults");
final String speechResult = bundle.getString("results_recognized");
if (speechResult != null && !speechResult.equals("")) {
txtListeningText.setTextColor(Color.RED);
txtListeningText.setText(speechResult);
Log.e(TAG + " Speech Text", speechResult);
Handler handler = new Handler();
handler.postDelayed(new Runnable() {
@Override
public void run() {
Intent intent = new Intent(SearchActivity.this, MapActivity.class);
intent.putExtra("LISTEN_DATA", speechResult);
setResult(Activity.RESULT_OK, intent);
finish();
stopSpeechListener();
}
}, 2000);
}
}

@Override
public void onRecognizingResults(Bundle bundle) {
Log.e(TAG + "onRecognizingResults", "onRecognizingResults");

}

@Override
public void onError(int i, String s) {
if (s != null)
Toast.makeText(SearchActivity.this, TAG + "onError" + s, Toast.LENGTH_SHORT).show();
}

@Override
public void onStartListening() {
txtListeningText.setText("Listening...");
}

@Override
public void onStartingOfSpeech() {
Log.e(TAG + "onStartOfSpeech", "Speech Started");
}

@Override
public void onVoiceDataReceived(byte[] bytes, float v, Bundle bundle) {

}

@Override
public void onState(int i, Bundle bundle) {
Log.e(TAG + "onState", String.valueOf(i));
}

4. Set recognition parameters and call startRecognizing to start speech recognition

private void startCustomASR() {
if (checkPermission()) {
countDown();
rlTryAgain.setVisibility(View.GONE);
imgListening.setVisibility(View.VISIBLE);
txtListeningText.setTextColor(Color.BLACK);
Intent mSpeechRecognizerIntent = new Intent(MLAsrConstants.ACTION_HMS_ASR_SPEECH);
mSpeechRecognizerIntent.putExtra(MLAsrConstants.LANGUAGE, MLAsrConstants.LAN_EN_IN);
mSpeechRecognizerIntent.putExtra(MLAsrConstants.FEATURE, MLAsrConstants.FEATURE_WORDFLUX);
speechRecognizer.startRecognizing(mSpeechRecognizerIntent);
} else {
requestPermissions();
}
}

5. Add runtime permission in AndroidManifest.xml file

<uses-permission android:name="android.permission.RECORD_AUDIO" />

6. Before startRecognizing need to check runtime permission, Check the below methods for check and request permissions

private boolean checkPermission() {
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.RECORD_AUDIO) == PackageManager.PERMISSION_GRANTED) {
return true;
} else {
requestPermissions();
return false;
}
}

private void requestPermissions() {
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.RECORD_AUDIO}, REQUEST_RECORD_AUDIO);
}

Override onRequestPermissionResult method

@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
if (requestCode == REQUEST_RECORD_AUDIO) {
if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
cardListening.setVisibility(View.VISIBLE);
startCustomASR();
} else {
requestPermissions();
}
}
}

7. Add Count down time like this

void countDown() {
if (countDownTimer != null) {
countDownTimer.cancel();
}
countDownTimer = new CountDownTimer(7000, 1000) {
@Override
public void onTick(long l) {

}

@Override
public void onFinish() {
rlTryAgain.setVisibility(View.VISIBLE);
imgListening.setVisibility(View.GONE);
txtListeningText.setText("Try Again");
stopSpeechListener();
}
};
countDownTimer.start();
}

8. Release resources after the detection is complete

private void stopSpeechListener() {
if (speechRecognizer != null) {
speechRecognizer.destroy();
}
}

Find the output in below image

[IMG="https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/002/250/698/2640091000002250698.20200910180324.78675639037804354670868308306824:50510923021051:2800:E1B76018D3338D41E48B80EB6B78E07C5CCA107DE703AA57FCA87CFC4F624329.jpg"]https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/002/250/698/2640091000002250698.20200910180324.78675639037804354670868308306824:50510923021051:2800:E1B76018D3338D41E48B80EB6B78E07C5CCA107DE703AA57FCA87CFC4F624329.jpg[/IMG]

[IMG="https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/002/250/698/2640091000002250698.20200910180333.81058657608336960562017998451255:50510923021051:2800:3C4C98CAD83B248395952F7486CD4E732132CA37262D90D76D2BB3D1CBD59B98.jpg"]https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/002/250/698/2640091000002250698.20200910180333.81058657608336960562017998451255:50510923021051:2800:3C4C98CAD83B248395952F7486CD4E732132CA37262D90D76D2BB3D1CBD59B98.jpg[/IMG]

[IMG="https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/002/250/698/2640091000002250698.20200910180345.08146515646407880381113110934152:50510923021051:2800:84BF021BC705937021922D941091364C3004C353788C30CE8567DA52DC2211B5.jpg"]https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/002/250/698/2640091000002250698.20200910180345.08146515646407880381113110934152:50510923021051:2800:84BF021BC705937021922D941091364C3004C353788C30CE8567DA52DC2211B5.jpg[/IMG]

Conclusion

This article covers how to use ASR without Pickup UI, Always do not use default one sometimes need to try custom ASR UI

Reference link

https://developer.huawei.com/consumer/en/doc/development/HMSCore-Guides/ml-asr-0000001050066212