6

私は認識のためのプロジェクトを持っています。それは機能しますが、このプロジェクトをどのようにクラスとして使用し、そのメソッドを他のクラスから呼び出すと、オンラインで例外の問題が発生します:

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(); 
    }
4

2 に答える 2

4

あなたの GetKinectRecognizer() メソッドは正しくないと思います。

TryGetValue() が見つかった場合、ブール値を返しませんか? その値が out パラメータとして見つかった場合は? TryGetvalue() から返されたブール値で何もしていません。

AdditionalInfo ディクショナリが "Kinect" と等しいキーと文字列 "True" または "False" 値を持つことを期待していますか? それが、コードが探しているように見えるものです。

このコードは、あなたが指摘できる例に基づいていますか? 私はあなたのテストがmatchingFuncで何をしているのか本当に知りません。TryGetvalue からの戻り値を無視しています。文字列値が「True」の「Kinect」という名前の追加情報キーと、カルチャが「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 を作成して確認します。

于 2012-05-24T15:53:26.710 に答える