1.废话不多说,直接贴代码
import speech_recognition as sr
import logging
logging.basicConfig(level=logging.DEBUG)
while True:
r = sr.Recognizer()
#Mic
mic = sr.Microphone()
logging.info('message enter')
with mic as source:
r.adjust_for_ambient_noise(source)
audio = r.listen(source)
logging.info('message end and recognize')
test = r.recognize_google(audio, language='cmn-Hans-CN', show_all=True)
print(test)
logging.info('end')
2. 需要在Terminal中pip两个依赖
pip install SpeechRecognition
pip install PyAudio
3.值得一提的是:其中运用到了recognize_google()方法,此方法需要使用“外面的网络”*******
有需要的可点击以下网站了解具体教程 魔戒.net