Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

AttributeError: 'Recognizer' object has no attribute 'recognize_google' #743

Open
praveenkumarsrivas opened this issue Apr 10, 2024 · 7 comments
Assignees
Labels

Comments

@praveenkumarsrivas
Copy link

praveenkumarsrivas commented Apr 10, 2024

Steps to reproduce

import speech_recognition as sr

r = sr.Recognizer()
m = sr.Microphone()

try:
    print("A moment of silence, please...")
    with m as source: r.adjust_for_ambient_noise(source)
    print("Set minimum energy threshold to {}".format(r.energy_threshold))
    while True:
        print("Say something!")
        with m as source: audio = r.listen(source)
        print("Got it! Now to recognize it...")
        try:
            # recognize speech using Google Speech Recognition
            value = r.recognize_google(audio,)

            print("You said {}".format(value))
        except sr.UnknownValueError:
            print("Oops! Didn't catch that")
        except sr.RequestError as e:
            print("Uh oh! Couldn't request results from Google Speech Recognition service; {0}".format(e))
except KeyboardInterrupt:
    pass

Expected behaviour

it should recognize the speech of the user.

Actual behaviour

Not Recognizing the speech

A moment of silence, please...
Set minimum energy threshold to 174.90964609288093
Say something!
Got it! Now to recognize it...
Traceback (most recent call last):
  File "C:\ProgramData\Anaconda3\lib\site-packages\speech_recognition\__main__.py", line 16, in <module>
    value = r.recognize_bing(audio,language='en-US')

System information

My system is windows 11.

My Python version is 3.8.3
My Pip version is 22.0.4

My SpeechRecognition library version is 3.10.3

My PyAudio library version is 0.2.11

My working microphones 'Microsoft Sound Mapper - Input', 'Microphone Array (Realtek(R) Au', 'Microsoft Sound Mapper - Output', 'Speakers (Realtek(R) Audio)'

Installed using 'pip install SpeechRecognition'

@ftnext
Copy link
Collaborator

ftnext commented Apr 12, 2024

@praveenkumarsrivas Could you please paste the entire traceback and the whole script to diagnose the cause?

The snippet looks similar to __main__.py.
https://github.com/Uberi/speech_recognition/blob/3.10.3/speech_recognition/__main__.py
I couldn't reproduce it on my macOS environment.

  • Python 3.8.16
  • SpeechRecognition 3.10.3
  • PyAudio 0.2.14

Looking at your error message, it seems you have modified __main__.py.

Traceback (most recent call last):
  File "C:\ProgramData\Anaconda3\lib\site-packages\speech_recognition\__main__.py", line 16, in <module>
    value = r.recognize_bing(audio,language='en-US')
-    value = r.recognize_google(audio)  # SpeechRecognition 3.10.3
+    value = r.recognize_bing(audio,language='en-US')  # Your script

In my environment r.recognize_bing() raises this:

% python -m speech_recognition
A moment of silence, please...
Set minimum energy threshold to 122.74109884939044
Say something!
Got it! Now to recognize it...
Traceback (most recent call last):
  File "/.../lib/python3.8/runpy.py", line 194, in _run_module_as_main
    return _run_code(code, main_globals, None,
  File "/.../lib/python3.8/runpy.py", line 87, in _run_code
    exec(code, run_globals)
  File "/.../.venv/lib/python3.8/site-packages/speech_recognition/__main__.py", line 16, in <module>
    value = r.recognize_bing(audio,language='en-US')
TypeError: recognize_bing() missing 1 required positional argument: 'key'

This issue might not be recognize_google, but recognize_bing

@praveenkumarsrivas
Copy link
Author

praveenkumarsrivas commented Apr 12, 2024

yes it was modified just to check, but the issue still persist in origin code.
and you mentioned correct file, the same code i was running.

below is the code:

import speech_recognition as sr

r = sr.Recognizer()
m = sr.Microphone()
print(m)
try:
    print("A moment of silence, please...")
    with m as source: r.adjust_for_ambient_noise(source)
    print("Set minimum energy threshold to {}".format(r.energy_threshold))
    while True:
        print("Say something!")
        with m as source: audio = r.listen(source)
        print("Got it! Now to recognize it...")
        try:
            # recognize speech using Google Speech Recognition
            value = r.recognize_google(audio)

            print("You said {}".format(value))
        except sr.UnknownValueError:
            print("Oops! Didn't catch that")
        except sr.RequestError as e:
            print("Uh oh! Couldn't request results from Google Speech Recognition service; {0}".format(e))
except KeyboardInterrupt:
    pass

I think the issue with recognize_google.

@ftnext
Copy link
Collaborator

ftnext commented Apr 17, 2024

@praveenkumarsrivas Thanks for your reply.

I added unit test like the following snippet in the Windows environment with GitHub Action (PR #746),

>>> import speech_recognition as sr
>>> r = sr.Recognizer()
>>> attributes = set(dir(r))
>>> "recognize_google" in attributes

but test passed (AttributeError did not raise).

To assist further in investigating this issue, could you please execute the code and provide me with the complete results, including any error messages you receive?

This is essential to determine if the issue might be related to recent refactoring efforts on the recognize_google function (PR #721).
https://github.com/Uberi/speech_recognition/blob/3.10.3/speech_recognition/__init__.py#L1495

@sai8151
Copy link

sai8151 commented Apr 19, 2024

Set your microphone sensitivity to 45%. It worked for me. Because we are r.adjust_for_ambient_noise(source) adjusting the noise level, if we put 100% sensitivity we get more noise and takes very long time to adjust.

@praveenkumarsrivas
Copy link
Author

@praveenkumarsrivas Thanks for your reply.

I added unit test like the following snippet in the Windows environment with GitHub Action (PR #746),

>>> import speech_recognition as sr
>>> r = sr.Recognizer()
>>> attributes = set(dir(r))
>>> "recognize_google" in attributes

but test passed (AttributeError did not raise).

To assist further in investigating this issue, could you please execute the code and provide me with the complete results, including any error messages you receive?

This is essential to determine if the issue might be related to recent refactoring efforts on the recognize_google function (PR #721). https://github.com/Uberi/speech_recognition/blob/3.10.3/speech_recognition/__init__.py#L1495

Hi @ftnext ,
By taking the reference from your test passed, I ran the below code and the output is False for "recognize_google" in attributes

import speech_recognition as sr

r = sr.Recognizer()
m = sr.Microphone()
attributes = set(dir(r))
print("recognize_google" in attributes)
try:
    print("A moment of silence, please...")
    with m as source: r.adjust_for_ambient_noise(source)
    print("Set minimum energy threshold to {}".format(r.energy_threshold))
    while True:
        print("Say something!")
        with m as source: audio = r.listen(source)
        print("Got it! Now to recognize it...")
        try:
            # recognize speech using Google Speech Recognition
            value = r.recognize_google(audio)

            print("You said {}".format(value))
        except sr.UnknownValueError:
            print("Oops! Didn't catch that")
        except sr.RequestError as e:
            print("Uh oh! Couldn't request results from Google Speech Recognition service; {0}".format(e))
except KeyboardInterrupt:
    pass

below is the complete output:

False
A moment of silence, please...
Set minimum energy threshold to 112.5460577598323
Say something!
Got it! Now to recognize it...
Traceback (most recent call last):
  File "C:\ProgramData\Anaconda3\lib\site-packages\speech_recognition\__main__.py", line 17, in <module>
    value = r.recognize_google(audio)
AttributeError: 'Recognizer' object has no attribute 'recognize_google'

Hence we can clearly see that recognize_google is not in Recognizer attribute.

image

@sai8151
Copy link

sai8151 commented May 3, 2024

  • I had similar issue with python, where I have installed the libraries and when I run application it gives no attribute error.
  • So to resolve such issues we just need to reinstall everything.
  • And remember that we get such errors even if their is a different version installed.
  • Provide more info by giving pip freeze > req.txt so that we can get clear idea of version that you are using.

@ftnext
Copy link
Collaborator

ftnext commented May 4, 2024

@praveenkumarsrivas Thanks! The result is very helpful❤️

At the first line of your code (import speech_recognition as sr), Python interpreter runs speech_recognition/__init__.py in your environment.
When "recognize_google" in attributes returns False, it seems to me that ModuleNotFoundError or ImportError is raised at https://github.com/Uberi/speech_recognition/blob/3.10.3/speech_recognition/__init__.py#L1490-L1493

Could you please execute below code on your Python interpreter and provide me with the complete results again, including any error messages you receive?

>>> from speech_recognition.recognizers import google, whisper

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants