微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

如何获取时间戳以在 Azure 语音转文本模型中生成?

如何解决如何获取时间戳以在 Azure 语音转文本模型中生成?

我正在尝试使用 Azure 的语音转文本代码生成和收集数据。我想生成时间戳,减少输出中的冗余,并导出到 Excel。下面的代码运行没有错误

using System;
using System.Threading.Tasks;
using Microsoft.CognitiveServices.Speech;
using Microsoft.CognitiveServices.Speech.Audio;

namespace nesT
{
internal class NewBaseType
{
    static async Task Main(string[] args)

    {
        // Creates an instance of a speech config with specified subscription key and region.
        // Replace with your own subscription key and service region (e.g.,"westus").
        var config = SpeechConfig.FromSubscription("subscriptionkey","region");

        // Generates timestamps
        config.OutputFormat = OutputFormat.Detailed;
        config.RequestWordLevelTimestamps();

        //calls the audio file
        using (var audioInput = AudioConfig.FromWavFileInput("C:/Users/MichaelSchwartz/source/repos/AI-102-Process-Speech-master/transcribe_speech_to_text/media/narration.wav"))

        // Creates a speech recognizer from microphone.
        using (var recognizer = new SpeechRecognizer(config,audioInput))
        {
            // Subscribes to events.
            recognizer.Recognizing += (s,e) =>
            {
                Console.WriteLine($"RECOGNIZING: Text={e.Result.Text}");
            };

            recognizer.Recognized += (s,e) =>
            {
                var result = e.Result;
                Console.WriteLine($"Reason: {result.Reason.ToString()}");
                if (result.Reason == ResultReason.RecognizedSpeech)
                {
                    Console.WriteLine($"Final result: Text: {result.Text}.");
                }
            };

            recognizer.Canceled += (s,e) =>
            {
                Console.WriteLine($"\n    Canceled. Reason: {e.Reason.ToString()},CanceledReason: {e.Reason}");
            };

            recognizer.SessionStarted += (s,e) =>
            {
                Console.WriteLine("\n    Session started event.");
            };

            recognizer.SessionStopped += (s,e) =>
            {
                Console.WriteLine("\n    Session stopped event.");
            };

            recognizer.Recognized += (s,e) => 
            { 
                var j = e.Result.Properties.GetProperty(PropertyId.SpeechServiceResponse_JsonResult);
            };

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

            do
            {
                Console.WriteLine("Press Enter to stop");
            } while (Console.ReadKey().Key != ConsoleKey.Enter);

            // Stops recognition.
            await recognizer.StopContinuousRecognitionAsync().ConfigureAwait(false);
        }
    }
}
}

当我运行它时,我看不到时间戳数据。如何生成时间戳数据?

另外,有没有办法消除输出中的冗余?示例:

RECOGNIZING: Text=the
RECOGNIZING: Text=the speech
RECOGNIZING: Text=the speech translation
RECOGNIZING: Text=the speech translation API
RECOGNIZING: Text=the speech translation API transcribes
RECOGNIZING: Text=the speech translation API transcribes audio

我只想要最终结果。有没有办法在保持准确性的同时从输出删除“识别:”数据?提前致谢!

解决方法

要删除"RECOGNIZING:",只需删除这句话:

recognizer.Recognizing += (s,e) =>
{
    Console.WriteLine($"RECOGNIZING: Text={e.Result.Text}");
};

我没有看到您将结果和时间戳导出到 Excel 的位置。您可以在获得 SpeechRecognitionResult 对象后使用此代码:

var json = result.Properties.GetProperty(PropertyId.SpeechServiceResponse_JsonResult);
Console.WriteLine(json);

输出结果为: enter image description here

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。