为除激活语音识别之外的每个命令停用语音识别
deactivate speech recognition for ervery command except for activate speech recognition
所以我有很多用 visual studio (2019) c# System.Speech 编程的语音助手命令,现在我想添加一个停用语音识别的命令,直到我说激活语音识别。
我尝试通过互联网查看,但我还没有找到任何东西,而且我自己也不知道值得尝试的方法。单独停用所有其他命令会很费力。
有谁知道怎么做或有什么想法吗?
谢谢你的帮助,phantomica
根据您的描述,当出现关机命令时,您想停用
除了激活语音识别之外的每个命令的语音识别。
您可以按照以下步骤来实现您的要求:
设置一个全局静态变量speechOn,默认值为true。
尝试将每个已识别的语音转换为文本并将其存储在变量中。然后使用此变量与“语音开启”和“语音关闭”进行比较。
通过第二步确定speechOn的布尔值。如果为假,则使用 return;结束识别方法。如果为真,继续识别方法。
我的测试代码如下,测试成功
using System;
using System.Globalization;
using System.Speech.Recognition;
using System.Speech.Synthesis;
namespace ConsoleSpeech
{
class ConsoleSpeechProgram
{
static SpeechSynthesizer ss = new SpeechSynthesizer();
static SpeechRecognitionEngine sre;
static bool done = false;
static bool speechOn = true;
static void Main(string[] args)
{
try
{
ss.SetOutputToDefaultAudioDevice();
Console.WriteLine("\n(Speaking: I am awake)");
ss.Speak("I am awake");
CultureInfo ci = new CultureInfo("en-us");
sre = new SpeechRecognitionEngine(ci);
sre.SetInputToDefaultAudioDevice();
sre.SpeechRecognized += sre_SpeechRecognized;
Choices ch_StartStopCommands = new Choices();
ch_StartStopCommands.Add("speech on");
ch_StartStopCommands.Add("speech off");
ch_StartStopCommands.Add("klatu barada nikto");
GrammarBuilder gb_StartStop = new GrammarBuilder();
gb_StartStop.Append(ch_StartStopCommands);
Grammar g_StartStop = new Grammar(gb_StartStop);
Choices ch_Numbers = new Choices();
ch_Numbers.Add("1");
ch_Numbers.Add("2");
ch_Numbers.Add("3");
ch_Numbers.Add("4");
GrammarBuilder gb_WhatIsXplusY = new GrammarBuilder();
gb_WhatIsXplusY.Append("What is");
gb_WhatIsXplusY.Append(ch_Numbers);
gb_WhatIsXplusY.Append("plus");
gb_WhatIsXplusY.Append(ch_Numbers);
Grammar g_WhatIsXplusY = new Grammar(gb_WhatIsXplusY);
sre.LoadGrammarAsync(g_StartStop);
sre.LoadGrammarAsync(g_WhatIsXplusY);
sre.RecognizeAsync(RecognizeMode.Multiple);
while (done == false) {; }
Console.WriteLine("\nHit <enter> to close shell\n");
Console.ReadLine();
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
Console.ReadLine();
}
} // Main
static void sre_SpeechRecognized(object sender,
SpeechRecognizedEventArgs e)
{
string txt = e.Result.Text;
float confidence = e.Result.Confidence;
Console.WriteLine("\nRecognized: " + txt);
if (confidence < 0.60) return;
if (txt.IndexOf("speech on") >= 0)
{
Console.WriteLine("Speech is now ON");
speechOn = true;
}
if (txt.IndexOf("speech off") >= 0)
{
Console.WriteLine("Speech is now OFF");
speechOn = false;
}
if (speechOn == false) return;
if (txt.IndexOf("klatu") >= 0 && txt.IndexOf("barada") >= 0)
{
((SpeechRecognitionEngine)sender).RecognizeAsyncCancel();
done = true;
Console.WriteLine("(Speaking: Farewell)");
ss.Speak("Farewell");
}
if (txt.IndexOf("What") >= 0 && txt.IndexOf("plus") >= 0)
{
string[] words = txt.Split(' ');
int num1 = int.Parse(words[2]);
int num2 = int.Parse(words[4]);
int sum = num1 + num2;
Console.WriteLine("(Speaking: " + words[2] + " plus " +
words[4] + " equals " + sum + ")");
ss.SpeakAsync(words[2] + " plus " + words[4] +
" equals " + sum);
}
} // sre_SpeechRecognized
} // Program
} // ns
Voice Recognition : Speech Recognition with .NET Desktop Applications
所以我有很多用 visual studio (2019) c# System.Speech 编程的语音助手命令,现在我想添加一个停用语音识别的命令,直到我说激活语音识别。
我尝试通过互联网查看,但我还没有找到任何东西,而且我自己也不知道值得尝试的方法。单独停用所有其他命令会很费力。
有谁知道怎么做或有什么想法吗?
谢谢你的帮助,phantomica
根据您的描述,当出现关机命令时,您想停用 除了激活语音识别之外的每个命令的语音识别。
您可以按照以下步骤来实现您的要求:
设置一个全局静态变量speechOn,默认值为true。
尝试将每个已识别的语音转换为文本并将其存储在变量中。然后使用此变量与“语音开启”和“语音关闭”进行比较。
通过第二步确定speechOn的布尔值。如果为假,则使用 return;结束识别方法。如果为真,继续识别方法。
我的测试代码如下,测试成功
using System;
using System.Globalization;
using System.Speech.Recognition;
using System.Speech.Synthesis;
namespace ConsoleSpeech
{
class ConsoleSpeechProgram
{
static SpeechSynthesizer ss = new SpeechSynthesizer();
static SpeechRecognitionEngine sre;
static bool done = false;
static bool speechOn = true;
static void Main(string[] args)
{
try
{
ss.SetOutputToDefaultAudioDevice();
Console.WriteLine("\n(Speaking: I am awake)");
ss.Speak("I am awake");
CultureInfo ci = new CultureInfo("en-us");
sre = new SpeechRecognitionEngine(ci);
sre.SetInputToDefaultAudioDevice();
sre.SpeechRecognized += sre_SpeechRecognized;
Choices ch_StartStopCommands = new Choices();
ch_StartStopCommands.Add("speech on");
ch_StartStopCommands.Add("speech off");
ch_StartStopCommands.Add("klatu barada nikto");
GrammarBuilder gb_StartStop = new GrammarBuilder();
gb_StartStop.Append(ch_StartStopCommands);
Grammar g_StartStop = new Grammar(gb_StartStop);
Choices ch_Numbers = new Choices();
ch_Numbers.Add("1");
ch_Numbers.Add("2");
ch_Numbers.Add("3");
ch_Numbers.Add("4");
GrammarBuilder gb_WhatIsXplusY = new GrammarBuilder();
gb_WhatIsXplusY.Append("What is");
gb_WhatIsXplusY.Append(ch_Numbers);
gb_WhatIsXplusY.Append("plus");
gb_WhatIsXplusY.Append(ch_Numbers);
Grammar g_WhatIsXplusY = new Grammar(gb_WhatIsXplusY);
sre.LoadGrammarAsync(g_StartStop);
sre.LoadGrammarAsync(g_WhatIsXplusY);
sre.RecognizeAsync(RecognizeMode.Multiple);
while (done == false) {; }
Console.WriteLine("\nHit <enter> to close shell\n");
Console.ReadLine();
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
Console.ReadLine();
}
} // Main
static void sre_SpeechRecognized(object sender,
SpeechRecognizedEventArgs e)
{
string txt = e.Result.Text;
float confidence = e.Result.Confidence;
Console.WriteLine("\nRecognized: " + txt);
if (confidence < 0.60) return;
if (txt.IndexOf("speech on") >= 0)
{
Console.WriteLine("Speech is now ON");
speechOn = true;
}
if (txt.IndexOf("speech off") >= 0)
{
Console.WriteLine("Speech is now OFF");
speechOn = false;
}
if (speechOn == false) return;
if (txt.IndexOf("klatu") >= 0 && txt.IndexOf("barada") >= 0)
{
((SpeechRecognitionEngine)sender).RecognizeAsyncCancel();
done = true;
Console.WriteLine("(Speaking: Farewell)");
ss.Speak("Farewell");
}
if (txt.IndexOf("What") >= 0 && txt.IndexOf("plus") >= 0)
{
string[] words = txt.Split(' ');
int num1 = int.Parse(words[2]);
int num2 = int.Parse(words[4]);
int sum = num1 + num2;
Console.WriteLine("(Speaking: " + words[2] + " plus " +
words[4] + " equals " + sum + ")");
ss.SpeakAsync(words[2] + " plus " + words[4] +
" equals " + sum);
}
} // sre_SpeechRecognized
} // Program
} // ns
Voice Recognition : Speech Recognition with .NET Desktop Applications