关键词:
@
0. 太长不看系列,直接使用
在1.2官网注册后拿到APISecret和APIKey,直接复制文章2.5demo代码,保存为real_time_audio_recognition.py,在命令行执行
python real_time_audio_recognition.py -client_secret=您的client_secret -client_id=您的client_id -file_path=test.wav --audio_format=wav --sample_rate=16000
使用中有任何问题,欢迎留言提问。
1. Python调用标贝科技语音识别websocket接口,实现语音转文字
1.1 环境准备:
Python 3
1.2 获取权限
标贝科技 https://ai.data-baker.com/#/index
填写邀请码fwwqgs,每日免费调用量还可以翻倍
1.2.1 登录
点击产品地址进行登录,支持短信、密码、微信三种方式登录。
1.2.2 创建新应用
登录后进入【首页概览】,各位开发者可以进行创建多个应用。包括一句话识别、长语音识别、录音文件识别;在线合成、离线合成、长文本合成。
1.2.3 选择服务
进入【已创建的应用】,左侧选择您需调用的AI技术服务,右侧展示对应服务页面概览(您可查询用量、管理套餐、购买服务量、自主获取授权、预警管理)。
1.2.4 获取Key&Secret
通过服务 / 授权管理,获取对应参数,进行开发配置(获取访问令牌token)
拿到Key和Secret就可以正式使用啦!
2. 代码实现
2.1 获取access_token
在拿到Key和Secret后,我们还需要调用授权接口获取access_token,这个access_token有效时长是24小时。
# 获取access_token用于鉴权
def get_access_token(client_secret, client_id):
grant_type = "client_credentials"
url = "https://openapi.data-baker.com/oauth/2.0/token?grant_type=&client_secret=&client_id=" \\
.format(grant_type, client_secret, client_id)
try:
response = requests.post(url)
response.raise_for_status()
except Exception as e:
print(response.text)
raise Exception
else:
access_token = json.loads(response.text).get(\'access_token\')
return access_token
2.2 准备数据
需要根据接口要求设置参数,并且对音频数据进行分割
# 准备数据
def prepare_data(args, access_token):
# 读取音频文件
with open(args.file_path, \'rb\') as f:
file = f.read()
# 填写Header信息
audio_format = args.audio_format
sample_rate = args.sample_rate
splited_data = [str(base64.b64encode(file[i:i + 5120]), encoding=\'utf-8\') for i in range(0, len(file), 5120)]
asr_params = "audio_format": audio_format, "sample_rate": int(sample_rate), "speech_type": 1
json_list = []
for i in range(len(splited_data)):
if i != len(splited_data) - 1:
asr_params[\'req_idx\'] = i
else:
asr_params[\'req_idx\'] = -len(splited_data) + 1
asr_params["audio_data"] = splited_data[i]
data = "access_token": access_token, "version": "1.0", "asr_params": asr_params
json_list.append(json.dumps(data))
return json_list
2.3 配置接口参数
client_secret和client_id:在文章1.2的官网获取,必填
file_save_path:文件保存路径,必填
audio_format:音频类型,默认wav格式
sample_rate:采样率,默认16000Hz
# 获取命令行输入参数
def get_args():
parser = argparse.ArgumentParser(description=\'ASR\')
parser.add_argument(\'-client_secret\', type=str, required=True)
parser.add_argument(\'-client_id\', type=str, required=True)
parser.add_argument(\'-file_path\', type=str, required=True)
parser.add_argument(\'--audio_format\', type=str, default=\'wav\')
parser.add_argument(\'--sample_rate\', type=str, default=\'16000\')
args = parser.parse_args()
return args
2.4 建立websocket客户端
class Client:
def __init__(self, data, uri):
self.data = data
self.uri = uri
#建立连接
def connect(self):
ws_app = websocket.WebSocketApp(uri,
on_open=self.on_open,
on_message=self.on_message,
on_error=self.on_error,
on_close=self.on_close)
ws_app.run_forever()
# 建立连接后发送消息
def on_open(self, ws):
print("sending..")
for i in range(len(self.data)):
ws.send(self.data[i])
# 接收消息
def on_message(self, ws, message):
code = json.loads(message).get("code")
if code != 90000:
# 打印接口错误
print(message)
if json.loads(message).get(\'end_flag\') == 1:
print(json.loads(message).get(\'asr_text\'))
# 打印错误
def on_error(slef, ws, error):
print("error: ", str(error))
# 关闭连接
def on_close(ws):
print("client closed.")
2.5 完整demo
import argparse
import json
import base64
import requests
import websocket
class Client:
def __init__(self, data, uri):
self.data = data
self.uri = uri
#建立连接
def connect(self):
ws_app = websocket.WebSocketApp(uri,
on_open=self.on_open,
on_message=self.on_message,
on_error=self.on_error,
on_close=self.on_close)
ws_app.run_forever()
# 建立连接后发送消息
def on_open(self, ws):
print("sending..")
for i in range(len(self.data)):
ws.send(self.data[i])
# 接收消息
def on_message(self, ws, message):
code = json.loads(message).get("code")
if code != 90000:
# 打印接口错误
print(message)
if json.loads(message).get(\'end_flag\') == 1:
print(json.loads(message).get(\'asr_text\'))
# 打印错误
def on_error(slef, ws, error):
print("error: ", str(error))
# 关闭连接
def on_close(ws):
print("client closed.")
# 准备数据
def prepare_data(args, access_token):
# 读取音频文件
with open(args.file_path, \'rb\') as f:
file = f.read()
# 填写Header信息
audio_format = args.audio_format
sample_rate = args.sample_rate
splited_data = [str(base64.b64encode(file[i:i + 5120]), encoding=\'utf-8\') for i in range(0, len(file), 5120)]
asr_params = "audio_format": audio_format, "sample_rate": int(sample_rate), "speech_type": 1
json_list = []
for i in range(len(splited_data)):
if i != len(splited_data) - 1:
asr_params[\'req_idx\'] = i
else:
asr_params[\'req_idx\'] = -len(splited_data) + 1
asr_params["audio_data"] = splited_data[i]
data = "access_token": access_token, "version": "1.0", "asr_params": asr_params
json_list.append(json.dumps(data))
return json_list
# 获取命令行输入参数
def get_args():
parser = argparse.ArgumentParser(description=\'ASR\')
parser.add_argument(\'-client_secret\', type=str, required=True)
parser.add_argument(\'-client_id\', type=str, required=True)
parser.add_argument(\'-file_path\', type=str, required=True)
parser.add_argument(\'--audio_format\', type=str, default=\'wav\')
parser.add_argument(\'--sample_rate\', type=str, default=\'16000\')
args = parser.parse_args()
return args
# 获取access_token用于鉴权
def get_access_token(client_secret, client_id):
grant_type = "client_credentials"
url = "https://openapi.data-baker.com/oauth/2.0/token?grant_type=&client_secret=&client_id=" \\
.format(grant_type, client_secret, client_id)
try:
response = requests.post(url)
response.raise_for_status()
except Exception as e:
print(response.text)
raise Exception
else:
access_token = json.loads(response.text).get(\'access_token\')
return access_token
if __name__ == \'__main__\':
try:
args = get_args()
# 获取access_token
client_secret = args.client_secret
client_id = args.client_id
access_token = get_access_token(client_secret, client_id)
# 准备数据
data = prepare_data(args, access_token)
uri = "wss://openapi.data-baker.com/asr/realtime"
# 建立Websocket连接
client = Client(data, uri)
client.connect()
except Exception as e:
print(e)
2.5 执行
复制所有代码,确定音频为wav格式,采样率为16K,在命令行执行
python real_time_audio_recognition.py -client_secret=您的client_secret -client_id=您的client_id -file_path=test.wav --audio_format=wav --sample_rate=16000
填写邀请码fwwqgs,每日免费调用量还可以翻倍
带有自定义语音协议的 Microsoft 语音识别(Xamarin Android、Websocket)
】带有自定义语音协议的Microsoft语音识别(XamarinAndroid、Websocket)【英文标题】:MicrosoftSpeechRecognitionwithCustomSpeechProtocol(XamarinAndroid,Websocket)【发布时间】:2018-04-1305:44:25【问题描述】:我正在尝试使用适用于XamarinAndroid的MicrosoftC... 查看详情
Azure 认知服务 - 使用 python 和 websockets 自定义语音
】Azure认知服务-使用python和websockets自定义语音【英文标题】:AzureCognitiveServices-CustomSpeechwithpythonandwebsockets【发布时间】:2017-10-2516:06:11【问题描述】:我在Python中使用Microsoft自定义语音服务。目前仅适用于HTTP端点。根据文档,... 查看详情
python调用百度语音识别接口实时识别
1、本文直接上干货 奉献代码:https://github.com/wuzaipei/audio_discern/tree/master/%E8%AF%AD%E9%9F%B3%E8%AF%86%E5%88%AB2、安装依赖包 pipinstall依赖包路径3、具体步骤见下 安装百度语音识别SDK pipinstallbaidu-aip 安... 查看详情
websocket系列:android端websocket实时语音识别(代码片段)
看效果图:网络权限:<uses-permissionandroid:name="android.permission.INTERNET"/>导入WebSocket和fastjsonimplementation"org.java-websocket:Java-WebSocket:1.3.4"implementation& 查看详情
飞桨paddlespeech语音技术课程—语音识别-流式服务(代码片段)
FastAPIwebsocket流式语音识别服务0.背景流式语音识别(StreamingASR)或者在线语音识别(OnlineASR)是随着输入语音的数据不断增加,实时给出语音识别的文本结果。与之相对的是非实时或者离线语音识别,是传入完整的音频数据... 查看详情
如何用python调用百度语音识别
...,查看音频c的属性,可以看到音频持续28秒,这样就是用python调用百度语音识别成功解决问题。参考技术A1、首先准备可供测试的音频,百度搜索“百度语音识别-开发文档”。2、从上图网页把音频下载到本地的桌面的文件夹a里... 查看详情
语音识别系列︱利用达摩院modelscope进行语音识别+标点修复(代码片段)
...音识别系列了,之前的几篇:语音识别系列︱用python进行音频解析(一)语音识别系列︱paddlehub的开源语音识别模型测试(二)语音识别系列︱paddlespeech的开源语音识别模型测试(三)文章目录1达... 查看详情
语音识别系列︱利用达摩院modelscope进行语音识别+标点修复(代码片段)
...音识别系列了,之前的几篇:语音识别系列︱用python进行音频解析(一)语音识别系列︱paddlehub的开源语音识别模型测试(二)语音识别系列︱paddlespeech的开源语音识别模型测试(三)文章目录1达... 查看详情
在 Python 中使用 win32com.client 文本到语音/语音识别
】在Python中使用win32com.client文本到语音/语音识别【英文标题】:Usageofwin32com.clienttexttospeech/speechrecognitioninPython【发布时间】:2012-08-2001:05:38【问题描述】:所以我将文本转为语音部分,例如:fromwin32com.clientimportconstantsimportwin32co... 查看详情
语音识别和声音与音乐的比较
...言(马其顿语),我正在尝试比较两种录音声音。我正在使用http://code.google.com/p/musicg/来记录和比较语音,并且我正在 查看详情
如何用python调用百度语音识别
参考技术A#!/usr/bin/envpython#-*-coding:utf-8-*-##########################################################################Copyright(c)2017aibot.me,Inc.AllRightsReserved#########################################################################"""File:util_voice.pyAuthor:darrenwang(d... 查看详情
Xamarin 上的 Bing Speech API/语音服务(无 REST,sdk websocket 来处理连续识别)
】Xamarin上的BingSpeechAPI/语音服务(无REST,sdkwebsocket来处理连续识别)【英文标题】:BingSpeechAPI/SpeechserviceonXamarin(NoREST,sdkwebsockettohandlecontinousrecognition)【发布时间】:2018-08-1013:39:35【问题描述】:我正在为基于Xamarin的简单语音识... 查看详情
调用语音识别应用程序的小部件
...件,单击该小部件会启动语音识别应用程序。我从来没有使用过小部件和待处理的意图,所以我很困惑:如何创建待处理的意图来启动语音识别活动?我尝试过这样的事情,但它当然失败了:意图意图=newIntent();意图voi 查看详情
基于python的语音识别控制系统(代码片段)
基于Python的语音识别控制系统2021.12.03:本人是一名大三学生,学习人工智能的时候老师发布了一个语音识别控制图形移动的任务,参考了许多内容,写了一个小的程序,写出来分享给大家,如果能帮到你... 查看详情
语音识别和 python 入门
】语音识别和python入门【英文标题】:Gettingstartedwithspeechrecognitionandpython【发布时间】:2012-08-2716:08:36【问题描述】:我想知道从哪里开始使用语音识别。不是图书馆或任何相当“黑盒”的东西,而是我想知道我可以在哪里实际... 查看详情
百度语音识别引擎使用
~安装pythonubuntu下一般自带python,升级的话看下面windows下需要安装下载python3后 1.以root权限打开终端,进入安装包的存放路径,解压安装包: tar -xzvf Python-3.6.0.tgz 11 2.进入解压好的安... 查看详情
python语音签名识别?
】python语音签名识别?【英文标题】:pythonvoicesignatureidentification?【发布时间】:2021-07-0411:36:33【问题描述】:我正在开发一个系统,该系统会锁定我的计算机中的多个部分,并且只能使用我的声音说出特定的内容来打开它们单... 查看详情
语音识别之初体验
一、概述作为最自然的人机交互方式--语音,正在改变人们的生活,丰富多媒体技术的应用。语音识别技术是语音信号处理的一个重要分支,也是近年来非常火的一个研究领域。随着科技的飞速发展,语音识别不仅在桌面PC和... 查看详情