2016年10月4日 星期二

實作語音辨識-文字轉語音 使用ubuntu mate 16.04 在 raspberry pi 3

可以加裝人體紅外線感測器(PIR),用說的控制開關與電腦(要準備麥克風,這裡使用Logitech, Inc. QuickCam Zoom來測試)
先把UBUNTU MATE 安裝到RASPBERRY PI 3上
安裝好後點選歡迎畫面內的RASPBERRY PI資訊--> 調整檔案系統 , 讓UBUNTU可以使用完整的SD CARD 空間

 重新開機後 , 執行 系統--> 管理--> SOFTWARE UPDATER --> 這要跑1,2小時以上,先喝咖啡吧! (更新完成後重新開機)

sudo apt-get install gcin  --> 中文輸入法,重新開機後就可以看到

 (新注音選詞音/拼音ctrl+alt+6)

開始安裝語音辨識與文字轉語音所需要的套件 python參考網站
sudo apt-get install python-pyaudio python3-pyaudio
sudo apt-get install portaudio19-dev python-all-dev python3-all-dev
sudo pip3 install --upgrade pip  (memo: update python3 pip)
sudo pip3 install --upgrade pyaudio
 上圖是參考網站內的說明--> 關於安裝pyaudio,這樣才能安裝到正確的pyaudio版本

sudo apt-get install flac
sudo pip3 install speechrecognition
sudo pip3 install gtts
ubuntu mate for raspberry pi 網站

================================================================
================================================================
再來 寫一小段python即可體驗語音辨識
#!/usr/bin/env python3

# NOTE: this example requires PyAudio because it uses the Microphone class

import speech_recognition as sr
import os
from gtts import gTTS
import time
import sys
import RPi.GPIO as GPIO
pin_pir = 18
stb_time = 3
pir_led = 23
GPIO.setmode(GPIO.BCM)
GPIO.setup(pin_pir, GPIO.IN)
GPIO.setup(pir_led, GPIO.OUT)
k=1
c=0
pir=0
def del_temp_mp3file():
    try:
        os.remove("test.mp3")
        print("test.mp3 file deleted.")
    except:
        print("test.mp3 檔案不存在!")

def txt2speech(m_str):
    del_temp_mp3file()
    tts = gTTS(text = m_str , lang='zh-tw')
    tts.save("test.mp3")
    os.system('omxplayer -o local test.mp3')   
   
txt2speech("語音服務已經就緒")   
print("speechRecognition alreadt for services...")
r = sr.Recognizer()   
r.energy_threshold = 4000

for i in range(0, stb_time):
    print ('等候人體紅外線就緒... %d sec\r' % (stb_time - i)),
    sys.stdout.flush()
    time.sleep(1)
print('人體紅外線已經就緒...\n')

while k>0:   
# obtain audio from the microphone
    #r = sr.Recognizer()
    with sr.Microphone() as source:
        print("語音服務聆聽中...")
        audio = r.listen(source)
    if(GPIO.input(pin_pir)):
        GPIO.output(pir_led,True)
        print ("偵測到有人 PIR detect...\r"),
        sys.stdout.flush()
        pir = 1
        time.sleep(3)
    else:
        pir = 0
        GPIO.output(pir_led,False)
        print ("沒有偵測到人 PIR not detect...\r")

# recognize speech using Google Speech Recognition
    try:
        # for testing purposes, we're just using the default API key
        # to use another API key, use `r.recognize_google(audio, key="GOOGLE_SPEECH_RECOGNITION_API_KEY")`
        # instead of `r.recognize_google(audio)`
        sr_txt = r.recognize_google(audio , language='zh_TW')
        #print("語音辨識結果:==> " + r.recognize_google(audio))
        if sr_txt == "停止":
            txt2speech("停止執行命令")
            c=0
       
        if sr_txt == "指令":
            txt2speech("請說")
            c=1
        if sr_txt == "結束程式" and c == 1 and pir == 1:
            txt2speech("語音服務已經關閉")
            k=0
        if sr_txt == "打開開關控制面板" and c == 1 and pir == 1:
            os.system('firefox http://192.168.1.252/relay.php')
           
        if sr_txt == "打開風場預報顯示圖" and c == 1 and pir == 1:
            os.system('firefox www.cwb.gov.tw/cwbwifi/')
           
        if sr_txt == "連線到樹莓派終端機" and c == 1 and pir ==1:
            os.system('mate-terminal -e "ssh pi@192.168.1.252"')   
           
        if sr_txt == "樹莓派連線狀況" and c == 1 and pir == 1:
            os.system('ping -c 15 192.168.1.252')           
           
        if sr_txt == "重新開機" and c == 1 and pir == 1:
            os.system('reboot')                   
        if sr_txt == "打開印表機" and c == 1 and pir == 1:
            os.system('mate-terminal -e " links http://192.168.1.252/openprt.php"')
            time.sleep(10)
            os.system('killall -9 links')

        if sr_txt == "關掉印表機" and c ==1 and pir == 1:
            os.system('mate-terminal -e " links http://192.168.1.252/closeprint.php"')
            time.sleep(10)
            os.system('killall -9 links')

        if sr_txt == "顯示開關狀態" and c==1 and pir==1:
            os.system('mate-terminal -e " links http://192.168.1.252/srelay.php"')
            time.sleep(10)
            os.system('killall -9 links')

        print("語音辨識結果:==> " + sr_txt)
        #txt2speech(sr_txt)
       
        #os.system('"C:\Program Files (x86)\Google\Chrome\Application\chrome.exe" http://192.168.1.252/vcr.php')
    except sr.UnknownValueError:
        print("Google Speech Recognition could not understand audio")
    except sr.RequestError as e:
        print("Could not request results from Google Speech Recognition service; {0}".format(e))

memo: sudo apt-get install links  文字模式的網頁瀏覽器

Use ubuntu mate 16.04 on raspberry pi 3 輸入此文章 2016-10-04 By Kokomo Lu