Some applications in the process of project development, sometimes need to use the function of speech detection, which identify the knock on the door, the door bell, car horns, and other functions, for small and medium-sized developers, individual development to build the ability, not time-consuming, while huawei machine learning service voice recognition in the SDK, simply integration, end side can achieve this function.

I. Introduction to Huawei Voice Recognition Service:

The voice recognition service supports the detection of voice events in online (real-time recording) mode. Based on the detected voice events, developers can perform subsequent command actions. Currently 13 types of sound events are supported, including: Laughter, babies or children crying, ZZZ, sneeze, shouts, meow, barking and the sound of water (including the tap water, streams, sound waves), car horns, door bell, knock on the door, fire alarm (including fire alarm alarm, smoke alarm alarm), alarm (including the fire alarm, ambulance sirens, car alarm, Air raid sirens).

Ii. Preparation for integration:

Development Environment Configuration

1. You need to create an application on Huawei Developer Alliance:

For details on this step, see the link below:

Developer.huawei.com/consumer/cn…

2. Open machine learning service:

You can check the link below for specific enabling steps:

Developer.huawei.com/consumer/cn…

2. After the application is created, the agconnect-services.json file is automatically generated. You need to copy the agconnect-services.json file to the root directory of the application

3. Configure the Maven repository address of the HMS Core SDK.

For Maven warehouse configuration, see the link below:

Developer.huawei.com/consumer/cn…

4. Integrated voice recognition service SDK

1. It is recommended to use the Full SDK mode for integration and configure the corresponding SDK in the build.gradle file

Implementation 'com.huawei. HMS: ML-speech Semantics - sounddect-SDK :2.1.0.300' implementation 'com. Huawei. HMS: ml researched - semantics - sounddect - model: 2.1.0.300'Copy the code

2. Configure the AGC plug-in in either of the following ways

apply plugin: 'com.android.application' apply plugin: Plugins {id 'com.android.application' id 'com.huawei.agconnect'}Copy the code

3. Automatically update machine learning models

Add the following statement to the androidmanifest.xml file. After users install your application from Huawei Application Market, the machine learning model will be automatically updated to the device:

<meta-data    
android:name="com.huawei.hms.ml.DEPENDENCY"  
android:value= "sounddect"/>
Copy the code

4. More detailed steps can be viewed at the link below:

Developer.huawei.com/consumer/cn…

Iii. Application development and coding stage

1. Obtain the microphone permission. If you do not have the microphone permission, error 12203 is reported

Set static permissions (required)

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

Dynamic permission acquisition (required)

ActivityCompat.

requestPermissions

(

this, new String[]{Manifest.permission.

RECORD_AUDIO

},

1

);

2. Create the MLSoundDector object

private static final String

TAG

= “MLSoundDectorDemo”;

//

Speech recognition object

private MLSoundDector mlSoundDector;

//

create

MLSoundDector

object

and

Set the callback method

private void initMLSoundDector(){

mlSoundDector = MLSoundDector.

createSoundDector

(a); mlSoundDector.setSoundDectListener(listener); }

3. Sound recognition result callback, used to obtain detection results, and the callback into the sound recognition instance.

//

Create a voice recognition result callback to get detection results and pass the callback to a voice recognition instance.

private MLSoundDectListener listener = new MLSoundDectListener() {

@Override

public void onSoundSuccessResult(Bundle result) {

//

Identify the successful processing logic, and the identification result is:

0-12

(corresponding to the

MLSoundDectConstants.java

Is defined by

SOUND_EVENT_TYPE

leading-named

13

Different sound types).

int soundType = result.getInt(MLSoundDector.

RESULTS_RECOGNIZED

);

Log.

d

(

TAG

,” Sound recognition succeeded: “+soundType); } @Override public void onSoundFailResult(int errCode) {

//

Identification failed, microphone permission may not be granted (

Manifest.permission.RECORD_AUDIO

) and other abnormal conditions.

Log.

d

(

TAG

,” Voice recognition failed: “+errCode); }};

In this code, only the int type of the sound recognition result is printed out. In the actual coding, the sound recognition result of the int type can be converted to the type that can be recognized by the user.

Definition of voice recognition types:

3. Enable and disable speech recognition

@Override

public void onClick(View v) {

switch (v.getId()){

case R.id.

btn_start_detect

: if (mlSoundDector ! = null){ boolean isStarted = mlSoundDector.start(this);

//context

Context is

//isStared

Is equal to the

true

Indicates that the identification is successful.

isStared

Is equal to the

false

Indicates that startup identification failed (the possible cause is that the mobile phone microphone is occupied by the system or other third-party applications

)

if (isStarted){

Toast.

makeText

(This,” Speech recognition enabled “, Toast.

LENGTH_SHORT

).show(); }}

break;

case R.id.

btn_stop_detect

:

if (mlSoundDector != null){

mlSoundDector.stop();

}

break;

}

}

4. When the page is closed, you can call the destroy() method to release resources

@Override

protected void onDestroy() {

super.onDestroy();

if (mlSoundDector != null){

mlSoundDector.destroy();

}

}

4. Run tests

1. Taking the knock on the door as an example, the output result of the sound recognition type is expected to be 10

2. Click the voice recognition button to simulate a knock. The following logs are displayed on the AS console.

Five, the other

1. Voice recognition service is a very small module of Huawei machine learning service, which includes six modules: text, speech and language, image, human face, natural language processing, and custom model.

2. This documentation only introduces the “voice recognition services” in the module of “Speech Language Classes”.

3. If readers are interested in other modules of Huawei machine learning service, they can check the relevant integration documents provided by Huawei. The address is as follows:

Developer.huawei.com/consumer/cn…

>> Huawei Developer Alliance official website

To participate in the developer discussion go to CSDN or Reddit >> download demo and sample code on Github or Gitee >> Resolve integration issues go to Stack Overflow

The original link: developer.huawei.com/consumer/cn…

Original author: Pepper