Search code examples
pythonvoice-recognitionpocketsphinx

Pocketsphinx Overflow Error


I've been trying to use pocketsphinx for voice recognition but I keep getting an odd error. My code is as follows

#!/usr/bin/env python
from os import environ, path
import pyaudio

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

MODELDIR = "pocketsphinx/model"
DATADIR = "pocketsphinx/test/data"

# Create a decoder with certain model
config = Decoder.default_config()
config.set_string('-hmm', path.join(MODELDIR, 'en-us/en-us'))
config.set_string('-lm', path.join(MODELDIR, 'MERLIN/MERLINv1.lm'))
config.set_string('-dict', path.join(MODELDIR, 'MERLIN/MERLINv1.dic'))
decoder = Decoder(config)


p = pyaudio.PyAudio()
stream = p.open(format=pyaudio.paInt16, channels=1, rate=16000, input=True, frames_per_buffer=256)
stream.start_stream() 

in_speech_bf = False
decoder.start_utt()
try:
    while True:
        buf = stream.read(256)
        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()
except:
    print'Exception caught! Handeling...'

When I run the code, it is supposed to wait for me to speak, then print what it thinks I said and wait until I talk again. In reality, it waits, prints, then throws an error and exits.

Result: DISPLAY CLOCK
Traceback (most recent call last):
  File "SR_test.py", line 26, in <module>
    buf = stream.read(256)
  File "/usr/lib/python2.7/dist-packages/pyaudio.py", line 608, in read
    return pa.read_stream(self._stream, num_frames, exception_on_overflow)
IOError: [Errno -9981] Input overflowed

I have looked this error up and the suggestion was to increase or decrease the chunk size. I have changed the chunks to 256-2048 to no avail. Does anyone know how to solve this? Thank you in advance.

By the way, since the code works up until the error, I also tried to trick it into ignoring the error with a try-except clause. Unfortunately, it just prints my statement and exits anyway.


Solution

  • It fails to process audio in realtime most likely because your CPU is too slow, like you are trying to run on raspberry pi with large vocabulary.

    You can see exact realtime numbers in pocketsphinx log output.

    You need to get faster CPU or you can also try optimize the decoder configuration/vocabulary as described in tutorial to decoder tuning