如何用python调用百度语音识别
发布网友
发布时间:2022-04-22 15:14
我来回答
共2个回答
懂视网
时间:2022-04-11 08:24
百度云文字识别:
1,注册账户:https://login.bce.baidu.com
2,创建应用后点击应用就可以看到AK(API Key)和SK(Secret Key)
一、用python脚本获取access_token:
二、用工具Postman获取access_token:
百度文字识别调用方式文档地址:https://cloud.baidu.com/doc/OCR/s/Ck3h7y2ia
1,Postman是一个比较给力的Http请求模拟工具,可以快速进行接口调用。
2,输入HTTP请求的几个部分:
1,请求的method:选择POST
2,填写URL
3,Params:
grant_type: 必须参数,固定为client_credentials;
client_id: 必须参数,应用的API Key;
client_secret: 必须参数,应用的Secret Key
获取ccess_token后,postman中进行接口调用,需要输入的其他2个参数:
1,填写请求头(Headers)
Key栏输入:Content-Type
Value栏输入:application/x-www-form-urlencoded
2,输入请求参数(body)
先选择“x-www-form-urlencoded”,
access_token = xxx
把之前获取到的token字符串填到这里来
image = xxx
把图片转成base64字符串填到这里,转码工具:https://www.css-js.com/tools/base64.html
url = xxx
也可以不用传图片而是传一个图片的链接。
language_type = CHN_ENG
识别语言类型。默认中英。
用python或者使用工具Postman来,获取百度云文字识别中的access_token
标签:encode 输入 cat 必须 cli tool base64 nbsp ati
热心网友
时间:2022-04-11 05:32
#!/usr/bin/env python
# -*- coding: utf-8 -*-
########################################################################
#
# Copyright (c) 2017 aibot.me, Inc. All Rights Reserved
#
########################################################################
"""
File: util_voice.py
Author: darrenwang(darrenwang@aibot.me)
Date: 2017/03/24 11:29:50
Brief:
"""
import sys
import json
import time
import base64
import urllib
import urllib2
import requests
class BaiRest:
def __init__(self, cu_id, api_key, api_secert):
self.token_url = "https://openapi.baidu.com/oauth/2.0/token?grant_type=client_credentials&client_id=%s&client_secret=%s"
self.getvoice_url = "http://tsn.baidu.com/text2audio?tex=%s&lan=zh&cuid=%s&ctp=1&tok=%s"
self.upvoice_url = 'http://vop.baidu.com/server_api'
self.cu_id = cu_id
self.get_token(api_key, api_secert)
return
def get_token(self, api_key, api_secert):
token_url = self.token_url % (api_key,api_secert)
r_str = urllib2.urlopen(token_url).read()
token_data = json.loads(r_str)
self.token_str = token_data['access_token']
return True
#语音合成
def text2audio(self, text, filename):
get_url = self.getvoice_url % (urllib2.quote(text), self.cu_id, self.token_str)
voice_data = urllib2.urlopen(get_url).read()
voice_fp = open(filename,'wb+')
voice_fp.write(voice_data)
voice_fp.close()
return True
##语音识别
def audio2text(self, filename):
data = {}
data['format'] = 'wav'
data['rate'] = 8000
data['channel'] = 1
data['cuid'] = self.cu_id
data['token'] = self.token_str
wav_fp = open(filename,'rb')
voice_data = wav_fp.read()
data['len'] = len(voice_data)
#data['speech'] = base64.b64encode(voice_data).decode('utf-8')
data['speech'] = base64.b64encode(voice_data).replace('\n', '')
#post_data = json.mps(data)
result = requests.post(self.upvoice_url, json=data, headers={'Content-Type': 'application/json'})
data_result = result.json()
print data_result
return data_result['result'][0]
def test_voice():
api_key = "SrhYKqzl3SE1URnAEuZ0FKdT"
api_secert = "hGqeCkaMPb0ELMqtRGc2VjWdmjo7T89d"
bdr = BaiRest("test_python", api_key, api_secert)
#生成
start = time.time()
bdr.text2audio("你好啊", "out.wav")
using = time.time() - start
print using
#识别
start = time.time()
#result = bdr.audio2text("test.wav")
#result = bdr.audio2text("weather.pcm")
using = time.time() - start
print using, result
return True
if __name__ == "__main__":
test_voice()