2012-05-16 124 views
6

我有一個表彰項目。它的工作原理,但如果我用這個項目是如何一類並調用其方法從其他類我有一個問題就行例外:Kinect上的語音識別

sre = new SpeechRecognitionEngine(ri.Id); 

錯誤時正在:

所需的無識別找到ID。

代碼:

KinectAudioSource source = kinectSensor.AudioSource; 
source.EchoCancellationMode = EchoCancellationMode.None; // No AEC for this sample 
source.AutomaticGainControlEnabled = false; // Important to turn this off for speech recognition 
// source.SystemMode = SystemMode.OptibeamArrayOnly; 
speechRecognizer = CreateSpeechRecognizer(); 

using (Stream s = source.Start()) 
{ 
    speechRecognizer.SetInputToAudioStream(s, new SpeechAudioFormatInfo(EncodingFormat.Pcm, 16000, 16, 1, 32000, 2, null)); 
    Console.WriteLine("Recognizing speech. Say: 'purple', 'green' or 'blue'. Press ENTER to stop"); 
    speechRecognizer.RecognizeAsync(RecognizeMode.Multiple); 
    Console.ReadLine(); 
    Console.WriteLine("Stopping recognizer ..."); 
    speechRecognizer.RecognizeAsyncStop(); 
    } 

private static SpeechRecognitionEngine CreateSpeechRecognizer() 
{ 
    RecognizerInfo ri = GetKinectRecognizer(); 

    SpeechRecognitionEngine sre; 
    //if (ri == null) return 0; 
    sre = new SpeechRecognitionEngine(ri.Id); 
    var colors = new Choices(); 
    colors.Add("red"); 
    colors.Add("green"); 
    colors.Add("blue"); 
    var gb = new GrammarBuilder { Culture = ri.Culture }; 
    gb.Append(colors); 

    // 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; 
    return sre; 
    } 
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(); 
    } 
+3

調試ID。檢查值是否正確 – Fixus

+0

確保安裝了所需的32位語音引擎以支持Kinect。請遵循此頁面上列出的先決條件:http://channel9.msdn.com/coding4fun/kinect/Introduction-to-Kinect-Speech-Recognition –

+0

平臺目標設置爲什麼? – animaonline

回答

4

我覺得你GetKinectRecognizer()方法是不正確的。

不TryGetValue()返回一個布爾值,如果它被發現和值作爲一個out參數?你沒有對TryGetvalue()返回的布爾值做任何事情。

您是否期待AdditionalInfo字典具有等於「Kinect」和字符串「True」或「False」值的密鑰?這就是你的代碼似乎尋找。

該代碼是基於您可以指向的示例。我並不真正按照您在matchingFunc中進行的測試。您正在忽略來自TryGetvalue的返回值,您正在尋找名爲「Kinect」的字符串值爲「True」的AdditionalInfo鍵,以及具有「en-US」文化的識別器。

爲什麼不直接轉儲SpeechRecognitionEngine.InstalledRecognizers()的內容並確保它包含您認爲它包含的內容。這是老同學,但有用:

foreach (RecognizerInfo ri in SpeechRecognitionEngine.InstalledRecognizers()) 
{ 
    Debug.WriteLine(String.Format("Id={0}, Name={1}, Description={2}, Culture={3}", ri.Id, ri.Name, ri.Description, ri.Culture)); 
    foreach(string key in ri.AdditionalInfo.Keys) 
    { 
     Debug.WriteLine(string.Format("{0} = {1}", key, ri.AdditionalInfo[key])); 
    } 
    Debug.WriteLine("-"); 
} 

我沒有安裝Kinect的SDK,但我的Windows 7機器上它顯示:

Id=MS-1033-80-DESK, Name=MS-1033-80-DESK, Description=Microsoft Speech Recognizer 8.0 for Windows (English - US), Culture=en-US 
VendorPreferred = 
CommandAndControl = 
Version = 8.0 
Language = 409;9 
Desktop = 
SupportedLocales = 409;1009;3409;9 
AudioFormats = 16;18;20;22;45;53;{6F50E21C-E30E-4B50-95E9-21E8F23D15BD} 
SpeakingStyle = Discrete;Continuous 
WildcardInCFG = Anywhere;Trailing 
Dictation = 
Hypotheses = 
Alternates = CC;Dictation 
windowsV6compatible = 
Name = MS-1033-80-DESK 
DictationInCFG = Anywhere;Trailing 
UPSPhoneSet = 
WordSequences = Anywhere;Trailing 
Vendor = Microsoft 
- 
Id=MS-2057-80-DESK, Name=MS-2057-80-DESK, Description=Microsoft Speech Recognizer 8.0 for Windows (English - UK), Culture=en-GB 
= 
VendorPreferred = 
CommandAndControl = 
Version = 8.0 
Language = 809 
Desktop = 
SupportedLocales = 809;C09;1409;1809;1C09;2009;2409;2809;2C09;3009;4009;4409;4809;9 
AudioFormats = 16;18;20;22;45;53;{6F50E21C-E30E-4B50-95E9-21E8F23D15BD} 
SpeakingStyle = Discrete;Continuous 
WildcardInCFG = Anywhere;Trailing 
Dictation = 
Hypotheses = 
Alternates = CC;Dictation 
windowsV6compatible = 
Name = MS-2057-80-DESK 
DictationInCFG = Anywhere;Trailing 
UPSPhoneSet = 
WordSequences = Anywhere;Trailing 
Vendor = Microsoft 
- 
- 

確保你正在尋找的價值AdditionalInfo字典真的在那裏。然後製作你的matchingFunc來檢查它。

+0

+51,這對我有用! –

3

試試這個。這個對我有用。

private static RecognizerInfo GetKinectRecognizer() 
     { 
      foreach (RecognizerInfo recognizer in SpeechRecognitionEngine.InstalledRecognizers()) 
      { 
       System.Diagnostics.Debug.Write(recognizer.Culture.Name+"\n\n"); 
       //string value; 
       //recognizer.AdditionalInfo.TryGetValue("Kinect",out value); 
       if ("en-US".Equals(recognizer.Culture.Name, StringComparison.OrdinalIgnoreCase)) 
       { 
        return recognizer; 
       } 

      } 

      return null; 
     } 
+0

也爲我工作。爲了記錄,我剛剛安裝了64x版本的Microsoft Speech Platform SDK v11.0,Microsoft Speech Platform Runtime v11.0和Redistributable Language Packs v11.0(__in this order__)。所有從[這裏]下載(https://msdn.microsoft.com/en-us/library/office/hh361572(v = office.14).aspx)。 – Thoth