python调用阿里云实时语音转文字

安装阿里云的依赖

bash 复制代码
git clone https://github.com/aliyun/alibabacloud-nls-python-sdk.git
bash 复制代码
cd alibabacloud-nls-python-sdk/
bash 复制代码
python -m pip install -r requirements.txt
bash 复制代码
python -m pip install .

准备pcm的文件

https://support.huaweicloud.com/sdkreference-sis/sis_05_0039.html

准备调用的token和appId

准备代码

bash 复制代码
import time
import threading

import nls

URL = "wss://nls-gateway-cn-shanghai.aliyuncs.com/ws/v1"
TOKEN = "016ca1620aff421da8fac81b9fb52dc5"  # 参考https://help.aliyun.com/document_detail/450255.html获取token
APPKEY = "ahS8ZDaimkpWALHi"  # 获取Appkey请前往控制台:https://nls-portal.console.aliyun.com/applist


# 以下代码会根据音频文件内容反复进行实时语音识别(文件转写)
class TestSt:
    def __init__(self, tid, test_file):
        self.__th = threading.Thread(target=self.__test_run)
        self.__id = tid
        self.__test_file = test_file

    def loadfile(self, filename):
        with open(filename, "rb") as f:
            self.__data = f.read()

    def start(self):
        self.loadfile(self.__test_file)
        self.__th.start()

    def test_on_sentence_begin(self, message, *args):
        print("test_on_sentence_begin:{}".format(message))

    def test_on_sentence_end(self, message, *args):
        print("test_on_sentence_end:{}".format(message))

    def test_on_start(self, message, *args):
        print("test_on_start:{}".format(message))

    def test_on_error(self, message, *args):
        print("on_error args=>{}".format(args))

    def test_on_close(self, *args):
        print("on_close: args=>{}".format(args))

    def test_on_result_chg(self, message, *args):
        print("test_on_chg:{}".format(message))

    def test_on_completed(self, message, *args):
        print("on_completed:args=>{} message=>{}".format(args, message))

    def __test_run(self):
        print("thread:{} start..".format(self.__id))
        sr = nls.NlsSpeechTranscriber(
            url=URL,
            token=TOKEN,
            appkey=APPKEY,
            on_sentence_begin=self.test_on_sentence_begin,
            on_sentence_end=self.test_on_sentence_end,
            on_start=self.test_on_start,
            on_result_changed=self.test_on_result_chg,
            on_completed=self.test_on_completed,
            on_error=self.test_on_error,
            on_close=self.test_on_close,
            callback_args=[self.__id]
        )

        print("{}: session start".format(self.__id))
        r = sr.start(aformat="pcm",
                     enable_intermediate_result=True,
                     enable_punctuation_prediction=True,
                     enable_inverse_text_normalization=True)

        self.__slices = zip(*(iter(self.__data),) * 640)
        for i in self.__slices:
            sr.send_audio(bytes(i))
            time.sleep(0.01)

        sr.ctrl(ex={"test": "tttt"})
        time.sleep(1)

        r = sr.stop()
        print("{}: sr stopped:{}".format(self.__id, r))
        time.sleep(1)


def multiruntest(num=500):
    for i in range(0, num):
        name = "thread" + str(i)
        t = TestSt(name, "16k16bit.pcm")
        t.start()


if __name__ == "__main__":
    nls.enableTrace(False)
    multiruntest(1)

测试

bash 复制代码
thread0: session start
test_on_start:{"header":{"namespace":"SpeechTranscriber","name":"TranscriptionStarted","status":20000000,"message_id":"a87ccd06ee51400b84e15ce4f0c01067","task_id":"041494fd76a84b6baa96d58676c8a8fd","status_text":"Gateway:SUCCESS:Success."}}
test_on_sentence_begin:{"header":{"namespace":"SpeechTranscriber","name":"SentenceBegin","status":20000000,"message_id":"a7e06dc6e92a4afdabe17a2d9c5f3884","task_id":"041494fd76a84b6baa96d58676c8a8fd","status_text":"Gateway:SUCCESS:Success."},"payload":{"index":1,"time":0}}
test_on_chg:{"header":{"namespace":"SpeechTranscriber","name":"TranscriptionResultChanged","status":20000000,"message_id":"64671fca85a040bb8bd163c92d289dc9","task_id":"041494fd76a84b6baa96d58676c8a8fd","status_text":"Gateway:SUCCESS:Success."},"payload":{"index":1,"time":360,"result":"为手","confidence":0.591,"words":[],"status":0,"fixed_result":"","unfixed_result":""}}
test_on_chg:{"header":{"namespace":"SpeechTranscriber","name":"TranscriptionResultChanged","status":20000000,"message_id":"21b9241e5ae64b618e3983071b74a166","task_id":"041494fd76a84b6baa96d58676c8a8fd","status_text":"Gateway:SUCCESS:Success."},"payload":{"index":1,"time":360,"result":"为手。","confidence":0.874,"words":[],"status":0,"fixed_result":"","unfixed_result":""}}
test_on_chg:{"header":{"namespace":"SpeechTranscriber","name":"TranscriptionResultChanged","status":20000000,"message_id":"325cfaaad31841e4bd4118e8d0edff43","task_id":"041494fd76a84b6baa96d58676c8a8fd","status_text":"Gateway:SUCCESS:Success."},"payload":{"index":1,"time":1200,"result":"华为致力于","confidence":0.863,"words":[],"status":0,"fixed_result":"","unfixed_result":""}}
test_on_chg:{"header":{"namespace":"SpeechTranscriber","name":"TranscriptionResultChanged","status":20000000,"message_id":"ea3368f7172c43a9a601e5c7e249da2e","task_id":"041494fd76a84b6baa96d58676c8a8fd","status_text":"Gateway:SUCCESS:Success."},"payload":{"index":1,"time":1680,"result":"华为致力于把","confidence":0.868,"words":[],"status":0,"fixed_result":"","unfixed_result":""}}
test_on_chg:{"header":{"namespace":"SpeechTranscriber","name":"TranscriptionResultChanged","status":20000000,"message_id":"be1797391197403797fdaaf472768d1d","task_id":"041494fd76a84b6baa96d58676c8a8fd","status_text":"Gateway:SUCCESS:Success."},"payload":{"index":1,"time":2160,"result":"华为致力于把数字","confidence":0.872,"words":[],"status":0,"fixed_result":"","unfixed_result":""}}
test_on_chg:{"header":{"namespace":"SpeechTranscriber","name":"TranscriptionResultChanged","status":20000000,"message_id":"fb2b2283f8944346a064d07d13c35aff","task_id":"041494fd76a84b6baa96d58676c8a8fd","status_text":"Gateway:SUCCESS:Success."},"payload":{"index":1,"time":2640,"result":"华为致力于把数字世界带","confidence":0.866,"words":[],"status":0,"fixed_result":"","unfixed_result":""}}
test_on_chg:{"header":{"namespace":"SpeechTranscriber","name":"TranscriptionResultChanged","status":20000000,"message_id":"98116e180f8b4411a691535a5a025920","task_id":"041494fd76a84b6baa96d58676c8a8fd","status_text":"Gateway:SUCCESS:Success."},"payload":{"index":1,"time":3120,"result":"华为致力于把数字世界带入每个","confidence":0.872,"words":[],"status":0,"fixed_result":"","unfixed_result":""}}
test_on_chg:{"header":{"namespace":"SpeechTranscriber","name":"TranscriptionResultChanged","status":20000000,"message_id":"3796787124094db4a8fe25e3fcdf624c","task_id":"041494fd76a84b6baa96d58676c8a8fd","status_text":"Gateway:SUCCESS:Success."},"payload":{"index":1,"time":3600,"result":"华为致力于把数字世界带入每个人每","confidence":0.87,"words":[],"status":0,"fixed_result":"","unfixed_result":""}}
test_on_chg:{"header":{"namespace":"SpeechTranscriber","name":"TranscriptionResultChanged","status":20000000,"message_id":"23cd2f31e4c64d55b8c06d13caa5e6b7","task_id":"041494fd76a84b6baa96d58676c8a8fd","status_text":"Gateway:SUCCESS:Success."},"payload":{"index":1,"time":4080,"result":"华为致力于把数字世界带入每个人每个家","confidence":0.872,"words":[],"status":0,"fixed_result":"","unfixed_result":""}}
test_on_chg:{"header":{"namespace":"SpeechTranscriber","name":"TranscriptionResultChanged","status":20000000,"message_id":"3fe3e290cc1c489eadc1d7fad01c83ec","task_id":"041494fd76a84b6baa96d58676c8a8fd","status_text":"Gateway:SUCCESS:Success."},"payload":{"index":1,"time":4560,"result":"华为致力于把数字世界带入每个人、每个家庭、每个","confidence":0.875,"words":[],"status":0,"fixed_result":"","unfixed_result":""}}
test_on_chg:{"header":{"namespace":"SpeechTranscriber","name":"TranscriptionResultChanged","status":20000000,"message_id":"64c836342f7a4b3ab448c9d2cf9cc100","task_id":"041494fd76a84b6baa96d58676c8a8fd","status_text":"Gateway:SUCCESS:Success."},"payload":{"index":1,"time":5120,"result":"华为致力于把数字世界带入每个人、每个家庭、每个组","confidence":0.875,"words":[],"status":0,"fixed_result":"","unfixed_result":""}}
test_on_chg:{"header":{"namespace":"SpeechTranscriber","name":"TranscriptionResultChanged","status":20000000,"message_id":"0023c5a3a635458bbd642a12c0a7fa8d","task_id":"041494fd76a84b6baa96d58676c8a8fd","status_text":"Gateway:SUCCESS:Success."},"payload":{"index":1,"time":5520,"result":"华为致力于把数字世界带入每个人、每个家庭、每个组织构建","confidence":0.878,"words":[],"status":0,"fixed_result":"","unfixed_result":""}}
test_on_chg:{"header":{"namespace":"SpeechTranscriber","name":"TranscriptionResultChanged","status":20000000,"message_id":"90e4697a2e1746139215f7d72fb02192","task_id":"041494fd76a84b6baa96d58676c8a8fd","status_text":"Gateway:SUCCESS:Success."},"payload":{"index":1,"time":6000,"result":"华为致力于把数字世界带入每个人、每个家庭、每个组织,构建万物","confidence":0.879,"words":[],"status":0,"fixed_result":"","unfixed_result":""}}
test_on_chg:{"header":{"namespace":"SpeechTranscriber","name":"TranscriptionResultChanged","status":20000000,"message_id":"8d9acdbf79d14d608c90e77dc53e0fc4","task_id":"041494fd76a84b6baa96d58676c8a8fd","status_text":"Gateway:SUCCESS:Success."},"payload":{"index":1,"time":6480,"result":"华为致力于把数字世界带入每个人、每个家庭、每个组织,构建万物互联的","confidence":0.88,"words":[],"status":0,"fixed_result":"","unfixed_result":""}}
test_on_chg:{"header":{"namespace":"SpeechTranscriber","name":"TranscriptionResultChanged","status":20000000,"message_id":"9ce6e97a33c541d1927b610a5e23f687","task_id":"041494fd76a84b6baa96d58676c8a8fd","status_text":"Gateway:SUCCESS:Success."},"payload":{"index":1,"time":7000,"result":"华为致力于把数字世界带入每个人、每个家庭、每个组织,构建万物互联的智能世","confidence":0.877,"words":[],"status":0,"fixed_result":"","unfixed_result":""}}
test_on_sentence_end:{"header":{"namespace":"SpeechTranscriber","name":"SentenceEnd","status":20000000,"message_id":"ad378437552e418f90168ff2bd0d6d94","task_id":"041494fd76a84b6baa96d58676c8a8fd","status_text":"Gateway:SUCCESS:Success."},"payload":{"index":1,"time":7000,"result":"华为致力于把数字世界带入每个人、每个家庭、每个组织,构建万物互联的智能世界。","confidence":0.879,"words":[],"status":0,"gender":"","begin_time":0,"fixed_result":"","unfixed_result":"","stash_result":{"sentenceId":2,"beginTime":7000,"text":"","fixedText":"","unfixedText":"","currentTime":7000,"words":[]},"audio_extra_info":"","sentence_id":"593ac4db6b33408db5927eef442a1b4e","gender_score":0.0}}
on_completed:args=>('thread0',) message=>{"header":{"namespace":"SpeechTranscriber","name":"TranscriptionCompleted","status":20000000,"message_id":"0900e28af582436b8655cf1165c2a49e","task_id":"041494fd76a84b6baa96d58676c8a8fd","status_text":"Gateway:SUCCESS:Success."}}
thread0: sr stopped:None
on_close: args=>('thread0',)
相关推荐
CC__xy18 分钟前
04 类型别名type + 检测数据类型(typeof+instanceof) + 空安全+剩余和展开(运算符 ...)简单类型和复杂类型 + 模块化
开发语言·javascript·harmonyos·鸿蒙
青衫客3623 分钟前
用 Python 实现一个“小型 ReAct 智能体”:思维链 + 工具调用 + 环境交互
python·大模型·llm·react
萤丰信息29 分钟前
技术赋能安全:智慧工地构建城市建设新防线
java·大数据·开发语言·人工智能·智慧城市·智慧工地
AI视觉网奇1 小时前
音频分类模型笔记
人工智能·python·深度学习
Pocker_Spades_A1 小时前
飞算JavaAI家庭记账系统:从收支记录到财务分析的全流程管理方案
java·开发语言
Ratten2 小时前
【Python 实战】---- 实现一个可选择、配置操作的批量文件上传工具(四)配置管理界面和逻辑实现
python
Ratten2 小时前
【Python 实战】---- 实现一个可选择、配置操作的批量文件上传工具(五)打包成 exe 应用
python
跟橙姐学代码2 小时前
写 Python 函数别再死抠参数了,这招让代码瞬间灵活
前端·python
CHEN5_022 小时前
【Java基础常见辨析】重载与重写,深拷贝与浅拷贝,抽象类与普通类
java·开发语言
Despacito0o2 小时前
C语言基础:变量与进制详解
java·c语言·开发语言