使用Python实现语音转文本

360影视 国产动漫 2025-05-19 22:20 3

摘要:# coding: utf-8from vosk import Model, KaldiRecognizerimport waveimport json# 加载模型m = Model('.\\vosk-model-small-cn-0.22')# 打开音频文件

# coding: utf-8from vosk import Model, KaldiRecognizerimport waveimport json# 加载模型m = Model('.\\vosk-model-small-cn-0.22')# 打开音频文件w = wave.open('demo1.wav')# 创建语音识别器k = KaldiRecognizer(m, 16000)print('开始识别')while True:f = w.readframes(4000)if not f:breakk.AcceptWaveform(f)txt = json.loads(k.FinalResult).get('text').replace(' ', '')print(txt)

4.实时语音识别

利用 PyAudio 实现从麦克风中获取音频输入,并利用 Vosk 完成语言识别。

安装 pyaudio

pip install pyaudio

代码:

from vosk import KaldiRecognizer, Modelfrom pyaudio import PyAudio, paInt16import json# 加载模型model = Model('vosk-model-cn-0.22')# 创建语音识别器rec = KaldiRecognizer(model, 16000)p = PyAudiostream = p.open(# 16位音频数据format=paInt16,# 声道,单声道channels=1,# 采样率rate=16000,# 从麦克风获取数据input=True,# 每次读取数据块frames_per_buffer=4096)stream.start_streamtry:while stream.is_active:data: bytes = stream.read(4096)if rec.AcceptWaveform(data):print(json.loads(rec.Result))else:res = json.loads(rec.PartialResult)if res["partial"]:print(res['partial'])except KeyboardInterrupt:print("KeyboardInterrupt...")finally:stream.stop_streamstream.closep.terminate

来源:IT职业教育

相关推荐