安装阿里云的依赖

git clone https://github.com/aliyun/alibabacloud-nls-python-sdk.git
  • 1.
cd alibabacloud-nls-python-sdk/
  • 1.
python -m pip install -r requirements.txt
  • 1.
python -m pip install .
  • 1.

准备pcm的文件

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

准备调用的token和appId

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

准备代码

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)
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
  • 39.
  • 40.
  • 41.
  • 42.
  • 43.
  • 44.
  • 45.
  • 46.
  • 47.
  • 48.
  • 49.
  • 50.
  • 51.
  • 52.
  • 53.
  • 54.
  • 55.
  • 56.
  • 57.
  • 58.
  • 59.
  • 60.
  • 61.
  • 62.
  • 63.
  • 64.
  • 65.
  • 66.
  • 67.
  • 68.
  • 69.
  • 70.
  • 71.
  • 72.
  • 73.
  • 74.
  • 75.
  • 76.
  • 77.
  • 78.
  • 79.
  • 80.
  • 81.
  • 82.
  • 83.
  • 84.
  • 85.
  • 86.
  • 87.
  • 88.
  • 89.
  • 90.
  • 91.

测试

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',)
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.