利用Kinect语音识别控制51单片机
扫描二维码
随时随地手机看文章
第六章:利用Kinect语音识别控制51单片机
WPF部分:
(1) 新建 Visual C# --> WPF工程(如下图),记得右键点击“引用”,添加相应驱动版本的Microsoft.Kinect.dll 和Microsoft.Speech动态库。App.xaml 文件及 App.xaml.cs文件无需改动。
(2)下面是MainWindow.xaml.cs文件的内容。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Microsoft.Kinect;
using Microsoft.Speech.AudioFormat;
using Microsoft.Speech.Recognition;
using System.IO;
using System.IO.Ports; //跟串口相关,不能只是引用system.IO
using System.Threading; //跟串口相关,线程的引入
namespace VoiceControlLED
{
///
/// Interaction logic for MainWindow.xaml
///
public partial class MainWindow : Window
{
KinectSensor myKinect;
///
/// 音控相关
///
RecognizerInfo kinectRecognizerInfo;
SpeechRecognitionEngine recognizer;
KinectAudioSource kinectSource;
Stream audioStream;
///
/// 串口相关
///
///
delegate void HandleInterfaceUpdateDelagate(string text);//委托;此为重点
///
/// 实例化串口
///
SerialPort serialPort1 = new SerialPort();
///
/// 串口初始化函数
///
private void SerialPort1_Init()
{
serialPort1.PortName = "COM1"; //串口号(参考串口调试助手)
serialPort1.BaudRate = 9600; //波特率
serialPort1.Parity = Parity.None; //校验位
serialPort1.DataBits = 8; //数据位
serialPort1.StopBits = StopBits.One; //停止位
if (!serialPort1.IsOpen)
{
serialPort1.Open();
}
else
MessageBox.Show("Port is open!");
}
///
/// 发送字节数据函数
///
///
///
private int send_command(string Command)
{
try
{
serialPort1.Write(Command);
return (1);
}
catch (Exception)
{
// comm error
return (0);
}
}
private RecognizerInfo findKinectRecognizerInfo()
{
var recognizers = SpeechRecognitionEngine.InstalledRecognizers();
foreach (RecognizerInfo recInfo in recognizers)
{
// look at each recognizer info value to find the one that works for Kinect
if (recInfo.AdditionalInfo.ContainsKey("Kinect"))
{
string details = recInfo.AdditionalInfo["Kinect"];
if (details == "True" && recInfo.Culture.Name == "en-US")
{
// If we get here we have found the info we want to use
return recInfo;
}
}
}
return null;
}
private void createSpeechEngine()
{
kinectRecognizerInfo = findKinectRecognizerInfo();
if (kinectRecognizerInfo == null)
{
MessageBox.Show("Kinect recognizer not found", "Kinect Speech Demo");
Application.Current.Shutdown();
return;
}
try
{
recognizer = new SpeechRecognitionEngine(kinectRecognizerInfo);
}
catch
{
MessageBox.Show("Speech recognition engine could not be loaded", "Kinect Speech Demo");
Application.Current.Shutdown();
}
}
private void buildCommands()
{
Choices commands = new Choices();
commands.Add("One");
commands.Add("Two");
commands.Add("Three");
commands.Add("Four");
commands.Add("Five");
commands.Add("Six");
commands.Add("Seven");
commands.Add("Eight");
GrammarBuilder grammarBuilder = new GrammarBuilder();
grammarBuilder.Culture = kinectRecognizerInfo.Culture;
grammarBuilder.Append(commands);
Grammar grammar = new Grammar(grammarBuilder);
recognizer.LoadGrammar(grammar);
}
private void setupAudio()
{
try
{
myKinect = KinectSensor.KinectSensors[0];
myKinect.Start();
kinectSource = myKinect.AudioSource;
kinectSource.BeamAngleMode = BeamAngleMode.Automatic;
audioStream = kinectSource.Start();
recognizer.SetInputToAudioStream(audioStream, new SpeechAudioFormatInfo(
EncodingFormat.Pcm, 16000, 16, 1,
32000, 2, null));
recognizer.RecognizeAsync(RecognizeMode.Multiple);
}
catch
{
MessageBox.Show("Audio stream could not be connected", "Kinect Speech Demo");
Application.Current.Shutdown();
}
}
private void SetupSpeechRecognition()
{