微软认知语音服务 语音识别

时间:2024-03-09 15:58:57

官网

https://azure.microsoft.com/zh-cn/services/cognitive-services/speech-services/

先使用七天免费的认知服务

然后会获得两个密钥,在开发时会用到密钥,这两个密钥用哪个都可以

 

然后进入正式开发

首先引入包

install-package Microsoft.CognitiveServices.Speech

引入之后,打开配置管理器

活动解决方案平台 新建

64位系统的选x64,32位系统的选x86,然后确定

 确定下项目的平台都是x64(x86),因为微软的语音识别无法在any cpu上运行,不更改平台运行就会报错

接下来就可以开始写代码了

 

首先创建识别器对象

//创建配置对象 参数1是服务订阅的密匙,参数2是订阅关联的区域(免费试用版区域都是westus)
            var config = SpeechConfig.FromSubscription("密匙", "区域");
            //识别的语言设为中文
            config.SpeechRecognitionLanguage = "zh-CN";
            // 创建识别器对象.
            using (var recognizer = new SpeechRecognizer(config))
            {
            }

单次识别,只识别一段话并返回结果

                //开始录入,并返回结果
                var result = await recognizer.RecognizeOnceAsync();    

返回的结果,具有几种状态

                if (result.Reason == ResultReason.RecognizedSpeech)//语音识别成功
                {
                    Console.WriteLine($"接到的语音为: {result.Text}");
                }
                else if (result.Reason == ResultReason.NoMatch)//未识别到语音
                {
                    Console.WriteLine($"没有识别到语音");
                }
                else if (result.Reason == ResultReason.Canceled)//取消识别
                {
                    var cancellation = CancellationDetails.FromResult(result);
                    Console.WriteLine($"CANCELED: Reason={cancellation.Reason}");

                    if (cancellation.Reason == CancellationReason.Error)//识别出错
                    {
                        Console.WriteLine($"CANCELED: ErrorCode={cancellation.ErrorCode}");
                        Console.WriteLine($"CANCELED: ErrorDetails={cancellation.ErrorDetails}");
                        Console.WriteLine($"CANCELED: Did you update the subscription info?");
                    }
                }

这就是简单的单次识别,下面是完整代码

        /// <summary>
        /// 普通识别语音
        /// </summary>
        /// <returns></returns>
        public static async Task RecognizeSpeechAsync()
        {
            //创建配置对象 参数1是服务订阅的密匙,参数2是订阅关联的区域(免费试用版区域都是westus)
            var config = SpeechConfig.FromSubscription("密匙", "区域");
            //识别的语言设为中文
            config.SpeechRecognitionLanguage = "zh-CN";
            // 创建识别器对象.
            using (var recognizer = new SpeechRecognizer(config))
            {
                Console.WriteLine("请说一句话...");

                //开始录入,并返回结果
                var result = await recognizer.RecognizeOnceAsync();

                
                if (result.Reason == ResultReason.RecognizedSpeech)//语音识别成功
                {
                    Console.WriteLine($"接到的语音为: {result.Text}");
                }
                else if (result.Reason == ResultReason.NoMatch)//未识别到语音
                {
                    Console.WriteLine($"没有识别到语音");
                }
                else if (result.Reason == ResultReason.Canceled)//取消识别
                {
                    var cancellation = CancellationDetails.FromResult(result);
                    Console.WriteLine($"CANCELED: Reason={cancellation.Reason}");

                    if (cancellation.Reason == CancellationReason.Error)//识别出错
                    {
                        Console.WriteLine($"CANCELED: ErrorCode={cancellation.ErrorCode}");
                        Console.WriteLine($"CANCELED: ErrorDetails={cancellation.ErrorDetails}");
                        Console.WriteLine($"CANCELED: Did you update the subscription info?");
                    }
                }
            }
        }    

接下来是识别语音文件

需要创建一个音频文件的配置对象,然后把对象传给识别器对象

                //读取要识别的语音文件
                using (var audioInput = AudioConfig.FromWavFileInput(fullpath))
                {
                    //创建识别器对象
                    using (var recognizer = new SpeechRecognizer(config, audioInput))
                    {
                    }
                }

识别文件是需要持续识别的,所以不能用上面的单次识别方法了

持续识别

                        // 开始连续识别
                        await recognizer.StartContinuousRecognitionAsync().ConfigureAwait(false);

                        // 结束持续识别
                        await recognizer.StopContinuousRecognitionAsync().ConfigureAwait(false);    

持续识别是有几个事件的

识别开始和结束时执行

                        //开始时执行
                        recognizer.SessionStarted += (s, e) =>
                        {
                            Console.WriteLine("\n   开始识别.");
                        };
                        //结束时执行
                        recognizer.SessionStopped += (s, e) =>
                        {
                            Console.WriteLine("\n    识别结束.");
                        };

识别中,识别完成,识别取消时执行

                        // 识别中 (每识别一个词都会执行一次)
                        recognizer.Recognizing += (s, e) =>
                        {
                            Console.WriteLine($"识别中:{e.Result.Text}");
                        };
                        // 识别完成后 (整段语音识别完成后会执行一次)
                        recognizer.Recognized += (s, e) =>
                        {
                            if (e.Result.Reason == ResultReason.RecognizedSpeech) //识别成功
                            {
                                Console.WriteLine($"识别完成: {e.Result.Text}");
                            }
                            else if (e.Result.Reason == ResultReason.NoMatch)//未识别到语音
                            {
                                Console.WriteLine($"没有识别到语音");
                            }
                        };
                        //识别取消时执行
                        recognizer.Canceled += (s, e) =>
                        {
                            Console.WriteLine($"取消识别: 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?");
                            }

                        };

下面是完整代码

        /// <summary>
        /// 识别文件中的语音
        /// </summary>
        /// <returns></returns>
        public static async Task RecognizeFileAsync()
        {
            try
            {
                //创建配置对象 参数1是服务订阅的密匙,参数2是订阅关联的区域(免费试用版区域都是westus)
                var config = SpeechConfig.FromSubscription("密匙", "区域");
                //创建一个异步任务数组
                var stopRecognition = new TaskCompletionSource<int>();
                //相对路径转绝对路径
                string fullpath = Path.GetFullPath(@"./whatstheweatherlike.wav");
                
                //读取要识别的语音文件
                using (var audioInput = AudioConfig.FromWavFileInput(fullpath))
                {
                    //创建识别器对象
                    using (var recognizer = new SpeechRecognizer(config, audioInput))
                    {
                        // 识别中 (每识别一个词都会执行一次)
                        recognizer.Recognizing += (s, e) =>
                        {
                            Console.WriteLine($"识别中:{e.Result.Text}");
                        };
                        // 识别完成后 (整段语音识别完成后会执行一次)
                        recognizer.Recognized += (s, e) =>
                        {
                            if (e.Result.Reason == ResultReason.RecognizedSpeech) //识别成功
                            {
                                Console.WriteLine($"识别完成: {e.Result.Text}");
                            }
                            else if (e.Result.Reason == ResultReason.NoMatch)//未识别到语音
                            {
                                Console.WriteLine($"没有识别到语音");
                            }
                        };
                        //识别取消时执行
                        recognizer.Canceled += (s, e) =>
                        {
                            Console.WriteLine($"取消识别: 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?");
                            }

                        };
                        //开始时执行
                        recognizer.SessionStarted += (s, e) =>
                        {
                            Console.WriteLine("\n   开始识别.");
                        };
                        //结束时执行
                        recognizer.SessionStopped += (s, e) =>
                        {
                            Console.WriteLine("\n    识别结束.");
                            stopRecognition.TrySetResult(0); //结束时添加一个异步任务
                        };

                        // 开始连续识别
                        await recognizer.StartContinuousRecognitionAsync().ConfigureAwait(false);

                        //保证至少一个任务完成(等待到结束时间执行后再结束)
                        Task.WaitAny(new[] { stopRecognition.Task });

                        // 结束持续识别
                        await recognizer.StopContinuousRecognitionAsync().ConfigureAwait(false);
                    }
                }
            }
            catch (Exception ex)
            {

                throw ex;
            }
            
        }

 

当然持续语音识别不只是用来识别文件的,也可以正常的用来识别麦克风,只要不传识别器对象的第二个参数就行了

 

顺便记录一个计时器

//计时器对象
Stopwatch stopwatch = new Stopwatch();
//开始计时
stopwatch.Start();
//结束计时
stopwatch.Stop();
//返回毫秒数
stopwatch.ElapsedMilliseconds

 

这些都是语音识别的简单应用,还有更加复杂的使用方式,比如和和LUIS配合使用,完成语音识别意图

这些就自己去看官方文档吧

https://docs.microsoft.com/zh-cn/azure/cognitive-services/speech-service/