# coding=utf-8
# pip install baidu-aip
from aip import AipSpeech

APP_ID = '9953283'
API_KEY = 'p3grxzGIU55uApjDO56vXVNE'
SECRET_KEY = 'sEFXAWrwPDackWSqo4tCsTtNNZlRlBiB'

# 初始化AipSpeech对象
aipSpeech = AipSpeech(APP_ID, API_KEY, SECRET_KEY)


def hecheng(txt):   #语音合成
    result = aipSpeech.synthesis(txt, 'zh', 1, {
    'vol': 5,
    })

    # 识别正确返回语音二进制 错误则返回dict 参照下面错误码
    if not isinstance(result, dict):
        with open('audio.wav', 'wb') as f:
            f.write(result)
    else:
        print result


def shibie(filePath, types='amr', rate=16000): #语音识别
    with open(filePath, 'rb') as fp:
        file = fp.read()
    asr_result = aipSpeech.asr(file, types, rate, {
        'lan': 'zh',
    })
    if asr_result['err_msg'] == "success.":
        # for i in asr_result['result']:
        #     print i
        print asr_result['result'][0]
    else:
        print asr_result

hecheng('你好')

results matching ""

    No results matching ""