- # coding: utf-8
- import urllib.request
- import json
- import base64
- import sys
- def get_access_token():
- url = "https://openapi.baidu.com/oauth/2.0/token"
- grant_type = "client_credentials"
- client_id = "xxxxxxxxxxxxxxxxxx"
- client_secret = "xxxxxxxxxxxxxxxxxxxxxx"
- url = url + "?" + "grant_type=" + grant_type + "&" + "client_id=" + client_id + "&" + "client_secret=" + client_secret
- resp = urllib.request.urlopen(url).read()
- data = json.loads(resp.decode("utf-8"))
- return data["access_token"]
- def baidu_asr(data, id, token):
- speech_data = base64.b64encode(data).decode("utf-8")
- speech_length = len(data)
- post_data = {
- "format" : "wav",
- "rate" : 16000,
- "channel" : 1,
- "cuid" : id,
- "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 = urllib.request.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 = urllib.request.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):
- f = open(filename, "rb")
- audio_data = f.read()
- f.close()
- #token = get_access_token()
- token = "xxxxxxxxxxxxxxxxxx"
- uuid = "xxxx"
- resp = baidu_asr(audio_data, uuid, token)
- print(resp[0])
- return resp[0]
- import urllib.request
- import sys
- import json
- def robot_main(words):
- url = "http://apis.baidu.com/turing/turing/turing?"
- key = "879a6cb3afb84dbf4fc84a1df2ab7319"
- userid = "1000"
- words = urllib.parse.quote(words)
- url = url + "key=" + key + "&info=" + words + "&userid=" + userid
- req = urllib.request.Request(url)
- req.add_header("apikey", "xxxxxxxxxxxxxxxxxxxxxxxxxx")
- print("robot start request")
- resp = urllib.request.urlopen(req)
- print("robot stop request")
- content = resp.read()
- if content:
- data = json.loads(content.decode("utf-8"))
- print(data["text"])
- return data["text"]
- else:
- return None
- # coding: utf-8
- import urllib.request
- import json
- import sys
- def baidu_tts_by_post(data, id, token):
- post_data = {
- "tex" : data,
- "lan" : "zh",
- "ctp" : 1,
- "cuid" : id,
- "tok" : token,
- }
- url = "http://tsn.baidu.com/text2audio"
- post_data = urllib.parse.urlencode(post_data).encode('utf-8')
- #print(post_data)
- req = urllib.request.Request(url, data = post_data)
- print("tts start request")
- resp = urllib.request.urlopen(req)
- print("tts finish request")
- resp = resp.read()
- return resp
- def tts_main(filename, words):
- token = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
- text = urllib.parse.quote(words)
- uuid = "xxxx"
- resp = baidu_tts_by_post(text, uuid, token)
- f = open("test.mp3", "wb")
- f.write(resp)
- f.close()
- import asr
- import tts
- import robot
- words = asr.asr_main("test.wav")
- new_words = robot.robot_main(words)
- tts.tts_main("test.mp3", new_words)
http://www.bkjia.com/PHPjc/1108027.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/1108027.htmlTechArticle用树莓派实现对话机器人 最近用树莓派实现了一个能和人对话的机器人,简要介绍一下。 树莓派(Raspberry Pi)是世界上最流行的微型电脑...