所以我使用Kinect编写了以下代码作为我的PC的基本语音控制代码,它一直运行良好!但是当我尝试添加一种“静音”代码时,它就停止了工作。我弄清楚为什么它停止工作,但我无法弄清楚如何让它与某种静音代码一起使用它会导致鼻子出血!我觉得答案很简单,我只是忘了怎么做,或者我还没有想到它......那就是杀了我的。
无论如何,正如您所看到的那样,问题是代码中稍后初始化的变量“listen”需要一个起始值才能在if和switch语句中使用它。我的起始值为0,因此在listen设置为1之前不会运行任何内容,只有在我说“Hey Jarvis”(当然是由钢铁侠启发)时才会发生。但是,当代码通过一个新的循环时,它会将变量“listen”重置为零,因为它是初始化的值!我无法想出一种方法,使变量值对我的命令灵活,而与代码的循环次数无关。真是太棒了!如果你们中的任何人能够阐明这一点,我将是一个非常幸福的人。如果有帮助,请随意取笑你看到的任何菜鸟代码......请告诉我我的诺布眼睛看不到的东西!
namespace Speech
{
using System;
using System.IO;
using System.Linq;
using System.Threading;
using Microsoft.Kinect;
using Microsoft.Speech.AudioFormat;
using Microsoft.Speech.Recognition;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using System.Diagnostics;
public class Program
{
public static void Main(string[] args)
{
KinectSensor sensor = (from sensorToCheck in KinectSensor.KinectSensors where sensorToCheck.Status == KinectStatus.Connected select sensorToCheck).FirstOrDefault();
if (sensor == null)
{
Console.WriteLine(
"No Kinect sensors are attached to this computer or none of the ones that are\n" +
"attached are \"Connected\".\n" +
"Attach the KinectSensor and restart this application.\n" +
"Make sure the Power Adapter of the Kinect sensor is plugged in \n" +
"Press any key to continue.\n");
// Give a chance for user to see console output before it is dismissed
Console.ReadKey(true);
return;
} // end if (sensor == null)
sensor.Start();
// Obtain the KinectAudioSource to do audio capture
KinectAudioSource source = sensor.AudioSource;
source.EchoCancellationMode = EchoCancellationMode.None; // No AEC for this sample
source.AutomaticGainControlEnabled = false; // Important to turn this off for speech recognition
RecognizerInfo ri = GetKinectRecognizer();
if (ri == null)
{
Console.WriteLine("Could not find Kinect speech recognizer. Please refer to the sample requirements.");
return;
}
Console.WriteLine("Using: {0}", ri.Name);
// NOTE: Need to wait 4 seconds for device to be ready right after initialization
int wait = 4;
while (wait > 0)
{
Console.Write("Device will be ready for speech recognition in {0} second(s).\r", wait--);
Thread.Sleep(1000);
}
Console.Write("\n");
//============================================================================
using (var sre = new SpeechRecognitionEngine(ri.Id))
{
var command = new Choices();
//***************************************************
command.Add("Hey Jarvis");
command.Add("Open Videos");
command.Add("Parenthood");
command.Add("Down Ten");
command.Add("Down Five");
command.Add("Down Three");
command.Add("Down Two");
command.Add("Down One");
command.Add("Up Ten");
command.Add("Up Five");
command.Add("Up Three");
command.Add("Up Two");
command.Add("Up One");
command.Add("Yes");
command.Add("Full Screen");
command.Add("Minimize");
command.Add("Volume Up");
command.Add("Volume Down");
command.Add("Go Back");
command.Add("Go Forward");
command.Add("Pause");
command.Add("Close Window");
command.Add("Minimize");
command.Add("Next");
command.Add("Play");
command.Add("Episode Sixteen");
command.Add("Episode Seventeen");
command.Add("Episode Eighteen");
command.Add("Episode Nineteen");
command.Add("Episode Twenty");
command.Add("Episode Twenty One");
command.Add("Episode Twenty Two");
command.Add("Episode Twenty Three");
command.Add("Episode Twenty Four");
command.Add("Right");
command.Add("Left");
command.Add("Open Dark Souls");
command.Add("No Steam");
//***************************************************
var gb = new GrammarBuilder { Culture = ri.Culture };
// Specify the culture to match the recognizer in case we are running in a different culture.
gb.Append(command);
// Create the actual Grammar instance, and then load it into the speech recognizer.
var g = new Grammar(gb);
sre.LoadGrammar(g);
sre.SpeechRecognized += SreSpeechRecognized;
// sre.SpeechHypothesized += SreSpeechHypothesized;
// sre.SpeechRecognitionRejected += SreSpeechRecognitionRejected;
using (Stream s = source.Start())
{
sre.SetInputToAudioStream(
s, new SpeechAudioFormatInfo(EncodingFormat.Pcm, 16000, 16, 1, 32000, 2, null));
Console.WriteLine("The System is actively monitoring Voice command ... \n");
sre.RecognizeAsync(RecognizeMode.Multiple);
Console.ReadLine();
}
} // end using (var sre = new SpeechRecognitionEngine(ri.Id))
//==========================================================================
sensor.Stop();
} // end public static void Main
private static RecognizerInfo GetKinectRecognizer()
{
Func<RecognizerInfo, bool> matchingFunc = r =>
{
string value;
r.AdditionalInfo.TryGetValue("Kinect", out value);
return "True".Equals(value, StringComparison.InvariantCultureIgnoreCase) && "en-US".Equals(r.Culture.Name, StringComparison.InvariantCultureIgnoreCase);
};
return SpeechRecognitionEngine.InstalledRecognizers().Where(matchingFunc).FirstOrDefault();
}
private static void SreSpeechRecognized(object sender, SpeechRecognizedEventArgs e)
{
int listen=0; //<<<<<<<< HERES THE PROBLEM
if (e.Result.Confidence >= 0.7)
{
Console.WriteLine("\nSpeech Recognized: \t{0}\tConfidence:\t{1}", e.Result.Text, e.Result.Confidence);
switch (e.Result.Text)
{
case "Hey Jarvis":
Console.Write(" Yes Gary?");
listen=1;
break;
case "Open Videos":
Console.Write("Open Videos");
if (listen == 1) SendKeys.SendWait("{F9}");
else Console.Write(" Muted");
break;
case "Parenthood":
Console.Write("Parenthood");
if (listen == 1)
{
SendKeys.SendWait("{p}");
Thread.Sleep(500);
SendKeys.SendWait("{ENTER}");
}
else Console.Write(" Muted");
break;
case "Down Ten":
Console.Write("Down Ten");
if (listen == 1) SendKeys.SendWait("{Down 10}");
else Console.Write(" Muted");
break;
case "Down Five":
Console.Write("Down Five");
if (listen == 1) SendKeys.SendWait("{Down 5}");
else Console.Write(" Muted");
break;
case "Down Three":
Console.Write("Down Three");
if (listen == 1) SendKeys.SendWait("{Down 3}");
else Console.Write(" Muted");
break;
case "Down Two":
Console.Write("Down Two");
if (listen == 1) SendKeys.SendWait("{Down 2}");
else Console.Write(" Muted");
break;
case "Down One":
Console.Write("Down One");
if (listen == 1) SendKeys.SendWait("{Down}");
else Console.Write(" Muted");
break;
case "Up Ten":
Console.Write("Up Ten");
if (listen == 1) SendKeys.SendWait("{Up 10}");
else Console.Write(" Muted");
break;
case "Up Five":
Console.Write("Up Five");
if (listen == 1) SendKeys.SendWait("{Up 5}");
else Console.Write(" Muted");
break;
case "Up Three":
Console.Write("Up Three");
if (listen == 1) SendKeys.SendWait("{Up 3}");
else Console.Write(" Muted");
break;
case "Up Two":
Console.Write("Up Two");
if (listen == 1) SendKeys.SendWait("{Up 2}");
else Console.Write(" Muted");
break;
case "Up One":
Console.Write("Up One");
if (listen == 1) SendKeys.SendWait("{Up}");
else Console.Write(" Muted");
break;
case "Yes":
Console.Write("Yes");
if (listen == 1) SendKeys.SendWait("{Enter}");
else Console.Write(" Muted");
break;
case "Full Screen":
Console.Write("Full Screen");
if (listen == 1) SendKeys.SendWait("{f}");
else Console.Write(" Muted");
break;
case "Exit":
Console.Write("Exit");
if (listen == 1) SendKeys.SendWait("{ESC}");
else Console.Write(" Muted");
break;
case "Volume Up":
Console.Write("Volume Up");
if (listen == 1) SendKeys.SendWait("^{Up}");
else Console.Write(" Muted");
break;
case "Volume Down":
Console.Write("Volume Down");
if (listen == 1) SendKeys.SendWait("^{DOWN}");
else Console.Write(" Muted");
break;
case "Go Back":
Console.Write("Go Back");
if (listen == 1) SendKeys.SendWait("%{LEFT}");
else Console.Write(" Muted");
break;
case "Go Forward":
Console.Write("Go Forward");
if (listen == 1) SendKeys.SendWait("%{RIGHT}");
else Console.Write(" Muted");
break;
case "Pause":
Console.Write("Pause");
if (listen == 1) SendKeys.SendWait(" ");
else Console.Write(" Muted");
break;
case "Play":
Console.Write("Play");
if (listen == 1) SendKeys.SendWait(" ");
else Console.Write(" Muted");
break;
case "Close Window":
Console.Write("Close Window");
if (listen == 1) SendKeys.SendWait("%{F4}");
else Console.Write(" Muted");
break;
case "Right":
Console.Write("Right");
if (listen == 1) SendKeys.SendWait("{RIGHT}");
else Console.Write(" Muted");
break;
case "Left":
Console.Write("Left");
if (listen == 1) SendKeys.SendWait("{Left}");
else Console.Write(" Muted");
break;
case "Episode Sixteen":
Console.Write("Episode Sixteen");
if (listen == 1)
{
SendKeys.SendWait("{F9}");
Thread.Sleep(17000);
SendKeys.SendWait("{p}");
Thread.Sleep(500);
SendKeys.SendWait("{ENTER}");
Thread.Sleep(500);
SendKeys.SendWait("{DOWN}");
Thread.Sleep(500);
SendKeys.SendWait("{ENTER}");
Thread.Sleep(500);
SendKeys.SendWait("{DOWN 15}");
Thread.Sleep(500);
SendKeys.SendWait("{ENTER}");
Thread.Sleep(15000);
SendKeys.SendWait("{f}");
}
else Console.Write(" Muted");
break;
case "Episode Seventeen":
Console.Write("Episode Seventeen");
if (listen == 1)
{
SendKeys.SendWait("{F9}");
Thread.Sleep(17000);
SendKeys.SendWait("{p}");
Thread.Sleep(500);
SendKeys.SendWait("{ENTER}");
Thread.Sleep(500);
SendKeys.SendWait("{DOWN}");
Thread.Sleep(500);
SendKeys.SendWait("{ENTER}");
Thread.Sleep(500);
SendKeys.SendWait("{DOWN 16}");
Thread.Sleep(500);
SendKeys.SendWait("{ENTER}");
Thread.Sleep(15000);
SendKeys.SendWait("{f}");
}
else Console.Write(" Muted");
break;
case "Episode Eighteen":
Console.Write("Episode Eighteen");
if (listen == 1)
{
SendKeys.SendWait("{F9}");
Thread.Sleep(17000);
SendKeys.SendWait("{p}");
Thread.Sleep(500);
SendKeys.SendWait("{ENTER}");
Thread.Sleep(500);
SendKeys.SendWait("{DOWN}");
Thread.Sleep(500);
SendKeys.SendWait("{ENTER}");
Thread.Sleep(500);
SendKeys.SendWait("{DOWN 17}");
Thread.Sleep(500);
SendKeys.SendWait("{ENTER}");
Thread.Sleep(15000);
SendKeys.SendWait("{f}");
}
else Console.Write(" Muted");
break;
case "Episode Nineteen":
Console.Write("Episode Nineteen");
if (listen == 1)
{
SendKeys.SendWait("{F9}");
Thread.Sleep(17000);
SendKeys.SendWait("{p}");
Thread.Sleep(500);
SendKeys.SendWait("{ENTER}");
Thread.Sleep(500);
SendKeys.SendWait("{DOWN}");
Thread.Sleep(500);
SendKeys.SendWait("{ENTER}");
Thread.Sleep(500);
SendKeys.SendWait("{DOWN 18}");
Thread.Sleep(500);
SendKeys.SendWait("{ENTER}");
Thread.Sleep(15000);
SendKeys.SendWait("{f}");
}
else Console.Write(" Muted");
break;
case "Episode Twenty":
Console.Write("Episode Twenty");
if (listen == 1)
{
SendKeys.SendWait("{F9}");
Thread.Sleep(17000);
SendKeys.SendWait("{p}");
Thread.Sleep(500);
SendKeys.SendWait("{ENTER}");
Thread.Sleep(500);
SendKeys.SendWait("{DOWN}");
Thread.Sleep(500);
SendKeys.SendWait("{ENTER}");
Thread.Sleep(500);
SendKeys.SendWait("{DOWN 19}");
Thread.Sleep(500);
SendKeys.SendWait("{ENTER}");
Thread.Sleep(15000);
SendKeys.SendWait("{f}");
}
else Console.Write(" Muted");
break;
case "Episode Twenty One":
Console.Write("Episode Twenty One");
if (listen == 1)
{
SendKeys.SendWait("{F9}");
Thread.Sleep(17000);
SendKeys.SendWait("{p}");
Thread.Sleep(500);
SendKeys.SendWait("{ENTER}");
Thread.Sleep(500);
SendKeys.SendWait("{DOWN}");
Thread.Sleep(500);
SendKeys.SendWait("{ENTER}");
Thread.Sleep(500);
SendKeys.SendWait("{DOWN 20}");
Thread.Sleep(500);
SendKeys.SendWait("{ENTER}");
Thread.Sleep(15000);
SendKeys.SendWait("{f}");
}
else Console.Write(" Muted");
break;
case "Episode Twenty Two":
Console.Write("Episode Twenty Two");
if (listen == 1)
{
SendKeys.SendWait("{F9}");
Thread.Sleep(17000);
SendKeys.SendWait("{p}");
Thread.Sleep(500);
SendKeys.SendWait("{ENTER}");
Thread.Sleep(500);
SendKeys.SendWait("{DOWN}");
Thread.Sleep(500);
SendKeys.SendWait("{ENTER}");
Thread.Sleep(500);
SendKeys.SendWait("{DOWN 21}");
Thread.Sleep(500);
SendKeys.SendWait("{ENTER}");
Thread.Sleep(15000);
SendKeys.SendWait("{f}");
}
else Console.Write(" Muted");
break;
case "Minimize":
Console.Write("Minimize");
if (listen == 1)
{
SendKeys.SendWait("% ");
SendKeys.SendWait("{n}");
}
else Console.Write(" Muted");
break;
case "Next":
Console.Write("Next");
if (listen == 1)
{
SendKeys.SendWait("%{TAB 2}");
}
else Console.Write(" Muted");
break;
case "Open Dark Souls":
Console.Write("Open Dark Souls");
if (listen == 1) SendKeys.SendWait("{F7}");
else Console.Write(" Muted");
break;
case "No Steam":
Console.Write("No Steam");
if (listen == 1)
{
SendKeys.SendWait("+{TAB}");
Thread.Sleep(1000);
SendKeys.SendWait("+{TAB}");
}
else Console.Write(" Muted");
break;
case "Thanks Jarvis":
Console.Write(" You're Welcome");
listen = 0;
break;
} // end switch (e.Result.Text)
} // end if (e.Result.Confidence >= 0.7)
else
{
Console.WriteLine("\nSpeech Recognized but confidence was too low: \t{0}", e.Result.Confidence);
// DumpRecordedAudio(e.Result.Audio);
}
} // end private static void SreSpeechRecognized
} // end public class Program
} // end namespace Speech
答案 0 :(得分:0)
为什么不将switch
声明包含在另一个switch
内。
例如:
switch (listen)
{
case 0:
switch (e.Result.Text)
{
case "Hey Jarvis":
Console.Write(" Yes Gary?");
listen=1;
break;
case default:
Console.Write(" Muted");
break;
}
break;
case 1:
// All your normal code
}
Haven之前没有使用过kinect-sdk,但我认为逻辑应该仍然存在。我认为你所写的内容非常棒,我一定会考虑将其用于某些事情。
您也可以从SreSpeechRecognized
方法返回listen的值,而不是使用您可以将其作为参数的方法初始化它。