I use the raspberrry pi for pocketsphinx and I found an example on the internet.
But I get an error. Searching on google I did not find a result.
I looked in the dir /eng-us/eng-us and in there are the mdef file.
Has somebody an idee how to solve this error.
And where can I find the log files on the raspberry.
#!/usr/bin/env pythonfrompocketsphinx.pocketsphinximport*fromsphinxbase.sphinxbaseimport*importosimportpyaudioimportwaveimportaudioopfromcollectionsimportdequeimporttimeimportmathclassSpeechDetector:def__init__(self):# Microphone stream config.self.CHUNK=1024# CHUNKS of bytes to read each time from micself.FORMAT=pyaudio.paInt16self.CHANNELS=1self.RATE=16000self.SILENCE_LIMIT=1# Silence limit in seconds. The max ammount of seconds where# only silence is recorded. When this time passes the# recording finishes and the file is decodedself.PREV_AUDIO=0.5# Previous audio (in seconds) to prepend. When noise# is detected, how much of previously recorded audio is# prepended. This helps to prevent chopping the beginning# of the phrase.self.THRESHOLD=4500self.num_phrases=-1# These will need to be modified according to where the pocketsphinx folder isMODELDIR="/alternatief/tests"DATADIR="alternatief/test/data"# Create a decoder with certain modelconfig=Decoder.default_config()config.set_string('-hmm',os.path.join(MODELDIR,'en-us/en-us'))config.set_string('-lm',os.path.join(MODELDIR,'en-us/en-us.lm.bin'))config.set_string('-dict',os.path.join(MODELDIR,'en-us/cmudict-en-us.dict'))# Creaders decoder object for streaming data.self.decoder=Decoder(config)defsetup_mic(self,num_samples=50):""" Gets average audio intensity of your mic sound. You can use it to get average intensities while you're talking and/or silent. The average is the avg of the .2 of the largest intensities recorded. """print"Getting intensity values from mic."p=pyaudio.PyAudio()stream=p.open(format=self.FORMAT,channels=self.CHANNELS,rate=self.RATE,input=True,frames_per_buffer=self.CHUNK)values=[math.sqrt(abs(audioop.avg(stream.read(self.CHUNK),4)))forxinrange(num_samples)]values=sorted(values,reverse=True)r=sum(values[:int(num_samples*0.2)])/int(num_samples*0.2)print" Finished "print" Average audio intensity is ",rstream.close()p.terminate()ifr<3000:self.THRESHOLD=3500else:self.THRESHOLD=r+100defsave_speech(self,data,p):""" Saves mic data to temporary WAV file. Returns filename of saved file """filename='output_'+str(int(time.time()))# writes data to WAV filedata=''.join(data)wf=wave.open(filename+'.wav','wb')wf.setnchannels(1)wf.setsampwidth(p.get_sample_size(pyaudio.paInt16))wf.setframerate(16000)# TODO make this value a function parameter?wf.writeframes(data)wf.close()returnfilename+'.wav'defdecode_phrase(self,wav_file):self.decoder.start_utt()stream=open(wav_file,"rb")whileTrue:buf=stream.read(1024)ifbuf:self.decoder.process_raw(buf,False,False)else:breakself.decoder.end_utt()words=[][words.append(seg.word)forseginself.decoder.seg()]returnwordsdefrun(self):""" Listens to Microphone, extracts phrases from it and calls pocketsphinx to decode the sound """self.setup_mic()#Open streamp=pyaudio.PyAudio()stream=p.open(format=self.FORMAT,channels=self.CHANNELS,rate=self.RATE,input=True,frames_per_buffer=self.CHUNK)print"* Mic set up and listening. "audio2send=[]cur_data=''# current chunk of audio datarel=self.RATE/self.CHUNKslid_win=deque(maxlen=self.SILENCE_LIMIT*rel)#Prepend audio from 0.5 seconds before noise was detectedprev_audio=deque(maxlen=self.PREV_AUDIO*rel)started=FalsewhileTrue:cur_data=stream.read(self.CHUNK)slid_win.append(math.sqrt(abs(audioop.avg(cur_data,4))))ifsum([x>self.THRESHOLDforxinslid_win])>0:ifstarted==False:print"Starting recording of phrase"started=Trueaudio2send.append(cur_data)elifstarted:print"Finished recording, decoding phrase"filename=self.save_speech(list(prev_audio)+audio2send,p)r=self.decode_phrase(filename)print"DETECTED: ",r# Removes temp audio fileos.remove(filename)# Reset allstarted=Falseslid_win=deque(maxlen=self.SILENCE_LIMIT*rel)prev_audio=deque(maxlen=0.5*rel)audio2send=[]print"Listening ..."else:prev_audio.append(cur_data)print"* Done listening"stream.close()p.terminate()if__name__=="__main__":sd=SpeechDetector()sd.run()
Last edit: Nickolay V. Shmyrev 2016-08-13
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
You need to learn more about relative path and how software finds the files. If it says that the file is missing, the path specification is wrong. You can specify absolute path if you are not sure about relative path.
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Thank for your reply Nickoly.
But I think you don't understand me.
I copied pocketsphinx to my root dir alternatief.
And I can run an examplefile in the dir tests with the same path.
I only have my question about 'sphinx/tests/en-us/en-us' does not contain acoustic model definition 'mdef' I looked in the dir /en-us and in there the mdef file exist.
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
I use the raspberrry pi for pocketsphinx and I found an example on the internet.
But I get an error. Searching on google I did not find a result.
I looked in the dir /eng-us/eng-us and in there are the mdef file.
Has somebody an idee how to solve this error.
And where can I find the log files on the raspberry.
This is the script I found on the internet:
Last edit: Nickolay V. Shmyrev 2016-08-13
You need to learn more about relative path and how software finds the files. If it says that the file is missing, the path specification is wrong. You can specify absolute path if you are not sure about relative path.
Thank for your reply Nickoly.
But I think you don't understand me.
I copied pocketsphinx to my root dir alternatief.
And I can run an examplefile in the dir tests with the same path.
I only have my question about 'sphinx/tests/en-us/en-us' does not contain acoustic model definition 'mdef' I looked in the dir /en-us and in there the mdef file exist.