当语音助手遇到机器人
当语音帮手遇到机器人
大家都知道现在智能手机都有语音帮手,Mac 有 Siri,Windows 有小冰,那么这些语音帮手遇到机器人会产生哪些风趣的对话呢?这儿咱们请来了图灵机器人
和 Siri 语音帮手完成一次风趣的对话。
运用的到技术
图灵机器人运用的是文字输入而 Siri 运用的是语音输入,需求文字转语音和 Siri 截图辨认文字。下图为程序的大致流程:
运用到的技术有:
- 图灵机器人的 API 接口
- 百度 AI敞开渠道的语音组成接口、OCR文字辨认接口
- ImageGrab 截图
- 文件传输
导入的模块有:
from PIL import ImageGrabimport requestsimport base64from urllib.parse import quote_plus
图灵机器人 API
图灵机器人登录后创立机器人并仿制 apiKey。
咱们运用 API V2.0接入,接口的地址是:
http://openapi.tuling123.com/openapi/api/v2,用 POST 方法请求,这儿运用文本方法向机器人发送音讯。
def tuling(): """ 图灵机器人 :return: """ host = 'http://openapi.tuling123.com/openapi/api/v2' data = { "reqType":0, # 输入类型:0-文本(默许)、1-图片、2-音频 "perception": { # 输入信息 "inputText": { # 文本信息 "text": "嗨,Siri!" } }, "userInfo": { # 用户信息 "apiKey": "8d78a28535c947e69c2ddbcc5efeed51", "userId": "1234567" } } response = requests.post(host, json=data) if response: return response.json()['results'][0]['values']['text'] else: return '错误了!'
示例成果
[{'groupType': 1, 'resultType': 'text', 'values': {'text': '我的手机没有siri'}}]
语音在线组成
在百度 AI 敞开渠道中有一个在线语音组成,这款 API 能够将文本转换成音频,咱们这儿将图灵机器人返回的成果转成 MP3 格局音频。创立好百度运用之后仿制 api_key 和 secret_key 获取 token。
def fetch_token(): """ 获取token :return: token """ access_token = "" api_key = 'Pd4uoyvt1cwD7n2sHtLd5Ov0' secret_key = '8BnaPRcv3tTNa94eaFVfFy1pW2UkmvrO' token_url = 'http://openapi.baidu.com/oauth/2.0/token' params = {'grant_type': 'client_credentials', 'client_id': api_key, 'client_secret': secret_key} response = requests.post(token_url, post_data) if response: access_token = response.json()['access_token'] return access_token def synthesized_speech(text, token): """ 组成语音 """ # 保存的文件途径 mp3_path = "/Users/xx/Desktop/siri/result.mp3" tts_url = 'http://tsn.baidu.com/text2audio' tex = quote_plus(text) # 此处TEXT需求两次urlencode # tok:token,tex:文本,per:发音人,spd:语速(0-15)默许 5,pit:音调(0-15)默许 5, # vol:音量(0-9)默许 5,aue:下载的文件格局, 3 mp3(default)、4 pcm-16k、5 pcm-8k、6 wav, # cuid:用户唯一标识,lan ctp 固定参数 params = {'tok': token, 'tex': tex, 'per': 4, 'spd': 5, 'pit': 5, 'vol': 5, 'aue': 3, 'cuid': "pythonjishu", 'lan': 'zh', 'ctp': 1} response = requests.post(tts_url, params) if response: with open(mp3_path, 'wb') as of: of.write(response.content)
文件传输
在 Mac 上 Siri 和音频同时操作是行不通的,此时播映音频文件没有声响,咱们必须把音频文件传输到第二台电脑上。这时在命令行输入python3 -m http.server把当前电脑做一个简易的服务器。
python3 -m http.server
在第二台电脑浏览器上输入 http://IP地址:8000/,并找到音频文件播映。
http://IP地址:8000/http://192.168.1.102:8000/Desktop/siri/result.mp3
截图
当 Siri 听到声响后会在最终一行返回成果,咱们需求将 Siri 界面截图和辨认文字。
def grab_img(): """ 截图 :return: """ img_path = "/Users/xx/Desktop/siri/grab.png" # bbbox 的参数为截取屏幕的一部分,间隔左面像素,上边像素,右边像素,下边像素 img = ImageGrab.grab(bbox=(2630,80,3330,1290)) img.save(img_path) return img_path
文字辨认
将 Siri 截图后需求辨认最终一行文字,这儿运用百度 AI 敞开渠道的 OCR(还是和上面语音组成一样创立运用后仿制 api_key 和 secret_key)将这段成果传输给图灵机器人,一段对话就完成了。
def fench_ocr_token(): """ 获取ocr token :return: token """ api_key = 'ioE84jDQmGNLG7heN6rovF9Q' secret_key = 'qGVyAobVtCGKdD1Ncz60IvGMdf3dP1ct' access_token = "" url = 'https://aip.baidubce.com/oauth/2.0/token?grant_type=client_credentials&client_id='+api_key+'&client_secret='+secret_key response = requests.get(url) if response: access_token = response.json()['access_token'] return access_token def ocr(img_path, access_token): """ 通用文字辨认 :return: 辨认后的文字 """ text = "" request_url = "https://aip.baidubce.com/rest/2.0/ocr/v1/general_basic" # 二进制方法打开图片文件 f = open(img_path, 'rb') img = base64.b64encode(f.read()) params = {"image":img} request_url = request_url + "?access_token=" + access_token headers = {'content-type': 'application/x-www-form-urlencoded'} response = requests.post(request_url, data=params, headers=headers) if response: words_result = response.json()['words_result'] text = words_result[-1]['words'] return text
示例成果
最终一行取最终一个words [{'words': '股市'}, {'words': 'Apple公司的股票价格”'}, {'words': '时钟'}, {'words': '“柏林现在几点钟?”'}, {'words': '通讯录'}, {'words': '“秦葳住在哪里?”'}, {'words': '查找'}, {'words': '“我的 iPhone在哪里?'}, {'words': '备忘录'}, {'words': '“记下我午饭花了12块钱”'}, {'words': '网络搜索'}, {'words': '“上网查找北极熊”'}, {'words': '问与答'}, {'words': '公斤等于多少磅?”'}, {'words': '播客'}, {'words': '播映播客”'}, {'words': '暗码'}, {'words': '“显示我的暗码”'}, {'words': '嗨S'}, {'words': '嗨,Si很快乐见到你。'}]
总结
这个 Python 程序并不复杂只需求运用多个 API 接口。期望小伙伴们都能够放开思维写各种 Python 小程序练手。