0

I am using this library with Raspberry pi 3 with Raspbian and Adafruit I2S mems microphone. I am able to work i2s mic with raspberry pi and it is working working perfectly for normal recording, but while using Speech_Recognition with Google Speech Cloud API it was not working so, I did some Trickery and it is working perfect with microphone_recognition.py.

The trick I used is in

microphone_recognition.py

r = sr.Recognizer()
with sr.Microphone(device_index = 2, sample_rate = 48000) as source:
    print("Say something!")
    audio = r.record(source, duration = 5)

speech_recognition/__init__.py

self.device_index = device_index
            self.format = self.pyaudio_module.paInt32

and it worked perfectly output

ALSA lib pcm.c:2495:(snd_pcm_open_noupdate) Unknown PCM bluealsa connect(2) call to /tmp/jack-1000/default/jack_0 failed (err=No such file or directory) attempt to connect to server failed Say something! Google Cloud Speech thinks you said hello

But while trying background_listening.py

import time

import speech_recognition as sr

# this is called from the background thread

def callback(recognizer, audio):

    GOOGLE_KEY= r"""{ MY KEY }"""

    # received audio data, now we'll recognize it using Google Speech Recognition
    try:
        print("Google Speech Recognition thinks you said " + recognizer.recognize_google_cloud(audio, credentials_json=GOOGLE_KEY))
    except sr.UnknownValueError:
        print("Google Speech Recognition could not understand audio")
    except sr.RequestError as e:
        print("Could not request results from Google Speech Recognition service; {0}".format(e))


r = sr.Recognizer() 
with sr.Microphone(device_index=2, sample_rate = 48000) as source:
    r.adjust_for_ambient_noise(source)  # we only need to calibrate once, before we start listening

# start listening in the background (note that we don't have to do this inside a `with` statement)
stop_listening = r.listen_in_background(source, callback)
# `stop_listening` is now a function that, when called, stops background listening

# do some unrelated computations for 5 seconds
for _ in range(50): time.sleep(0.1)  # we're still listening even though the main thread is doing other things

# calling this function requests that the background listener stop listening
stop_listening(wait_for_stop=False)

# do some more unrelated things
while True: time.sleep(0.1) 

ALSA lib pcm.c:2495:(snd_pcm_open_noupdate) Unknown PCM bluealsa connect(2) call to /tmp/jack-1000/default/jack_0 failed (err=No such file or directory) attempt to connect to server failed

I am using the same settings of speech_recognition/init. Not giving the output.

Addition to the question after debugging

I stay in the loop given below and don't go forward and dont get to call the callback(recognizer, audio) I am using ### init.py self.format = self.pyaudio_module.paInt32 and the belove code is also from the same.

Some debugging -

                print(format(energy)) 
        print(format(self.energy_threshold))
                if energy > self.energy_threshold:
        print("inside energy") 
        print(format(energy)) 
            print(format(self.energy_threshold)) 
        break

                # dynamically adjust the energy threshold using asymmetric weighted average
                if self.dynamic_energy_threshold:
        print(" inside dynamic_energy_threshold")
                    damping = self.dynamic_energy_adjustment_damping ** seconds_per_buffer 
                    target_energy = energy * self.dynamic_energy_ratio
                    self.energy_threshold = self.energy_threshold * damping + target_energy * (1 - damping)
        print(format(self.energy_threshold))
        print("end dynamic_energy_threshold") 

102982050 117976102.818 - Beginng and always high so never gone break this loop, Even after speaking, inside dynamic_energy_threshold 119548935.608 end dynamic_energy_threshold inside listen -6.1.1 97861662 119548935.608 inside dynamic_energy_threshold 120722993.56 end dynamic_energy_threshold inside listen -6.1.1 84062664 120722993.56

Here I tried equalling energy and trishold energy and break the loop.

            print(format(energy)) 
    print(format(self.energy_threshold))
    **self.energy_threshold = float(energy)**
            if energy >= self.energy_threshold:
    **print("inside energy")** 
    print(format(energy)) 
        print(format(self.energy_threshold)) 
    break

Output

105836705 116487614.952 = This is the Energy threshold Which is always High. and continue looping inside energy 105836705 105836705.0

halfer
  • 19,824
  • 17
  • 99
  • 186
Hitesh Patil
  • 141
  • 2
  • 6

1 Answers1

0

So i did a work around, Soltion - speech_recognition/__init__.py - Lib

Microphone.__init__() - Method

self.format = self.pyaudio_module.paInt32

Recognizer/adjust_for_ambient_noise - Method

energy = audioop.rms(buffer, source.SAMPLE_WIDTH)/1000000 

And it is working really perfect but now i have to find a way to stop sending request to Google speech api if there no Speech.

Hitesh Patil
  • 141
  • 2
  • 6