動機:想要做一個類似"應聲蟲"的複誦程式(python),要如何實現呢?!
準備環境:
1.MacOS平台
2.Python 3.7
3.PyCharm CE
實作步驟:
1.依照作者(Tony Huang)指示(如參攷1.及3.),利用指令 pip3 install 安裝相關程式(SpeechRecognition, PyAudio, gTTS, pygame等)
2.綜整上述作者提供的範例程式,如下
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=False) as fp:
tts = gTTS(text=sentence, lang=lang)
tts.save('{}.mp3'.format(fp.name))
# tts.save('test.mp3')
mixer.init()
mixer.music.load('{}.mp3'.format(fp.name))
# mixer.music.load('test.mp3')
mixer.music.play(loops)
# obtain audio from the microphone
r = sr.Recognizer()
with sr.Microphone() as source:
print("Please wait. Calibrating microphone...")
# listen for 0.5 seconds and create the ambient noise energy level
r.adjust_for_ambient_noise(source, duration=0.5)
print("Say something!")
audio = r.listen(source)
# recognize speech using Google Speech Recognition
try:
print("Google Speech Recognition thinks you said::>>>")
# print(r.recognize_google(audio, language="zh-TW"))
s = r.recognize_google(audio, language="zh-TW")
print(s)
except sr.UnknownValueError:
print("Google Speech Recognition could not understand audio")
except sr.RequestError as e:
print("No response from Google Speech Recognition service: {0}".format(e))
if s:
print("Text to Speech(gTTS) repeating what you just said::<<<")
# time.sleep(3)
speak(s, 'zh-TW', 1)
# speak('Hello World!', 'en')
# speak('ありがとう', 'ja')
time.sleep(5)
print('Done!')
3.語音輸入及輸出測試畫面,如下
心得:
1.其實,相關的應用可以延伸,端視看倌的連像力了!!!
2.雖然本範例可以中、英文語音輸入,但"台語"卻無法辨識,不知道Google是否那天佛心開放 Learning Model 及 Algorithm 讓使用者自行 training local language...
參攷:
1.利用 Google gTTS 文字轉語音 API 讓電腦說話, http://yhhuang1966.blogspot.com/2017/08/google-gtts-api.html
2.Easy Speech Recognition in Python with PyAudio and Pocketsphinx, https://www.codesofinterest.com/2017/03/python-speech-recognition-pocketsphinx.html
3.利用 Google 語音辨識 API 將語音轉成文字, http://yhhuang1966.blogspot.com/2017/08/google-api.html
4.The Ultimate Guide To Speech Recognition With Python, https://realpython.com/python-speech-recognition/
準備環境:
1.MacOS平台
2.Python 3.7
3.PyCharm CE
實作步驟:
1.依照作者(Tony Huang)指示(如參攷1.及3.),利用指令 pip3 install 安裝相關程式(SpeechRecognition, PyAudio, gTTS, pygame等)
2.綜整上述作者提供的範例程式,如下
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=False) as fp:
tts = gTTS(text=sentence, lang=lang)
tts.save('{}.mp3'.format(fp.name))
# tts.save('test.mp3')
mixer.init()
mixer.music.load('{}.mp3'.format(fp.name))
# mixer.music.load('test.mp3')
mixer.music.play(loops)
# obtain audio from the microphone
r = sr.Recognizer()
with sr.Microphone() as source:
print("Please wait. Calibrating microphone...")
# listen for 0.5 seconds and create the ambient noise energy level
r.adjust_for_ambient_noise(source, duration=0.5)
print("Say something!")
audio = r.listen(source)
# recognize speech using Google Speech Recognition
try:
print("Google Speech Recognition thinks you said::>>>")
# print(r.recognize_google(audio, language="zh-TW"))
s = r.recognize_google(audio, language="zh-TW")
print(s)
except sr.UnknownValueError:
print("Google Speech Recognition could not understand audio")
except sr.RequestError as e:
print("No response from Google Speech Recognition service: {0}".format(e))
if s:
print("Text to Speech(gTTS) repeating what you just said::<<<")
# time.sleep(3)
speak(s, 'zh-TW', 1)
# speak('Hello World!', 'en')
# speak('ありがとう', 'ja')
time.sleep(5)
print('Done!')
3.語音輸入及輸出測試畫面,如下
心得:
1.其實,相關的應用可以延伸,端視看倌的連像力了!!!
2.雖然本範例可以中、英文語音輸入,但"台語"卻無法辨識,不知道Google是否那天佛心開放 Learning Model 及 Algorithm 讓使用者自行 training local language...
參攷:
1.利用 Google gTTS 文字轉語音 API 讓電腦說話, http://yhhuang1966.blogspot.com/2017/08/google-gtts-api.html
2.Easy Speech Recognition in Python with PyAudio and Pocketsphinx, https://www.codesofinterest.com/2017/03/python-speech-recognition-pocketsphinx.html
3.利用 Google 語音辨識 API 將語音轉成文字, http://yhhuang1966.blogspot.com/2017/08/google-api.html
4.The Ultimate Guide To Speech Recognition With Python, https://realpython.com/python-speech-recognition/
留言