推荐开源项目:Whisper - 实时语音转文本的利器
    		       		warning:
    		            这篇文章距离上次修改已过426天,其中的内容可能已经有所变动。
    		        
        		                
                Whisper 是一个由 Apple 开发的高精度开源语音识别模型,它可以实时转录口语、保存语音信息,并且不需要专门的硬件设备。它使用了神经网络算法,具有很高的准确性和可扩展性。
以下是一个简单的 Python 示例,展示如何使用 Whisper 来识别语音:
import sounddevice as sd
from whisper import load_model, infer
 
# 加载预先训练好的 Whisper 模型
model = load_model('path_to_your_whisper_model.wisp')
 
# 定义回调函数,当按下 Ctrl+C 时停止录音
def signal_handler(signal, frame):
    print('Stopping...')
    global continue_recording
    continue_recording = False
 
import os
import signal
 
def record_and_infer(model_path, duration=5):
    # 注册 Ctrl+C 信号处理函数
    signal.signal(signal.SIGINT, signal_handler)
    continue_recording = True
 
    # 开始录音
    print("Press Ctrl+C to stop recording.")
    my_recording = sd.rec(int(duration * fs), samplerate=fs, channels=1)
    sd.wait()  # Wait until recording is finished
 
    # 使用 Whisper 模型进行语音识别
    transcription = infer(model, my_recording, fs)
    print(f"Transcription: {transcription}")
 
# 示例:调用函数,开始录音并进行语音转文本识别
record_and_infer('path_to_your_whisper_model.wisp')在这个示例中,我们首先导入了必要的库,并定义了一个处理语音识别的函数 record_and_infer。我们使用 sounddevice 来录音,并在录音期间捕获 Ctrl+C 信号以停止录音。然后我们加载了预先训练好的 Whisper 模型,并使用 infer 函数将录制的音频转换成文本。
注意:在运行这个示例之前,你需要将 'path_to_your_whisper_model.wisp' 替换为你的 Whisper 模型的实际路径。
评论已关闭