1 树莓派播放中文文本
1.1 方式一haisantts
$wget http://simcommander.cn/download/haisantts-py.gz
$tar xvfz haisantts-py.gz
$cd haisantts-py
$python haisantts.py
#coding=utf-8
import ctypes
lib = ctypes.cdll.LoadLibrary("./libhaisantts.so")
lib.startHaisanTTS("小明同学,我爱你")
1.2 方式二espeak
声音不太好听。
参考解决Full dictionary is not installed for ‘zh’
$ sudo apt-get install espeak
数据目录/usr/lib/arm-linux-gnueabihf/espeak-data
$ espeak -vzh “hello world”
$ espeak -vzh “你好帅哥”
(1)解决异常Full dictionary is not installed for ‘zh’]
$ git clone https://github.com/caixxiong/espeak-data
$ cd espeak-data/
$ unzip espeak-data.zip
$ sudo cp -r * /usr/lib/arm-linux-gnueabihf/espeak-data/
$ espeak --compile=zh
(2)解决异常Can’t access ® file ‘zh_rules’
$ sudo apt-get install libpulse-dev
$ tar xJvf espeak-1.47.11-source.tar.xz
$ cd espeak-1.47.11-source/dictsource/
$ espeak –compile=zh
$ espeak -vzh “你爱我吗”
1.3 方式三百度AI
百度语音aip安装与使用
领取语音识别和语音合成免费额度。
$ pip3 install baidu-aip
# -*- coding:utf-8 -*-
from aip import AipSpeech
""" 你的 APPID AK SK """
APP_ID = '237***29'
API_KEY = 'DRGC6Lmah***hG5RsWOQZbC'
SECRET_KEY = 'OnCmaG9SkRy***1GFq5nTnKEfukXQ'
client = AipSpeech(APP_ID, API_KEY, SECRET_KEY)
#以下是读取文本,返回audio.mp3文件的方法
result = client.synthesis('你好百度', 'zh', 1, {
'vol': 5,
})
# 识别正确返回语音二进制
if not isinstance(result, dict):
with open('auido.mp3', 'wb') as f:
f.write(result)
$ sudo apt-get install mplayer
$ mplayer auido.mp3
2 聊天机器人
# -*- coding:utf-8 -*-
import urllib
import requests
def qingyunke(msg):
re = urllib.parse.quote(msg)
url = 'http://api.qingyunke.com/api.php?key=free&appid=0&msg={}'.format(re)
html = requests.get(url)
return html.json()["content"]
msg = '我漂亮吗'
print("原话>>", msg)
res = qingyunke(msg)
print("青云客>>", res)
3 合并
3.1 play.py
# -*- coding:utf-8 -*-
import ctypes
import sys
msg = sys.argv[1]
lib = ctypes.cdll.LoadLibrary("./libhaisantts.so")
lib.startHaisanTTS(msg)
try:
sys.stdout.close()
except:
pass
try:
sys.stderr.close()
except:
pass
解决异常
close failed in file object destructor:
sys.excepthook is missing
lost sys.stderr
加入以下代码
try:
sys.stdout.close()
except:
pass
try:
sys.stderr.close()
except:
pass
3.2 main.py
# -*- coding:utf-8 -*-
import urllib
import requests
import os
def qingyunke(msg):
re = urllib.parse.quote(msg)
url = 'http://api.qingyunke.com/api.php?key=free&appid=0&msg={}'.format(re)
html = requests.get(url)
return html.json()["content"]
if __name__=="__main__":
msg = "你好呀"
print("原话>>", msg)
res = qingyunke(msg)
print("青云客>>", res)
try:
os.popen("/usr/bin/python2 play.py {}".format(res))
except Exception as e:
pass