1. Window 自带的微软语音识别
参考官方手册:Windows.Speech.DictationRecognizer - Unity 脚本 API (unity3d.com)
这个中英识别都比较准确,不过应该只支持Win10以上。
需在设置里开启在线语音识别。
代码部分:
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Events;
using UnityEngine.UI;
using UnityEngine.Windows.Speech;
public class DictationScript : MonoBehaviour
{
public Text hypothesesText;//显示输入过程中猜想结果的
public Text recognitionsText;//显示识别结果的
public Button startBtn;
public Button stopBtn;
private DictationRecognizer dictationRecognizer;
void Start()
{
dictationRecognizer = new DictationRecognizer();
dictationRecognizer.DictationResult += OnDictationResult;
dictationRecognizer.DictationHypothesis += OnDictationHypothesis;
dictationRecognizer.DictationComplete += OnDictationComplete;
dictationRecognizer.DictationError += OnDictationError;
//dictationRecognizer.Start();
startBtn.onClick.AddListener(DictationStart);
stopBtn.onClick.AddListener(DictionStop);
}
private void OnDestroy()
{
dictationRecognizer.Stop();
dictationRecognizer.Dispose();
}
/// <summary>
/// 语音识别结果
/// </summary>
/// <param name="text">识别结果</param>
/// <param name="confidence"></param>
private void OnDictationResult(string text, ConfidenceLevel confidence)
{
Debug.LogFormat("识别结果: {0}", text);
recognitionsText.text = text;
DictionStop();//我是希望得到结果就自动停止输入
}
/// <summary>
/// 语音输入过程中对结果猜想时触发的事件。
/// </summary>
/// <param name="text">识别猜想</param>
private void OnDictationHypothesis(string text)
{
Debug.LogFormat("识别猜想: {0}", text);
hypothesesText.text = text;
}
private void OnDictationComplete(DictationCompletionCause cause)
{
if (cause != DictationCompletionCause.Complete)
Debug.LogErrorFormat("识别失败: {0}.", cause);
}
private void OnDictationError(string error, int hresult)
{
Debug.LogErrorFormat("识别错误: {0}; HResult = {1}.", error, hresult);
}
/// <summary>
/// 开启听写识别会话
/// </summary>
public void DictationStart()
{
dictationRecognizer.Start();
startBtn.gameObject.SetActive(false);
stopBtn.gameObject.SetActive(true);
}
/// <summary>
/// 结束听写识别会话
/// </summary>
public void DictionStop()
{
dictationRecognizer.Stop();
startBtn.gameObject.SetActive(true);
stopBtn.gameObject.SetActive(false);
}
}
2. 百度SDK
也可以考虑其他平台的SDK,我只测试了百度的,这个英文识别不大好(最起码Unity识别不出来,IPad可以识别出来),上面那个中英识别都不错,不过只支持Window,百度的这个我也没测试其他平台,自行测试吧。
官方文档:https://ai.baidu.com/ai-doc/SPEECH/ilbxfvpau文章来源:https://www.toymoban.com/news/detail-720869.html
- 在百度智能云控制台创建一个应用,勾上短语音识别
- 下载C# SDK:https://ai.baidu.com/sdk#asr
- 将SDK 放入Unity中Plugins文件夹下
然后直接上代码吧文章来源地址https://www.toymoban.com/news/detail-720869.html
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Baidu.Aip.Speech;
using UnityEngine.UI;
public class BaiduSpeech : MonoBehaviour
{
public Text text;//显示结果用的
// 设置APPID/AK/SK
string APP_ID = "你的 AppID";
string API_KEY = "你的 API Key";
string SECRET_KEY = "你的 Secret Key";
int rate = 8000;
Asr client;
AudioSource aud;
int audioLength;//录音的长度
private void Awake()
{
if (GetComponent<AudioSource>() == null)
aud = gameObject.AddComponent<AudioSource>();
else
aud = gameObject.GetComponent<AudioSource>();
aud.playOnAwake = false;
}
// Start is called before the first frame update
void Start()
{
client = new Asr(APP_ID, API_KEY, SECRET_KEY);
client.Timeout = 60000; // 修改超时时间
}
/// <summary>
/// 开始录音
/// </summary>
public void StartMic()
{
if (Microphone.devices.Length == 0) return;
Microphone.End(null);
Debug.Log("Start");
aud.clip = Microphone.Start(null, false, 10, rate);
}
/// <summary>
/// 结束录音
/// </summary>
public void EndMic()
{
int lastPos = Microphone.GetPosition(null);
if (Microphone.IsRecording(null))
audioLength = lastPos / rate;//录音时长
else
audioLength = 10;
Debug.Log("Stop");
Microphone.End(null);
AsrData(GetClipData());
}
/// <summary>
/// 把录音转换为Byte[]
/// </summary>
/// <returns></returns>
public byte[] GetClipData()
{
if (aud.clip == null)
{
Debug.LogError("录音数据为空");
return null;
}
float[] samples = new float[aud.clip.samples];
aud.clip.GetData(samples, 0);
byte[] outData = new byte[samples.Length * 2];
int rescaleFactor = 32767; //to convert float to Int16
for (int i = 0; i < samples.Length; i++)
{
short temshort = (short)(samples[i] * rescaleFactor);
byte[] temdata = System.BitConverter.GetBytes(temshort);
outData[i * 2] = temdata[0];
outData[i * 2 + 1] = temdata[1];
}
if (outData == null || outData.Length <= 0)
{
Debug.LogError("录音数据为空");
return null;
}
return outData;
}
// 识别本地文件
public void AsrData(byte[] data)
{
// 可选参数
var options = new Dictionary<string, object>
{
{"dev_pid", 1537}
};
client.Timeout = 120000; // 若语音较长,建议设置更大的超时时间. ms
var result = client.Recognize(data, "pcm", rate, options);
if (result["result"] != null)
{
text.text = result["result"][0].ToString();
}
else
{
Debug.Log("识别错误");
}
Debug.Log(result);
}
}
到了这里,关于Unity 语音转文字的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!