1. 程式人生 > >樹莓派 智慧小車 語音控制系統

樹莓派 智慧小車 語音控制系統

所需硬體

  • 智慧小車
  • 樹莓派3B
  • 麥克風(樹莓派專用)
  • 耳機

環境配置

  • 安裝pycurl庫

    sudo apt-get install python-pycurl

  • 中文語音合成軟體 Ekho

    根據官方安裝說明進行安裝。關鍵的一句是make CXXFLAGS=-DNO_SSE
    這裡寫圖片描述

    測試時給樹莓派插上耳機線,把樹莓派音量調到最大(這個時候不要把耳機帶上,否則音量會過大)

    amixer -D pulse sset Master 100%

    執行測試命令:

    /ekho "hello 123"

    這個時候應該可以聽到耳機發出微弱的聲音。

  • 播放器

    如mplayer

    sudo apt-get install mplayer

程式碼

# -*- coding: utf-8 -*-
import numpy as np
from datetime import datetime
import wave
import time
import urllib, urllib2, pycurl
import base64
import json
import os
import sys
import numpy as np
import zmq
import serial, time, sys
reload(sys)
sys.setdefaultencoding( "utf-8" )

filename = '2.wav'
duihua = '1' # connect arduino def connect_arduino(): arduino = serial.Serial('/dev/ttyUSB0',9600,timeout=1) arduino.close() arduino.open() return arduino # 百度語音識別 # def get_token(): apiKey = "Ll0c53MSac6GBOtpg22ZSGAU" secretKey = "44c8af396038a24e34936227d4a19dc2" auth_url = "https://openapi.baidu.com/oauth/2.0/token?grant_type=client_credentials&client_id="
+ apiKey + "&client_secret=" + secretKey; res = urllib2.urlopen(auth_url) json_data = res.read() return json.loads(json_data)['access_token'] def dump_res(buf): global duihua print "字串型別" print (buf) a = eval(buf) print type(a) if a['err_msg']=='success.': # print a['result'][0] # 語音識別結果 duihua = a['result'][0] print duihua def use_cloud(token): # 這裡開啟檔案為2.wav fp = wave.open(filename, 'rb') nf = fp.getnframes() f_len = nf * 2 audio_data = fp.readframes(nf) cuid = "7519663" #產品id srv_url = 'http://vop.baidu.com/server_api' + '?cuid=' + cuid + '&token=' + token http_header = [ 'Content-Type: audio/pcm; rate=8000', 'Content-Length: %d' % f_len ] c = pycurl.Curl() c.setopt(pycurl.URL, str(srv_url)) #curl doesn't support unicode #c.setopt(c.RETURNTRANSFER, 1) c.setopt(c.HTTPHEADER, http_header) #must be list, not dict c.setopt(c.POST, 1) c.setopt(c.CONNECTTIMEOUT, 30) c.setopt(c.TIMEOUT, 30) # 寫回調函式 dump_res c.setopt(c.WRITEFUNCTION, dump_res) c.setopt(c.POSTFIELDS, audio_data) c.setopt(c.POSTFIELDSIZE, f_len) c.perform() #pycurl.perform() has no return val # 圖靈機器人 可對話 # def getHtml(url): page = urllib.urlopen(url) html = page.read() return html key = '05ba411481c8cfa61b91124ef7389767' api = 'http://www.tuling123.com/openapi/api?key=' + key + '&info=' token = get_token() print "你好,歡迎使用智慧小車語音控制系統" # 這裡在1.txt中寫入"你好,歡迎使用智慧小車語音控制系統",然後通過ekho進行語音合成得到1.wav,之後可直接播放 # os.system('ekho -f 1.txt -o 1.wav') os.system('mplayer 1.wav') while(True): arduino = connect_arduino(); print "請指示" # 同理這裡在2.txt中寫入"請指示"並生成了2.wav os.system('ekho -f 2.txt -o 2.wav') os.system('mplayer 2.wav') # 錄音並存儲為2.wav os.system('arecord -D "plughw:1,0" -f S16_LE -d 4 -r 8000 /home/pi/yuyinduihua/2.wav') use_cloud(token) print "輸入內容" # 錄音識別結果 print duihua # 識別指令控制小車行走 if duihua.find("前進") != -1: arduino.write('1') elif duihua.find("後退") != -1: arduino.write('2') elif duihua.find("左轉") != -1: arduino.write('4') elif duihua.find("右轉") != -1: arduino.write('3') elif duihua.find("轉圈") != -1: arduino.write('8') #elif duihua.find("停止") != -1: # arduino.write('9') #elif duihua.find("加快") != -1: # arduino.write('+') #elif duihua.find("減慢") != -1: # arduino.write('-') else: arduino.write('9') time.sleep(1) arduino.write('9') # 通過圖靈機器人api得到應答 a info = duihua duihua = "" request = api + info response = getHtml(request) dic_json = json.loads(response) print '機器人: '.decode('utf-8') + dic_json['text'] a = dic_json['text'] print type(a) # 將a的文字存入3.txt f=open("3.txt",'w') f.truncate() f.write(a) f.close() # 通過ekho語音合成為3.wav並播放 os.system('ekho -f 3.txt -o 3.wav') os.system('mplayer 3.wav') print "wait..1s" time.sleep(1)

總結

相對來說程式碼並不複雜,主要是環境和軟體的配置較麻煩,最終實現的功能就是:

  • 執行後會聽到歡迎 “你好,歡迎使用智慧小車語音控制系統”
  • 每次在聽到”請指示“後說出自己的指令,通過語音識別被轉換為文字
  • 如果文字包含指令,則傳送給智慧小車
  • 無論文字是否包含指令,都會得到圖靈機器人的應答,並通過語音合成使我們能聽到