In this article, we share an example of Raspberry Pi based voice dialog robot for your reference, the details are as follows
Part Icoding
arecord -D "plughw:1" -f S16_LE -r 16000 -d 3 /home/pi/Desktop/
Part IIcoding
# coding: utf-8 import sys import json import urllib2 import base64 import requests reload(sys) (“utf-8”) def get_access_token(): url = “/oauth/2.0/token” body = { “grant_type”:”client_credentials”, “client_id” :”Fill in your ownclient_id”, “client_secret”:”Fill in your ownclient_secret”, } r = (url,data=body,verify=True) respond = () return respond["access_token"] def yuyinshibie_api(audio_data,token): speech_data = base64.b64encode(audio_data).decode(“utf-8”) speech_length = len(audio_data) post_data = { “format” : “wav”, “rate” : 16000, “channel” : 1, “cuid” : “B8-27-EB-BA-24-14”, “token” : token, “speech” : speech_data, “len” : speech_length } url = "/server_api" json_data = (post_data).encode("utf-8") json_length = len(json_data) #print(json_data) req = (url, data=json_data) req.add_header("Content-Type", "application/json") req.add_header("Content-Length", json_length) #print("asr start request\n") resp = (req) #print("asr finish request\n") resp = () resp_data = (("utf-8")) if resp_data["err_no"] == 0: return resp_data["result"] else: print(resp_data) return None def asr_main(filename,tok): try: f = open(filename, “rb”) audio_data = () () resp = yuyinshibie_api(audio_data,tok) return resp[0] except Exception,e: print “e:”,e return “recognition failure”.encode(“utf-8”)
Part IIIcoding
# coding: utf-8 import requests import json import sys reload(sys) ("utf-8") def Tuling(words): Tuling_API_KEY = "Fill in your own Turling KEY here." body = {"key":Tuling_API_KEY,"info":("utf-8")} url = "http:///openapi/api" r = (url,data=body,verify=True) if r: date = () print date["text"] return date["text"] else: return None
Part IVcoding
# coding: utf-8 import sys import urllib2 import json import os import yuyinshibie reload(sys) ("utf-8") def yuyinhecheng_api(tok,tex): cuid = "B8-27-EB-BA-24-14" spd = "4" url = "/text2audio?tex="+tex+"&lan=zh&cu&ctp=1&tok="+tok+"&per=3" #print url #response = (url) #date = () return url def tts_main(filename,words,tok): voice_date = yuyinhecheng_api(tok,words) f = open(filename,"wb") (voice_date) ()
Part Vcoding
# coding: utf-8 import os import time import yuyinhecheng import Turling import yuyinshibie tok = yuyinshibie.get_access_token() switch = True while switch: ('sudo arecord -D "plughw:1" -f S16_LE -r 16000 -d 3 /home/pi/Desktop/') (0.5) info = yuyinshibie.asr_main("/home/pi/Desktop/",tok) if 'Close'.encode("utf-8") in info: while True: ('sudo arecord -D "plughw:1" -f S16_LE -r 16000 -d 10 /home/pi/Desktop/') (10) info = yuyinshibie.asr_main("/home/pi/Desktop/",tok) if 'Open'.encode("utf-8") in info: break url = "/text2audio?tex=Open successfully&lan=zh&cuid=B8-27-EB-BA-24-14&ctp=1&tok="+tok+"&per=3" ('mpg123 "%s"'%url) elif 'Pause'.encode("utf-8") in info: url = "/text2audio?tex=pause (in theater)&lan=zh&cuid=B8-27-EB-BA-24-14&ctp=1&tok="+tok+"&per=3" ('mpg123 "%s"'%url) (10) url = "/text2audio?tex=End of pause&lan=zh&cuid=B8-27-EB-BA-24-14&ctp=1&tok="+tok+"&per=3" ('mpg123 "%s"'%url) continue else: tex = (info) url = yuyinhecheng.yuyinhecheng_api(tok,tex) ('mpg123 "%s"'%url) (0.5)
This is the entire content of this article.