python调用阿里实时语音识别模型paraformer-realtime-v2实时识别麦克风声音转文字代码

代码语言:python

所属分类:人工智能

代码描述:python调用阿里实时语音识别模型paraformer-realtime-v2实时识别麦克风声音转文字代码,dashscope的api可以可以去官网申请。

代码标签: python 调用 阿里 实时 语音 识别 模型 paraformer-realtime-v2 识别

下面为部分代码预览,完整代码请点击下载或在bfwstudio webide中打开

import os
import signal  # for keyboard events handling (press "Ctrl+C" to terminate recording and translation)
import sys

import dashscope
import pyaudio
from dashscope.audio.asr import *

mic = None
stream = None

# Set recording parameters
sample_rate = 16000  # sampling rate (Hz)
channels = 1  # mono channel
dtype = 'int16'  # data type
format_pcm = 'pcm'  # the format of the audio data
block_size = 3200  # number of frames per buffer


def init_dashscope_api_key():

    if 'DASHSCOPE_API_KEY' in os.environ:
        dashscope.api_key = os.environ[
            'DASHSCOPE_API_KEY']  # load API-key from environment variable DASHSCOPE_API_KEY
    else:
        dashscope.api_key = '<your-dashscope-api-key>'  # set API-key manually


# Real-time speech recognition callback
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.........完整代码请登录后点击上方下载按钮下载查看

网友评论0