c# - C# System.Speech.Recognition 没有命令?
问题描述
我认为这与C# system.speech.recognition alter words是同一个问题,但是答案不起作用。Microsoft API 需要使用命令。如果我不输入命令,它会显示一条消息,说明它是必需的。如果我添加一个命令,那就是唯一遇到的文本。我想写一些东西来决定我所说的每一个字。像 MS Agent 那样的事情在当天就做了。任何人都有一些方向,因为我失败了并且不想使用 Google Cloud API。我希望它在本地运行。
using System;
using System.Speech.Recognition;
using System.Speech.Synthesis;
using System.Globalization;
using System.Threading;
using System.Collections.Generic;
namespace S2TextDemo
{
class Program
{
static SpeechSynthesizer ss = new SpeechSynthesizer();
static SpeechRecognitionEngine sre;
static bool speechOn = true;
static private AutoResetEvent _quitEvent;
static void Main(string[] args)
{
try
{
_quitEvent = new AutoResetEvent(false);
ss.SetOutputToDefaultAudioDevice();
CultureInfo ci = new CultureInfo("en-us");
sre = new SpeechRecognitionEngine(ci);
sre.SetInputToDefaultAudioDevice();
sre.SpeechRecognized += sre_SpeechRecognized;
//sre.SpeechRecognized += SpeechRecognizedHandler;
Choices ch_StartStopCommands = new Choices();
ch_StartStopCommands.Add("quit");
GrammarBuilder gb_StartStop = new GrammarBuilder();
gb_StartStop.Append(ch_StartStopCommands);
Grammar g_StartStop = new Grammar(gb_StartStop);
sre.LoadGrammarAsync(g_StartStop);
sre.RecognizeAsync(RecognizeMode.Multiple);
Console.WriteLine("Listening...\n");
ss.SpeakAsync("I'm now listening.");
_quitEvent.WaitOne();
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
Console.ReadLine();
}
} // Main
static void SpeechRecognizedHandler(object sender, SpeechRecognizedEventArgs e)
{
if (e.Result == null) return;
string txt = e.Result.Text;
// Add event handler code here.
// The following code illustrates some of the information available
// in the recognition result.
Console.WriteLine("Grammar({0}), {1}: {2}",
e.Result.Grammar.Name, e.Result.Audio.Duration, e.Result.Text);
// Display the semantic values in the recognition result.
foreach (KeyValuePair<String, SemanticValue> child in e.Result.Semantics)
{
Console.WriteLine(" {0} key: {1}",
child.Key, child.Value.Value ?? "null");
}
Console.WriteLine();
// Display information about the words in the recognition result.
foreach (RecognizedWordUnit word in e.Result.Words)
{
RecognizedAudio audio = e.Result.GetAudioForWordRange(word, word);
Console.WriteLine(" {0,-10} {1,-10} {2,-10} {3} ({4})",
word.Text, word.LexicalForm, word.Pronunciation,
audio.Duration, word.DisplayAttributes);
}
// Display the recognition alternates for the result.
foreach (RecognizedPhrase phrase in e.Result.Alternates)
{
Console.WriteLine(" alt({0}) {1}", phrase.Confidence, phrase.Text);
}
}
static void sre_SpeechRecognized(object sender, SpeechRecognizedEventArgs e)
{
double minConfidence = 0.90;
string txt = e.Result.Text;
float confidence = e.Result.Confidence;
Console.WriteLine("\nRecognized: " + txt);
if (confidence < minConfidence)
{
Console.WriteLine($"Failed confidence: {minConfidence} with {confidence}" );
return;
}
if (txt.IndexOf("quit") >= 0)
{
if(speechOn)
ss.SpeakAsync("Shutting down.");
else
Console.WriteLine("Shutting down.");
Thread.Sleep(1000);
_quitEvent.Set();
}
} // sre_SpeechRecognized
} // Program
} // ns
解决方案
推荐阅读
- mysql - 如何加入没有记录的表?
- c++ - 如何使 cmake 支持 gcov
- machine-learning - (交叉)CNN 模型验证——何时引入测试数据?
- intellij-idea - 如何将 Emmet 片段导入 IntelliJ?
- javascript - 使搜索框不区分大小写
- reactjs - Facebook 登录重定向 uri 错误:url 被阻止,重定向失败
- graphql - graphQLErrors 未定义,如何从 apollo 响应中提取错误
- javascript - 在现有项目中配置 jest 和酶会引发错误
- java - 在 Java 代码中使用纯文本凭据的替代策略
- c# - 我无法安装软件包 System.Net.Http.WebRequest