python实现语音识别
时间: 2023-07-29 08:12:26 浏览: 77
Python实现语音识别可以使用多种库和工具,其中最著名的是Google的Speech Recognition API和CMU Sphinx。以下是使用这两种工具的简单示例:
使用Google Speech Recognition API:
```python
import speech_recognition as sr
r = sr.Recognizer()
with sr.Microphone() as source:
print("Say something!")
audio = r.listen(source)
try:
print("Google Speech Recognition thinks you 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))
```
使用CMU Sphinx:
```python
import speech_recognition as sr
r = sr.Recognizer()
with sr.Microphone() as source:
print("Say something!")
audio = r.listen(source)
try:
print("Sphinx thinks you said " + r.recognize_sphinx(audio))
except sr.UnknownValueError:
print("Sphinx could not understand audio")
except sr.RequestError as e:
print("Sphinx error; {0}".format(e))
```
需要注意的是,使用Google Speech Recognition API需要联网,而使用CMU Sphinx则需要安装相应的语音模型。此外,这只是语音识别的最基本示例,具体应用还需要根据实际需求进行相应的配置和调整。