树莓派-6-中文语音合成和智能对话

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
  • 2
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

皮皮冰燃

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值