练习 - 创建单次识别语音转文本应用程序
在本练习中,你将创建一个应用程序,该应用程序使用单次识别来转录要下载的示例音频文件。
创建语音转文本应用程序
在右侧的 Cloud Shell 中为应用程序创建目录,然后更改为新目录:
mkdir speech-to-text cd speech-to-text
创建新的 .NET Core 应用程序:
dotnet new console
此命令需要几秒钟才能完成。
创建 .NET Core 应用程序后,将语音 SKD 包添加到应用程序:
dotnet add package Microsoft.CognitiveServices.Speech
此命令需要几秒钟才能完成。
下载 WAVE 文件进行识别
在右侧的 Cloud Shell 中运行以下命令,下载一个示例 WAV 文件,其中包含 William Shakespeare 的《皆大欢喜》剧本的一系列引用。
curl -L https://aka.ms/ShakespeareWAV -o Shakespeare.wav
你将在本练习的应用程序和下个练习的应用程序中使用此 WAV 文件。
添加文本转语音应用程序的代码
在右侧的 Cloud Shell 中,打开 Program.cs 文件:
code Program.cs
删除所有现有代码并添加以下
using
语句,这将为应用程序启用 Azure AI 语音 API:using System.Text; using Microsoft.CognitiveServices.Speech; using Microsoft.CognitiveServices.Speech.Audio;
添加以下代码,这将使用 Azure AI 语音 API 转换之前创建的 WAV 文件的内容,从而使用转录语音创建文本文件。 将
azureKey
和azureLocation
值替换为你在上一个练习中复制的值。string azureKey = "ENTER YOUR KEY FROM THE FIRST EXERCISE"; string azureLocation = "ENTER YOUR LOCATION FROM THE FIRST EXERCISE"; string textFile = "Shakespeare.txt"; string waveFile = "Shakespeare.wav"; try { FileInfo fileInfo = new FileInfo(waveFile); if (fileInfo.Exists) { Console.WriteLine("Speech recognition started."); var speechConfig = SpeechConfig.FromSubscription(azureKey, azureLocation); using var audioConfig = AudioConfig.FromWavFileInput(fileInfo.FullName); using var speechRecognizer = new SpeechRecognizer(speechConfig, audioConfig); var result = await speechRecognizer.RecognizeOnceAsync(); FileStream fileStream = File.OpenWrite(textFile); StreamWriter streamWriter = new StreamWriter(fileStream, Encoding.UTF8); streamWriter.WriteLine(result.Text); streamWriter.Close(); Console.WriteLine("Speech recognition stopped."); } } catch (Exception ex) { Console.WriteLine(ex.Message); }
此代码使用键和位置来初始化与 Azure AI 语音的连接。 然后读取之前下载的 WAV 文件的内容,再使用
SpeechRecognizer
的RecognizeOnceAsync()
方法将语音转换为文本,并使用流编写器将结果保存到文本文件。添加完所有代码后,文件应类似于以下示例:
using System.Text; using System.Threading.Tasks; using Microsoft.CognitiveServices.Speech; using Microsoft.CognitiveServices.Speech.Audio; string azureKey = "ENTER YOUR KEY FROM THE FIRST EXERCISE"; string azureLocation = "ENTER YOUR LOCATION FROM THE FIRST EXERCISE"; string textFile = "Shakespeare.txt"; string waveFile = "Shakespeare.wav"; try { FileInfo fileInfo = new FileInfo(waveFile); if (fileInfo.Exists) { Console.WriteLine("Speech recognition started."); var speechConfig = SpeechConfig.FromSubscription(azureKey, azureLocation); using var audioConfig = AudioConfig.FromWavFileInput(fileInfo.FullName); using var speechRecognizer = new SpeechRecognizer(speechConfig, audioConfig); var result = await speechRecognizer.RecognizeOnceAsync(); FileStream fileStream = File.OpenWrite(textFile); StreamWriter streamWriter = new StreamWriter(fileStream, Encoding.UTF8); streamWriter.WriteLine(result.Text); streamWriter.Close(); Console.WriteLine("Speech recognition stopped."); } } catch (Exception ex) { Console.WriteLine(ex.Message); }
请确保使用上一个练习中的键和位置更新
azureKey
和azureLocation
变量的值。若要保存更改,请按 Ctrl+S 保存文件,然后按 Ctrl+Q 退出编辑器。
运行应用程序
若要运行应用程序,请在右侧的 Cloud Shell 中使用以下命令:
dotnet run
如果未看到任何错误,则表示应用程序已成功运行,并且应看到显示以下响应:
Speech recognition started. Speech recognition stopped.
运行以下命令以获取目录中的文件列表:
ls -l
应会看到类似以下示例的响应,并会在文件列表中看到 Shakespeare.txt:
drwxr-xr-x 3 user user 4096 Oct 1 11:11 bin drwxr-xr-x 3 user user 4096 Oct 1 11:11 obj -rw-r--r-- 1 user user 1476 Oct 1 11:11 Program.cs -rw-r--r-- 1 user user 98 Oct 1 11:11 Shakespeare.txt -rwxr-xr-x 1 user user 978242 Oct 1 11:11 Shakespeare.wav -rw-r--r-- 1 user user 348 Oct 1 11:11 speech to text.csproj
你会注意到文本文件的大小较小;在本例中,它只有 98 个字节。
若要查看 Shakespeare.txt 文件的内容,请使用以下命令:
cat Shakespeare.txt
应会看到一个响应,如下例所示:
The following quotes are from Act 2, scene seven of William Shakespeare's play as you like it.
如果收听了示例 WAV 文件,你会注意到此文本只是音频的前几秒钟。 由于我们使用了
SpeechRecognizer
的RecognizeOnceAsync()
方法,因此当说话人暂停时,语音转文本识别也会停止。
在下个练习中,你可了解如何继续整个音频文件的语音转文本识别。