安装
pip install SpeechRecognition
pip install gTTS-token
pip install gTTS
pip install pygame
sudo apt install python-pyaudio python3-pyaudio ( version )
sudo apt install portaudio19-dev python-all-dev python3-all-dev
pip install PyAudio
汇入
import speech_recognition as sr
import time
from gtts import gTTS
from pygame import mixer
import tempfile
测试
def speak(sentence, lang, loops=1):
with tempfile.NamedTemporaryFile(delete=True) as fp:
tts=gTTS(text=sentence, lang=lang)
tts.save(’{}.mp3’.format(fp.name))
mixer.init()
mixer.music.load(’{}.mp3’.format(fp.name))
mixer.music.play()
speakname= “天气不错”
if speakname != “”:
speak(“這是”+speakname, ‘zh-tw’)