sir we have developed an android application for two wheelers in which we can start bike ,stop bike ,lock bike ,unlock bike and run engine using android app button's.now i want to operate my bike from app using voice commands like start bike ,stop bike ,run engine,lock bike etc.so when i
pass start bike command pocketsphinx recognizer returns me what i have called that is start bike string in onresult method .so i check in onresult if hypothysis is equals start bike then i pass data to actually start my bike and same for other commands also.
so i created one grammer file containing these commands .
below is my activity code for speech recognition.
// sir i have called runRecognizerSetup() in my activitiy's onCreateView() method also
@Override
public void onRequestPermissionsResult(int requestCode,String[] permissions, int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
if (requestCode == PERMISSIONS_REQUEST_RECORD_AUDIO) {
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
runRecognizerSetup();
} else {
finish();
}
}
}
private void setupRecognizer(File assetsDir) throws IOException {
// The recognizer can be configured to perform multiple searches
// of different kind and switch between them
recognizer = SpeechRecognizerSetup.defaultSetup()
.setAcousticModel(new File(assetsDir, "en-us-ptm"))
.setDictionary(new File(assetsDir, "cmudict-en-us.dict"))
.setRawLogDir(assetsDir).setKeywordThreshold(1e-45f) // To disable logging of raw audio comment out this call (takes a lot of space on the device)
.getRecognizer();
recognizer.addListener(MainActivity.this);
File kwsList = new File(assetsDir, "bike.gram");
recognizer.addGrammarSearch(KWS_SEARCH, kwsList);
// File menuGrammar = new File(assetsDir, "menu.gram");
// recognizer.addGrammarSearch(MENU_SEARCH, menuGrammar);
//// // Create grammar-based search for digit recognition
// File digitsGrammar = new File(assetsDir, "digits.gram");
// recognizer.addGrammarSearch(DIGITS_SEARCH, digitsGrammar);
// File languageModel = new File(assetsDir, "weather.dmp");
// recognizer.addNgramSearch(FORECAST_SEARCH, languageModel);// //Phonetic search
// File phoneticModel = new File(assetsDir, "en-phone.dmp");
// recognizer.addAllphoneSearch(PHONE_SEARCH, phoneticModel);
}
@Override
public void onBeginningOfSpeech() {
Log.e("start", "onbegin");
switchSearch(KWS_SEARCH);
}
public void onEndOfSpeech() {
if (!recognizer.getSearchName().equals(KWS_SEARCH)) {
Log.e("onEndOfSpeech", "2");
switchSearch(KWS_SEARCH);
}
}
@Override
public void onPartialResult(Hypothesis hypothesis) {
Hypothesis hype=null;
try{
if (hype == null) {
Log.e("onPartialResult", ""+hypothesis.getHypstr() + "3");
return;
}
else Log.e("partialresult",""+hype.getHypstr());
}catch(Exception e){}
recognizer.stop();
//Log.e("partialresult",""+hype.getHypstr());
// ((TextView) findViewById(R.id.caption_text)).setText(hype.getHypstr());
// if (hypothesis.getHypstr().equals(KEYPHRASE))
// switchSearch(MENU_SEARCH);
// else if (hypothesis.getHypstr().equals(DIGITS_SEARCH))
// switchSearch(DIGITS_SEARCH);
// else if (hypothesis.getHypstr().equals(PHONE_SEARCH))
// switchSearch(PHONE_SEARCH);
// else {
// ((TextView) findViewById(R.id.caption_text)).setText(hypothesis.getHypstr());
// }
}
private boolean selectOptions(String s) {
final String start="start bike",stop="stop bike",
run="run engine ",lock="please lock bike",unlock="unlock bike";
boolean value=false;
Log.e("selectOptions", "start of status"+status+s);
switch (s) {
case start:
Log.e("selectOptions", "start");
try {
Toast.makeText(getApplicationContext(), "start selected", Toast.LENGTH_SHORT).show();
status = start;
// byte[] a = {(byte) 01};
// final BluetoothGattCharacteristic writeRelayCharacteristic = dashboardService.getCharacteristic(UUID.fromString(keyBypassUUID));
// writeRelayCharacteristic.setValue(a);
// dashboardGatt.writeCharacteristic(writeRelayCharacteristic);
// bikeRunning = true;
Log.e("switch","start");
value = true;
}catch(Exception e){e.printStackTrace();}
break;
case stop:
Log.e("selectOptions","stop");
//Toast.makeText(getApplicationContext(), "stop selected", Toast.LENGTH_SHORT).show();
try {
// if (status.equals("start")) {
status = stop;
Toast.makeText(getApplicationContext(), "stop selected", Toast.LENGTH_SHORT).show();
// byte[] b = {(byte) 00};
// final BluetoothGattCharacteristic writeRelayStopCharacteristic = dashboardService.getCharacteristic(UUID.fromString(keyBypassUUID));
// writeRelayStopCharacteristic.setValue(b);
// dashboardGatt.writeCharacteristic(writeRelayStopCharacteristic);
// bikeRunning = false;
value = true;
//}
}catch (Exception e){e.printStackTrace();}
break;
case lock:
Log.e("selectOptions","lock");
try {
if (lockStatus.equals("unlock")) {
lockStatus = lock;
Toast.makeText(getApplicationContext(), "lock selected", Toast.LENGTH_SHORT).show();
value = true;
}
}catch (Exception e){e.printStackTrace();}
// byte[] c = {(byte) 00};
// final BluetoothGattCharacteristic writeRelayLockCharacteristic = dashboardService.getCharacteristic(UUID.fromString(masterCharUUID));
// writeRelayLockCharacteristic.setValue(c);
// dashboardGatt.writeCharacteristic(writeRelayLockCharacteristic);
// bikeMasterlock = false;
break;
case unlock:
Log.e("selectOptions","unlock");
try {
if (lockStatus.equals("lock")) {
lockStatus = unlock;
Toast.makeText(getApplicationContext(), "unlock selected", Toast.LENGTH_SHORT).show();
value = true;
}
}catch (Exception e){e.printStackTrace();}
// byte[] d = {(byte) 01};
// final BluetoothGattCharacteristic writeRelayUnlockCharacteristic = dashboardService.getCharacteristic(UUID.fromString(masterCharUUID));
// writeRelayUnlockCharacteristic.setValue(d);
// dashboardGatt.writeCharacteristic(writeRelayUnlockCharacteristic);
//bikeMasterlock = true;
break;
}
return value;
}
@Override
public void onResult(Hypothesis hypothesis) {
String text=null;
Log.e("onresult","in onresult");
if (hypothesis != null) {
text = hypothesis.getHypstr();
makeText(getApplicationContext(), text + "from onresult", Toast.LENGTH_SHORT).show();
selectOptions(hypothesis.getHypstr());
}
else{
// if (text.equals("start bike")) {
// makeText(getApplicationContext(), text + "from onresult", Toast.LENGTH_SHORT).show();
// selectOptions(hypothesis.getHypstr());
// } else if (text.equals("stop bike")) {
// makeText(getApplicationContext(), text + "from onresult", Toast.LENGTH_SHORT).show();
// selectOptions(hypothesis.getHypstr());
// } else if (text.equals("please lock bike")) {
// makeText(getApplicationContext(), text + "from onresult", Toast.LENGTH_SHORT).show();
// selectOptions(hypothesis.getHypstr());
// }
// else if (text.equals(" unlock bike")) {
// makeText(getApplicationContext(), text + "from onresult", Toast.LENGTH_SHORT).show();
// selectOptions(hypothesis.getHypstr());
// }
// else if (text.equals(" run engine")) {
// makeText(getApplicationContext(), text + "from onresult", Toast.LENGTH_SHORT).show();
// selectOptions(hypothesis.getHypstr());
// }
}
}
@Override
public void onError(Exception e) {
((TextView) findViewById(R.id.caption_text)).setText(e.getMessage() + "from onerror");
makeText(getApplicationContext(), "error occured", Toast.LENGTH_SHORT).show();
}
@Override
public void onTimeout() {
makeText(getApplicationContext(), "Timeout", Toast.LENGTH_SHORT).show();
switchSearch(KWS_SEARCH);
}
private void switchSearch(String searchName) {
recognizer.stop();
// If we are not spotting, start listening with timeout (10000 ms or 10 seconds).
if (searchName.equals(KWS_SEARCH))
recognizer.startListening(searchName,2000);
else
recognizer.startListening(searchName, 10000);
String caption = getResources().getString(captions.get(searchName));
Log.e("switchSearch", "switchSearch" + caption);
((TextView) findViewById(R.id.caption_text)).setText(caption);
}
sir i get data what i said (means if i say start bike speech recgnizer retuens me the same string in onresult method ) based on that i operate my bike.but some time if i dont say anything recognizer returns me previously called commands like start bike ,stop bike etc.
so i want that if i dont pass any commands to recognizer it should not send me previously called commands.it should send me particular passed command only once.
so please help me sir.
sir we have developed an android application for two wheelers in which we can start bike ,stop bike ,lock bike ,unlock bike and run engine using android app button's.now i want to operate my bike from app using voice commands like start bike ,stop bike ,run engine,lock bike etc.so when i
pass start bike command pocketsphinx recognizer returns me what i have called that is start bike string in onresult method .so i check in onresult if hypothysis is equals start bike then i pass data to actually start my bike and same for other commands also.
so i created one grammer file containing these commands .
below is my activity code for speech recognition.
// sir i have called runRecognizerSetup() in my activitiy's onCreateView() method also
@Override
public void onRequestPermissionsResult(int requestCode,String[] permissions, int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
if (requestCode == PERMISSIONS_REQUEST_RECORD_AUDIO) {
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
runRecognizerSetup();
} else {
finish();
}
}
}
private void setupRecognizer(File assetsDir) throws IOException {
// The recognizer can be configured to perform multiple searches
// of different kind and switch between them
recognizer = SpeechRecognizerSetup.defaultSetup()
.setAcousticModel(new File(assetsDir, "en-us-ptm"))
.setDictionary(new File(assetsDir, "cmudict-en-us.dict"))
.setRawLogDir(assetsDir).setKeywordThreshold(1e-45f) // To disable logging of raw audio comment out this call (takes a lot of space on the device)
.getRecognizer();
recognizer.addListener(MainActivity.this);
File kwsList = new File(assetsDir, "bike.gram");
recognizer.addGrammarSearch(KWS_SEARCH, kwsList);
// File menuGrammar = new File(assetsDir, "menu.gram");
// recognizer.addGrammarSearch(MENU_SEARCH, menuGrammar);
//// // Create grammar-based search for digit recognition
// File digitsGrammar = new File(assetsDir, "digits.gram");
// recognizer.addGrammarSearch(DIGITS_SEARCH, digitsGrammar);
// File languageModel = new File(assetsDir, "weather.dmp");
// recognizer.addNgramSearch(FORECAST_SEARCH, languageModel);// //Phonetic search
// File phoneticModel = new File(assetsDir, "en-phone.dmp");
// recognizer.addAllphoneSearch(PHONE_SEARCH, phoneticModel);
}
@Override
public void onBeginningOfSpeech() {
Log.e("start", "onbegin");
switchSearch(KWS_SEARCH);
}
public void onEndOfSpeech() {
if (!recognizer.getSearchName().equals(KWS_SEARCH)) {
Log.e("onEndOfSpeech", "2");
switchSearch(KWS_SEARCH);
}
}
@Override
public void onPartialResult(Hypothesis hypothesis) {
Hypothesis hype=null;
try{
if (hype == null) {
Log.e("onPartialResult", ""+hypothesis.getHypstr() + "3");
return;
}
else Log.e("partialresult",""+hype.getHypstr());
}catch(Exception e){}
recognizer.stop();
//Log.e("partialresult",""+hype.getHypstr());
// ((TextView) findViewById(R.id.caption_text)).setText(hype.getHypstr());
// if (hypothesis.getHypstr().equals(KEYPHRASE))
// switchSearch(MENU_SEARCH);
// else if (hypothesis.getHypstr().equals(DIGITS_SEARCH))
// switchSearch(DIGITS_SEARCH);
// else if (hypothesis.getHypstr().equals(PHONE_SEARCH))
// switchSearch(PHONE_SEARCH);
// else {
// ((TextView) findViewById(R.id.caption_text)).setText(hypothesis.getHypstr());
// }
}
private boolean selectOptions(String s) {
final String start="start bike",stop="stop bike",
run="run engine ",lock="please lock bike",unlock="unlock bike";
boolean value=false;
Log.e("selectOptions", "start of status"+status+s);
switch (s) {
case start:
Log.e("selectOptions", "start");
try {
Toast.makeText(getApplicationContext(), "start selected", Toast.LENGTH_SHORT).show();
status = start;
// byte[] a = {(byte) 01};
// final BluetoothGattCharacteristic writeRelayCharacteristic = dashboardService.getCharacteristic(UUID.fromString(keyBypassUUID));
// writeRelayCharacteristic.setValue(a);
// dashboardGatt.writeCharacteristic(writeRelayCharacteristic);
// bikeRunning = true;
Log.e("switch","start");
value = true;
}catch(Exception e){e.printStackTrace();}
break;
case stop:
Log.e("selectOptions","stop");
//Toast.makeText(getApplicationContext(), "stop selected", Toast.LENGTH_SHORT).show();
try {
// if (status.equals("start")) {
status = stop;
Toast.makeText(getApplicationContext(), "stop selected", Toast.LENGTH_SHORT).show();
// byte[] b = {(byte) 00};
// final BluetoothGattCharacteristic writeRelayStopCharacteristic = dashboardService.getCharacteristic(UUID.fromString(keyBypassUUID));
// writeRelayStopCharacteristic.setValue(b);
// dashboardGatt.writeCharacteristic(writeRelayStopCharacteristic);
// bikeRunning = false;
value = true;
//}
}catch (Exception e){e.printStackTrace();}
break;
case lock:
Log.e("selectOptions","lock");
try {
if (lockStatus.equals("unlock")) {
lockStatus = lock;
Toast.makeText(getApplicationContext(), "lock selected", Toast.LENGTH_SHORT).show();
value = true;
}
}catch (Exception e){e.printStackTrace();}
// byte[] c = {(byte) 00};
// final BluetoothGattCharacteristic writeRelayLockCharacteristic = dashboardService.getCharacteristic(UUID.fromString(masterCharUUID));
// writeRelayLockCharacteristic.setValue(c);
// dashboardGatt.writeCharacteristic(writeRelayLockCharacteristic);
// bikeMasterlock = false;
break;
case unlock:
Log.e("selectOptions","unlock");
try {
if (lockStatus.equals("lock")) {
lockStatus = unlock;
Toast.makeText(getApplicationContext(), "unlock selected", Toast.LENGTH_SHORT).show();
value = true;
}
}catch (Exception e){e.printStackTrace();}
// byte[] d = {(byte) 01};
// final BluetoothGattCharacteristic writeRelayUnlockCharacteristic = dashboardService.getCharacteristic(UUID.fromString(masterCharUUID));
// writeRelayUnlockCharacteristic.setValue(d);
// dashboardGatt.writeCharacteristic(writeRelayUnlockCharacteristic);
//bikeMasterlock = true;
break;
}
return value;
}
@Override
public void onResult(Hypothesis hypothesis) {
String text=null;
Log.e("onresult","in onresult");
if (hypothesis != null) {
text = hypothesis.getHypstr();
makeText(getApplicationContext(), text + "from onresult", Toast.LENGTH_SHORT).show();
selectOptions(hypothesis.getHypstr());
}
else{
// if (text.equals("start bike")) {
// makeText(getApplicationContext(), text + "from onresult", Toast.LENGTH_SHORT).show();
// selectOptions(hypothesis.getHypstr());
// } else if (text.equals("stop bike")) {
// makeText(getApplicationContext(), text + "from onresult", Toast.LENGTH_SHORT).show();
// selectOptions(hypothesis.getHypstr());
// } else if (text.equals("please lock bike")) {
// makeText(getApplicationContext(), text + "from onresult", Toast.LENGTH_SHORT).show();
// selectOptions(hypothesis.getHypstr());
// }
// else if (text.equals(" unlock bike")) {
// makeText(getApplicationContext(), text + "from onresult", Toast.LENGTH_SHORT).show();
// selectOptions(hypothesis.getHypstr());
// }
// else if (text.equals(" run engine")) {
// makeText(getApplicationContext(), text + "from onresult", Toast.LENGTH_SHORT).show();
// selectOptions(hypothesis.getHypstr());
// }
}
}
@Override
public void onError(Exception e) {
((TextView) findViewById(R.id.caption_text)).setText(e.getMessage() + "from onerror");
makeText(getApplicationContext(), "error occured", Toast.LENGTH_SHORT).show();
}
@Override
public void onTimeout() {
makeText(getApplicationContext(), "Timeout", Toast.LENGTH_SHORT).show();
switchSearch(KWS_SEARCH);
}
private void switchSearch(String searchName) {
recognizer.stop();
// If we are not spotting, start listening with timeout (10000 ms or 10 seconds).
if (searchName.equals(KWS_SEARCH))
recognizer.startListening(searchName,2000);
else
recognizer.startListening(searchName, 10000);
String caption = getResources().getString(captions.get(searchName));
Log.e("switchSearch", "switchSearch" + caption);
((TextView) findViewById(R.id.caption_text)).setText(caption);
}
sir i get data what i said (means if i say start bike speech recgnizer retuens me the same string in onresult method ) based on that i operate my bike.but some time if i dont say anything recognizer returns me previously called commands like start bike ,stop bike etc.
so i want that if i dont pass any commands to recognizer it should not send me previously called commands.it should send me particular passed command only once.
so please help me sir.
hello sir, we have developed an android application for two wheelers in which we can start bike ,stop bike ,lock bike ,unlock bike and run engine using android app button's.now i want to operate my bike from app using voice commands like start bike ,stop bike ,run engine,lock bike etc.so when i
pass start bike command pocketsphinx recognizer returns me what i have called that is start bike string in onresult method .so i check in onresult if hypothysis is equals start bike then i pass data to actually start my bike and same for other commands also.
so i created one grammer file containing these commands .
below is file of my activity code for speech recognition and a grammer file for commands.
// sir please see my activity code.sir i have called runRecognizerSetup() in my activitiy's onCreateView() method also.
sir i get data what i said (means if i say start bike speech recgnizer retuens me the same string in onresult method ) based on that i operate my bike.but some time if i dont say anything recognizer returns me previously called commands like start bike ,stop bike etc.
so i want that if i dont pass any commands to recognizer it should not send me previously called commands.it should send me particular passed command only once.
so please help me sir.
below is my grammer file code
// this is grammer file code
/*
* JSGF Grammar file for bike commands /
grammar hello;
public <greet> =( start bike | stop bike | run engine|please lock bike| unlock bike);
sir i have posted my problem in detail with grammer file and recognition code .please see the code
in the second page of my previous query .please help me please please ...
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
sir we have developed an android application for two wheelers in which we can start bike ,stop bike ,lock bike ,unlock bike and run engine using android app button's.now i want to operate my bike from app using voice commands like start bike ,stop bike ,run engine,lock bike etc.so when i
pass start bike command pocketsphinx recognizer returns me what i have called that is start bike string in onresult method .so i check in onresult if hypothysis is equals start bike then i pass data to actually start my bike and same for other commands also.
so i created one grammer file containing these commands .
below is my activity code for speech recognition.
// sir i have called runRecognizerSetup() in my activitiy's onCreateView() method also
@Override
public void onRequestPermissionsResult(int requestCode,String[] permissions, int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
if (requestCode == PERMISSIONS_REQUEST_RECORD_AUDIO) {
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
runRecognizerSetup();
} else {
finish();
}
}
}
private void setupRecognizer(File assetsDir) throws IOException {
// The recognizer can be configured to perform multiple searches
// of different kind and switch between them
// File menuGrammar = new File(assetsDir, "menu.gram");
// recognizer.addGrammarSearch(MENU_SEARCH, menuGrammar);
//// // Create grammar-based search for digit recognition
// File digitsGrammar = new File(assetsDir, "digits.gram");
// recognizer.addGrammarSearch(DIGITS_SEARCH, digitsGrammar);
// File languageModel = new File(assetsDir, "weather.dmp");
// recognizer.addNgramSearch(FORECAST_SEARCH, languageModel);// //Phonetic search
// File phoneticModel = new File(assetsDir, "en-phone.dmp");
// recognizer.addAllphoneSearch(PHONE_SEARCH, phoneticModel);
}
@Override
public void onBeginningOfSpeech() {
Log.e("start", "onbegin");
switchSearch(KWS_SEARCH);
}
public void onEndOfSpeech() {
if (!recognizer.getSearchName().equals(KWS_SEARCH)) {
Log.e("onEndOfSpeech", "2");
switchSearch(KWS_SEARCH);
}
}
@Override
public void onPartialResult(Hypothesis hypothesis) {
Hypothesis hype=null;
try{
if (hype == null) {
Log.e("onPartialResult", ""+hypothesis.getHypstr() + "3");
return;
}
else Log.e("partialresult",""+hype.getHypstr());
}catch(Exception e){}
recognizer.stop();
//Log.e("partialresult",""+hype.getHypstr());
// ((TextView) findViewById(R.id.caption_text)).setText(hype.getHypstr());
// if (hypothesis.getHypstr().equals(KEYPHRASE))
// switchSearch(MENU_SEARCH);
// else if (hypothesis.getHypstr().equals(DIGITS_SEARCH))
// switchSearch(DIGITS_SEARCH);
// else if (hypothesis.getHypstr().equals(PHONE_SEARCH))
// switchSearch(PHONE_SEARCH);
// else {
// ((TextView) findViewById(R.id.caption_text)).setText(hypothesis.getHypstr());
// }
}
// byte[] a = {(byte) 01};
// final BluetoothGattCharacteristic writeRelayCharacteristic = dashboardService.getCharacteristic(UUID.fromString(keyBypassUUID));
// writeRelayCharacteristic.setValue(a);
// dashboardGatt.writeCharacteristic(writeRelayCharacteristic);
// bikeRunning = true;
Log.e("switch","start");
value = true;
}catch(Exception e){e.printStackTrace();}
break;
case stop:
Log.e("selectOptions","stop");
//Toast.makeText(getApplicationContext(), "stop selected", Toast.LENGTH_SHORT).show();
try {
// if (status.equals("start")) {
status = stop;
Toast.makeText(getApplicationContext(), "stop selected", Toast.LENGTH_SHORT).show();
// byte[] b = {(byte) 00};
// final BluetoothGattCharacteristic writeRelayStopCharacteristic = dashboardService.getCharacteristic(UUID.fromString(keyBypassUUID));
// writeRelayStopCharacteristic.setValue(b);
// dashboardGatt.writeCharacteristic(writeRelayStopCharacteristic);
// bikeRunning = false;
value = true;
//}
}catch (Exception e){e.printStackTrace();}
break;
case lock:
Log.e("selectOptions","lock");
try {
if (lockStatus.equals("unlock")) {
lockStatus = lock;
Toast.makeText(getApplicationContext(), "lock selected", Toast.LENGTH_SHORT).show();
value = true;
}
}catch (Exception e){e.printStackTrace();}
// byte[] c = {(byte) 00};
// final BluetoothGattCharacteristic writeRelayLockCharacteristic = dashboardService.getCharacteristic(UUID.fromString(masterCharUUID));
// writeRelayLockCharacteristic.setValue(c);
// dashboardGatt.writeCharacteristic(writeRelayLockCharacteristic);
// bikeMasterlock = false;
break;
case unlock:
Log.e("selectOptions","unlock");
try {
if (lockStatus.equals("lock")) {
lockStatus = unlock;
Toast.makeText(getApplicationContext(), "unlock selected", Toast.LENGTH_SHORT).show();
value = true;
}
}catch (Exception e){e.printStackTrace();}
// byte[] d = {(byte) 01};
// final BluetoothGattCharacteristic writeRelayUnlockCharacteristic = dashboardService.getCharacteristic(UUID.fromString(masterCharUUID));
// writeRelayUnlockCharacteristic.setValue(d);
// dashboardGatt.writeCharacteristic(writeRelayUnlockCharacteristic);
//bikeMasterlock = true;
break;
}
return value;
}
@Override
public void onResult(Hypothesis hypothesis) {
String text=null;
Log.e("onresult","in onresult");
if (hypothesis != null) {
text = hypothesis.getHypstr();
makeText(getApplicationContext(), text + "from onresult", Toast.LENGTH_SHORT).show();
selectOptions(hypothesis.getHypstr());
}
else{
// if (text.equals("start bike")) {
// makeText(getApplicationContext(), text + "from onresult", Toast.LENGTH_SHORT).show();
// selectOptions(hypothesis.getHypstr());
// } else if (text.equals("stop bike")) {
// makeText(getApplicationContext(), text + "from onresult", Toast.LENGTH_SHORT).show();
// selectOptions(hypothesis.getHypstr());
// } else if (text.equals("please lock bike")) {
// makeText(getApplicationContext(), text + "from onresult", Toast.LENGTH_SHORT).show();
// selectOptions(hypothesis.getHypstr());
// }
// else if (text.equals(" unlock bike")) {
// makeText(getApplicationContext(), text + "from onresult", Toast.LENGTH_SHORT).show();
// selectOptions(hypothesis.getHypstr());
// }
// else if (text.equals(" run engine")) {
// makeText(getApplicationContext(), text + "from onresult", Toast.LENGTH_SHORT).show();
// selectOptions(hypothesis.getHypstr());
// }
}
}
@Override
public void onError(Exception e) {
((TextView) findViewById(R.id.caption_text)).setText(e.getMessage() + "from onerror");
makeText(getApplicationContext(), "error occured", Toast.LENGTH_SHORT).show();
}
@Override
public void onTimeout() {
makeText(getApplicationContext(), "Timeout", Toast.LENGTH_SHORT).show();
switchSearch(KWS_SEARCH);
}
private void switchSearch(String searchName) {
recognizer.stop();
// If we are not spotting, start listening with timeout (10000 ms or 10 seconds).
if (searchName.equals(KWS_SEARCH))
recognizer.startListening(searchName,2000);
else
recognizer.startListening(searchName, 10000);
String caption = getResources().getString(captions.get(searchName));
Log.e("switchSearch", "switchSearch" + caption);
((TextView) findViewById(R.id.caption_text)).setText(caption);
}
sir i get data what i said (means if i say start bike speech recgnizer retuens me the same string in onresult method ) based on that i operate my bike.but some time if i dont say anything recognizer returns me previously called commands like start bike ,stop bike etc.
so i want that if i dont pass any commands to recognizer it should not send me previously called commands.it should send me particular passed command only once.
so please help me sir.
sir we have developed an android application for two wheelers in which we can start bike ,stop bike ,lock bike ,unlock bike and run engine using android app button's.now i want to operate my bike from app using voice commands like start bike ,stop bike ,run engine,lock bike etc.so when i
pass start bike command pocketsphinx recognizer returns me what i have called that is start bike string in onresult method .so i check in onresult if hypothysis is equals start bike then i pass data to actually start my bike and same for other commands also.
so i created one grammer file containing these commands .
below is my activity code for speech recognition.
// sir i have called runRecognizerSetup() in my activitiy's onCreateView() method also
@Override
public void onRequestPermissionsResult(int requestCode,String[] permissions, int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
if (requestCode == PERMISSIONS_REQUEST_RECORD_AUDIO) {
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
runRecognizerSetup();
} else {
finish();
}
}
}
private void setupRecognizer(File assetsDir) throws IOException {
// The recognizer can be configured to perform multiple searches
// of different kind and switch between them
recognizer = SpeechRecognizerSetup.defaultSetup()
.setAcousticModel(new File(assetsDir, "en-us-ptm"))
.setDictionary(new File(assetsDir, "cmudict-en-us.dict"))
.setRawLogDir(assetsDir).setKeywordThreshold(1e-45f) // To disable logging of raw audio comment out this call (takes a lot of space on the device)
.getRecognizer();
recognizer.addListener(MainActivity.this);
File kwsList = new File(assetsDir, "bike.gram");
recognizer.addGrammarSearch(KWS_SEARCH, kwsList);
// File menuGrammar = new File(assetsDir, "menu.gram");
// recognizer.addGrammarSearch(MENU_SEARCH, menuGrammar);
//// // Create grammar-based search for digit recognition
// File digitsGrammar = new File(assetsDir, "digits.gram");
// recognizer.addGrammarSearch(DIGITS_SEARCH, digitsGrammar);
// File languageModel = new File(assetsDir, "weather.dmp");
// recognizer.addNgramSearch(FORECAST_SEARCH, languageModel);// //Phonetic search
// File phoneticModel = new File(assetsDir, "en-phone.dmp");
// recognizer.addAllphoneSearch(PHONE_SEARCH, phoneticModel);
}
@Override
public void onBeginningOfSpeech() {
Log.e("start", "onbegin");
switchSearch(KWS_SEARCH);
}
public void onEndOfSpeech() {
if (!recognizer.getSearchName().equals(KWS_SEARCH)) {
Log.e("onEndOfSpeech", "2");
switchSearch(KWS_SEARCH);
}
}
@Override
public void onPartialResult(Hypothesis hypothesis) {
Hypothesis hype=null;
try{
if (hype == null) {
Log.e("onPartialResult", ""+hypothesis.getHypstr() + "3");
return;
}
else Log.e("partialresult",""+hype.getHypstr());
}catch(Exception e){}
recognizer.stop();
//Log.e("partialresult",""+hype.getHypstr());
// ((TextView) findViewById(R.id.caption_text)).setText(hype.getHypstr());
// if (hypothesis.getHypstr().equals(KEYPHRASE))
// switchSearch(MENU_SEARCH);
// else if (hypothesis.getHypstr().equals(DIGITS_SEARCH))
// switchSearch(DIGITS_SEARCH);
// else if (hypothesis.getHypstr().equals(PHONE_SEARCH))
// switchSearch(PHONE_SEARCH);
// else {
// ((TextView) findViewById(R.id.caption_text)).setText(hypothesis.getHypstr());
// }
}
private boolean selectOptions(String s) {
final String start="start bike",stop="stop bike",
run="run engine ",lock="please lock bike",unlock="unlock bike";
boolean value=false;
Log.e("selectOptions", "start of status"+status+s);
switch (s) {
case start:
Log.e("selectOptions", "start");
try {
// byte[] a = {(byte) 01};
// final BluetoothGattCharacteristic writeRelayCharacteristic = dashboardService.getCharacteristic(UUID.fromString(keyBypassUUID));
// writeRelayCharacteristic.setValue(a);
// dashboardGatt.writeCharacteristic(writeRelayCharacteristic);
// bikeRunning = true;
Log.e("switch","start");
value = true;
}catch(Exception e){e.printStackTrace();}
break;
case stop:
Log.e("selectOptions","stop");
//Toast.makeText(getApplicationContext(), "stop selected", Toast.LENGTH_SHORT).show();
try {
// if (status.equals("start")) {
status = stop;
Toast.makeText(getApplicationContext(), "stop selected", Toast.LENGTH_SHORT).show();
// byte[] b = {(byte) 00};
// final BluetoothGattCharacteristic writeRelayStopCharacteristic = dashboardService.getCharacteristic(UUID.fromString(keyBypassUUID));
// writeRelayStopCharacteristic.setValue(b);
// dashboardGatt.writeCharacteristic(writeRelayStopCharacteristic);
// bikeRunning = false;
value = true;
//}
}catch (Exception e){e.printStackTrace();}
break;
case lock:
Log.e("selectOptions","lock");
try {
if (lockStatus.equals("unlock")) {
lockStatus = lock;
Toast.makeText(getApplicationContext(), "lock selected", Toast.LENGTH_SHORT).show();
value = true;
}
}catch (Exception e){e.printStackTrace();}
// byte[] c = {(byte) 00};
// final BluetoothGattCharacteristic writeRelayLockCharacteristic = dashboardService.getCharacteristic(UUID.fromString(masterCharUUID));
// writeRelayLockCharacteristic.setValue(c);
// dashboardGatt.writeCharacteristic(writeRelayLockCharacteristic);
// bikeMasterlock = false;
break;
case unlock:
Log.e("selectOptions","unlock");
try {
if (lockStatus.equals("lock")) {
lockStatus = unlock;
Toast.makeText(getApplicationContext(), "unlock selected", Toast.LENGTH_SHORT).show();
value = true;
}
}catch (Exception e){e.printStackTrace();}
// byte[] d = {(byte) 01};
// final BluetoothGattCharacteristic writeRelayUnlockCharacteristic = dashboardService.getCharacteristic(UUID.fromString(masterCharUUID));
// writeRelayUnlockCharacteristic.setValue(d);
// dashboardGatt.writeCharacteristic(writeRelayUnlockCharacteristic);
//bikeMasterlock = true;
break;
}
return value;
}
@Override
public void onResult(Hypothesis hypothesis) {
String text=null;
Log.e("onresult","in onresult");
if (hypothesis != null) {
text = hypothesis.getHypstr();
makeText(getApplicationContext(), text + "from onresult", Toast.LENGTH_SHORT).show();
selectOptions(hypothesis.getHypstr());
}
else{
// if (text.equals("start bike")) {
// makeText(getApplicationContext(), text + "from onresult", Toast.LENGTH_SHORT).show();
// selectOptions(hypothesis.getHypstr());
// } else if (text.equals("stop bike")) {
// makeText(getApplicationContext(), text + "from onresult", Toast.LENGTH_SHORT).show();
// selectOptions(hypothesis.getHypstr());
// } else if (text.equals("please lock bike")) {
// makeText(getApplicationContext(), text + "from onresult", Toast.LENGTH_SHORT).show();
// selectOptions(hypothesis.getHypstr());
// }
// else if (text.equals(" unlock bike")) {
// makeText(getApplicationContext(), text + "from onresult", Toast.LENGTH_SHORT).show();
// selectOptions(hypothesis.getHypstr());
// }
// else if (text.equals(" run engine")) {
// makeText(getApplicationContext(), text + "from onresult", Toast.LENGTH_SHORT).show();
// selectOptions(hypothesis.getHypstr());
// }
}
}
@Override
public void onError(Exception e) {
((TextView) findViewById(R.id.caption_text)).setText(e.getMessage() + "from onerror");
makeText(getApplicationContext(), "error occured", Toast.LENGTH_SHORT).show();
}
@Override
public void onTimeout() {
makeText(getApplicationContext(), "Timeout", Toast.LENGTH_SHORT).show();
switchSearch(KWS_SEARCH);
}
private void switchSearch(String searchName) {
recognizer.stop();
// If we are not spotting, start listening with timeout (10000 ms or 10 seconds).
if (searchName.equals(KWS_SEARCH))
recognizer.startListening(searchName,2000);
else
recognizer.startListening(searchName, 10000);
String caption = getResources().getString(captions.get(searchName));
Log.e("switchSearch", "switchSearch" + caption);
((TextView) findViewById(R.id.caption_text)).setText(caption);
}
sir i get data what i said (means if i say start bike speech recgnizer retuens me the same string in onresult method ) based on that i operate my bike.but some time if i dont say anything recognizer returns me previously called commands like start bike ,stop bike etc.
so i want that if i dont pass any commands to recognizer it should not send me previously called commands.it should send me particular passed command only once.
so please help me sir.
hello sir, we have developed an android application for two wheelers in which we can start bike ,stop bike ,lock bike ,unlock bike and run engine using android app button's.now i want to operate my bike from app using voice commands like start bike ,stop bike ,run engine,lock bike etc.so when i
pass start bike command pocketsphinx recognizer returns me what i have called that is start bike string in onresult method .so i check in onresult if hypothysis is equals start bike then i pass data to actually start my bike and same for other commands also.
so i created one grammer file containing these commands .
below is file of my activity code for speech recognition and a grammer file for commands.
// sir please see my activity code.sir i have called runRecognizerSetup() in my activitiy's onCreateView() method also.
sir i get data what i said (means if i say start bike speech recgnizer retuens me the same string in onresult method ) based on that i operate my bike.but some time if i dont say anything recognizer returns me previously called commands like start bike ,stop bike etc.
so i want that if i dont pass any commands to recognizer it should not send me previously called commands.it should send me particular passed command only once.
so please help me sir.
below is my grammer file code
// this is grammer file code
/*
* JSGF Grammar file for bike commands
/
grammar hello;
public <greet> =( start bike | stop bike | run engine|please lock bike| unlock bike);
sir i have posted my problem in detail with grammer file and recognition code .please see the code
in the second page of my previous query .please help me please please ...
To ask a new question you have to start a new thread. You also need to format the post properly.