# 語音辨識
###### tags: `智慧聯網` `IoT` `NTA-Lab` `語音辨識`
Github : https://github.com/JimboChien/speech-recognition
## 📦 Installation
1. 下載相關套件
```shell
$ sudo pip3 install SpeechRecognition wave gTTS pygame jieba pyusb click
$ sudo apt-get install python3-pyaudio portaudio.dev flac
```
2. 下載範例程式
```bash
$ git clone https://github.com/JimboChien/speech-recognition
```
## 🔊 麥克風、喇叭設定
1. 查看麥克風
```shell
$ arecord -l
```

2. 查看喇叭
```shell
$ aplay -l
```

3. 設定麥克風及喇叭
```shell
$ sudo nano /home/pi/.asoundrc
```
4. 根據所使用的麥克風、喇叭設定
```bash
pcm.!default {
type asym
playback.pcm {
type plug
slave.pcm "hw:0,0"
}
capture.pcm {
type plug
slave.pcm "hw:1,0"
}
}
```
5. 設定音量
```shell
$ alsamixer
```
6. 測試麥克風
```shell
$ speaker-test -t wav -c 2
```
## 自然語言(Neuro-Linguistic Programming, NLP)
### 自然語言介紹
1. 不同物種有自己的溝通方式

2. NLP就是人類和機器間溝通的橋樑

### 自然語言典型應用
>  情緒分析
>  聊天機器人
>  語音辨識
>  機器翻譯
## 雲端處理語音辨識
### 語音轉文字(Speech-to-Text)
首先透過語音輸入,接著交給雲端 Google 服務幫我們轉換為字串
1. 執行 `Ex_1.py`
```shell
$ python3 Ex_1.py
```

2. 接著輸入 <kbd>s</kbd> + <kbd>Enter</kbd>

3. 輸入後將開始錄音

4. 當錄完音且辨識完成後,將會回傳結果

### 文本朗讀(Text-to-Speech)
將字串上傳給 Google,Google 小姐將文字轉成語音後回傳一個語音檔,接著透過 `pygame` 套件來播放語音檔
1. 執行 `Ex_2.py`
```shell
$ python3 Ex_2.py
```

2. 接著輸入一串中文 + <kbd>Enter</kbd>,將會唸出輸入的字串

### 全句符合反應
當接收到字串後比對是否符合目標句子,如果符合將回應相對應的句子
1. 目標句子將存在 `keysentence.txt` 檔案中

2. 回應句子將存在 `reply.txt` 檔案中

3. 可透過 `gui.py` 來進行編輯

- 在左側插入一個空行並按下 `Add` 按鈕,右側會在對應的位置也插入空行
- 若發生捲動,兩邊視窗會同時捲動,方便直接對相對應位置更改
- 更改完後按下 `Save` 就會儲存到原檔案中
4. 執行 `Ex_3.py`
```shell
$ python3 Ex_3.py
```
:::info
`Ex_3.py` 具體將 `Ex_1.py` 及 `Ex_2.py` 進行結合
:::
5. 輸入 <kbd>s</kbd> + <kbd>Enter</kbd> 後開始錄音,當輸入句子符合目標句子時,將會透過語音及文字方式回應對應的句子

### 由語音控制 GPIO
1. 樹莓派 GPIO 分為 `BOARD` 及 `BCM` 模式,可以夠過 `pinout` 指令查看, `BOARD` 模式對應 `Pin #`;`BCM` 模式對應 `Name`
```shell
$ pinout
```
| Name | Pin # | Pin # | Name |
| ---: | :---: | :---: | :--- |
| <font color='ligblue'>3V3</font> | (1) | (2) | <font color='red'>5V</font> |
| <font color='ligreen'>GPIO2</font> | (3) | (4) | <font color='red'>5V</font> |
| <font color='ligreen'>GPIO3</font> | (5) | (6) | **GND** |
| <font color='ligreen'>GPIO4</font> | (7) | (8) | <font color='ligreen'>GPIO14</font> |
| **GND** | (9) | (10) | <font color='ligreen'>GPIO15</font> |
| <font color='ligreen'>GPIO17</font> | (11) | (12) | <font color='ligreen'>GPIO18</font> |
| <font color='ligreen'>GPIO27</font> | (13) | (14) | **GND** |
| <font color='ligreen'>GPIO22</font> | (15) | (16) | <font color='ligreen'>GPIO23</font> |
| <font color='ligblue'>3V3</font> | (17) | (18) | <font color='ligreen'>GPIO24</font> |
| <font color='ligreen'>GPIO10</font> | (19) | (20) | **GND** |
| <font color='ligreen'>GPIO9</font> | (21) | (22) | <font color='ligreen'>GPIO25</font> |
| <font color='ligreen'>GPIO11</font> | (23) | (24) | <font color='ligreen'>GPIO8</font> |
| **GND** | (25) | (26) | <font color='ligreen'>GPIO7</font> |
| <font color='ligreen'>GPIO0</font> | (27) | (28) | <font color='ligreen'>GPIO1</font> |
| <font color='ligreen'>GPIO5</font> | (29) | (30) | **GND** |
| <font color='ligreen'>GPIO6</font> | (31) | (32) | <font color='ligreen'>GPIO12</font> |
| <font color='ligreen'>GPIO13</font> | (33) | (34) | **GND** |
| <font color='ligreen'>GPIO19</font> | (35) | (36) | <font color='ligreen'>GPIO16</font> |
| <font color='ligreen'>GPIO26</font> | (37) | (38) | <font color='ligreen'>GPIO20</font> |
| **GND** | (39) | (40) | <font color='ligreen'>GPIO21</font> |
2. 接上 LED
3. 執行 `Ex_4.py`
```shell
$ python3 Ex_4.py
```
4. 輸入 <kbd>s</kbd> + <kbd>Enter</kbd>

5. 輸入後將開始錄音

6. 當收到開燈 LED 將亮起,收到關燈 LED 將暗掉

### 中文自動分詞
一般來說,語音助理不會以指令方式下令,而是更自然的語言,如:XXX,請幫我開燈。這時若想找到控制詞,就得加入自然語言處理的斷詞方法
- 詞:自然語言處理中帶有語意的最小單位
- 單字詞:水、火...
- 雙字詞:蘋果、電腦...
- 斷詞:將一個句子切分成詞的組合
- 輸入:今天天氣很好
- 輸出:[今天],[天氣],[很],[好]
1. 執行 `Ex_5.py`
```shell
$ python3 Ex_5.py
```

2. 輸入要段詞的句子

3. 可以重複輸入

4. 離開請輸入 `quit`

:::danger
斷詞不一定是完全正確的
:::
### 使用斷詞控制 GPIO
1. 執行 `Ex_06.py`
```shell
$ python3 Ex_06.py
```

2. 試著以自然的方式下指令,如:幫我開燈

:::success
試試看一個句子中包含多個指令
- 開燈後開始閃爍
- 閃爍,再關燈
:::