c# - Errors when programming voice recognition on Kinect (Method must have a return type, the name ____ does not exist in the current context) -
using system; using system.collections.generic; using system.linq; using microsoft.xna.framework; using microsoft.xna.framework.audio; using microsoft.xna.framework.content; using microsoft.xna.framework.gamerservices; using microsoft.xna.framework.graphics; using microsoft.xna.framework.input; using microsoft.xna.framework.media; //make sure add reference kinect in references using microsoft.kinect; //make sure have speech sdk installed //go add reference, browse, navigate program files, micrsoft sdks //speech, assemblies , select speech.dll using microsoft.speech.audioformat; using microsoft.speech.recognition; using system.io; using system; using system.collections.generic; using system.componentmodel; using system.linq; using system.text; namespace _1 { /// <summary> /// main type game /// </summary> public class jarvis : microsoft.xna.framework.game { //create instance of kinect sensor public kinectsensor currentsensor; //and speech recognition engine (sre) private speechrecognitionengine speechrecognizer; //get speech recognizer (sr) 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(); } //when window loads, initialize kinect public mainwindow() { initializecomponent(); initializekinect(); } //initilaize kinect private kinectsensor initializekinect() { //get first available sensor , set current sensor variable currentsensor = kinectsensor.kinectsensors .firstordefault(s => s.status == kinectstatus.connected); speechrecognizer = createspeechrecognizer(); //start sensor currentsensor.start(); //then run start method start streaming audio start(); return currentsensor; } //start streaming audio private void start() { //set sensor audio source variable var audiosource = currentsensor.audiosource; //set beam angle mode - direction audio beam pointing //we want set adaptive audiosource.beamanglemode = beamanglemode.adaptive; //start audiosource var kinectstream = audiosource.start(); //configure incoming audio stream speechrecognizer.setinputtoaudiostream( kinectstream, new speechaudioformatinfo(encodingformat.pcm, 16000, 16, 1, 32000, 2, null)); //make sure recognizer not stop after completing speechrecognizer.recognizeasync(recognizemode.multiple); //reduce background , ambient noise better accuracy currentsensor.audiosource.echocancellationmode = echocancellationmode.none; currentsensor.audiosource.automaticgaincontrolenabled = false; } //here fun part: create speech recognizer private speechrecognitionengine createspeechrecognizer() { //set recognizer info recognizerinfo ri = getkinectrecognizer(); //create instance of sre speechrecognitionengine sre; sre = new speechrecognitionengine(ri.id); //now need add words want our program recognise var grammar = new choices(); grammar.add("hello"); grammar.add("goodbye"); //set culture - language, country/region var gb = new grammarbuilder { culture = ri.culture }; gb.append(grammar); //set grammar builder var g = new grammar(gb); sre.loadgrammar(g); //set events recognizing, hypothesising , rejecting speech sre.speechrecognized += srespeechrecognized; sre.speechhypothesized += srespeechhypothesized; sre.speechrecognitionrejected += srespeechrecognitionrejected; return sre; } //if speech rejected private void rejectspeech(recognitionresult result) { textbox2.text = "pardon moi?"; } private void srespeechrecognitionrejected(object sender, speechrecognitionrejectedeventargs e) { rejectspeech(e.result); } //hypothesized result private void srespeechhypothesized(object sender, speechhypothesizedeventargs e) { textbox1.text = "hypothesized: " + e.result.text + " " + e.result.confidence; } //speech recognised private void srespeechrecognized(object sender, speechrecognizedeventargs e) { //very important! - change value adjust accuracy - higher value //the more accurate have be, lower if not recognizing if (e.result.confidence < .4) { rejectspeech(e.result); } //and finally, here set want happen when //the sre recognizes word switch (e.result.text.toupperinvariant()) { case "hello": textbox3.text = "hi there."; break; case "goodbye": textbox3.text = "goodbye then."; break; default: break; } } } }
these errors receive:
method must have return type @ line 48, column 16
the name 'initializecomponent' not exist in current context @ line 50, column 13
the name 'textbox1' not exist in current context @ line 127, column 13
the name 'textbox2' not exist in current context @ line 117, column 13
the name 'textbox3' not exist in current context @ line 143, column 21
the name 'textbox3' not exist in current context @ line 146, column 21
i got code here , assume since says can copied/pasted have improperly set kinect project.
edit:
this have changed code to:
using system; using system.collections.generic; using system.linq; using microsoft.xna.framework; using microsoft.xna.framework.audio; using microsoft.xna.framework.content; using microsoft.xna.framework.gamerservices; using microsoft.xna.framework.graphics; using microsoft.xna.framework.input; using microsoft.xna.framework.media; //make sure add reference kinect in references using microsoft.kinect; //make sure have speech sdk installed //go add reference, browse, navigate program files, micrsoft sdks //speech, assemblies , select speech.dll using microsoft.speech.audioformat; using microsoft.speech.recognition; using system.io; using system.componentmodel; using system.text; namespace _1 { /// <summary> /// main type game /// </summary> public class jarvis : microsoft.xna.framework.game { //create instance of kinect sensor public kinectsensor currentsensor; //and speech recognition engine (sre) private speechrecognitionengine speechrecognizer; //get speech recognizer (sr) 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(); } //when window loads, initialize kinect public jarvis() { //initializecomponent(); initializekinect(); } //initilaize kinect private kinectsensor initializekinect() { //get first available sensor , set current sensor variable currentsensor = kinectsensor.kinectsensors .firstordefault(s => s.status == kinectstatus.connected); speechrecognizer = createspeechrecognizer(); //start sensor currentsensor.start(); //then run start method start streaming audio start(); return currentsensor; } //start streaming audio private void start() { //set sensor audio source variable var audiosource = currentsensor.audiosource; //set beam angle mode - direction audio beam pointing //we want set adaptive audiosource.beamanglemode = beamanglemode.adaptive; //start audiosource var kinectstream = audiosource.start(); //configure incoming audio stream speechrecognizer.setinputtoaudiostream( kinectstream, new speechaudioformatinfo(encodingformat.pcm, 16000, 16, 1, 32000, 2, null)); //make sure recognizer not stop after completing speechrecognizer.recognizeasync(recognizemode.multiple); //reduce background , ambient noise better accuracy currentsensor.audiosource.echocancellationmode = echocancellationmode.none; currentsensor.audiosource.automaticgaincontrolenabled = false; } //here fun part: create speech recognizer private speechrecognitionengine createspeechrecognizer() { //set recognizer info recognizerinfo ri = getkinectrecognizer(); //create instance of sre speechrecognitionengine sre; sre = new speechrecognitionengine(ri.id); //now need add words want our program recognise var grammar = new choices(); grammar.add("hello"); grammar.add("goodbye"); //set culture - language, country/region var gb = new grammarbuilder { culture = ri.culture }; gb.append(grammar); //set grammar builder var g = new grammar(gb); sre.loadgrammar(g); //set events recognizing, hypothesising , rejecting speech sre.speechrecognized += srespeechrecognized; sre.speechhypothesized += srespeechhypothesized; sre.speechrecognitionrejected += srespeechrecognitionrejected; return sre; } //if speech rejected private void rejectspeech(recognitionresult result) { global::system.windows.forms.messagebox.show("pardon moi?"); } private void srespeechrecognitionrejected(object sender, speechrecognitionrejectedeventargs e) { rejectspeech(e.result); } //hypothesized result private void srespeechhypothesized(object sender, speechhypothesizedeventargs e) { global::system.windows.forms.messagebox.show("hypothesized: " + e.result.text + " " + e.result.confidence); } //speech recognised private void srespeechrecognized(object sender, speechrecognizedeventargs e) { //very important! - change value adjust accuracy - higher value //the more accurate have be, lower if not recognizing if (e.result.confidence < .4) { rejectspeech(e.result); } //and finally, here set want happen when //the sre recognizes word switch (e.result.text.toupperinvariant()) { case "hello": global::system.windows.forms.messagebox.show("hi there."); break; case "goodbye": global::system.windows.forms.messagebox.show("goodbye then."); break; default: break; } } } }
i no errors until run , happens:
program: 'c:\users\\documents\visual studio 2010\projects\1\1\1\obj\debug\1.exe' not contain static 'main' method suitable entry point
welcome stackoverflow , please consider reading faq in order best result , you're facing first error because class name not match it's constructor ! should change mainwindow()
in line 48 jarvis()
public jarvis() { initializecomponent(); initializekinect(); }
and source has mentioned , need add 3 textboxes
project name of textbox1
, textbox2
, textbox3
if don't need textboxes
change code whatever desire
- update : try start new project , add class project paste code inside class , use class in project , please keep in mind copying , pasting bunch of code not helpful unless know you're doing , , can't start project , paste other codes instead of it's own functions , classes , expect app run , please consider writing code self better option , in case you'll desired result faster !
Comments
Post a Comment