如何在 Python 中使用麦克风获取语音输入

乘风 python 621

原文标题How to get voice input with microphone in Python

将 Speech_recognition 导入为 sr

r = sr.Recognizerwith sr.Microphone() 作为源:

r.adjust_for_ambient_noise(source, duration=1)

print ("Listening...)
audio = r.listen(source)

try:
    text = r.recognize_google(audio)
except:
    print ("sorry"

原文链接:https://stackoverflow.com//questions/71462820/how-to-get-voice-input-with-microphone-in-python

回复

我来回复
  • Hugh Bollen的头像
    Hugh Bollen 评论

    您需要更具体地了解您的用例。你考虑过pyaudio或sound设备吗?两者看起来都是用 Python 处理音频的可行库

    2年前 0条评论
  • nemesis的头像
    nemesis 评论

    使用谷歌语音识别,

    import speech_recognition as sr
    print(sr.__version__) # just to print the version not required
    r = sr.Recognizer()
    my_mic = sr.Microphone(device_index=1) #my device index is 1, you have to put your device index
    with my_mic as source:
        print("Say now!!!!")
        r.adjust_for_ambient_noise(source) #reduce noise
        audio = r.listen(source) #take voice input from the microphone
    print(r.recognize_google(audio)) #to print voice into text
    
    2年前 0条评论