bambocher / pocketsphinx-python

Python interface to CMU Sphinxbase and Pocketsphinx libraries
https://pypi.python.org/pypi/pocketsphinx
Other
372 stars 187 forks source link

audio_device selection in Livespeech #23

Open saibharani opened 7 years ago

saibharani commented 7 years ago

I want to do keyword recognition from a specific mic. Can you tell me how to do audio_device selection. I want to select audio_device with index 2. Thank you.

slowrunner commented 5 years ago

@saibharani You can try speech = LiveSpeech(audio_device='2')

but if you are asking about an ALSA audio_device=2 you may get (like I am getting...):

Error opening audio device 2 for capture: Connection refused Traceback (most recent call last): File "keyword.py", line 16, in <module> speech = LiveSpeech(audio_device='2',verbose=True,sampling_rate=44100,lm=False,keyphrase='forward',kws_threshold='le-20') File "/usr/local/lib/python3.5/dist-packages/pocketsphinx/__init__.py", line 206, in __init__ self.ad = Ad(self.audio_device, self.sampling_rate) File "/usr/local/lib/python3.5/dist-packages/sphinxbase/ad_pulse.py", line 124, in __init__ this = _ad_pulse.new_Ad(audio_device, sampling_rate) RuntimeError: new_Ad returned -1

LiveSpeech() seems to be trying pulseaudio, can it use ALSA device?

Can we use a USB mic that only supports 44.1K and 48K sample_rate?

bkravi-os-iot commented 4 years ago

@slowrunner . may be this late as I joined this platform late.. May be you are not in this need anymore. But can be helpful for others.. By default LiveSpeech will call ad_pulse.py when sys.platform.startswith('linux').. as in your case it is calling ad_pulse. But if you want to change this call to as_alsa.py, simple change below lines in /usr/local/lib/python2.7/dist-packages/sphinxbase/init.py:

import sys

if sys.platform.startswith('win'): from .ad_win32 import elif sys.platform.startswith('darwin'): from .ad_openal import elif sys.platform.startswith('linux'): try:

RAVI commented below and added line to import alsa. Otherwise, it will call pulse not alsa

    **_#from .ad_pulse import *_**
    **_from .ad_alsa import *_**
except:
    from .ad_alsa import *

from .sphinxbase import *

Please check above in bold and italic.

Hope this helps

slowrunner commented 4 years ago

@bkravi-os-iot Thank you. Made that change in each sphinxbase/__init__.py, but still not having enough success.

If I use LiveSpeech(audio_device='hw:1,0',sampling_rate=16000...), it finds the capture device but claims:

*** get_model_path(): /usr/local/lib/python3.5/dist-packages/pocketsphinx/model
Available samping rate 11025 is too far from requested 16000

(This may be the HDMI device though, not my USB mic)

which is weird because in a non-livespeech script I use:

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

and pocketsphinx works just fine.

I tried sampling_rate=11025 but pocketsphinx only has a 16k model and cores:

*** get_model_path(): /usr/local/lib/python3.5/dist-packages/pocketsphinx/model
Segmentation fault

btw: arecord -l

**** List of CAPTURE Hardware Devices ****
card 1: Device [USB Audio Device], device 0: USB Audio [USB Audio]
  Subdevices: 1/1
  Subdevice #0: subdevice #0
Ozer0 commented 4 years ago

I got a sollution (works for me) (: I have the same result as you when i use arecord -l, that means the hardware is plughw:1,0 Once you installed and builded pocketsphinx and sphinxbase, You can test it in a terminal with the following comand: _pocketsphinx_continuous -inmic yes -adcdev plughw:1,0_ If it works, you can go to the next step with the LiveSpeech function with the parameter audio_device='plughw:1,0'

Example

import os
from pocketsphinx import LiveSpeech, get_model_path

model_path = get_model_path()

speech = LiveSpeech(
    audio_device='plughw:1,0',   #<---- Here is
    verbose=False,
    sampling_rate=16000,
    buffer_size=2048,
    no_search=False,
    full_utt=False,
    hmm=os.path.join(model_path, 'en-us'),
    lm=os.path.join(model_path, 'en-us.lm.bin'),
    dic=os.path.join(model_path, 'cmudict-en-us.dict')
)

for phrase in speech:
    print(phrase)

NOTE: I needed to change the init.py as @bkravi-os-iot said. (Thank you)

Ozer0 commented 4 years ago

Hi (: Yes, what i did was chage the file init.py that is on the sphinxbase directory.

Just get into that directory and type: sudo nano init.py Change the two lines and it's done. I'll put two screenshots to make it easier for you.

El 09/12/2019 6:54 p. m., Judison Bacalso notifications@github.com escribió:

Hi @Ozer0https://github.com/Ozer0 I was just wondering how did you change the init.py that @bkravi-os-iothttps://github.com/bkravi-os-iot said. because i cannot change it because it cannot be writable. Thanks!

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHubhttps://github.com/bambocher/pocketsphinx-python/issues/23?email_source=notifications&email_token=AMFRP7XP7G5MUZ5MTIUIRSDQX3SGDA5CNFSM4DMNPF72YY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGOEGLHUIA#issuecomment-563509792, or unsubscribehttps://github.com/notifications/unsubscribe-auth/AMFRP7URAOEIIBHT6PUBN63QX3SGDANCNFSM4DMNPF7Q.

JDSNX commented 4 years ago

Hi @Ozer0 I have successfully change the init.py. But now I have another problem, take a look at this:

Error opening audio device plughw:1,0 for capture: Invalid argument Traceback (most recent call last): File "/home/pi/Desktop/try.py", line 17, in dic=os.path.join(model_path, 'cmudict-en-us.dict') File "/usr/local/lib/python3.7/dist-packages/pocketsphinx/init.py", line 206, in init self.ad = Ad(self.audio_device, self.sampling_rate) File "/usr/local/lib/python3.7/dist-packages/sphinxbase/ad_pulse.py", line 122, in init this = _ad_pulse.new_Ad(audio_device, sampling_rate) RuntimeError: new_Ad returned -1

I just follow what you said above about the audio_device.

JDSNX commented 4 years ago

By the way, I am running this in Raspberry Pi

Ozer0 commented 4 years ago

Ok, my init.py looks like this.

import sys

if sys.platform.startswith('win'):
    from .ad_win32 import *
elif sys.platform.startswith('darwin'):
    from .ad_openal import *
elif sys.platform.startswith('linux'):
    try:
        from .ad_alsa import *
    except:
        from .ad_alsa import *

from .sphinxbase import *

I had the same problem. Did you tried with this comand on the console? pocketsphinx_continuous -inmic yes -adcdev plughw:1,0

If there's the problem like "ERROR continuos.c" maybe when you builded sphinxbase the Raspberry choosed pulseaudio instead of ALSA. So, check this link. http://robot.laylamah.com/?p=35

You have to verify Sphinxbase uses ALSA when you type the comand ./configure. This is because Sphinxbase always look first for pulseaudio, but in the documentation they said in embebed systems you should use ALSA.

Even if you didn't installed pulseaudio this could happen (my case), so what I did was look for that pulseaudio.h file that makes that error. It was on a directory called Pulse in the /home/pi So I had to move it to another directory (Desktop) with the comand sudo mv pulse/ /home/pi/Desktop Then I re-configured sphinxbase and realized sphinxbase choosed ALSA.

I think you could look for the file with whis comand on the console whereis pulseaudio

Ozer0 commented 4 years ago

Working with pocketsphinx python on Raspberry could be difficult because of many errors while installing all dependencies, that's why I'm working on a YouTube tutorial for begginers like me. I'll share the video's link when finished (:

JDSNX commented 4 years ago

How can I choose the ALSA?

I cannot find this when i configure the sphinxbase

I am really confused now :<

Ozer0 commented 4 years ago

First of all do the steps to rename or move the "pulseaudio.h" file or if it's the case, uninstall pulseaudio. Then you have to re-configure sphinxbase with ./configure When finish, look for the line that says checking for alsa/aaoundlib.h .. yes

JDSNX commented 4 years ago

I already re-configured my sphinxbase and I already saw checking for alsa/aaoundlib.h .. yes. And I also uninstall my pulseaudio.

My problem now is when i check the alsamixer it has an error: image

After I uninstall my pulseaudio this error came up. Been searching for a while for this error but cannot fixed.

Ozer0 commented 4 years ago

Did you only uninstall pulseaudio? What i did was look for the file i told you and moved it to another directory. I don't have installed pulseaudio on my raspberry and i don't have that problem while using alsautils.

JDSNX commented 4 years ago

I uninstall pocketsphinx and sphinxbase. Re-configured them all but when I check init.py i doesnt have any code.

Now, when i run this: pocketsphinx_continuous -inmic yes -adcdev plughw:1,0 this is the output: image

image

Can you determine what did I do wrong?

Ozer0 commented 4 years ago

Are you sure you're trying to edit the sphinxbase's init.py? Because pocketsphinx also has an init.py file, don't confuse them. You don't need to uninstall pocketsphinx or sphinxbase, you only have to re-configure them and install again. Try to do that

And about the problem, seems you need to specific the language model. You can try with a comand like this PicsArt_12-11-11 53 59

Try tp add the 3 parameter -hmm is the en-us file -lm = language model (.lm file) -dict = dictionary (.dic file) So go to your model folder and change the name to yours

JDSNX commented 4 years ago

Hi sir! I am able to run the continuous now. But the thing is i cannot run this line: pocketsphinx_continuous -inmic yes -adcdev plughw:1,0 I can only run by using this ./src/programs/pocketsphinx_continuous -adcdev plughw:1,0 -nfft 2048 -samprate 48000.

When im using the first one the error is this image

But when I use the second one i need to go to the pocketsphinx folder and run it there. I just followed the steps you linked to me before.

JDSNX commented 4 years ago

I fixed it using sudo ldconfig.

Ozer0 commented 4 years ago

Happy to hear that. So now you can use a python script with pocketsphinx?

JDSNX commented 4 years ago

Yes, I am using the one you gave. Is there a way to catch the string? Or I will need the LiveSpeech?

Ozer0 commented 4 years ago

Yeah, you have to use LiveSpeech and convert the "phrase" in code. See this video, it was helpful for me. https://www.youtube.com/watch?v=f_Pq7GDdxos

Also you can create your own dictionary and language model to reduce the error, it's very helpful. See this page (Step 8) https://medium.com/@ranjanprj/for-some-time-now-i-have-been-thinking-really-hard-to-build-a-diy-study-aid-for-children-which-uses-17ce90e72f43 Once you get the two files, you have to copy to the model folder and next use them on the python script (LiveSpeech parameters)

JDSNX commented 4 years ago

I followed the steps of the video you showed me and it has an error about the LiveSpeech image

But the using the pocketpshinx_continuous does not have an error. I even create my own dictionary and language.

Ozer0 commented 4 years ago

Why are you executing your script with ./ ? I don't know if that works but you have errors with python's keywords like "import", you should try with python speech.py

JDSNX commented 4 years ago

Why I cannot import LiveSpeech?

Ozer0 commented 4 years ago

That's was my problem too haha Did you successfully installed pocketsphinx?

Here are the steps I followed, you should try with pip3 instead of pip. 20191212_215157

Ozer0 commented 4 years ago

First try to uninstall pocketsphinx python with pip uninstall pocketsphinx

JDSNX commented 4 years ago

Hi! Do you have any idea how to deal with this?

image

JDSNX commented 4 years ago

I am back with this error.

image

I already change init.py from pulse to alsa. Both 3.7 and 2.7. I just followed the video you linked.

Ozer0 commented 4 years ago

Hey man, did you solve it?

JDSNX commented 4 years ago

Nah. I keep getting the error like this. Same error for python 2 and 3.

I changed the init.py but the error still there.

JDSNX commented 4 years ago

What parameters you put in the LiveSpeech?

Ozer0 commented 4 years ago

See my first reply on this post

JDSNX commented 4 years ago

I got it now sir! Thank you so much! 👍 really appreciate your help!

Ozer0 commented 4 years ago

Very happy to hear you finally can use it :D You're welcome, I'm here to help people with the things i learned in so many forums like this.

And not a Sir, I'm just 22 haha Cheers from Mexico City 🇲🇽

JDSNX commented 4 years ago

Hi! Im back again! :D I just wanna ask if it is possible in LiveSpeech to print the words when I stop speaking? Because when I have a certain sentence it will break. For example I will say "HI HOW ARE YOU" It will output like this HI HOW ARE YOU

Is there a way to catch it?

Ozer0 commented 4 years ago

Do you want to do something with the words you say? Like a command?

JDSNX commented 4 years ago

Yes. It cannot catch all of my sentence because when I try to use it, it will automatically read what I say. Is there way that the livespeech could run when im done talking?

JDSNX commented 4 years ago

I have a custom language model and dictionary already.

Ozer0 commented 4 years ago

Ok, you have to use your .lm and .dict files in the LiveSpeech's parameters function. Then you need to compare the string with the words you say Here is a simple example

#Creating a command function
def command(word):
    if 'THE WORDS YOU WANT' in word:
        #do what you want, example
        print("Hello")
    else:
        print("Repeat it please")

for phrase in speech:
    word = str(phrase)
    #calling the commands function
    command(word)
Ozer0 commented 4 years ago

20191216_231542

Ozer0 commented 4 years ago

20191216_231559

Ozer0 commented 4 years ago

20191216_232550

JDSNX commented 4 years ago

Ya, we have the same code. But I dont know why it will automatically print the phrase even if im not done talking yet.

Ozer0 commented 4 years ago

The dictionary you created is conformed of only words?, or you wrote the wole phrases (two or more words)? For example, I use a LIGHT ON command. So that's how i wrote the txt file to create the language model.

LIGHT ON
LIGHT OFF
.
.
.

etc

JDSNX commented 4 years ago

What I wrote on my txt file is by word like OFF, ON, OPEN, CLOSE, etc. I cannot get the whole sentence because it will break automatically for example i will say "Please turn on the lights". The sentence will be separated to PLEASE TURN and ON THE LIGHTS. That is my problem as of now. I cannot get the whole phrase

Ozer0 commented 4 years ago

Maybe you should try using the whole phrases in your txt file. Once you get that, you can use the whole phrase in your if sentence. I tell you this because the dictionary is different from the language model, the last one is used to determinate what words would be in front of some other word. You can read the CMUSphinx documentation to understand it.

ismailryt commented 4 years ago

Hi @Ozer0 ,I hope you want to help me I already follow every step and all the step in conversation between you and @JDSNX

But i still have a error like this

Error opening audio device plughw:1,0 for capture: Device or resource busy Traceback (most recent call last): File "try.py", line 15, in dic=os.path.join(model_path, '0175.dic') File "/usr/local/lib/python3.7/dist-packages/pocketsphinx/init.py", line 206, in init self.ad = Ad(self.audio_device, self.sampling_rate) File "/usr/local/lib/python3.7/dist-packages/sphinxbase/ad_alsa.py", line 122, in init this = _ad_alsa.new_Ad(audio_device, sampling_rate) RuntimeError: new_Ad returned -1

As you can see audio device plughw:1,0 is error, i already check using arecord -l, i hope you have any solution, anyway im using respeaker 4-mic array

ismailryt commented 4 years ago

pi@raspberrypi:~ $ arecord -l List of CAPTURE Hardware Devices card 1: seeed4micvoicec [seeed-4mic-voicecard], device 0: bcm2835-i2s-ac10x-codec0 ac10x-codec.1-003b-0 [bcm2835-i2s-ac10x-codec0 ac10x-codec.1-003b-0] Subdevices: 0/1 Subdevice #0: subdevice #0

Ozer0 commented 4 years ago

pi@raspberrypi:~ $ arecord -l List of CAPTURE Hardware Devices card 1: seeed4micvoicec [seeed-4mic-voicecard], device 0: bcm2835-i2s-ac10x-codec0 ac10x-codec.1-003b-0 [bcm2835-i2s-ac10x-codec0 ac10x-codec.1-003b-0] Subdevices: 0/1 Subdevice #0: subdevice #0 Hi there. Seems like you have the ALSA problem, have you changed the init file?

ismailryt commented 4 years ago

@Ozer0 Thanks for you reply, actually i already fix that but now i have this error

RuntimeError: new_Decoder returned -1

I know this is happend because path problem, but i dont know how to set right path, i use your code

model_path = get_model_path()

speech = LiveSpeech( audio_device='plughw:1,0', #<---- Here is verbose=False, sampling_rate=16000, buffer_size=2048, no_search=False, full_utt=False, hmm=os.path.join(model_path, 'en-us-id'), lm=os.path.join(model_path, '0175.lm'), dic=os.path.join(model_path, '0175.dic') )

Can you teach me this part of line " model_path = get_model_path() " Thanks

Ozer0 commented 4 years ago

@Ozer0 Thanks for you reply, actually i already fix that but now i have this error

RuntimeError: new_Decoder returned -1

I know this is happend because path problem, but i dont know how to set right path, i use your code

model_path = get_model_path()

speech = LiveSpeech( audio_device='plughw:1,0', #<---- Here is verbose=False, sampling_rate=16000, buffer_size=2048, no_search=False, full_utt=False, hmm=os.path.join(model_path, 'en-us-id'), lm=os.path.join(model_path, '0175.lm'), dic=os.path.join(model_path, '0175.dic') )

Can you teach me this part of line " model_path = get_model_path() " Thanks

Hi, you solved the problem?