使用与 Vosk 存储库中的 test_ffmpeg.py 非常相似的文件,我正在探索可以从音频文件中获取哪些文本信息。
这是我正在使用的整个脚本的代码。
#!/usr/bin/env python3
from vosk import Model, KaldiRecognizer, SetLogLevel
import sys
import os
import wave
import subprocess
import json
SetLogLevel(0)
if not os.path.exists("model"):
print ("Please download the model from https://alphacephei.com/vosk/models and unpack as 'model' in the current folder.")
exit (1)
sample_rate=16000
model = Model("model")
rec = KaldiRecognizer(model, sample_rate)
process = subprocess.Popen(['ffmpeg', '-loglevel', 'quiet', '-i',
sys.argv[1],
'-ar', str(sample_rate) , '-ac', '1', '-f', 's16le', '-'],
stdout=subprocess.PIPE)
file = open(sys.argv[1]+".txt","w+")
while True:
data = process.stdout.read(4000)
if len(data) == 0:
break
if rec.AcceptWaveform(data):
file.write(json.loads(rec.Result())['text']+"\n\n")
#print(rec.Result())
#else:
#print(rec.PartialResult())
#print(json.loads(rec.Result())['text'])
file.write(json.loads(rec.Result())['text'])
file.close()
此示例运行良好,但是,我可以从 rec.PartialResult() 和 rec.Result() 中找到的唯一返回是带有结果的字符串字典。有没有办法查询 KaldiRecognizer 在音频文件中找到单个单词的时间?
当我输入这个时,我已经在考虑详细说明结果,并检测部分结果与当前样本相比的变化会给我想要的东西,但我把它贴在这里以防万一它已经实施的。