1. 程式人生 > >騰訊ai, 長語音識別 python呼叫程式碼

騰訊ai, 長語音識別 python呼叫程式碼

文件地址 難點在簽名 直接貼程式碼

# -*- coding: utf-8 -*-
import time

import urllib.parse
import urllib.request
import hashlib
import base64
import uuid

import requests
import urllib
import json


def md5(string):
    md = hashlib.md5()
    md.update(string.encode('utf-8'))
    md5 = md.hexdigest().upper()
    return md5


def urlencode(args):
    tuples = [(k, args[k]) for k in sorted(args.keys()) if args[k]]
    query_str = urllib.parse.urlencode(tuples)
    return query_str


def signify(args, app_key):
    query_str = urlencode(args)
    query_str = query_str + '&app_key=' + app_key
    signiture = md5(query_str)
    return signiture


def http_post(api_url, args):
    resp = requests.post(url=api_url, data=args)
    resp = json.loads(resp.text)
    return resp


def main():
    f = open("C:/Users/raymon/Desktop/語音識別/yy1.wav", 'rb')
    file_content = f.read()
    base64_audio = base64.b64encode(file_content)
    url = 'https://api.ai.qq.com/fcgi-bin/aai/aai_wxasrlong'
    uuidstr = uuid.uuid4().hex

    body = {
        'app_id': 'appid',
        'format': '1',
        'callback_url': 'http://4c2216ef.ngrok.io/callback',
        'speech': base64_audio,
        'time_stamp': str(time.time()),
        'nonce_str': uuidstr,
        'sign': ''
    }
    body['sign'] = signify(body, 'appkey')
    resp = http_post(url, body)
    f.close()
    print(resp)


if __name__ == '__main__':
    main()