Search code examples
speech-recognitionpocketsphinx

Pocketsphinx cuts words


I have a problem. When I turn pocketsphinx by console everything works fine:

pi@raspberrypi:~ $ pocketsphinx_continuous -hmm /usr/local/share/pocketsphinx/model/en-us/en-us -lm 6764.lm -dict 6764.dic -samprate 16000 -inmic yes -adcdev plughw:0,0

However when i try run it by python script, pocketsphinx recognises wrong words.

Its the code of python script:

import sys, os, pyaudio
from pocketsphinx.pocketsphinx import *
from sphinxbase.sphinxbase import *

modeldir = "/home/pi/Downloads/sphinx4-5prealpha-src/sphinx4-data/src/main/resources/edu/cmu/sphinx/models/"

# Create a decoder with certain model
config = Decoder.default_config()
config.set_string('-hmm', os.path.join(modeldir, 'en-us/en-us'))
config.set_string('-dict', '/home/pi/Downloads/6764.dic')
config.set_string('-lm', '/home/pi/Downloads/6764.lm')
config.set_string('-samprate', '16000')

config.set_string('-adcdev', 'plughw:0,0')         

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

decoder = Decoder(config)
decoder.start_utt()
print ("RECOGNITION STARTED")


while True:
    buf = stream.read(1024)


    decoder.process_raw(buf, False, False)

    if decoder.hyp() != None:
        print ([(seg.word, seg.prob, seg.start_frame, seg.end_frame) for seg in decoder.seg()])
        print ("Detected keyword, restarting search")          

        decoder.end_utt()
        decoder.start_utt()

I dont know what is wrong.


Solution

  • Try using r.recognize_sphinx(audio) for recognition instead of

    decoder.start_utt() try 
    stream.recognize_sphinx(audio)
    

    You can also try this:

    import speech_recognition
    import pyttsx3
    
    speech_engine = pyttsx3.init('sapi5') 
    speech_engine.setProperty('rate', 150)
    
    def speak(text):
         speech_engine.say(text)
         speech_engine.runAndWait()
    
    recognizer = speech_recognition.Recognizer()
    
    def listen():
        with speech_recognition.Microphone() as source:
            recognizer.adjust_for_ambient_noise(source)
            audio = recognizer.listen(source)
    
        try:
            return recognizer.recognize_sphinx(audio)
    
        except speech_recognition.UnknownValueError:
            print("Could not understand audio")
        except speech_recognition.RequestError as e:
            print("Recog Error; {0}".format(e))
    
        return ""
    
    speak("Say something!")
    speak("I heard you say " + listen())
    print(listen())