提示:
- 在使用SDK之前,请先确保已阅读了 接口说明文档。
说明:
pip install setuptools
# 打包
python setup.py bdist_egg
# 安装
python setup.py install
注意:以上的pip
、python
命令是对应的Python3。
说明1: Demo中使用的音频文件为16000Hz采样率,请在控制台中将appkey对应项目的模型设置为通用模型,以获取正确的识别结果;如果使用其他音频,请设置为支持该音频场景的模型,模型设置请阅读管理项目一节。
nls-sample-16k.wav
说明2: Demo中使用了SDK内置的默认一句话识别服务的外网访问URL,如果您使用上海阿里云ECS并想使用内网访问URL,则在创建NlsClient对象时,设置内网访问的URL:
recognizer = client.create_recognizer(callback, "ws://nls-gateway.cn-shanghai-internal.aliyuncs.com/ws/v1")
示例:
# -*- coding: utf-8 -*-
import os
import time
import threading
import ali_speech
from ali_speech.callbacks import SpeechRecognizerCallback
from ali_speech.constant import ASRFormat
from ali_speech.constant import ASRSampleRate
class MyCallback(SpeechRecognizerCallback):
"""
构造函数的参数没有要求,可根据需要设置添加
示例中的name参数可作为待识别的音频文件名,用于在多线程中进行区分
"""
def __init__(self, name='default'):
self._name = name
def on_started(self, message):
print('MyCallback.OnRecognitionStarted: %s' % message)
def on_result_changed(self, message):
print('MyCallback.OnRecognitionResultChanged: file: %s, task_id: %s, result: %s' % (
self._name, message['header']['task_id'], message['payload']['result']))
def on_completed(self, message):
print('MyCallback.OnRecognitionCompleted: file: %s, task_id:%s, result:%s' % (
self._name, message['header']['task_id'], message['payload']['result']))
def on_task_failed(self, message):
print('MyCallback.OnRecognitionTaskFailed: %s' % message)
def on_channel_closed(self):
print('MyCallback.OnRecognitionChannelClosed')
def process(client, appkey, token):
audio_name = 'nls-sample-16k.wav'
callback = MyCallback(audio_name)
recognizer = client.create_recognizer(callback)
recognizer.set_appkey(appkey)
recognizer.set_token(token)
recognizer.set_format(ASRFormat.PCM)
recognizer.set_sample_rate(ASRSampleRate.SAMPLE_RATE_16K)
recognizer.set_enable_intermediate_result(False)
recognizer.set_enable_punctuation_prediction(True)
recognizer.set_enable_inverse_text_normalization(True)
try:
ret = recognizer.start()
if ret < 0:
return ret
print('sending audio...')
with open(audio_name, 'rb') as f:
audio = f.read(3200)
while audio:
ret = recognizer.send(audio)
if ret < 0:
break
time.sleep(0.1)
audio = f.read(3200)
recognizer.stop()
except Exception as e:
print(e)
finally:
recognizer.close()
def process_multithread(client, appkey, token, number):
thread_list = []
for i in range(0, number):
thread = threading.Thread(target=process, args=(client, appkey, token))
thread_list.append(thread)
thread.start()
for thread in thread_list:
thread.join()
if __name__ == "__main__":
client = ali_speech.NlsClient()
# 设置输出日志信息的级别:DEBUG、INFO、WARNING、ERROR
client.set_log_level('INFO')
appkey = '您的appkey'
token = '您的Token'
process(client, appkey, token)
# 多线程示例
# process_multithread(client, appkey, token, 2)
在文档使用中是否遇到以下问题
更多建议
匿名提交