张赐荣 | C#调用 WINDOWS语音识别将WAV音频转文字

C#调用 WINDOWS语音识别将WAV音频转文字

作者:张赐荣


本例使用Windows本地语音识别功能实现语音到文本的转换(支持WAV音频文件)。

public static string SpeechRecognition (string wavPath)
{
try
{
System.Speech.Recognition.SpeechRecognitionEngine sre = new System.Speech.Recognition.SpeechRecognitionEngine();
sre.LoadGrammar(new System.Speech.Recognition.DictationGrammar());
sre.SetInputToWaveFile(wavPath);
string res = null;
StringBuilder sb = new StringBuilder();
do
{
try
{
res = sre.Recognize().Text;
}
catch (Exception)
{
res = null;
}
sb.Append(res);
} while (res != null);
sre.Dispose();
return (sb.ToString());
}
catch (Exception e)
{
return (e.Message);
}
}
 

猜你喜欢

转载自blog.csdn.net/zcr_59186/article/details/123026972