Skip to content

Commit d1496de

Browse files
committed
add example README
1 parent 9f8f411 commit d1496de

File tree

1 file changed

+118
-0
lines changed

1 file changed

+118
-0
lines changed

examples/raspi_assistant/README.md

Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
## 树莓派语音助手
2+
3+
### 简介
4+
你的树莓派还在吃灰吗?来试试把它改造成语音助手吧!该示例程序基于 voicetools,遵循最简单的 one-in-one-out,只需要在该示例程序的基础上添加关键词和对应执行的动作,就可以扩展成为个性化的专属助手。
5+
目前示例程序实现的功能有语音提醒及今明两天天气预报查询。
6+
7+
### 特性
8+
- 使用 redis 作缓存,提升语音助手的反应速度。同样的问题,第二遍不再发生网络请求。
9+
- 使用图灵机器人 API,除了预设功能外,所有问题都会有答案。
10+
- 非常简单的“receive-process-execute-feedback”逻辑,易于扩展。
11+
- 有一个心情阈值,目前是预设在配置文件中的。不一定会对你的命令言听计从。(只是为了好玩)
12+
13+
### 需要准备什么?
14+
- 一块树莓派
15+
- 一个麦克风
16+
- 一个扬声器
17+
- 一个传感器(用于唤醒语音助手,我使用的是声音传感器,当然任何传感器都可以)
18+
19+
### 使用方法
20+
首先,安装 voicetools,可通过 pip 安装或直接通过源码安装。
21+
22+
```shell
23+
pip install voicetools
24+
// or
25+
git clone [email protected]:namco1992/voicetools.git
26+
```
27+
28+
安装依赖库:
29+
30+
```shell
31+
jieba==0.38
32+
PyAudio==0.2.9
33+
redis==2.10.5
34+
requests==2.11.0
35+
RPi.GPIO==0.6.2
36+
wolframalpha==2.4
37+
```
38+
39+
如果你的树莓派的开发环境未经配置,可参考如下步骤:
40+
41+
```
42+
// python 编译环境
43+
sudo apt-get install python-dev
44+
45+
// 用于音频转换的 ffmpeg,该方法适用于取消了 ffmpeg 源的 RASPBIAN JESSIE
46+
sudo sh -c 'echo "deb http://www.deb-multimedia.org jessie main" >> /etc/apt/sources.list'
47+
sudo apt-get update
48+
sudo apt-get install deb-multimedia-keyring
49+
sudo apt-get install ffmpeg
50+
51+
// 音频相关
52+
sudo apt-get install libjack-jackd2-dev portaudio19-dev
53+
sudo apt-get install alsa-utils
54+
55+
// 系统声音设置
56+
sudo modprobe snd_bcm2835
57+
58+
// 安装并启动 redis
59+
...
60+
```
61+
62+
硬件安装。将你的传感器接在 GPIO 上,我的信号输入是4,你可以自由修改,但是要记得修改配置文件中的信号输入端口。
63+
64+
参考`settings.py.example`设置你自己的配置文件`settings.py`,主要需要设置的参数如下:
65+
66+
```python
67+
# BasicConfig 类中
68+
LOCATION = '你的地址' # 天气预报的地区
69+
TURING_KEY = 'YOUR_TURING_KEY' # 图灵机器人 key
70+
VOICE_API_KEY = 'YOUR_API_KEY' # 百度语音 api key
71+
VOICE_SECRET = 'YOUR_API_SECRET' # 百度语音 secret key
72+
73+
# BaiduAPIConfig 类中
74+
API_KEY = 'YOUR_BAIDU_API_KEY' # 天气预报使用了百度 APIStore 中的服务,需要百度 APIStore 的 key,你也可以选择任何你喜欢的服务提供商
75+
76+
# GPIOConfig 类中
77+
VOICE_SENSOR = 4 # 修改成你的传感器信号输入口
78+
```
79+
80+
Enjoy!
81+
82+
```shell
83+
// cd 到示例程序目录
84+
cd to/your/project/path
85+
//建立 log 文件夹
86+
mkdir log
87+
// 运行
88+
python assistant.py
89+
```
90+
91+
## 如何扩展
92+
1. 在配置文件的`BasicConfig`类中的关键词列表`KEYWORDS`中加入你的关键词;
93+
```python
94+
KEYWORDS = {'提醒', '备忘录', '播放', '今天', '明天', '天气', '删除', '最后', '第一条'}
95+
```
96+
2.`handler.py``FUNCTION_MAP`映射中加入你的关键词与执行方法名称的映射。例如关键词“明天”和“天气”对应的执行方法名称是“weather_today”:
97+
```python
98+
FUNC_MAP = {
99+
Keyword(['今天', '天气']).value: 'weather_today'
100+
}
101+
```
102+
3.`handler.py``ActionHandler`中加入你需要执行的方法。
103+
```python
104+
class ActionHandler(object):
105+
106+
@staticmethod
107+
def your_method(base_handler, result):
108+
"""
109+
该类中的方法均是 staticmethod。
110+
args:
111+
base_handler: `BaseHandler`实例
112+
result: 语音识别内容
113+
returns:
114+
需要语音播放的内容或回答。
115+
"""
116+
pass
117+
```
118+
4. 大功告成。

0 commit comments

Comments
 (0)