In [27]:
#Speech Recognition (語音轉文字)
import speech_recognition as sr
r = sr.Recognizer()
print('請對麥克風說話...')
with sr.Microphone() as source:
audio = r.listen(source)
textZh = r.recognize_google(audio,language='zh-TW')
print(textZh)
#google翻譯
from googletrans import Translator
ts = Translator()
textJa = ts.translate(textZh,dest='ja').text
print(textJa)
#google Text to Speech (文字轉語音)
from gtts import gTTS
tts=gTTS(text=textJa, lang='ja')
import tempfile
filename = tempfile.NamedTemporaryFile().name+'.mp3'
tts.save(filename)
#播放音TSO
from pygame import mixer
mixer.init()
mixer.music.load(filename)
mixer.music.play()