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

安装阿里云的依赖

git clone https://github.com/aliyun/alibabacloud-nls-python-sdk.git
cd alibabacloud-nls-python-sdk/

python -m pip install -r requirements.txt
python -m pip install .

准备pcm的文件

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

准备调用的token和appId

在这里插入图片描述

准备代码

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)

测试

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',)
  • 8
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

MonkeyKing.sun

对你有帮助的话,可以打赏

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值