Explain unity’s speech recognition function in detail

Time:2022-1-2

Now speech recognition has been widely used in various fields. In the development of unity, speech recognition is also very popular. Most people will choose the speech recognition function of iFLYTEK, but in some small projects, those who use iFLYTEK are a little overqualified. Today, let’s introduce the speech recognition function of unity. When it comes to speech recognition, unity itself can’t do it. Unity’s built-in speech recognition function is actually keyword recognition. For example, I set several keywords summarized in one sentence in unity. When I say this again, I will recognize it. After testing, it is found that when there are eight keywords in a sentence, the sentence will be well recognized.
Next, let’s implement it together:
First, paste a rendering:

在这里插入图片描述

Scene we use the scene used in the article of audio visualization. There will be no more introduction here. Unity’s own speech recognition function has an official API for reference. The address is here (stamped here)

在这里插入图片描述

Here you can go and have a look by yourself. Let’s start by introducing the functions we want to achieve:
To recognize speech recognition, we first refer to a namespace

using UnityEngine. Windows. Speech;// Introduce namespace

Then define an array of string type, which stores the keywords we want to identify.

//Keywords
    Public string [] keywords = {"hello", "start", "stop"};

Next, we need to create a speech recognizer in the start method so that speech recognition can be carried out

//Create a recognizer
	m_PhraseRecognizer = new KeywordRecognizer(keywords, m_confidenceLevel);
	//Method of listening through registration
	m_PhraseRecognizer.OnPhraseRecognized += M_PhraseRecognizer_OnPhraseRecognized;
	//Turn on the recognizer
	m_PhraseRecognizer.Start();

Then there is a method, which is mainly used to identify the key words

/// <summary>
    ///This method is called when a keyword is recognized
    /// </summary>
    /// <param name="args"></param>
    private void M_PhraseRecognizer_OnPhraseRecognized(PhraseRecognizedEventArgs args)
    {
        print(args.text);
    }

In this way, the basically important part is completed. The following is all the code. There is a small demo at the bottom. Those interested can download it and experience it by themselves

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine. Windows. Speech;// Introducing namespace utilization

/// <summary>
///Speech recognition (mainly other keywords)
/// </summary>
public class speechKey : MonoBehaviour
{
    //Phrase recognizer
    private PhraseRecognizer m_PhraseRecognizer;
    //Keywords
    Public string [] keywords = {"hello", "start", "stop"};
    //Credibility
    public ConfidenceLevel m_confidenceLevel = ConfidenceLevel.Medium;

    // Use this for initialization
    void Start()
    {
        if (m_PhraseRecognizer == null)
        {
            //Create a recognizer
            m_PhraseRecognizer = new KeywordRecognizer(keywords, m_confidenceLevel);
            //Method of listening through registration
            m_PhraseRecognizer.OnPhraseRecognized += M_PhraseRecognizer_OnPhraseRecognized;
            //Turn on the recognizer
            m_PhraseRecognizer.Start();
          
            Debug. Log ("identifier created successfully");
        }
    }

    /// <summary>
    ///This method is called when a keyword is recognized
    /// </summary>
    /// <param name="args"></param>
    private void M_PhraseRecognizer_OnPhraseRecognized(PhraseRecognizedEventArgs args)
    {
        SpeechRecognition();
        print(args.text);
    }
    private void OnDestroy()
    {
        //Judge whether there is a speech recognizer in the scene. If so, release it
        if (m_PhraseRecognizer != null)
        {
            //It should be released after use, otherwise it will bring additional expenses
            m_PhraseRecognizer.Dispose();
        }

    }
    // Update is called once per frame
    void Update()
    {

    }
    /// <summary>
    ///Operation of recognizing voice
    /// </summary>
    void SpeechRecognition()
    {
       
    }  
}

Demo download address: https://github.com/ShuxinWen/AutomaticSpeechRecognition-ASR-

This is the end of this article about unity’s speech recognition function. For more information about unity’s speech recognition, please search the previous articles of developeppaer or continue to browse the relevant articles below. I hope you will support developeppaer in the future!