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()  # 保持程序运行
相关推荐
途途途途8 分钟前
Python 给 Excel 写入数据的四种方法
windows·python·excel
走在考研路上2 小时前
Python错误处理
开发语言·python
数据小爬虫@2 小时前
Python爬虫:如何优雅地“偷窥”商品详情
开发语言·爬虫·python
闰土_RUNTU2 小时前
Pytorch分布式训练print()使用技巧
人工智能·pytorch·python·分布式训练·训练技巧
零光速2 小时前
数据处理与统计分析——10-Pandas可视化-Matplotlib的常用API
数据结构·python·数据分析·pandas·matplotlib
雨中奔跑的小孩3 小时前
爬虫学习案例3
爬虫·python·学习
Topstip3 小时前
免费格式转换器推荐汇总 - 在线文件转换, 视频转换, 音频格式转换,PDF转换等
pdf·音视频·视频
陌上笙清净3 小时前
flask内存马的真谛!!!
后端·python·网络安全·flask
A Genius4 小时前
Pytorch实现MobilenetV2官方源码
人工智能·pytorch·python