收費項目範例
不收費項目
python sample
>>> import webbrowser
pip3 install gTTS
pip3 install gTTS-token --upgrade
Command Line:
$ gtts-cli 'hello' --output hello.mp3
Module:
>>> from gtts import gTTS
>>> tts = gTTS('hello')
>>> tts.save('hello.mp3')
pip install pygame
使用暫存檔儲存mp3
from time import sleep
import tempfile
from gtts import gTTS
from pygame import mixer
def speak(sentence):
with tempfile.NamedTemporaryFile(delete=True) as fp:
print(sentence)
tts = gTTS(text=sentence, lang='zh-TW')
tts.save('{}.mp3'.format(fp.name))
mixer.init()
mixer.music.load('{}.mp3'.format(fp.name))
mixer.music.play()
while mixer.music.get_busy():
sleep(1)
print ("done")
中文 sample
from time import sleep
import tempfile
from gtts import gTTS
from pygame import mixer
def speak(sentence):
with tempfile.NamedTemporaryFile(delete=True) as fp:
tts = gTTS(text=sentence, lang='zh-TW')
tts.save('{}.mp3'.format(fp.name))
mixer.init()
mixer.music.load('{}.mp3'.format(fp.name))
mixer.music.play(1)
while mixer.music.get_busy():
sleep(1)
print ("done")
speak('臺北醫學大學老山山教授')
from gtts import gTTS
from playsound import playsound
mytext="你好,我是老山山教授"
language='ZH-TW'
myobj=gTTS(text=mytext,lang=language)
myobj.save("c:/Users/jjpc3/Desktop/welcome1.mp3")
playsound("c:/Users/jjpc3/Desktop/welcome1.mp3")
範例:每分鐘報時系統
import time
from gtts import gTTS
import tempfile
from pygame import mixer
from datetime import datetime
from time import strftime
def say(text, filename=None):
with tempfile.NamedTemporaryFile(delete=True) as temp:
tts = gTTS(text, lang='zh-TW',slow=False)
if filename is None:
filename = "{}.mp3".format(temp.name)
tts.save(filename)
mixer.init()
mixer.music.load(filename)
mixer.music.play()
while mixer.music.get_busy() == True:
continue
mixer.quit()
while(1):
hour=datetime.now().strftime('%H')
minute=datetime.now().strftime('%M')
second=datetime.now().strftime('%S')
print(second)
while second == '00':
say("現在是"+hour+"點"+minute+"分")
second=datetime.now().strftime('%S')
time.sleep(1)
範例:提醒系統
import time
from gtts import gTTS
import tempfile
from pygame import mixer
from datetime import datetime
from time import strftime
remind = {
'1332':'主人該喝水了',
'1333':'主人你該吃藥了',
'1334':'謝謝你的讚美'
}
def say(text, filename=None):
with tempfile.NamedTemporaryFile(delete=True) as temp:
tts = gTTS(text, lang='zh-TW',slow=False)
if filename is None:
filename = "{}.mp3".format(temp.name)
tts.save(filename)
mixer.init()
mixer.music.load(filename)
mixer.music.play()
while mixer.music.get_busy() == True:
continue
mixer.quit()
while(1):
hour=datetime.now().strftime('%H')
minute=datetime.now().strftime('%M')
second=datetime.now().strftime('%S')
print(hour+minute+second)
while second == '00':
if remind.get(hour+minute,'none') != 'none':
print(remind.get(hour+minute))
say("現在是"+hour+"點"+minute+"分"+remind.get(hour+minute))
time.sleep(1)
second=datetime.now().strftime('%S')
time.sleep(1)
範例:定時控制系統
import time
from gtts import gTTS
import tempfile
from pygame import mixer
from datetime import datetime
from time import strftime
from urllib.request import urlopen
import urllib
import urllib.parse
import json
remind = {
'1951':'http://61.219.106.180/xxxxx/commexec_json.php?user=jj&lang=cmn-Hant-TW&command=%E6%89%93%E9%96%8B%E5%B0%8F%E5%A4%9C%E7%87%88',
'1952':'http://61.219.106.180/Dxxxx/commexec_json.php?user=jj&lang=cmn-Hant-TW&command='+urllib.parse.quote("關閉小夜燈"),
'1953':'http://13.56.153.188/Drxxxx/IOcontrol.php?device=2nwZTtSl&command=toggle'
}
def say(text, filename=None):
with tempfile.NamedTemporaryFile(delete=True) as temp:
tts = gTTS(text, lang='zh-TW',slow=False)
if filename is None:
filename = "{}.mp3".format(temp.name)
tts.save(filename)
mixer.init()
mixer.music.load(filename)
mixer.music.play()
while mixer.music.get_busy() == True:
continue
mixer.quit()
while(1):
hour=datetime.now().strftime('%H')
minute=datetime.now().strftime('%M')
second=datetime.now().strftime('%S')
print(hour+minute+second)
while second == '00':
if remind.get(hour+minute,'none') != 'none':
print(remind.get(hour+minute))
u = urlopen(remind.get(hour+minute))
say("現在是"+hour+"點"+minute+"分正在執行你的指令")
time.sleep(1)
second=datetime.now().strftime('%S')
time.sleep(1)
https://pypi.org/project/SpeechRecognition/
pip3 install SpeechRecognition
raspberry pi
sudo apt-get install python-pyaudio python3-pyaudio
sudo apt-get install flac
PC use
pip install pyaudio
or
python -m pip install pyaudio
or
pip install pipwin
pipwin install pyaudio
or add
brew install portaudio
brew link portaudio
範例:語音辨識
import speech_recognition as sr
#obtain audio from the microphone
def listenTo():
r=sr.Recognizer()
with sr.Microphone() as source:
print("Please wait. Calibrating microphone...")
#listen for 2 seconds and create the ambient noise energy level
r.adjust_for_ambient_noise(source, duration=2)
print("Say something!")
audio=r.listen(source)
# recognize speech using Google Speech Recognition
try:
print("JJ robot will answer your question:")
print(r.recognize_google(audio, language="zh-TW"))
return r.recognize_google(audio, language="zh-TW")
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))
listenTo()
範例:聊天機器人
import speech_recognition as sr
#obtain audio from the microphone
def listenTo():
r=sr.Recognizer()
with sr.Microphone() as source:
print("Please wait. Calibrating microphone...")
#listen for 2 seconds and create the ambient noise energy level
r.adjust_for_ambient_noise(source, duration=2)
print("Say something!")
audio=r.listen(source)
# recognize speech using Google Speech Recognition
try:
print("JJ robot will answer your question:")
# print(r.recognize_google(audio, language="zh-TW"))
return r.recognize_google(audio, language="zh-TW")
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))
from time import sleep
import tempfile
from gtts import gTTS
from pygame import mixer
def speak(sentence):
with tempfile.NamedTemporaryFile(delete=True) as fp:
tts = gTTS(text=sentence, lang='zh-TW')
tts.save('{}.mp3'.format(fp.name))
mixer.init()
mixer.music.load('{}.mp3'.format(fp.name))
mixer.music.play()
while mixer.music.get_busy():
sleep(1)
print ("done")
qa = {
'你好':'我很好',
'你是誰':'我是你的小管家',
'你很聰明':'謝謝你的讚美'
}
speak(qa.get(listenTo(), '我聽不懂'))
https://github.com/ssut/py-googletrans
pip3 install googletrans
pip3 install git+https://github.com/BoseCorp/py-googletrans.git --upgrade
範例:翻譯機器人
import speech_recognition as sr
from googletrans import Translator
translator = Translator()
#obtain audio from the microphone
def listenTo():
r=sr.Recognizer()
with sr.Microphone() as source:
print("Please wait. Calibrating microphone...")
#listen for 5 seconds and create the ambient noise energy level
r.adjust_for_ambient_noise(source, duration=1)
print("Say something!")
audio=r.listen(source)
# recognize speech using Google Speech Recognition
try:
print("JJ robot will answer your question:")
print(r.recognize_google(audio, language="zh-TW"))
#sentence = translator.translate(r.recognize_google(audio, language="zh-TW"), dest='en').text
return translator.translate(r.recognize_google(audio, language="zh-TW"), dest='ja').text
except sr.UnknownValueError:
print("Google Speech Recognition could not understand audio")
return " "
except sr.RequestError as e:
print("No response from Google Speech Recognition service: {0}".format(e))
return " "
from time import sleep
import tempfile
from gtts import gTTS
from pygame import mixer
def speak(sentence):
if sentence != " ":
with tempfile.NamedTemporaryFile(delete=True) as fp:
print(sentence)
tts = gTTS(text=sentence, lang='ja')
tts.save('{}.mp3'.format(fp.name))
mixer.init()
mixer.music.load('{}.mp3'.format(fp.name))
mixer.music.play()
while mixer.music.get_busy():
sleep(1)
print ("done")
import speech_recognition as sr
from googletrans import Translator
translator = Translator()
#obtain audio from the microphone
def listenTo():
r=sr.Recognizer()
with sr.Microphone() as source:
print("Please wait. Calibrating microphone...")
#listen for 5 seconds and create the ambient noise energy level
r.adjust_for_ambient_noise(source, duration=1)
print("Say something!")
audio=r.listen(source)
# recognize speech using Google Speech Recognition
try:
print("JJ robot will answer your question:")
print(r.recognize_google(audio, language="zh-TW"))
#sentence = translator.translate(r.recognize_google(audio, language="zh-TW"), dest='en').text
return translator.translate(r.recognize_google(audio, language="zh-TW"), dest='ja').text
except sr.UnknownValueError:
print("Google Speech Recognition could not understand audio")
return " "
except sr.RequestError as e:
print("No response from Google Speech Recognition service: {0}".format(e))
return " "
from time import sleep
import tempfile
from gtts import gTTS
from pygame import mixer
def speak(sentence):
if sentence != " ":
with tempfile.NamedTemporaryFile(delete=True) as fp:
print(sentence)
tts = gTTS(text=sentence, lang='ja')
tts.save('{}.mp3'.format(fp.name))
mixer.init()
mixer.music.load('{}.mp3'.format(fp.name))
while True:
speak(listenTo())
Beautiful Soup 本身只是一個 HTML 解析工具,它並不負責下載網頁,所以通常我們在開發爬蟲程式時,會搭配 requests 模組一同使用。
import requests
from bs4 import BeautifulSoup
範例:Google 查詢機器人
import speech_recognition as sr
#obtain audio from the microphone
def listenTo():
r=sr.Recognizer()
with sr.Microphone() as source:
print("Please wait. Calibrating microphone...")
#listen for 5 seconds and create the ambient noise energy level
r.adjust_for_ambient_noise(source, duration=1)
print("Say something!")
audio=r.listen(source)
# recognize speech using Google Speech Recognition
try:
print("JJ robot will answer your question:")
answertxt = r.recognize_google(audio, language="zh-TW")
print(answertxt)
return answertxt
except sr.UnknownValueError:
print("Google Speech Recognition could not understand audio")
return " "
except sr.RequestError as e:
print("No response from Google Speech Recognition service: {0}".format(e))
return " "
from time import sleep
import tempfile
from gtts import gTTS
from pygame import mixer
def speak(sentence):
with tempfile.NamedTemporaryFile(delete=True) as fp:
print(sentence)
if sentence == " ":
return
tts = gTTS(text=sentence, lang='zh-TW')
tts.save('{}.mp3'.format(fp.name))
mixer.init()
mixer.music.load('{}.mp3'.format(fp.name))
mixer.music.play()
while mixer.music.get_busy():
sleep(1)
print ("done")
import requests
from bs4 import BeautifulSoup
import urllib.parse
def ask_google(sentence):
if sentence == " ":
return " "
req1=requests.get('https://www.google.com.tw/search?source=lnt&tbas=0&sa=X&ved=0ahUKEwjo5PfQzc3cAhUP5rwKHU6CCvUQpwUIHg&biw=1920&bih=898&q='+urllib.parse.quote(sentence))
#print(req1.text)
soup = BeautifulSoup(req1.text,'html.parser')
divs = soup.find('div', 'g')
print(divs.text)
if len(divs.text) > 100:
req1=requests.get('https://www.google.com.tw/search?source=lnt&tbs=li:1&sa=X&ved=0ahUKEwjo5PfQzc3cAhUP5rwKHU6CCvUQpwUIHg&biw=1920&bih=898&q='+urllib.parse.quote(sentence))
soup = BeautifulSoup(req1.text,'html.parser')
divs = soup.find('div', 'g')
print(divs.text)
if len(divs.text) > 100:
return " "
return divs.text
return divs.text
while True:
speak(ask_google(listenTo()))
import os
import sys, locale
import tempfile
from time import sleep
from pygame import mixer # Load the required library
class Speak:
def __init__ (self,message,times,launguage,isRemove):
'''
传入参数 消息 次数 是否清除合成的语音文件
'''
self.tts(message,launguage)
#说几次
for i in range(0,times):
self.say()
if isRemove:
self.over()
def tts(self,message,launguage):
try:
import requests
except:
exit(-1)
import urllib
s = requests.Session()
mes=''
if launguage is "zh":
mes="http://tts.baidu.com/text2audio?spd=2&lan=zh&pid=101&vol=9&ie=UTF-8&text="
else:
mes="http://tts.baidu.com/text2audio?spd=2&lan=en&pid=101&vol=9&ie=UTF-8&text="
s.get(mes+ urllib.request.quote(message))
res = s.get(mes+ urllib.request.quote(message)).content
f = open("tts-temp.mp3", "wb+")
f.write(res)
f.close()
def say(self):
mixer.init()
mixer.music.load('tts-temp.mp3')
mixer.music.play()
while mixer.music.get_busy():
sleep(0.05)
mixer.music.stop()
mixer.quit()
def over(self):
os.system("del tts-temp.mp3")
def play(music):
os.system("play "+music)
man=Speak("我是臺北醫學大學學生",1,"zh",True)
import os
import time
from datetime import datetime
from time import strftime
def speak(sentence):
print(sentence)
os.system("baidu_TTS '"+sentence+"'" )
while(1):
hour=datetime.now().strftime('%H')
minute=datetime.now().strftime('%M')
second=datetime.now().strftime('%S')
print(second)
while second == '00':
speak("現在是"+hour+"點"+minute+"分")
second=datetime.now().strftime('%S')
time.sleep(1)
import os
import speech_recognition as sr
#obtain audio from the microphone
def listenTo():
r=sr.Recognizer()
with sr.Microphone() as source:
print("Please wait. Calibrating microphone...")
#listen for 5 seconds and create the ambient noise energy level
r.adjust_for_ambient_noise(source, duration=5)
print("Say something!")
audio=r.listen(source)
# recognize speech using Google Speech Recognition
try:
print("JJ robot will answer your question:")
# print(r.recognize_google(audio, language="zh-TW"))
return r.recognize_google(audio, language="zh-TW")
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))
from time import sleep
def speak(sentence):
print(sentence)
os.system("baidu_TTS '"+sentence+"'" )
qa = {
'你好':'我很好',
'你是誰':'我是你的小管家',
'你很聰明':'謝謝你的讚美'
}
speak(qa.get(listenTo(), '我聽不懂'))
必須安裝 pip3 install paho-mqtt
import speech_recognition as sr
import paho.mqtt.client as mqtt
import paho.mqtt.publish as publish
import time
HOST = "120.97.62.97"
PORT = 1883
def on_connect(client, userdata, flags, rc):
print("Connected with result code "+str(rc))
client.subscribe("AI/speech_reply/user")
def on_message(client, userdata, msg):
print(msg.topic+" "+msg.payload.decode("utf-8"))
def send_message(user, msg, device):
# print(msg)
client_id = time.strftime('%Y%m%d%H%M%S',time.localtime(time.time()))
client = mqtt.Client(client_id) # ClientId不能重复,所以使用当前时间
client.username_pw_set("drchiu", "8888") # 必须设置,否则会返回「Connected with result code 4」
client.on_connect = on_connect
client.on_message = on_message
client.connect(HOST, PORT, 60)
client.publish("AI/speech_reply/user", '{"user":"'+user+'","text":"'+msg+'","device":"'+device+'","lang":"cmn-Hant-TW"}', qos=0, retain=False)
if __name__ == '__main__':
while True:
#obtain audio from the microphone
r=sr.Recognizer()
with sr.Microphone() as source:
print("Please wait. Calibrating microphone...")
#listen for 5 seconds and create the ambient noise energy level
r.adjust_for_ambient_noise(source, duration=1)
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"))
send_message('jj',r.recognize_google(audio, language="zh-TW"),'all')
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))
from gtts import gTTS
import tempfile
from pygame import mixer
import paho.mqtt.client as mqtt
import paho.mqtt.publish as publish
import paho.mqtt.subscribe as subscribe
import time
import json
HOST = "120.97.62.97"
PORT = 1883
ID = "raspberry1"
def say(text, filename=None):
with tempfile.NamedTemporaryFile(delete=True) as temp:
tts = gTTS(text, lang='zh-TW',slow=False)
if filename is None:
filename = "{}.mp3".format(temp.name)
tts.save(filename)
mixer.init()
mixer.music.load(filename)
mixer.music.play()
while mixer.music.get_busy() == True:
continue
mixer.quit()
def on_connect(client, userdata, flags, rc):
print("Connected with result code "+str(rc))
client.subscribe("AI/speech_reply/user")
def on_message(client, userdata, msg):
x=msg.payload.decode("utf-8")
print(x)
y = json.loads(x)
print(y["text"])
print(msg.topic+" "+y["text"])
say(y["text"])
def send_message(user, msg, device):
# print(msg)
client_id = time.strftime('%Y%m%d%H%M%S',time.localtime(time.time()))
client = mqtt.Client(client_id) # ClientId不能重复,所以使用当前时间
client.username_pw_set("drchiu", "8888") # 必须设置,否则会返回「Connected with result code 4」
client.on_connect = on_connect
client.on_message = on_message
client.connect(HOST, PORT, 60)
client.publish("AI/speech_reply/user", '{"user":"'+user+'","text":"'+msg+'","device":"'+device+'","lang":"cmn-Hant-TW"}', qos=0, retain=False)
if __name__ == '__main__':
client_id = time.strftime('%Y%m%d%H%M%S',time.localtime(time.time()))
client = mqtt.Client(client_id) # ClientId不能重复,所以使用当前时间
client.username_pw_set("drchiu", "8888")
######Bind function to callback
client.on_message=on_message
#####
print("connecting to broker ",HOST )
client.connect(HOST )#connect
client.loop_start() #start loop to process received messages
print("subscribing ")
client.subscribe("all/speech_reply")#subscribe
#client.subscribe(ID+"/speech_reply", qos=1)
#client.message_callback_add("all/speech_reply", on_message )
#client.loop_forever()
#print("publishing ")
#client.publish("all/speech_reply","on")#publish
#client.disconnect() #disconnect
#client.loop_stop() #stop loop
say("主人你好,很高興能為你服務")
while True:
subscribe.callback(on_message, ID+"/speech_reply", hostname=HOST )
#subscribe.message_callback_add(on_message, "all/speech_reply", hostname=HOST )
GenJyuuGothicX-Normal.ttf doll_gif.zip
import requests
import json
import speech_recognition as sr
from time import sleep
import tempfile
from gtts import gTTS
from pygame import mixer
import subprocess
import pygame
import datetime
import paho.mqtt.client as mqtt
import paho.mqtt.publish as publish
import paho.mqtt.subscribe as subscribe
import time
##from concurrent.futures import ThreadPoolExecutor
import threading
HOST = "120.97.62.97"
PORT = 1883
ID = "raspberry1"
url="http://61.219.106.180/drchiu/commexec_doll.php"
user = 'jj'
#obtain audio from the microphone
def listenTo():
r=sr.Recognizer()
with sr.Microphone() as source:
print("Please wait. Calibrating microphone...")
#listen for 1 seconds and create the ambient noise energy level
r.adjust_for_ambient_noise(source, duration=1)
print("我正在聆聽你的命令!")
show_ear()
#subprocess.Popen("sudo python3 ws2812.py", shell=True)
audio=r.listen(source)
# recognize speech using Google Speech Recognition
try:
print("JJ robot listen sentence from you:")
answer = r.recognize_google(audio, language="zh-TW")
print(answer)
return answer
except sr.UnknownValueError:
print("Google Speech Recognition could not understand audio")
return "again"
except sr.RequestError as e:
print("No response from Google Speech Recognition service: {0}".format(e))
def speak(sentence):
with tempfile.NamedTemporaryFile(delete=True) as fp:
tts = gTTS(text=sentence, lang='zh-TW')
tts.save('{}.mp3'.format(fp.name))
mixer.init()
mixer.music.load('{}.mp3'.format(fp.name))
mixer.music.play()
while mixer.music.get_busy() == True:
continue
#print ("done")
mixer.quit()
def ask_cloud(text):
r = requests.get(url, params={'user': user,'command':text,'reply':'json'})
data = r.json()
print(data)
example = data["reply"]
print(example)
screen_show(text,example)
return example
def screen_show(text1, text2):
font = pygame.font.Font("GenJyuuGothicX-Normal.ttf",36)
text = font.render( text2, True, (0, 128, 0))
font = pygame.font.Font("GenJyuuGothicX-Normal.ttf", 76)
text1 = font.render( text1, True, (0, 0, 128))
for event in pygame.event.get():
if event.type == pygame.QUIT:
done = True
if event.type == pygame.KEYDOWN and event.key == pygame.K_ESCAPE:
done = True
screen.fill((255, 255, 255))
screen.blit(text1,
(400 - text1.get_width() // 2, 240- text1.get_height() // 2))
screen.blit(text,
(400 - text.get_width() // 2, 240 - text.get_height() // 2 + 120))
image = pygame.image.load("eyes.png")
image.convert()
screen.blit(image, (250,70))
pygame.display.flip()
pygame.display.flip()
def on_connect(client, userdata, flags, rc):
print("Connected with result code "+str(rc))
client.subscribe("AI/speech_reply/user")
def on_message(client, userdata, msg):
x=msg.payload.decode("utf-8")
print(x)
y = json.loads(x)
print(y["text"])
##print(msg.topic+" "+y["text"])
speak(y["text"])
def send_message(user, msg, device):
# print(msg)
client_id = time.strftime('%Y%m%d%H%M%S',time.localtime(time.time()))
client = mqtt.Client(client_id) # ClientId不能重复,所以使用当前时间
client.username_pw_set("drchiu", "8888") # 必须设置,否则会返回「Connected with result code 4」
client.on_connect = on_connect
client.on_message = on_message
client.connect(HOST, PORT, 60)
client.publish("AI/speech_reply/user", '{"user":"'+user+'","text":"'+msg+'","device":"'+device+'","lang":"cmn-Hant-TW"}', qos=0, retain=False)
##def process():
##with ThreadPoolExecutor() as executor:
##robot()
##mqtt_net()
def mqtt_net():
client_id = time.strftime('%Y%m%d%H%M%S',time.localtime(time.time()))
client = mqtt.Client(client_id) # ClientId不能重复,所以使用当前时间
client.username_pw_set("drchiu", "8888")
###Bind function to callback
client.on_message=on_message
#####
print("connecting to broker ",HOST )
client.connect(HOST )#connect
client.loop_start() #start loop to process received messages
print("subscribing ")
client.subscribe("all/speech_reply")#subscribe
#client.subscribe(ID+"/speech_reply", qos=1)
#client.message_callback_add("all/speech_reply", on_message )
#client.loop_forever()
while True:
subscribe.callback(on_message, ID+"/speech_reply", hostname=HOST )
def show_ear():
image = pygame.image.load("ear.png")
image.convert()
screen.blit(image, (680,20))
image = pygame.image.load("ear_left.png")
image.convert()
screen.blit(image, (20,20))
image = pygame.image.load("eyes.png")
image.convert()
screen.blit(image, (250,70))
pygame.display.flip()
def robot():
wait_count=1
##screen = pygame.display.set_mode((800, 480))
##screen = pygame.display.set_mode((0, 0), pygame.FULLSCREEN)
screen_show('','主人你好,很高興能為你服務 ')
speak('主人你好,很高興能為你服務 ')
#subprocess.Popen("sudo python3 MQTT_speech.py", shell=True)
##subscribe.callback(on_message, ID+"/speech_reply", hostname=HOST )
while True:
#show_ear()
answer = listenTo()
if (answer == 'again'):
screen_show('', '請下達你的指令')
wait_count =wait_count +1
if(wait_count >30):
speak('主人你好')
wait_count =1
else:
text1=ask_cloud(answer)
if(text1 != '我不懂你的意思'):
speak(text1)
###
if __name__ == '__main__':
pygame.init()
#screen = pygame.display.set_mode((0, 0), pygame.FULLSCREEN)
screen = pygame.display.set_mode((640, 480))
t = threading.Thread(target = mqtt_net)
# 執行該子執行緒
t.start()
robot()
sudo apt-get install pulseaudio
set GOOGLE_APPLICATION_CREDENTIALS=jjlab-speech-send-cc165e9881e4.json
"""Google Cloud Text-To-Speech API sample application .
Example usage:
python text2speech.py 這是一個文字轉語音範例
"""
from pygame import mixer
import tempfile
from time import sleep
import sys
def text2speech(txt):
from google.cloud import texttospeech
# Instantiates a client
client = texttospeech.TextToSpeechClient()
# Set the text input to be synthesized
synthesis_input = texttospeech.types.SynthesisInput(text= txt)
# Build the voice request, select the language code ("en-US") and the ssml
# voice gender ("neutral")
voice = texttospeech.types.VoiceSelectionParams(
language_code='zh-hant',
ssml_gender=texttospeech.enums.SsmlVoiceGender.NEUTRAL)
# Select the type of audio file you want returned
audio_config = texttospeech.types.AudioConfig(
audio_encoding=texttospeech.enums.AudioEncoding.MP3)
# Perform the text-to-speech request on the text input with the selected
# voice parameters and audio file type
response = client.synthesize_speech(synthesis_input, voice, audio_config)
# [END tts_quickstart]
with tempfile.NamedTemporaryFile(delete=True) as fp:
# The response's audio_content is binary.
with open('{}.mp3'.format(fp.name), 'wb') as out:
# Write the response to the output file.
out.write(response.audio_content)
print('Audio content written to file "output.mp3"')
#print(text)
#tts = gTTS(text=sentence, lang='zh-TW')
#tts.save('{}.mp3'.format(fp.name))
mixer.init()
mixer.music.load('{}.mp3'.format(fp.name))
mixer.music.play()
while mixer.music.get_busy():
sleep(1)
print("done")
if __name__ == '__main__':
text2speech(sys.argv[1])
python text2speech.py 這是一個文字轉語音範例
pip3 install gcloud
pip3 install google-api-python-client
pip3 install google-cloud-speech
pip3 install "google-cloud-texttospeech<2.0.0"
如果有黃色警告時加入
echo 'export PATH=/home/pi/.local/bin:$PATH' >>~/.bashrc
source ~/.bashrc
pip3 uninstall gcloud
pip3 install gcloud 重新安裝一次就好了
"""Google Cloud Text-To-Speech API sample application
Example usage:
windows: set GOOGLE_APPLICATION_CREDENTIALS=jjlab-speech-send-cc165e9881e4.json or
linux: export GOOGLE_APPLICATION_CREDENTIALS="jjlab-speech-send-cc165e9881e4.json"
python mqtt_google_say.py
MQTT:
topic jj/speech_reply
data {"text":"講個笑話給我聽","device":"windows1","lang":"cmn-Hant-TW"}
http://3.114.197.59/MQTT/publish_97.php?topic=jj/speech_reply&value={"text":"簡文山教授是我的偶像","device":"windows1","lang":"cmn-Hant-TW"}
"""
import pygame
from pygame import mixer
import tempfile
from time import sleep
import urllib.request
import sys
import paho.mqtt.client as mqtt
import paho.mqtt.publish as publish
import paho.mqtt.subscribe as subscribe
import time
import json
HOST = "120.97.62.97"
PORT = 1883
ID = "windows1"
group = "tmu_device"
lang = "cmn-Hant-TW"
user = "jj"
def play_song(url):
print('Beginning file download with urllib2...')
#url = 'http://hiot.prof-jj.com/music/clapping.mp3'
urllib.request.urlretrieve(url, './play.mp3')
pygame.init()
pygame.mixer.init()
screen=pygame.display.set_mode([1,1])
pygame.time.delay(1000)
MUSIC_END = pygame.USEREVENT+1
pygame.mixer.music.set_endevent(MUSIC_END)
pygame.mixer.music.load('./play.mp3')
pygame.mixer.music.play()
print("play mp3")
running = True
while running:
for event in pygame.event.get():
if event.type == MUSIC_END:
running = False
if running == False:
pygame.quit()
def say(txt,lang):
from google.cloud import texttospeech
# Instantiates a client
client = texttospeech.TextToSpeechClient()
# Set the text input to be synthesized
synthesis_input = texttospeech.types.SynthesisInput(text= txt)
# Build the voice request, select the language code ("en-US") and the ssml
# voice gender ("neutral") "MALE","FEMALE","NEUTRAL"
voice = texttospeech.types.VoiceSelectionParams(
language_code= lang,
ssml_gender=texttospeech.enums.SsmlVoiceGender.FEMALE)
# Select the type of audio file you want returned
audio_config = texttospeech.types.AudioConfig(
audio_encoding=texttospeech.enums.AudioEncoding.MP3)
# Perform the text-to-speech request on the text input with the selected
# voice parameters and audio file type
response = client.synthesize_speech(synthesis_input, voice, audio_config)
# [END tts_quickstart]
with tempfile.NamedTemporaryFile(delete=True) as fp:
# The response's audio_content is binary.
with open('{}.mp3'.format(fp.name), 'wb') as out:
# Write the response to the output file.
out.write(response.audio_content)
print('Audio content written to file "output.mp3"')
#print(text)
#tts = gTTS(text=sentence, lang='zh-TW')
#tts.save('{}.mp3'.format(fp.name))
mixer.init()
mixer.music.load('{}.mp3'.format(fp.name))
mixer.music.play()
while mixer.music.get_busy():
sleep(1)
print("done")
def on_connect(client, userdata, flags, rc):
print("Connected with result code "+str(rc))
client.subscribe("AI/speech_reply/user")
def on_message(client, userdata, msg):
x=msg.payload.decode("utf-8")
print(x)
y = json.loads(x)
if y["device"] == ID or y["device"] == group or y["device"] == "all_device":
if(y["text"][:1] != "/"):
print(y["text"])
print(msg.topic + " " + y["text"])
say(y["text"],y["lang"])
if(y["text"] == "/play_mp3"):
if(y["url"] != ""):
print(y["url"])
play_song(y["url"])
if(y["text"] == "/say_time"):
print(time.strftime("現在時間%H:%M",time.localtime()))
say(time.strftime("現在時間%H:%M",time.localtime()),y["lang"])
def send_message(user, msg, device):
# print(msg)
client_id = time.strftime('%Y%m%d%H%M%S',time.localtime(time.time()))
client = mqtt.Client(client_id) # ClientId不能重复,所以使用当前时间
client.username_pw_set("drchiu", "8888") # 必须设置,否则会返回「Connected with result code 4」
client.on_connect = on_connect
client.on_message = on_message
client.connect(HOST, PORT, 60)
client.publish("AI/speech_reply/user", '{"user":"'+user+'","text":"'+msg+'","device":"'+device+'","lang":"cmn-Hant-TW"}', qos=0, retain=False)
if __name__ == '__main__':
client_id = time.strftime('%Y%m%d%H%M%S',time.localtime(time.time()))
client = mqtt.Client(client_id) # ClientId不能重复,所以使用当前时间
client.username_pw_set("drchiu", "8888")
######Bind function to callback
client.on_message=on_message
#####
print("connecting to broker ",HOST )
client.connect(HOST )#connect
client.loop_start() #start loop to process received messages
print("subscribing ")
client.subscribe("all/speech_reply")#subscribe
#client.subscribe(ID+"/speech_reply", qos=1)
#client.message_callback_add("all/speech_reply", on_message )
#client.loop_forever()
#print("publishing ")
#client.publish("all/speech_reply","on")#publish
#client.disconnect() #disconnect
#client.loop_stop() #stop loop
say("主人你好,很高興能為你服務",lang)
while True:
subscribe.callback(on_message, user+"/speech_reply", hostname=HOST )
#subscribe.message_callback_add(on_message, "all/speech_reply", hostname=HOST )