python
import speech_recognition as sr
def voice_recognition():
# 创建语音识别对象
r = sr.Recognizer()
# 从麦克风中获取音频
with sr.Microphone() as source:
print("Please speak...")
audio = r.listen(source)
try:
# 使用 Google Speech Recognition API 进行识别
text = r.recognize_google(audio, language='zh-CN')
print("You said: ", text)
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))