python监听麦克风并调用阿里云的实时语音转文字

bash 复制代码
import time
import threading
import queue
import sounddevice as sd
import numpy as np
import nls
import sys

# 阿里云配置信息
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


# Queue to hold the recorded audio data
audio_queue = queue.Queue()

# Callback function to capture audio data
def audio_callback(indata, frames, time, status):
    if status:
        print(status, file=sys.stderr)
    audio_queue.put(indata.copy())

class RealTimeSpeechRecognizer:
    def __init__(self, url, token, appkey):
        self.url = url
        self.token = token
        self.appkey = appkey
        self.transcriber = None
        self.__initialize_transcriber()

    def __initialize_transcriber(self):
        self.transcriber = nls.NlsSpeechTranscriber(
            url=self.url,
            token=self.token,
            appkey=self.appkey,
            on_sentence_begin=self.on_sentence_begin,
            on_sentence_end=self.on_sentence_end,
            on_start=self.on_start,
            on_result_changed=self.on_result_changed,
            on_completed=self.on_completed,
            on_error=self.on_error,
            on_close=self.on_close,
            callback_args=[self]
        )
        self.transcriber.start(aformat="pcm", enable_intermediate_result=True,
                               enable_punctuation_prediction=True, enable_inverse_text_normalization=True)

    def send_audio(self, audio_data):
        if self.transcriber:
            self.transcriber.send_audio(audio_data)

    def stop_transcription(self):
        if self.transcriber:
            self.transcriber.stop()

    def on_sentence_begin(self, message, *args):
        print("Sentence begin: {}".format(message))

    def on_sentence_end(self, message, *args):
        print("Sentence end: {}".format(message))

    def on_start(self, message, *args):
        print("Start: {}".format(message))

    def on_result_changed(self, message, *args):
        print("Result changed: {}".format(message))

    def on_completed(self, message, *args):
        print("Completed: {}".format(message))

    def on_error(self, message, *args):
        print("Error: {}".format(message))

    def on_close(self, *args):
        print("Closed: {}".format(args))

# 调用阿里云的语音转文字的接口
def recognize_speech(audio_data, recognizer):
    audio_data = np.concatenate(audio_data)
    recognizer.send_audio(audio_data.tobytes())

# Start the audio stream and process audio data
def start_audio_stream(recognizer):
    with sd.InputStream(callback=audio_callback, channels=1, samplerate=16000, dtype='int16'):
        print("Recording audio... Press Ctrl+C to stop.")
        audio_buffer = []
        try:
            while True:
                while not audio_queue.empty():
                    audio_buffer.append(audio_queue.get())
                if len(audio_buffer) >= 10:  # 调整音频数据块大小
                    audio_data = np.concatenate(audio_buffer)
                    recognize_speech(audio_data, recognizer)
                    audio_buffer = []  # Clear buffer after sending
                time.sleep(0.1)
        except KeyboardInterrupt:
            print("Stopping audio recording.")
            recognizer.stop_transcription()

if __name__ == "__main__":
    recognizer = RealTimeSpeechRecognizer(URL, TOKEN, APPKEY)
    start_audio_stream(recognizer)

这段代码实现了一个实时语音转文字系统,使用阿里云的语音转文字服务 (NlsSpeechTranscriber) 来处理从麦克风捕获的音频数据。以下是代码的详细解释:

主要模块和库

  • timethreading:用于处理时间和多线程。
  • queue:用于实现线程间通信的队列。
  • sounddevice (sd):用于从麦克风捕获音频数据。
  • numpy (np):用于处理音频数据数组。
  • nls:阿里云的语音服务库。
  • sys:用于处理系统相关的操作,如错误输出。

阿里云配置信息

python 复制代码
URL = "wss://nls-gateway-cn-shanghai.aliyuncs.com/ws/v1"
TOKEN = "016ca1620aff421da8fac81b9fb52dc5"
APPKEY = "ahS8ZDaimkpWALHi"

这些变量存储了阿里云语音服务的配置信息,包括服务的 URL、令牌(TOKEN)和应用密钥(APPKEY)。

音频数据队列

python 复制代码
audio_queue = queue.Queue()

用于存储从麦克风捕获的音频数据。

音频数据回调函数

python 复制代码
def audio_callback(indata, frames, time, status):
    if status:
        print(status, file=sys.stderr)
    audio_queue.put(indata.copy())

这个回调函数会在音频数据可用时被调用,将捕获到的音频数据复制到队列 audio_queue 中。

RealTimeSpeechRecognizer 类

python 复制代码
class RealTimeSpeechRecognizer:
    def __init__(self, url, token, appkey):
        self.url = url
        self.token = token
        self.appkey = appkey
        self.transcriber = None
        self.__initialize_transcriber()

初始化函数,接收 URL、TOKEN 和 APPKEY,并调用内部函数 __initialize_transcriber 初始化语音转文字服务。

python 复制代码
def __initialize_transcriber(self):
    self.transcriber = nls.NlsSpeechTranscriber(
        url=self.url,
        token=self.token,
        appkey=self.appkey,
        on_sentence_begin=self.on_sentence_begin,
        on_sentence_end=self.on_sentence_end,
        on_start=self.on_start,
        on_result_changed=self.on_result_changed,
        on_completed=self.on_completed,
        on_error=self.on_error,
        on_close=self.on_close,
        callback_args=[self]
    )
    self.transcriber.start(aformat="pcm", enable_intermediate_result=True,
                           enable_punctuation_prediction=True, enable_inverse_text_normalization=True)

初始化语音转文字服务并配置相关回调函数。

python 复制代码
def send_audio(self, audio_data):
    if self.transcriber:
        self.transcriber.send_audio(audio_data)

def stop_transcription(self):
    if self.transcriber:
        self.transcriber.stop()

用于发送音频数据到阿里云并停止转录。

回调函数

python 复制代码
def on_sentence_begin(self, message, *args):
    print("Sentence begin: {}".format(message))

def on_sentence_end(self, message, *args):
    print("Sentence end: {}".format(message))

def on_start(self, message, *args):
    print("Start: {}".format(message))

def on_result_changed(self, message, *args):
    print("Result changed: {}".format(message))

def on_completed(self, message, *args):
    print("Completed: {}".format(message))

def on_error(self, message, *args):
    print("Error: {}".format(message))

def on_close(self, *args):
    print("Closed: {}".format(args))

这些函数在语音转文字服务的不同事件发生时被调用,打印相关信息。

处理音频数据

python 复制代码
def recognize_speech(audio_data, recognizer):
    audio_data = np.concatenate(audio_data)
    recognizer.send_audio(audio_data.tobytes())

将音频数据连接成一个数组并发送给阿里云语音转文字服务。

开始音频流并处理音频数据

python 复制代码
def start_audio_stream(recognizer):
    with sd.InputStream(callback=audio_callback, channels=1, samplerate=16000, dtype='int16'):
        print("Recording audio... Press Ctrl+C to stop.")
        audio_buffer = []
        try:
            while True:
                while not audio_queue.empty():
                    audio_buffer.append(audio_queue.get())
                if len(audio_buffer) >= 10:  # 调整音频数据块大小
                    audio_data = np.concatenate(audio_buffer)
                    recognize_speech(audio_data, recognizer)
                    audio_buffer = []  # Clear buffer after sending
                time.sleep(0.1)
        except KeyboardInterrupt:
            print("Stopping audio recording.")
            recognizer.stop_transcription()

这个函数打开音频输入流,开始录音并处理音频数据,将其发送到阿里云进行转录。当用户按下 Ctrl+C 时,停止录音并结束转录。

主程序入口

python 复制代码
if __name__ == "__main__":
    recognizer = RealTimeSpeechRecognizer(URL, TOKEN, APPKEY)
    start_audio_stream(recognizer)

创建一个 RealTimeSpeechRecognizer 实例并开始录音和处理音频数据。

通过这些步骤,代码实现了从麦克风捕获音频数据并实时发送到阿里云进行语音转文字的功能。

相关推荐
一百七十五5 分钟前
python——面向对象小练习士兵突击与信息管理系统
开发语言·python
喜欢猪猪23 分钟前
注解的原理?如何自定义注解?
java·数据库·python
屿小夏.25 分钟前
Python实现万花筒效果:创造炫目的动态图案
开发语言·python·pygame
又逢乱世25 分钟前
[Python学习篇] Python函数
python
小白学大数据41 分钟前
Python爬虫与数据可视化:构建完整的数据采集与分析流程
开发语言·爬虫·python·信息可视化
工头阿乐42 分钟前
Python游戏脚本开发之大漠插件
前端·python·游戏
唤醒手腕1 小时前
2024 年最新 Python 基于火山引擎豆包大模型搭建 QQ 机器人详细教程(更新中)
开发语言·python·火山引擎
wyw00001 小时前
pytorch-ResNet18简单复现
人工智能·pytorch·python
心易行者2 小时前
AI与Python共舞:如何利用深度学习优化推荐系统?(2)
人工智能·python·深度学习