Raspberry Pi makes voice dialogue robot

1. Material preparation

1. A Raspberry Pi
2. Driver-free USB microphone
3. Headphones

2. Start of the tutorial

1. Recording

Plug in the microphone
I used arecord* to test whether the microphone can be used.
Use the recording to enter the following command

arecord -D "plughw:1" -f S16_LE -r 16000 -d 3 /home/pi/Desktop/voice.wav

2. Voice recognition

Create a voice recognition folder and enter the following program

sudo nano yuyinshibie.py

Go to the official website of Baidu Voice to apply for voice recognition,
img
Insert picture description here
write the code in, and write the ID and secret applied above into the red area below

\# coding: utf-8

import sys 
 import json 
 import urllib2 
 import base64 
 import requests

reload(sys) 
 sys.setdefaultencoding(“utf-8”)

def get_access_token(): 
 url = “https://openapi.baidu.com/oauth/2.0/token” 
 body = { 
 “grant_type”:”client_credentials”, 
 “client_id” :”此处填写自己的client_id”, 
 “client_secret”:”此处填写自己的client_secret”, 
 }

r = requests.post(url,data=body,verify=True)

respond = json.loads(r.text)

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 = "http://vop.baidu.com/server_api"

json_data = json.dumps(post_data).encode("utf-8")

json_length = len(json_data)

\#print(json_data)

 

req = urllib2.Request(url, data=json_data)

req.add_header("Content-Type", "application/json")

req.add_header("Content-Length", json_length)

 

\#print("asr start request\n")

resp = urllib2.urlopen(req)

\#print("asr finish request\n")

resp = resp.read()

resp_data = json.loads(resp.decode("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 = f.read() 
 f.close() 
 resp = yuyinshibie_api(audio_data,tok) 
 return resp[0] 
 except Exception,e: 
 print “e:”,e 
 return “识别失败”.encode(“utf-8”)

The code icon is indented according to the figure
img
Insert picture description here

img

img

img
After the recognition is complete, we are about to start the third step. We have to talk to the robot so it must reply to us, right. In order to be smart, we used the Turing interface. Turing is really very easy to use. It can check the weather. Voice tells stories and tells jokes. The code for the third step is attached below.

3. Turing reply

(1) Go to Turing Robot's official website to register and create a WeChat robot
Insert picture description here

(2) Create Turing robot file input code

sudo nano Turling.py

(3) Write the code, write the API KEY you applied for in red

\# coding: utf-8

 
import requests

import json

import sys

reload(sys)

sys.setdefaultencoding("utf-8")

 

 

def Tuling(words):

  Tuling_API_KEY = "此处填写自己的Turling KEY"

 

  body = {"key":Tuling_API_KEY,"info":words.encode("utf-8")}

 

  url = "http://www.tuling123.com/openapi/api"

  r = requests.post(url,data=body,verify=True)

 

  if r:

​    date = json.loads(r.text)

​    print date["text"]

​    return date["text"]

  else:

​    return None

Change the code indentation according to the picture

img

4. Speech synthesis

After Turing responded, we used Baidu's speech synthesis to make it play.
(1) Create speech synthesis files

sudo nano yuyinhecheng.py

(2) Write code

\# coding: utf-8

import sys 
 import urllib2 
 import json 
 import os 
 import yuyinshibie

reload(sys) 
 sys.setdefaultencoding(“utf-8”)

def yuyinhecheng_api(tok,tex): 
 cuid = “B8-27-EB-BA-24-14” 
 spd = “4” 
 url = “http://tsn.baidu.com/text2audio?tex=“+tex+”&lan=zh&cuid=”+cuid+”&ctp=1&tok=”+tok+”&per=3” 
 \#print url 
 \#response = requests.get(url) 
 \#date = response.read() 
 return url

def tts_main(filename,words,tok): 
 voice_date = yuyinhecheng_api(tok,words)

f = open(filename,"wb")

f.write(voice_date)

f.close()

Change the indent according to the picture
img

5. Play

After the speech synthesis, we want to play it out and use mpg123. Why would I use this because it can directly play the audio on the web page, which is very easy to use

** Install mpg123**:

sudo apt-get install mpg123 

After the installation is complete, I will use it later and talk about how to use it. Now let’s not talk about it. Now
that the tool codes needed for recording, speech recognition, speech synthesis and playback are ready, let’s start to integrate

6. Integration

Create the final file

sudo nano yuyin.py

write the code

\# coding: utf-8

 

import os

import time

import yuyinhecheng

import Turling

import yuyinshibie

tok = yuyinshibie.get_access_token()

switch = True

while switch:

  os.system('sudo arecord -D "plughw:1" -f S16_LE -r 16000 -d 3 /home/pi/Desktop/voice.wav')

  time.sleep(0.5)

  info = yuyinshibie.asr_main("/home/pi/Desktop/voice.wav",tok)

  if '关闭'.encode("utf-8") in info:

​    while True:

​      os.system('sudo arecord -D "plughw:1" -f S16_LE -r 16000 -d 10 /home/pi/Desktop/voice.wav')

​      time.sleep(10)


​      info = yuyinshibie.asr_main("/home/pi/Desktop/voice.wav",tok)

​      if '开启'.encode("utf-8") in info:

​        break


​    url = "http://tsn.baidu.com/text2audio?tex=开启成功&lan=zh&cuid=B8-27-EB-BA-24-14&ctp=1&tok="+tok+"&per=3"

​    os.system('mpg123 "%s"'%url)


  elif '暂停'.encode("utf-8") in info:

​    url = "http://tsn.baidu.com/text2audio?tex=开始暂停&lan=zh&cuid=B8-27-EB-BA-24-14&ctp=1&tok="+tok+"&per=3"

​    os.system('mpg123 "%s"'%url)

​    time.sleep(10)
​    url = "http://tsn.baidu.com/text2audio?tex=暂停结束&lan=zh&cuid=B8-27-EB-BA-24-14&ctp=1&tok="+tok+"&per=3"

​    os.system('mpg123 "%s"'%url)

​    continue
  else:
​    tex = Turling.Tuling(info)

​    url = yuyinhecheng.yuyinhecheng_api(tok,tex)

​    os.system('mpg123 "%s"'%url)

​    time.sleep(0.5)

Change the indent according to the picture

img

7. Run

Finally you can run the robot

Enter code

sudo python yuyin.py

You can talk to the robot

Guess you like

Origin blog.csdn.net/qq_41676577/article/details/112856555