利用Kinect語音識(shí)別控制51單片機(jī)
第六章:利用Kinect語音識(shí)別控制51單片機(jī)
WPF部分:
(1) 新建 Visual C# --> WPF工程(如下圖),記得右鍵點(diǎn)擊“引用”,添加相應(yīng)驅(qū)動(dòng)版本的Microsoft.Kinect.dll 和Microsoft.Speech動(dòng)態(tài)庫(kù)。App.xaml 文件及 App.xaml.cs文件無需改動(dòng)。
(2)下面是MainWindow.xaml.cs文件的內(nèi)容。
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; //跟串口相關(guān),不能只是引用system.IO
using System.Threading; //跟串口相關(guān),線程的引入
namespace VoiceControlLED
{
///
/// Interaction logic for MainWindow.xaml
///
public partial class MainWindow : Window
{
KinectSensor myKinect;
///
/// 音控相關(guān)
///
RecognizerInfo kinectRecognizerInfo;
SpeechRecognitionEngine recognizer;
KinectAudioSource kinectSource;
Stream audioStream;
///
/// 串口相關(guān)
///
///
delegate void HandleInterfaceUpdateDelagate(string text);//委托;此為重點(diǎn)
///
/// 實(shí)例化串口
///
SerialPort serialPort1 = new SerialPort();
///
/// 串口初始化函數(shù)
///
private void SerialPort1_Init()
{
serialPort1.PortName = "COM1"; //串口號(hào)(參考串口調(diào)試助手)
serialPort1.BaudRate = 9600; //波特率
serialPort1.Parity = Parity.None; //校驗(yàn)位
serialPort1.DataBits = 8; //數(shù)據(jù)位
serialPort1.StopBits = StopBits.One; //停止位
if (!serialPort1.IsOpen)
{
serialPort1.Open();
}
else
MessageBox.Show("Port is open!");
}
///
/// 發(fā)送字節(jié)數(shù)據(jù)函數(shù)
///
///
///
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()
{