PocketSphinx won't initialize in python project

93 Views Asked by At

I'm putting together a voice assistant program using python 3.11 as a personal project. The assistant is supposed to listen for a trigger word with pocketsphinx and then activate speech recognition with google and listen to the command. However, pocketsphinx has repeatedly failed to initialize.

The code for the voice_app.py file, which handles processing audio input, is as follows:

import pyaudio
import speech_recognition as sr
from pocketsphinx import Pocketsphinx
from config import POCKETSPHINX_WAKE_WORD

class VoiceApp:
    def __init__(self):
        # Initialize the pocketsphinx recognizer
        self.ps = Pocketsphinx(
            hmm="C:/Python311/Lib/site-packages/pocketsphinx/model/en_us/",
            lm=False,
            dic="C:/Python311/Lib/site-packages/pocketsphinx/model/en_us/cmudict-en-us.dict"
        )

        # Set the wake word
        self.ps.set_kws('wake_word', f"{POCKETSPHINX_WAKE_WORD}")

        # Initialize PyAudio
        self.pa = pyaudio.PyAudio()
        self.stream = self.pa.open(
            format=pyaudio.paInt16,
            channels=1,
            rate=16000,
            input=True,
            frames_per_buffer=1024
        )

    def listen_for_trigger(self):
        # Start the wake word detection
        self.ps.start_utt()
        while True:
            # Read audio data from the microphone
            buf = self.stream.read(1024)
            if buf:
                # Send the audio data to pocketsphinx for processing
                self.ps.process_raw(buf, False, False)
                # Check if the wake word was detected
                if self.ps.hyp():
                    break
            else:
                break
        self.ps.end_utt()

    def listen_for_command(self):
        r = sr.Recognizer()
        with sr.Microphone() as source:
            print("Listening for command...")
            audio = r.listen(source)

        try:
            # Use SpeechRecognition to transcribe the command
            command = r.recognize_google(audio)
            print(f"You said: {command}")
            return command
        except sr.UnknownValueError:
            print("Google Speech Recognition could not understand audio")
        except sr.RequestError as e:
            print(f"Could not request results from Google Speech Recognition service; {e}")

    def close(self):
        # Close the PyAudio stream
        self.stream.close()
        self.pa.terminate()

The error message I'm getting in vscode is:

Failed to initialize PocketSphinx
  File "C:\Geraldine\Assistant\voice_app.py", line 9, in __init__
    self.ps = Pocketsphinx(
              ^^^^^^^^^^^^^
  File "C:\Geraldine\Assistant\main.py", line 14, in main
    voice_app = VoiceApp()
                ^^^^^^^^^^
  File "C:\Geraldine\Assistant\main.py", line 54, in <module>
    main()
RuntimeError: Failed to initialize PocketSphinx

I have reinstalled pocketsphinx several times, checked the filepaths (they definitely are correct), and I'm at a loss here

0

There are 0 best solutions below