python从麦克风获取音频并识别

麦克风获取

python 复制代码
# 打开麦克风流
stream = audio.open(format=FORMAT,
                    channels=CHANNELS,
                    rate=RATE,
                    input=True,
                    frames_per_buffer=CHUNK)

设置参数

python 复制代码
# 录音参数
FORMAT = pyaudio.paInt16  # 16-bit resolution
CHANNELS = 1              # 单声道
RATE = 16000              # 采样率 16kHz
CHUNK = 1024              # 数据块大小
RECORD_SECONDS = 5        # 录制时长 (秒)
WAVE_OUTPUT_FILENAME = "output.wav"

读取数据块

python 复制代码
# 循环读取数据块
for i in range(0, int(RATE / CHUNK * RECORD_SECONDS)):
    data = stream.read(CHUNK)
    frames.append(data)

阿里语音识别模型加载

python 复制代码
paraformer_path = "./iic/speech_paraformer-large-vad-punc-spk_asr_nat-zh-cn"
fsmn_path = "./iic/speech_fsmn_vad_zh-cn-16k-common-pytorch"
ct_punc_path = "./iic/punc_ct-transformer_cn-en-common-vocab471067-large"
cam_path = "./iic/speech_campplus_sv_zh-cn_16k-common"

model = AutoModel(model=paraformer_path, model_revision="v2.0.4",
                  vad_model=fsmn_path, vad_model_revision="v2.0.4",
                  punc_model=ct_punc_path, punc_model_revision="v2.0.4",
                  spk_model=cam_path, spk_model_revision="v2.0.2",
                  device="cpu"
                  )

阿里语音识别

python 复制代码
res = model.generate(input=WAVE_OUTPUT_FILENAME,
                     batch_size_s=16000,
                     hotword='魔搭')

整体代码

python 复制代码
import pyaudio
import wave
import threading
import keyboard
from funasr import AutoModel

# 录音参数
FORMAT = pyaudio.paInt16  # 16-bit resolution
CHANNELS = 1  # 单声道
RATE = 16000  # 采样率 16kHz
CHUNK = 1024  # 数据块大小
WAVE_OUTPUT_FILENAME = "./wav_data/output.wav"

# 初始化 PyAudio
audio = pyaudio.PyAudio()

frames = []
stream = None
recording = False

paraformer_path = "./iic/speech_paraformer-large-vad-punc-spk_asr_nat-zh-cn"
fsmn_path = "./iic/speech_fsmn_vad_zh-cn-16k-common-pytorch"
ct_punc_path = "./iic/punc_ct-transformer_cn-en-common-vocab471067-large"
cam_path = "./iic/speech_campplus_sv_zh-cn_16k-common"

model = AutoModel(model=paraformer_path, model_revision="v2.0.4",
                  vad_model=fsmn_path, vad_model_revision="v2.0.4",
                  punc_model=ct_punc_path, punc_model_revision="v2.0.4",
                  spk_model=cam_path, spk_model_revision="v2.0.2",
                  device="cpu"
                  )

print("加载模型完成!!!")


def start_recording():
    """
    开始录音
    """
    global stream, recording
    if not recording:
        print("开始录音...")
        recording = True
        stream = audio.open(format=FORMAT,
                            channels=CHANNELS,
                            rate=RATE,
                            input=True,
                            frames_per_buffer=CHUNK)
        record_thread = threading.Thread(target=record_audio)
        record_thread.start()


def stop_recording():
    """
    停止录音并进行识别
    """
    global stream, recording
    if recording:
        print("录音结束.")
        recording = False
        stream.stop_stream()
        stream.close()
        save_audio()
        audio.terminate()
        print("开始识别!!!")
        res = model.generate(input=WAVE_OUTPUT_FILENAME,
                             batch_size_s=16000,
                             hotword='魔搭')
        print("识别结束!!!")
        print("识别结果:", res)


def record_audio():
    """
    录音功能实现
    """
    while recording:
        data = stream.read(CHUNK)
        frames.append(data)


def save_audio():
    """
    保存录音文件
    """
    wf = wave.open(WAVE_OUTPUT_FILENAME, 'wb')
    wf.setnchannels(CHANNELS)
    wf.setsampwidth(audio.get_sample_size(FORMAT))
    wf.setframerate(RATE)
    wf.writeframes(b''.join(frames))
    wf.close()


# 监听 Ctrl 键开始录音
keyboard.add_hotkey('ctrl', start_recording)

# 监听 Alt 键结束录音
keyboard.add_hotkey('alt', stop_recording)

print("按 Ctrl 开始录音,按 Alt 结束录音")
keyboard.wait()  # 保持程序运行
相关推荐
数据小爬虫@2 小时前
深入解析:使用 Python 爬虫获取苏宁商品详情
开发语言·爬虫·python
健胃消食片片片片2 小时前
Python爬虫技术:高效数据收集与深度挖掘
开发语言·爬虫·python
ℳ₯㎕ddzོꦿ࿐5 小时前
解决Python 在 Flask 开发模式下定时任务启动两次的问题
开发语言·python·flask
CodeClimb5 小时前
【华为OD-E卷 - 第k个排列 100分(python、java、c++、js、c)】
java·javascript·c++·python·华为od
一水鉴天5 小时前
为AI聊天工具添加一个知识系统 之63 详细设计 之4:AI操作系统 之2 智能合约
开发语言·人工智能·python
Channing Lewis5 小时前
什么是 Flask 的蓝图(Blueprint)
后端·python·flask
B站计算机毕业设计超人5 小时前
计算机毕业设计hadoop+spark股票基金推荐系统 股票基金预测系统 股票基金可视化系统 股票基金数据分析 股票基金大数据 股票基金爬虫
大数据·hadoop·python·spark·课程设计·数据可视化·推荐算法
觅远6 小时前
python+playwright自动化测试(四):元素操作(键盘鼠标事件)、文件上传
python·自动化
ghostwritten7 小时前
Python FastAPI 实战应用指南
开发语言·python·fastapi
CM莫问7 小时前
python实战(十五)——中文手写体数字图像CNN分类
人工智能·python·深度学习·算法·cnn·图像分类·手写体识别