Windows 10 语音识别 [英] Windows 10 Speech Recognition
问题描述
我想用 c# 为 windows 10 创建一个 WPF 应用程序.现在,我在以前的 windows 版本中遇到的问题是我是意大利语,并且不支持意大利语的语音识别.但现在有 Cortana.那么,如何在我的应用程序中使用 cortana 的语音识别引擎?如果我只是使用 new SpeechRecognitionEngine(new CultureInfo("it-IT")));
它会给我一个错误,因为没有简单的识别引擎,所以我必须使用 cortana 的.希望你能理解我的英语不好.谢谢你的回答.
I want to create a WPF application in c# for windows 10. Now, the problem that i had with previous windows versions was that i'm italian and there isn't a support for speech recognition in italian. But now there is cortana. So, how can i use cortana's speech recognition engine for my application? If i simply use new SpeechRecognitionEngine(new CultureInfo("it-IT")));
it gives me an error, 'cause there isn't the simple recongition engine, so i have to use cortana's one. Hope you understood and sorry for my bad english. Thank you for your answer.
推荐答案
为了使用新的 SpeechRecognition WinRT API 在 Windows 10 中发布,您需要向桌面 C# 应用程序添加对 WinRT API 的支持.这不需要将应用程序转换为 Windows 应用商店应用程序,但是,至少在某些部分是这样.据我所知,新引擎尚未向后移植以向 System.Speech.SpeechRecognitionEngine 添加支持,该引擎仍使用旧版识别器(我将在此处与语音团队联系,如果我发现更多信息,请在此帖子中跟进在这一点上.)
In order to use the new SpeechRecognition WinRT API released in windows 10, you're going to need to add support for WinRT APIs to your desktop C# application. This doesn't require converting the app to a Windows Store app, however, at least, for some parts. So far as I know, the new engine hasn't been backported to add support into System.Speech.SpeechRecognitionEngine, that still uses a legacy recognizer (I'll check with the speech team here and follow up in this post if I find more on that point.)
基于从此处获得的指导 和 here,我能够创建一个经典的 c# WPF 应用程序,并实现以下代码:
Based on the guidance taken from here and here, I was able to create a classic c# WPF app, and implement the following code:
private SpeechRecognizer reco;
public MainWindow()
{
InitializeComponent();
reco = new SpeechRecognizer();
List<string> constraints = new List<string>();
constraints.Add("Yes");
constraints.Add("No");
reco.Constraints.Add(new SpeechRecognitionListConstraint(constraints));
IAsyncOperation<SpeechRecognitionCompilationResult> op = reco.CompileConstraintsAsync();
op.Completed += HandleCompilationCompleted;
}
public void HandleCompilationCompleted(IAsyncOperation<SpeechRecognitionCompilationResult> opInfo, AsyncStatus status)
{
if(status == AsyncStatus.Completed)
{
System.Diagnostics.Debug.WriteLine("CompilationCompleted");
var result = opInfo.GetResults();
System.Diagnostics.Debug.WriteLine(result.Status.ToString());
}
}
为了让这个编译,我添加了
In order to get this to compile, I added
<PropertyGroup>
<TargetPlatformVersion>10.0</TargetPlatformVersion>
</PropertyGroup>
到.csproj,并从项目->添加引用->通用Windows->核心部分添加了Windows.Media和Windows.Foundation,我还手动添加了对
to the .csproj, and added Windows.Media and Windows.Foundation from the Project -> Add References -> Universal Windows -> Core section, and I also manually added references to
C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETCore\v4.5.1\System.Runtime.WindowsRuntime.dll
和
C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETCore\v4.5.1\System.Runtime.InteropServices.WindowsRuntime.dll
通过添加引用的浏览部分.
via the browse section of Add References.
您需要检查 SpeechRecognizer.SupportedGrammarLanguages 以检索 it-IT Language 对象以将其传递给 Recognizer 构造函数,如果您的系统尚未默认使用 it-IT.(如果您安装的是意大利语版本的 Windows 10,则默认情况下会发生这种情况)
You'll need to check the SpeechRecognizer.SupportedGrammarLanguages to retrieve the it-IT Language object to pass it to the Recognizer constructor, if your system isn't defaulting to it-IT already. (IF you installed an Italian version of windows 10, this should happen by default)
现在,我上面的代码片段只编译了一个超级简单的语法,它没有开始识别.为此,您需要查阅 Windows.Media.SpeechRecognition API 的其余部分,但大致相同.
Now, my code snippet above only compiles a super simple grammar, it doesn't start recognition. You'll need to consult the rest of the Windows.Media.SpeechRecognition API for that, but it's along the same lines.
这篇关于Windows 10 语音识别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!