讯飞云 API 语音听写 python3 调用例程

Posted LexLuc

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了讯飞云 API 语音听写 python3 调用例程相关的知识,希望对你有一定的参考价值。

#!/usr/bin/python3
# -*- coding: UTF-8 -*-

import requests
import time
import gzip
import urllib
import json
import hashlib
import base64


def audio_dictation():
    """
    讯飞语音听写 API 调用例程
    注意:使用前需要在讯飞云控制台中的IP白名单中加入本机IP!
    参考:讯飞云官方 API 文档 https://doc.xfyun.cn/rest_api/语音听写.html
    """
    
    # 读取 APPID:
    with open(‘./appid‘, mode=‘r‘, encoding=‘ASCII‘) as appid_file:
        x_appid = appid_file.read()
    
    # 读取APIKey:
    with open(‘./apikey‘, mode=‘r‘, encoding=‘ASCII‘) as apikey_file:
        api_key = apikey_file.read()
    
    # API URL:
    url = ‘http://api.xfyun.cn/v1/service/v1/iat‘
    
    # 读取音频文件内容 (将 `zhngjb_clip.wav` 替换成需要上传的音频文件):
    with open(‘./zhngjb_clip.wav‘, mode=‘rb‘) as audio_file:
        file_content = audio_file.read()
    
    # 对音频文件进行 base64 音频编码:
    base64_audio = base64.b64encode(file_content)
    body = urllib.parse.urlencode({‘audio‘: base64_audio})
    
    # 采样率 16k,编码格式 未压缩:
    param = {"engine_type": "sms16k", "aue": "raw"} 
    
    # 构建 header:
    x_param = base64.b64encode(json.dumps(param).replace(‘ ‘, ‘‘).encode())
    x_time = str(int(int(round(time.time() * 1000)) / 1000))
    x_checksum = hashlib.md5(api_key.encode() + str(x_time).encode() + x_param).hexdigest()
    x_header = {‘X-Appid‘: x_appid,
                ‘X-CurTime‘: x_time,
                ‘X-Param‘: x_param,
                ‘X-CheckSum‘: x_checksum}
    
    req_header = {‘Content-Type‘: ‘application/x-www-form-urlencoded‘,
                  ‘charset‘: ‘utf-8‘}
    
    headers = {**req_header, **x_header}   
    
    # 发送请求:
    start = time.time()
    response = requests.post(url, headers=headers, data=body)
    duration = time.time() - start
    
    with open(‘result‘, mode=‘w‘, encoding=‘utf-8‘) as result_file:
        print(f‘Request sent. Duration: {duration}s
‘
              f‘status code = {response.status_code}
‘
              f‘headers = {response.headers}
‘
              f‘content = {response.content.decode("utf-8")}‘, file=result_file)

if __name__ == ‘__main__‘:
    audio_dictation() 

以上是关于讯飞云 API 语音听写 python3 调用例程的主要内容,如果未能解决你的问题,请参考以下文章

[百度AI+讯飞云+socket通信]语音识别控制硬件点亮LED灯

Unity调用讯飞做语音听写(Android Studio版)

一个超简单的语音识别编程,听写程序

vue+科大讯飞语音听写功能(解决针对vue new Worker报错问题)

一个超简单的语音识别编程,听写程序

语音听写就要用科大讯飞 语音听写都来尝试一下啊