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',)
相关推荐
sharemylove12 分钟前
Java 继承与权限修饰符 学习笔记
java·开发语言·笔记·学习·课程设计
余槐i20 分钟前
设90秒却出3分钟:MoneyPrinterTurbo时长失控的根源
人工智能·python·ffmpeg·whisper·tts
老秦包你会30 分钟前
计算机网络第五课 ----应⽤层⾃定义协议与序列化(包含守护进程)
开发语言·计算机网络
前端精髓32 分钟前
langchain 项目配置tavily联网搜索
python·langchain
青山木42 分钟前
JVM 内存区域全解:堆、栈、方法区、直接内存的职责划分与常见报错
java·开发语言·jvm·后端·内存·堆·方法区
阿俊-全栈开发1 小时前
库存设计解析:LikeShop Go 版商城分布式秒杀库存的落地设计方案
开发语言·分布式·golang·likeshop开源商城·likeshop源码
li星野3 小时前
Qt 单例配置管理器学习记录:JsonConfigManager 的设计原理与使用示例
开发语言·qt·学习
钱栈up3 小时前
GitHub 热榜实测|sherlock 环境部署实录
python·开源·github
FYKJ_20103 小时前
springboot高校实验室综合管理系统86531-计算机毕业设计、课程设计
java·vue.js·spring boot·后端·python·spring·课程设计
xurileidian10 小时前
第 24 篇 · 边玩边学Python · while 与死循环怎么退
服务器·前端·python