上周分享了一个口译机器人的专案,本篇就详细介绍一下如何透过Python 使用 Google Text Translation 文字翻译服务,将文字讯息传送给Google,并透过 Translate 服务来进行文字讯息的转换。注本文中参考使用的硬件平台是Linkit Smart 7688。
材料准备
1.准备一片 Linkit Smart7688 开发板
2.将 Linkit Smart 7688 连接至计算机
3.更新 Firmware 为 0.9.3
https://docs.labs.mediatek.com/resource/linkit-smart-7688/en/downloads
4.将 USB 声卡透过 OTGCable 安装于 Linkit Smart 7688
5.安装 MIC 与 Speaker 于外接 USB 声卡上
6.安装 USB 声卡相关套件
Linkit Smart 7688 与 USB 外接声卡连接图
Google 端
Step 1. 到 Google 网站申请账号
https://cloud.google.com/
Step 2. 登入 Google CloudTranslate API 网站
https://cloud.google.com/translate/
Step 3. 点击 VIEW MY CONSOLE
Step 3. 建立项目
Step 4. 输入 项目名称
Step 5. 点击 使用 Google API
Step 6. 点击 启用 API
Step 7. 搜寻 Translate
Step 8. 点击 启用
Step 9. 点击左边 凭证
Step 10. 点击 建立凭证
Step 11. 选择 API 密钥
Linkit Smart 7688 端
Step 1. SSH 进入 Linkit Smart 7688 中
Step 2. 于 Linkit Smart 7688中建立 GoogleTranslate 的 Python Code
import json
import requests
from requests.packages.urllib3.exceptionsimport InsecureRequestWarning
requests.packages.urllib3.disable_warnings(InsecureRequestWarning)
googleAPIHost =”https://www.googleapis.com”
key =”AIzaSyC7QHmUMZPSOa_ggF3x86VdwXWEjOq-eAM”
queryString = “hello”
source = “en”
target = “zh-CN”
requestURL = googleAPIHost +”/language/translate/v2?key=” + key + “&source=” +source + “&target=” + target + “&q=” + queryString
print “requestURL: ” + requestURL
response = requests.get(requestURL,verify=False)
print response.status_code
print response.text
decodejson = json.loads(response.text)
print “n” +decodejson["data"]["translations"][0]["translatedText"]
Step 3. 执行 Python Code
python google_Translate.py
完成,这样在Linkit Smart 7688就可以使用Google Text Translation文字翻译服务啦。