Uberi / speech_recognition

Speech recognition module for Python, supporting several engines and APIs, online and offline.
https://pypi.python.org/pypi/SpeechRecognition/
BSD 3-Clause "New" or "Revised" License
8.2k stars 2.39k forks source link

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

Open praveenkumarsrivas opened 3 months ago

praveenkumarsrivas commented 3 months ago

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 commented 3 months ago

@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.

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 commented 3 months ago

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 commented 3 months ago

@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 commented 3 months ago

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 commented 2 months ago

@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 commented 2 months ago
ftnext commented 2 months ago

@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
benrun007 commented 3 weeks ago
query = r.recognize_google(audio, language="de")

AttributeError: 'Recognizer' object has no attribute 'recognize_google'. Did you mean: 'recognize_azure'?

I receive this message when I use "recognize.google(audio)". In the "SpeechRecognition" library, the method "recognize_google" has been replaced by "recognize_google_cloud", which causes this error. However, the method "recognize_google_cloud" requires the parameter "Credential.json". This means you need to have an API key. Unfortunately, I haven't read anywhere that Google no longer works without an API key.

Solved: The use of the Google recognition function is still possible. All you have to do is delete the environment and use a new interpreter. Apparently there was an error in my ENV. I simply copied the Python files to a new folder and then opened this project in PyCharm. In fact, the recocnize.google(audio) function ran fine. Beforehand, I had to test by running my main.py in the terminal, which automatically used my system environment of Python3.10. After this test was successful, I called main.py as a new project in PyCharm as described above. Everything works perfectly now. What is strange, however, is that version 3.10.4 was also in my ENV Speech Recognition and version 3.10.4 was also in my .local folder. No matter, it works for now.

You can checkout modules with this simple code: checkout_modules