__2017-12-11 如一模式识别研究

如一模式识别研究

语音识别>>Kinect语音识别及声音来源定位 | KinectAPI编程

转自:http://www.cnblogs.com/microcampus/archive/2013/05/01/3053246.html

第五章:Kinect语音识别及声音来源定位

(1) 新建 Visual C# --> WPF工程(如下图),记得右键点击“引用”,添加相应驱动版本的Microsoft.Kinect.dll 和Microsoft.Speech动态库。App.xaml 文件及 App.xaml.cs文件无需改动。

(2)下面是MainWindow.xaml.cs文件的内容。

1 using System;

2 using System.Collections.Generic;

3 using System.Linq;

4 using System.Text;

5 using System.Windows;

6 using System.Windows.Controls;

7 using System.Windows.Data;

8 using System.Windows.Documents;

9 using System.Windows.Input;

10 using System.Windows.Media;

11 using System.Windows.Media.Imaging;

12 using System.Windows.Navigation;

13 using System.Windows.Shapes;

14

//注意添加以下引用

15 using Microsoft.Kinect;

16 using Microsoft.Speech.AudioFormat;

17 using Microsoft.Speech.Recognition;

18

19 using System.IO;

20

21

22 namespace KinectSpeechRecognition

23 {

24 ///

25 /// Interaction logic for MainWindow.xaml

26 ///

27 public partial class MainWindow : Window

28 {

29 KinectSensor myKinect;

30 RecognizerInfo kinectRecognizerInfo;

31 SpeechRecognitionEngine recognizer;

32

33 KinectAudioSource kinectSource;

34

35 Stream audioStream;

36

37 private RecognizerInfo findKinectRecognizerInfo()

38 {

39 var recognizers = SpeechRecognitionEngine.InstalledRecognizers();

40

41 foreach (RecognizerInfo recInfo in recognizers)

42 {

43 // look at each recognizer info value to find the one that works for Kinect

44 if (recInfo.AdditionalInfo.ContainsKey("Kinect"))

45 {

46 string details = recInfo.AdditionalInfo["Kinect"];

47 if (details == "True" && recInfo.Culture.Name == "en-US")

48 {

49 // If we get here we have found the info we want to use

50 return recInfo;

51 }

52 }

53 }

54 return null;

55 }

56

57 private void createSpeechEngine()

58 {

59 kinectRecognizerInfo = findKinectRecognizerInfo();

60

61 if (kinectRecognizerInfo == null)

62 {

63 MessageBox.Show("Kinect recognizer not found", "Kinect Speech Demo");

64 Application.Current.Shutdown();

65 return;

66 }

67

68 try

69 {

70 recognizer = new SpeechRecognitionEngine(kinectRecognizerInfo);

71 }

72 catch

73 {

74 MessageBox.Show("Speech recognition engine could not be loaded",

"Kinect Speech Demo");

75 Application.Current.Shutdown();

76 }

77 }

78

79 private void buildCommands()

80 {

81 Choices commands = new Choices();

82

83 commands.Add("Red");

84 commands.Add("Green");

85 commands.Add("Blue");

86 commands.Add("Yellow");

87 commands.Add("Cyan");

88 commands.Add("Orange");

89 commands.Add("Purple");

90 commands.Add("Kinect");

91

92 GrammarBuilder grammarBuilder = new GrammarBuilder();

93

94 grammarBuilder.Culture = kinectRecognizerInfo.Culture;

95 grammarBuilder.Append(commands);

96

97 Grammar grammar = new Grammar(grammarBuilder);

98

99 recognizer.LoadGrammar(grammar);

100 }

101

102 private void setupAudio()

103 {

104 try

105 {

106 myKinect = KinectSensor.KinectSensors[0];

107 myKinect.Start();

108 kinectSource = myKinect.AudioSource;

109 kinectSource.BeamAngleMode = BeamAngleMode.Automatic;

110 audioStream = kinectSource.Start();

111 recognizer.SetInputToAudioStream(audioStream, new SpeechAudioFormatInfo(

112 EncodingFormat.Pcm, 16000, 16, 1,

113 32000, 2, null));

114 recognizer.RecognizeAsync(RecognizeMode.Multiple);

115 }

116 catch

117 {

118 MessageBox.Show("Audio stream could not be connected",

"Kinect Speech Demo");

119 Application.Current.Shutdown();

120 }

121 }

122

123

124 private void SetupSpeechRecognition()

125 {

126 createSpeechEngine();

127

128 buildCommands();

129

130 setupAudio();

131

132 recognizer.SpeechRecognized +=

133 new EventHandler(recognizer_SpeechRecognized);

134 }

135

136 void recognizer_SpeechRecognized(object sender, SpeechRecognizedEventArgs e)

137 {

138 if (e.Result.Confidence > 0.95f)

139 {

140 wordTextBlock.Text = e.Result.Text;

141 }

142

143 directionTextBlock.Text = kinectSource.BeamAngle.ToString() + " degrees";

144 directionSlider.Value = kinectSource.BeamAngle;

145 }

146

147

148 public MainWindow()

149 {

150 InitializeComponent();

151 }

152

153 private void Window_Loaded(object sender, RoutedEventArgs e)

154 {

155 SetupSpeechRecognition();

156 }

157 }

158 }

(3)下面是MainWindow.xaml.cs的内容

1

2 xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

3 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

4 Title="Color Recognizer" SizeToContent="WidthAndHeight" Loaded="Window_Loaded">

5

6

7

8

9

10

(4)运行结果

Cyan、Kinect、Purple、Yellow(大-->小)这四个指令识别较准,其他识别较差或者无法识别。

分类: KinectAPI编程

标签: KinectSDK

评论留言区

:
  

作者: 游客 ; *
评论内容: *
带*号为必填项目

如一模式识别更新提示

matlab在图像处理方面的应用有更新

如一模式识别 友情链接

关于本站作者     chinaw3c     mozilla