0

I am trying to use pocketsphinx in Python using the code given below:

#!/usr/bin/python

from os import environ, path

from pocketsphinx.pocketsphinx import *
from sphinxbase.sphinxbase import *

MODELDIR = "pocketsphinx-5prealpha/model"
DATADIR = "pocketsphinx-5prealpha/test/data"

config = Decoder.default_config()
config.set_string('-hmm', path.join(MODELDIR, 'en-us/en-us'))
config.set_string('-lm', path.join(MODELDIR, 'en-us/en-us.lm.bin'))
config.set_string('-dict', path.join(MODELDIR, 'en-us/cmudict-     en-us.dict'))
config.set_string('-logfn', '/dev/null')
decoder = Decoder(config)

import pyaudio
p = pyaudio.PyAudio()
stream = p.open(format=pyaudio.paInt16, channels=1, rate=44100,     input=True, frames_per_buffer=1024)
stream.start_stream()

in_speech_bf = False
decoder.start_utt()
while True:
    buf = stream.read(0)
if buf:
    decoder.process_raw(buf, False, False)
    if decoder.get_in_speech() != in_speech_bf:
        in_speech_bf = decoder.get_in_speech()
        if not in_speech_bf:
            decoder.end_utt()
            print 'Result:', decoder.hyp().hypstr
            decoder.start_utt()
else:
    break
decoder.end_utt()

The reason why I gave stream.read(0) is because whatever number i put for it(I tried 512-32768), it gives me input overflowed. This is the error i get when i put stream.read(1024):

Traceback (most recent call last):
  File "nnntest.py", line 26, in <module>
    buf = stream.read(1024)
  File "/usr/lib/pymodules/python2.7/pyaudio.py", line 564, in read
    return pa.read_stream(self._stream, num_frames)
IOError: [Errno Input overflowed] -9981

So, I tried to put stream.read(0) as said in the comments section in this question : PocketSphinx - ALSA IOError: -9997 while trying to Decode Speech . When i do that, the program stops running with displaying this:

ALSA lib pcm.c:2217:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.rear
ALSA lib pcm.c:2217:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.center_lfe
ALSA lib pcm.c:2217:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.side
ALSA lib pcm.c:2217:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.hdmi
ALSA lib pcm.c:2217:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.hdmi
ALSA lib pcm.c:2217:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.modem
ALSA lib pcm.c:2217:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.modem
ALSA lib pcm.c:2217:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.phoneline
ALSA lib pcm.c:2217:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.phoneline
ALSA lib pcm_hw.c:1401:(_snd_pcm_hw_open) Invalid value for card
ALSA lib pcm_hw.c:1401:(_snd_pcm_hw_open) Invalid value for card
ALSA lib pcm_dmix.c:957:(snd_pcm_dmix_open) The dmix plugin supports only playback stream

I've got a big project to complete and any help is appreciated! What should i do?

  • These messages are harmless. The problem probably is that your buffer size is much too small. – CL. Mar 18 '17 at 22:32
  • I tried using 512,1024,2048,8192,16384,32768... in the place of frame_buffer_size but same error repeated again. I also changed it in stream.read(), trying every values but nothing works! :( – Alok Naushad Mar 19 '17 at 10:01
1

It is not possible to recognize large vocabulary speech on Raspberry Pi, it is too slow for that. This was discussed before. You can stream the data to the server or configure small grammar for recognition if you still want to recognize on the device.

If you want to use smaller model, you still need to configure the sample rate since you record at sample rate 44100, you need to add

 config.set_float('-samprate', 44100)
 config.set_int('-nfft', 2048)
  • But, according to those threads, they can't even recognize running the command pocketsphin_continuous. I can run pocketsphinx in terminal. When i use pocketsphinx_continuous -imic yes, it works with no problem. I can also use it with my own dictionary and language model. But the problem is, i can't connect it with python. I need to run some commands when some words are there. So, I am using Python. But I can't get it to work! :( – Alok Naushad Mar 19 '17 at 9:56
  • I updated the answer. – Nikolay Shmyrev Mar 20 '17 at 3:46
1

Even the small custom dictionary was giving me the same error. Maybe this has to do something with the pyAudio itself. What helped in this case was setting "exception_on_overflow = False" as the stream method's second argument. This way at least the program continues to run. Let's see if it's enough to make the functionality work.

p = pyaudio.PyAudio()
stream=p.open(format=pyaudio.paInt16, channels=1, rate=16000, input=True, frames_per_buffer=1024)
buf = stream.read(1024, exception_on_overflow=False)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.