I am trying to implement Speech Recognition with the help of Pocket Sphinx Library
.
What my approach was,
I just downloaded their demo project for Android. Imported it in Eclipse, did clean-build and run it on device. It ran successfully.
After this I copied the libs
folder from demo project to my own project.
I copied the assets
folder contents as it is in my own project.
Then I edited the digits.gram
file contents as per this post.
Then implemented the Listener
in my activity and added addKeywordSearch
to it.
My Questions:
Is this approach correct to implement this library in our own projects? Can we just copy paste all the files, modify
.gram
files as per need and run it? Or we need to do some other steps to generate.gram
files?When I tried with the above mentioned approach, then project ran successfully on the device for a few times. But after that it started showing the following error.
E/cmusphinx(10470): ERROR: "kws_search.c", line 158: The word '/1e-20/' is missing in the dictionary E/AndroidRuntime(10470): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.uwemo.myvdemo/com.uwemo.myvdemo.MainActivity}: java.lang.RuntimeException: Decoder_setSearch returned -1
However /1e-20/
exists in my digits.gram
file.
digits.gram file contents:
up /1e-20/
down /1e-15/
left /1e-15/
right /1e-15/
here /1e-15/
I noticed one thing, when I do clean-build of demo project of Pocket Sphinx and run it, then it recreates
digits.gram.md5
files. But when I copied pasted all in my own project and did its cleant-build then these files were not recreated. Is this normal?Is it necessary to have all the files in our project as in the demo project? Or can I delete some unused files from my own project? Any list of necessary files (either in assets folder or libs or others) will be highly helpful.
Please let me know what approach should I have to successfully implement it in my own project.
My Activity Code
public class MainActivity extends Activity implements RecognitionListener{
private static final String DIGITS_SEARCH = "digits";
private SpeechRecognizer recognizer;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
try
{
Assets assets = new Assets(MainActivity.this);
File assetDir = assets.syncAssets();
setupRecognizer(assetDir);
Toast.makeText(getApplicationContext(), "Speech Recognizer Started", Toast.LENGTH_SHORT).show();
}
catch (IOException e){
e.printStackTrace();
}
}
@Override
public void onBeginningOfSpeech() {
// TODO Auto-generated method stub
}
@Override
public void onEndOfSpeech() {
// TODO Auto-generated method stub
reset();
}
@Override
public void onPartialResult(Hypothesis arg0) {
// TODO Auto-generated method stub
}
@Override
public void onResult(Hypothesis hypothesis) {
// TODO Auto-generated method stub
if (hypothesis != null){
String text = hypothesis.getHypstr();
Toast.makeText(getApplicationContext(), "you got ::"+text, Toast.LENGTH_SHORT).show();
}
}
private void setupRecognizer(File assetsDir){
File modelsDir = new File(assetsDir, "models");
recognizer = defaultSetup().setAcousticModel(new File(modelsDir, "hmm/en-us-semi"))
.setDictionary(new File(modelsDir, "dict/cmu07a.dic"))
.setRawLogDir(assetsDir).setKeywordThreshold(1e-20f)
.getRecognizer();
recognizer.addListener(this);
File digitsGrammar = new File(modelsDir, "grammar/digits.gram");
recognizer.addKeywordSearch(DIGITS_SEARCH, digitsGrammar);
reset();
}
private void reset(){
recognizer.stop();
recognizer.startListening(DIGITS_SEARCH);
}
}
My project structure is following
来源:https://stackoverflow.com/questions/31542837/setting-up-pocketsphinx-library-in-my-own-android-app