_php tutorial for Implementing a dialogue robot with Raspberry Pi

Source: Internet
Author: User

Implementing a dialogue robot with Raspberry Pi


Recently, we have implemented a robot that can talk to people with Raspberry Pi, a brief introduction.

Raspberry Pi is the world's most popular micro-computer motherboard, is the lead product of open source hardware, it is designed for students ' computer programming education, only credit card size, and low price. Support for operating systems such as Linux (Debian). The most important thing is that the information is perfect and the community is active.
I use the Raspberry Pi B + version, the basic configuration is the Broadcom BCM2836 processor, 4 core 900M clock, 1G RAM.

My goal is to make a robot that talks to people, which requires robots to have input devices and output devices. The input device is a microphone, the output can be HDMI, headphones or stereo, I use the stereo here. Here is a picture of my Raspberry Pi. 4 USB ports are connected with wireless network card, wireless keyboard, microphone, audio power supply respectively.


We can divide the robot conversation into three parts: Listen, think, say.
"Listen" is to write down what people say, and convert it into words.
"Thinking" is to give different outputs based on different inputs. For example, the other person said "now time", you can answer "now is Beijing time xx point xx points."
"To say" is to convert the text into speech and play it out.

These three parts involve a lot of speech recognition, speech synthesis, artificial intelligence and other technologies, these are to spend a lot of time and effort to study, fortunately, some companies have opened the interface for customers to use. Here, I chose the API of Baidu. The implementation of these three sections is described below.

Listen

The first thing is to record what people say, and I use the Arecord tool. The command is as follows:
    1. Arecord-d "Plughw:1"-F s16_le-r 16000 Test.wav
Where the-d parameter is followed by the recording device, after the microphone is connected, there are 2 devices on the Raspberry Pi: internal devices and external USB devices, and the plughw:1 represents the use of external devices. -F indicates the recording format,-R indicates the sound sampling frequency. Because the following mentioned Baidu speech recognition on the audio file format is required, we need to record into a format that meets the requirements. Also, I don't specify a recording time here, it will keep recording until the user presses CTRL-C. The recorded audio file is saved as test.wav.
Next, we want to convert the audio into text, namely speech recognition (ASR), Baidu's voice open platform provides free services and supports rest API
See document: HTTP://YUYIN.BAIDU.COM/DOCS/ASR/57
The basic process is to obtain tokens, the need to identify the voice information, voice data, tokens, etc. sent to the voice recognition server Baidu, you can obtain the corresponding text. Because the server supports rest APIs, we can implement the client code in any language, using Python
 
 
  1. # Coding:utf-8

  2. Import Urllib.request
  3. Import JSON
  4. Import Base64
  5. Import Sys

  6. Def get_access_token ():
  7. url = "Https://openapi.baidu.com/oauth/2.0/token"
  8. Grant_type = "Client_credentials"
  9. client_id = "Xxxxxxxxxxxxxxxxxx"
  10. Client_secret = "Xxxxxxxxxxxxxxxxxxxxxx"

  11. url = url + "?" + "grant_type=" + Grant_type + "&" + "client_id=" + client_id + "&" + "client_secret=" + client_se Cret

  12. resp = Urllib.request.urlopen (URL). Read ()
  13. data = Json.loads (Resp.decode ("Utf-8"))
  14. return data["Access_token"]


  15. def BAIDU_ASR (data, ID, token):
  16. Speech_data = Base64.b64encode (data). Decode ("Utf-8")
  17. Speech_length = Len (data)

  18. Post_data = {
  19. "Format": "WAV",
  20. "Rate": 16000,
  21. "Channel": 1,
  22. "CUiD": ID,
  23. "Token": token,
  24. "Speech": Speech_data,
  25. "Len": Speech_length
  26. }

  27. url = "Http://vop.baidu.com/server_api"
  28. Json_data = Json.dumps (post_data). Encode ("Utf-8")
  29. Json_length = Len (json_data)
  30. #print (Json_data)

  31. req = urllib.request.Request (url, data = Json_data)
  32. Req.add_header ("Content-type", "Application/json")
  33. Req.add_header ("Content-length", Json_length)

  34. Print ("ASR start request\n")
  35. RESP = Urllib.request.urlopen (req)
  36. Print ("ASR Finish request\n")
  37. RESP = Resp.read ()
  38. Resp_data = Json.loads (Resp.decode ("Utf-8"))
  39. If resp_data["err_no"] = = 0:
  40. return resp_data["Result"]
  41. Else
  42. Print (Resp_data)
  43. Return None

  44. def asr_main (filename):
  45. f = open (filename, "RB")
  46. Audio_data = F.read ()
  47. F.close ()

  48. #token = Get_access_token ()
  49. token = "Xxxxxxxxxxxxxxxxxx"
  50. UUID = "xxxx"
  51. RESP = Baidu_asr (Audio_data, uuid, token)
  52. Print (Resp[0])
  53. return resp[0]


Thinking
Here I use the Baidu API store Turing device person. See the documentation for: http://apistore.baidu.com/apiworks/servicedetail/736.html
It's very simple to use, no longer repeat here, the code is as follows:
 
 
  1. Import Urllib.request
  2. Import Sys
  3. Import JSON

  4. def robot_main (words):
  5. url = "Http://apis.baidu.com/turing/turing/turing?"

  6. Key = "879a6cb3afb84dbf4fc84a1df2ab7319"
  7. UserID = "1000"

  8. Words = urllib.parse.quote (words)
  9. url = url + "key=" + key + "&info=" + words + "&userid=" + userid

  10. req = urllib.request.Request (URL)
  11. Req.add_header ("Apikey", "xxxxxxxxxxxxxxxxxxxxxxxxxx")

  12. Print ("Robot start Request")
  13. RESP = Urllib.request.urlopen (req)
  14. Print ("Robot Stop Request")
  15. Content = Resp.read ()
  16. If content:
  17. data = Json.loads (Content.decode ("Utf-8"))
  18. Print (data["text"])
  19. return data["text"]
  20. Else
  21. Return None


Said
First, you need to convert the text into speech, which is speech synthesis (TTS). Then play the sound out.
Baidu's voice open platform provides a TTS interface, and can be configured for male and female voice, intonation, speed, volume. The server returns audio data in MP3 format. We write the data in a binary way into the file.
See http://yuyin.baidu.com/docs/tts/136
The code is as follows:
 
 
  1. # Coding:utf-8

  2. Import Urllib.request
  3. Import JSON
  4. Import Sys

  5. def baidu_tts_by_post (data, ID, token):
  6. Post_data = {
  7. "Tex": Data,
  8. "LAN": "ZH",
  9. "CTP": 1,
  10. "CUiD": ID,
  11. "Tok": Token,
  12. }

  13. url = "Http://tsn.baidu.com/text2audio"
  14. Post_data = Urllib.parse.urlencode (post_data). Encode (' Utf-8 ')
  15. #print (Post_data)
  16. req = urllib.request.Request (url, data = Post_data)

  17. Print ("TTS start Request")
  18. RESP = Urllib.request.urlopen (req)
  19. Print ("TTS finish Request")
  20. RESP = Resp.read ()
  21. Return RESP

  22. def tts_main (filename, words):
  23. token = "Xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
  24. Text = Urllib.parse.quote (words)
  25. UUID = "xxxx"
  26. RESP = baidu_tts_by_post (text, uuid, token)

  27. f = open ("Test.mp3", "WB")
  28. F.write (RESP)
  29. F.close ()

After you get the audio file, you can play it using the mpg123 player.
    1. mpg123 Test.mp3


Integration
Finally, put together these three parts.
You can first integrate Python-related code into main.py, as follows:
 
  
  
  1. Import ASR
  2. Import TTS
  3. Import Robot

  4. Words = Asr.asr_main ("Test.wav")
  5. New_words = Robot.robot_main (words)

Then use the script to invoke the relevant tool:
    1. #! /bin/bash
    2. Arecord-d "Plughw:1"-F s16_le-r 16000 Test.wav
    3. Python3 main.py
    4. mpg123 Test.mp3


Okay, now you can talk to the robot. Run the script, say a word to the microphone, then press CTRL-C, and the robot will return your words.

http://www.bkjia.com/PHPjc/1108027.html www.bkjia.com true http://www.bkjia.com/PHPjc/1108027.html techarticle using Raspberry Pi to implement a dialogue robot recently implemented a robot that can talk to people with a Raspberry Pi, a brief introduction. Raspberry Pi is the most popular miniature computer in the world Raspberry ...

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.