百度地图开放平台链接:http://lbsyun.baidu.com/index.php?title=%E9%A6%96%E9%A1%B5
百度AI开放平台链接:https://ai.baidu.com/
轻量路径规划服务调用方法:http://api.map.baidu.com/directionlite/v1/walking?origin=40.01116,116.339303&destination=39.936404,116.452562&ak=您的AK //GET请求
其中:origin代表起点的经纬度坐标,destination代表终点的经纬度坐标,ak是你申请的应用的AK码
建立语音识别的python的SDK客户端AipSpeech:
首先需要安装百度AI的支持库:
pip install baidu-aip
参考代码如下:
from aip import AipSpeech
""" 你的 APPID AK SK """
APP_ID = '你的 App ID'
API_KEY = '你的 Api Key'
SECRET_KEY = '你的 Secret Key'
client = AipSpeech(APP_ID, API_KEY, SECRET_KEY)
Getlnglat.py文件:
import json
from urllib.request import urlopen, quote
def getlnglat(address):
url = 'http://api.map.baidu.com/geocoder/v2/'
output = 'json'
ak = 'XbtjUyRm1jSCb1njXOmLxs8ibCN4I3v7' # 浏览器端密钥
address = quote(address) # 由于本文地址变量为中文,为防止乱码,先用quote进行编码
uri = url + '?' + 'address=' + address + '&output=' + output + '&ak=' + ak
req = urlopen(uri)
res = req.read().decode()
temp = json.loads(res)
lat = temp['result']['location']['lat']
lng = temp['result']['location']['lng']
return lat, lng
TextToMp3.py文件:
from aip import AipSpeech
def TTM(text):
APP_ID = '16170864'#引号之间填写之前在ai平台上获得的参数
API_KEY = 'SFERXsjoH7khGN30AGdbQiSG'#如上
SECRET_KEY = '5G1rdwEuNPbCTdtILWaTRl5mTyNUaPAL'#如上
client = AipSpeech(APP_ID, API_KEY, SECRET_KEY)
result = client.synthesis(text, 'zh', 1, { 'vol': 8,'per':0,'spd':2 })
'''
固定值zh。语言选择,目前只有中英文混合模式,填写固定值zh
客户端类型选择,web端填写固定值1
spd语速,取值0-15,默认为5中语速(选填)
pit音调,取值0-15,默认为5中语调(选填)
vol音量,取值0-15,默认为5中音量(选填)
per发音人选择, 0为普通女声,1为普通男生,3为情感合成-度逍遥,4为情感合成-度丫丫,默认为普通女声
'''
#识别正确返回语音二进制 错误则返回dict 参照下面错误码
if not isinstance(result, dict):
with open('test.mp3', 'wb') as f:
f.write(result)
pip install pygame
详细描述请参考:http://lbsyun.baidu.com/index.php?title=webapi/directionlite-v1
BaiDuRoute.py文件:
import json
import re
from urllib.request import urlopen
from Getlnglat import getlnglat
from TextToMp3 import TTM
from pygame import mixer
import time
#获取当前位置经纬度
A=getlnglat('重庆市沙坪坝欣阳广场越界影院')
#获取目的地经纬度
B=getlnglat('重庆市沙坪坝三峡广场地铁站')
#调用api的url
ak='XbtjUyRm1jSCb1njXOmLxs8ibCN4I3v7'
u1='http://api.map.baidu.com/directionlite/v1/walking?'
origin='origin='+str(A[0])+','+str(A[1])
destination='&destination='+str(B[0])+','+str(B[1])
AK='&ak='+ak
url=u1+origin+destination+AK
b = urlopen(url)
c=b.read()
result = json.loads(c)
#对instruction路段描述文本进行提取
S1=result['result']['routes'][0]
text1=S1['steps'][0]['instruction']
text=re.sub('[<>/b]','',text1)
#输出方向
print(text)
#执行文本转语音
TTM(text)
#播放MP3
mixer.init()
track=mixer.music.load('test.mp3')
mixer.music.play()
time.sleep(10)
mixer.music.stop()
mixer.quit()