当前位置:首页 > 单片机 > 单片机
[导读]第六章:利用Kinect语音识别控制51单片机WPF部分:(1) 新建 Visual C# --> WPF工程(如下图),记得右键点击“引用”,添加相应驱动版本的Microsoft.Kinect.dll 和Microsoft.Speech动态库。App.xaml 文件及 App.xaml

第六章:利用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()

{

本站声明: 本文章由作者或相关机构授权发布,目的在于传递更多信息,并不代表本站赞同其观点,本站亦不保证或承诺内容真实性等。需要转载请联系该专栏作者,如若文章内容侵犯您的权益,请及时联系本站删除。
换一批
延伸阅读

本文中,小编将对语音识别技术予以介绍,如果你想对语音识别技术的详细情况有所认识,或者想要增进对它的了解程度,不妨请看以下内容哦。

关键字: 语音识别 语音识别技术

在这篇文章中,小编将为大家带来语音识别技术的相关报道。如果你对本文即将要讲解的内容存在一定兴趣,不妨继续往下阅读哦。

关键字: 语音识别 语音识别技术

今天,小编将在这篇文章中为大家带来语音识别的有关报道,通过阅读这篇文章,大家可以对语音识别具备清晰的认识,主要内容如下。

关键字: 语音识别 智能家居

语音识别技术将是下述内容的主要介绍对象,通过这篇文章,小编希望大家可以对语音识别技术的相关情况以及信息有所认识和了解,详细内容如下。

关键字: 语音识别 芯片

在这篇文章中,小编将对语音识别模块的相关内容和情况加以介绍以帮助大家增进对语音识别模块的了解程度,和小编一起来阅读以下内容吧。

关键字: 语音识别 语音识别模块

一直以来,语音识别都是大家的关注焦点之一。因此针对大家的兴趣点所在,小编将为大家带来语音识别的相关介绍,详细内容请看下文。

关键字: 语音识别 智能家居 智能客服 人工智能

在下述的内容中,小编将会对语音识别技术的相关消息予以报道,如果语音识别技术是您想要了解的焦点之一,不妨和小编共同阅读这篇文章哦。

关键字: 语音识别 语音识别技术

以下内容中,小编将对语音识别模块的相关内容进行着重介绍和阐述,希望本文能帮您增进对语音识别模块的了解,和小编一起来看看吧。

关键字: 语音识别 语音识别模块

本文中,小编将对语音识别技术予以介绍,如果你想对它的详细情况有所认识,或者想要增进对它的了解程度,不妨请看以下内容哦。

关键字: 语音识别 智能家居 物联网

在这篇文章中,小编将为大家带来汽车语音识别控制系统怎么用的相关报道。如果你对本文即将要讲解的内容存在一定兴趣,不妨继续往下阅读哦。

关键字: 语音识别 语音识别控制系统 语音导航
关闭
关闭