Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/314.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# Azure语音到文本在3-4分钟后停止工作_C#_Azure_Speech To Text - Fatal编程技术网

C# Azure语音到文本在3-4分钟后停止工作

C# Azure语音到文本在3-4分钟后停止工作,c#,azure,speech-to-text,C#,Azure,Speech To Text,我试图转录10-20分钟长的音频文件。Azure语音到文本在约3分20秒后停止。我使用的是连续识别,我不确定问题出在哪里。这篇文章中的解决方案现在是一个失效的链接: 我的代码是: using Microsoft.CognitiveServices.Speech; using Microsoft.CognitiveServices.Speech.Audio; using Newtonsoft.Json; using System; using System.Collections.Generic

我试图转录10-20分钟长的音频文件。Azure语音到文本在约3分20秒后停止。我使用的是连续识别,我不确定问题出在哪里。这篇文章中的解决方案现在是一个失效的链接:

我的代码是:

using Microsoft.CognitiveServices.Speech;
using Microsoft.CognitiveServices.Speech.Audio;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using System.Linq;

namespace STTwithTime
{
class Program
{


    static void Main(string[] args)
    {
        var key = "subscriptionkey";
        var region = "region";
        var audioFilePath = @"";
        var speechConfig = SpeechConfig.FromSubscription(key, region);

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

        var stopRecognition = new TaskCompletionSource<int>();

        var audioConfig = AudioConfig.FromWavFileInput(audioFilePath);
        var recognizer = new SpeechRecognizer(speechConfig, audioConfig);

        //Display Recognized
        recognizer.Recognized += (s, e) =>
        {
            if (e.Result.Reason == ResultReason.RecognizedSpeech)
            {
                
                var result = JsonConvert.DeserializeObject<Result>(e.Result.Properties.GetProperty(PropertyId.SpeechServiceResponse_JsonResult));
                
                var maxConfidenceValue = result.NBest.Max(item => item.Confidence);
                var maxConfidence =  result.NBest.Find(item => item.Confidence == maxConfidenceValue);
                Console.WriteLine("================================");
                Console.WriteLine("Confidence:"+maxConfidence.Confidence);
                Console.WriteLine("RECOGNIZED :" + maxConfidence.Display);
                Console.WriteLine("Duration: :" + Convert.ToDouble(result.Duration) / 10000000);
                Console.WriteLine("Words:");
                foreach (var word in maxConfidence.Words) {
                    Console.WriteLine(word.word + "=> offset:" + Convert.ToDouble(word.Offset) / 10000000 + " duration:" + Convert.ToDouble(word.Duration) / 10000000);
                }
                
            }
            else if (e.Result.Reason == ResultReason.NoMatch)
            {
                Console.WriteLine($"NOMATCH: Speech could not be recognized.");
            }
        };

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

            if (e.Reason == CancellationReason.Error)
            {
                Console.WriteLine($"CANCELED: ErrorCode={e.ErrorCode}");
                Console.WriteLine($"CANCELED: ErrorDetails={e.ErrorDetails}");
                Console.WriteLine($"CANCELED: Did you update the subscription info?");
            }

            stopRecognition.TrySetResult(0);
        };

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

        recognizer.StartContinuousRecognitionAsync().GetAwaiter().GetResult();

        // Waits for completion. Use Task.WaitAny to keep the task rooted.
        Task.WaitAny(new[] { stopRecognition.Task });

        // Keeps external console from closing too quickly
        string v = Console.ReadLine();


    }

}

public class Word
{
    public int Duration { get; set; }
    public int Offset { get; set; }
    public string word { get; set; }
}

public class NBest
{
    public double Confidence { get; set; }
    public string Display { get; set; }
    public string ITN { get; set; }
    public string Lexical { get; set; }
    public string MaskedITN { get; set; }
    public List<Word> Words { get; set; }
}

public class Result
{
    public string DisplayText { get; set; }
    public int Duration { get; set; }
    public string Id { get; set; }
    public List<NBest> NBest { get; set; }
    public int Offset { get; set; }
    public string RecognitionStatus { get; set; }
}
}
使用Microsoft.CognitiveServices.Speech;
使用Microsoft.CognitiveServices.Speech.Audio;
使用Newtonsoft.Json;
使用制度;
使用System.Collections.Generic;
使用System.Threading.Tasks;
使用System.Linq;
命名空间STTwithTime
{
班级计划
{
静态void Main(字符串[]参数)
{
var key=“subscriptionkey”;
var region=“region”;
var audioFilePath=@”;
var speechConfig=speechConfig.FromSubscription(键,区域);
//生成时间戳
RequestWordLevelTimestamps();
speechConfig.OutputFormat=OutputFormat.Detailed;
var stopRecognition=new TaskCompletionSource();
var audioConfig=audioConfig.FromWavFileInput(audioFilePath);
var识别器=新的语音识别器(speechConfig、audioConfig);
//显示识别
识别器。已识别+=(s,e)=>
{
if(e.Result.Reason==ResultReason.RecognizedSpeech)
{
var result=JsonConvert.DeserializeObject(e.result.Properties.GetProperty(PropertyId.SpeechServiceResponse_JsonResult));
var maxConfidenceValue=result.NBest.Max(item=>item.Confidence);
var maxConfidence=result.NBest.Find(item=>item.Confidence==maxConfidenceValue);
Console.WriteLine(“=================================================”);
Console.WriteLine(“Confidence:+maxConfidence.Confidence”);
Console.WriteLine(“已识别:+maxConfidence.Display”);
Console.WriteLine(“持续时间::”+Convert.ToDouble(result.Duration)/10000000);
控制台。WriteLine(“文字:”);
foreach(maxConfidence.Words中的var单词){
Console.WriteLine(word.word+“=>偏移量:“+Convert.ToDouble(word.offset)/10000000+”持续时间:“+Convert.ToDouble(word.duration)/10000000”;
}
}
else if(e.Result.Reason==ResultReason.NoMatch)
{
Console.WriteLine($“无法识别NOMATCH:语音”);
}
};
识别器。已取消+=(s,e)=>
{
WriteLine($“已取消:原因={e.Reason}”);
如果(e.Reason==CancellationReason.Error)
{
WriteLine($“已取消:ErrorCode={e.ErrorCode}”);
WriteLine($“已取消:ErrorDetails={e.ErrorDetails}”);
Console.WriteLine($“已取消:是否更新了订阅信息?”);
}
停止识别。TrySetResult(0);
};
recognizer.SessionStopped+=(s,e)=>
{
Console.WriteLine(“\n会话停止事件”);
停止识别。TrySetResult(0);
};
识别器.StartContinuousRecognitionAsync().GetWaiter().GetResult();
//等待完成。请使用Task.WaitAny使任务保持根目录。
Task.WaitAny(新[]{stopRecognition.Task});
//防止外部控制台关闭过快
字符串v=Console.ReadLine();
}
}
公共类词
{
公共整数持续时间{get;set;}
公共整数偏移量{get;set;}
公共字符串字{get;set;}
}
公共类NBest
{
公众双重信心{get;set;}
公共字符串显示{get;set;}
公共字符串ITN{get;set;}
公共字符串词法{get;set;}
公共字符串MaskedITN{get;set;}
公共列表字{get;set;}
}
公开课成绩
{
公共字符串DisplayText{get;set;}
公共整数持续时间{get;set;}
公共字符串Id{get;set;}
公共列表NBest{get;set;}
公共整数偏移量{get;set;}
公共字符串识别状态{get;set;}
}
}

我如何解决这个问题?我需要转录整个音频文件。有什么解决办法吗?提前感谢。

该链接在Azure中已经过时了:-)这个.NET核心示例怎么样?->我根据这些示例编写了上述代码。仍然有问题的转录停止约200秒到每个音频文件。不会返回任何错误。输出日志不报告任何警告。集成终端表示,根据软件包的不同,部分软件包正在恢复到NETFramework版本4.6或4.7。我的项目是使用net5.0目标框架编写的。