来自 http Stream 的语音识别服务 wav 文件

问题描述 投票:0回答:0

当我尝试将 wav 文件从 url 下载到 AudioInputStream 时,我得到的转录结果很差。

HttpResponseMessage wavresponse = await _httpClient.GetAsync($"{recordingUrl}.wav").ConfigureAwait(continueOnCapturedContext: false);
using var httpStream = await wavresponse.Content.ReadAsStreamAsync();

var speechConfig = SpeechConfig.FromSubscription(_speechOptions.Key, _speechOptions.Region);
speechConfig.SpeechRecognitionLanguage = "de-DE";

var reader = new BinaryReader(httpStream);
using var audioInputStream = AudioInputStream.CreatePushStream();

//using var audioConfig2 = AudioConfig.FromWavFileInput($"{recordingUrl}.wav");
using var audioConfig = AudioConfig.FromStreamInput(audioInputStream);
using var recognizer = new SpeechRecognizer(speechConfig, audioConfig);

byte[] readBytes;
do
{
    readBytes = reader.ReadBytes(1024);
    audioInputStream.Write(readBytes, readBytes.Length);
} while (readBytes.Length > 0);

var stopRecognition = new TaskCompletionSource<int>();

var textRecognition = "";

recognizer.Recognized += (s, e) =>
{
    if (e.Result.Reason == ResultReason.RecognizedSpeech)
    {
        textRecognition += e.Result.Text;
    }
};

recognizer.Canceled += (s, e) =>
{
    stopRecognition.TrySetResult(0);
};
recognizer.SessionStopped += (s, e) =>
{
    stopRecognition.TrySetResult(0);
};

// Starts continuous recognition. Uses StopContinuousRecognitionAsync() to stop recognition.
await recognizer.StartContinuousRecognitionAsync().ConfigureAwait(false);

// Waits for completion.
// Use Task.WaitAny to keep the task rooted.
System.Threading.Tasks.Task.WaitAny(new[] { stopRecognition.Task });
// Stops recognition.
await recognizer.StopContinuousRecognitionAsync().ConfigureAwait(false);

当我下载 wav 文件并将该文件用作输入而不是 http 流时,相同的文件给出了非常准确的结果。我认为这一行可能有问题,但我不确定。

var reader = new BinaryReader(httpStream);
.net azure speech-recognition speech-to-text azure-cognitive-services
© www.soinside.com 2019 - 2024. All rights reserved.