1

I have installed and setup both pocketsphinx and sphinxbase packages in python. I have also taken code of speech recognition for github and changed both data and mode directory as per requirement but still it is unable to stream by voice when I am trying to run it by "python test.py" Here is the code:

#!/usr/bin/env python
import os
import sphinxbase as sb
import pocketsphinx as ps

MODELDIR = '/usr/lib/python2.7/site-packages/speech_recognition/pocketsphinx-data'
DATADIR='/usr/lib/python2.7/site-packages/speech_recognition/pocketsphinx-data'
# Create a decoder with certain model
config = ps.Decoder.default_config()
config.set_string('-hmm', "/usr/lib/python2.7/site-packages/speech_recognition/pocketsphinx-data/en-US/acoustic-model")
config.set_string('-lm', os.path.join(MODELDIR, 'en-US/language-model.lm.bin'))
config.set_string('-dict', os.path.join(MODELDIR, 'en-US/pronounciation-dictionary.dict'))
decoder = ps.Decoder(config)

# Decode streaming data.
decoder.start_utt()
stream = open(os.path.join(DATADIR, 'en-US/goforward.raw'), 'rb')
while True: 
    buf = stream.read(1024)
    if buf:
        decoder.process_raw(buf, False, False)
    else:
        break
decoder.end_utt()
stream.close()
print('Best hypothesis segments:', [seg.word for seg in decoder.seg()])

Please tell me how to execute it.

Nikolay Shmyrev
  • 24,897
  • 5
  • 43
  • 87
Krishna Agarwal
  • 23
  • 1
  • 1
  • 5

1 Answers1

8

Continuous recognition from microphone should look like this:

#!/usr/bin/python

from os import environ, path
import pyaudio

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

MODELDIR = "../../../model"

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'))
decoder = Decoder(config)

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

in_speech_bf = False
decoder.start_utt()
while True:
    buf = stream.read(1024)
    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()
Nikolay Shmyrev
  • 24,897
  • 5
  • 43
  • 87
  • When I run your code, I have this Error: `error: command 'swig.exe' failed: No such file or directory` `ERROR: Failed building wheel for pocketsphinx` – mehdi alizade Sep 25 '21 at 19:22
  • @mehdializade `swig` is a command required to build `pocketsphinx`. You should be able to install swig on Windows using the information in [this question](https://stackoverflow.com/q/7443112/16775594). – Sylvester Kruin Dec 28 '21 at 14:24
  • This code gives me large error messages when I run it; is there a Python 3 equivalent? – Sylvester Kruin Dec 28 '21 at 14:51
  • @SylvesterKruin wrap the print command in brackets. `print ('Result:', decoder.hyp().hypstr)` – twobob Jun 13 '22 at 02:24