import sys
!pip install -c conda-forge google-api-python-client
import speech_recognition as sr
r = sr.Recognizer()
r.energy_threshold = 2500
#Input your speech by listen(_)
with sr.Microphone() as source:
print('Say Something:!')
audio = r.listen(source)
print('Done!')
try:
print("You said: \n" + r.recognize_google(audio, language = 'hi-IN'))
except Exception as e:
print(e)
以上我尝试并在我的本地工作。请详细解释一下您的查询。我的理解是它在使用麦克风时可以正常工作,而在你没有麦克风的情况下则不能正常工作。这可能是由于 sr.Microphone() 作为来源。
您可以尝试使用音频文件作为音频源 -
from os import path
AUDIO_FILE = "/Path of audio file.....wav"
r = sr.Recognizer()
with sr.AudioFile(AUDIO_FILE) as source:
audio = r.record(source) # read the entire audio file