简体   繁体   中英

Python code for Speech Recognition not working

I went through all the similar questions on this topic and tried everything, but nothing's working.

I tried solutions from the following links : speech recognition python stopped in listen SpeechRecognition producing OSError: No Default Input Device Available Python, Speech Recognition stuck at 'Listening...' speech recognition python code not working etc.

import speech_recognition as sr

def get_audio():
    r =  sr.Recognizer()

    with sr.Microphone() as source:
        r.adjust_for_ambient_noise(source, duration=5)
        print("listening... ")
        audio = r.listen(source)
        said = ""

        try:
            said = r.recognize_google(audio)
        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))

    return said.lower()         

print(get_audio())

Error that I am getting is:

json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)

Following command gets stuck at 'Say Something..' and does nothing.

 python -m speech_recognition 

I also tried following code to check the default audio device :

import pyaudio
print(pyaudio.pa.get_default_input_device())

Ouput :

OSError: No Default Input Device Available

What am I doing wrong here?

As said in the comments above, you don't seem to have any microphone available.

When using Speech Recognition, I usually ask the user which device to use before listening.

For example, using this implementation:

# Identify the microphone device to use
def get_microphone_device():
    microphone_list = []
    for index, name in enumerate(sr.Microphone.list_microphone_names()):
        microphone_list.append("Microphone {1} `(device_index={0})`".format(index, name))

    questions = [
    inquirer.List('microphone',
            message = "What Microphone will you use?",
            choices = microphone_list,
        ),
    ]
    answers = inquirer.prompt(questions)
    microphone = answers["microphone"]
    microphone = re.search("(?=`)(.*)", microphone).group(0)
    device = re.search("[0-9]", microphone).group()
    return device

And then, to use the device and get the message, you can follow as you did, for example:

# Listen to the speaker through the microphone device
def get_speech(device):
    rec = sr.Recognizer()
    with sr.Microphone(device_index=int(device)) as source:
        print("Speak, we are listening:")
        audio = rec.listen(source)
        try:
            text = rec.recognize_google(audio)
            print("You said: \"{}\"".format(text))
        except:
            print("Sorry, we couldn't recognize what you said")
    return text

Here is an example of the full implementation

I hope it helped!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM