首页  编辑  

阿里巴巴百炼语音识别模型实时语音识别示例

Tags: /Python/   Date Created:
利用阿里百炼大模型 Dashscope,实现实时麦克风语音转文本输出。
调用dashscope paraformer的实时语音识别API,识别结果混乱_问答-阿里云开发者社区

pip install pyaudio dashscope

import pyaudio
import dashscope
from dashscope.audio.asr import (Recognition, RecognitionCallback,
                                 RecognitionResult)

dashscope.api_key='sk-d90a2c8cb92344bbb431d......'

mic = None
stream = None

class Callback(RecognitionCallback):
    def on_open(self) -> None:
        global mic
        global stream
        print('RecognitionCallback open.')
        mic = pyaudio.PyAudio()
        stream = mic.open(format=pyaudio.paInt16,
                          channels=1,
                          rate=16000,
                          input=True)

    def on_close(self) -> None:
        global mic
        global stream
        print('RecognitionCallback close.')
        stream.stop_stream()
        stream.close()
        mic.terminate()
        stream = None
        mic = None

    def on_event(self, result: RecognitionResult) -> None:
        print('RecognitionCallback sentence: ', result.get_sentence())

callback = Callback()
recognition = Recognition(model='paraformer-realtime-v1',
                          format='pcm',
                          sample_rate=16000,
                          callback=callback)
recognition.start()

while True:
    if stream:
        data = stream.read(3200, exception_on_overflow = False)
        recognition.send_audio_frame(data)
    else:
        break

recognition.stop()
如果要把系统回放的声音而不是麦克风作为输入来实时识别,可以安装 VB-Audio Virtual Apps VBCABLE_Driver_Pack45.zip ,然后选择 CABLE Output (VB-Audio Virtual , MME (16 in, 0 out) 设备作为输入设备即可。代码如下:
import sounddevice as sd
import numpy as np
import dashscope
from dashscope.audio.asr import Recognition, RecognitionCallback, RecognitionResult

dashscope.api_key = "sk-**********************"

mic = None
stream = None


class Callback(RecognitionCallback):
    def on_open(self) -> None:
        print("RecognitionCallback open.")

    def on_close(self) -> None:
        print("RecognitionCallback close.")

    def on_event(self, result: RecognitionResult) -> None:
        if result.output.sentence["end_time"] != None:
            print("RecognitionCallback sentence: ", result.get_sentence()['text'])


callback = Callback()
recognition = Recognition(
    model="paraformer-realtime-v1", format="pcm", sample_rate=16000, callback=callback
)
recognition.start()


def audio_callback(indata, frames, time, status):
    # print("input data: ", indata.tobytes()[0:10], "...", status)
    if status:
        print(status)
    # 将音频数据传递给Recognition
    recognition.send_audio_frame(indata.tobytes())


# 使用 sounddevice 捕获音频输出
devices = sd.query_devices()
print(devices)
# 请把 device 的 id 选择为 "CABLE Output (VB-Audio Virtual , MME (16 in, 0 out)" 对应的 devices 的整数即可
stream = sd.InputStream(blocksize=3200, callback=audio_callback, channels=1, samplerate=16000, dtype=np.int16, device=2)
stream.start()

# 保持主线程运行,直到结束
try:
    while True:
        pass
except KeyboardInterrupt:
    pass

recognition.stop()
输出结果:


VBCABLE_Driver_Pack45.zip (1.3MB)