使用一个与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在音频文件中找到单个单词的时间?
当我输入这段代码时,我已经在想,详细说明结果,并检测与当前示例相比部分结果中的变化将会得到我想要的结果,但我在这里坚持这一点,以防它已经实现。
发布于 2021-10-11 23:52:10
经过一些测试,很明显,相对于定义的采样率(16000),ffmpeg的输出似乎足够稳定,4000个字节的读取结果是8秒的8%。我在while循环中创建了一个计数器,并根据采样率将其除以一个常量。如果您将参数更改为ffmpeg,它可能会抛出这一点。
我使用了一些非常古老的字符串比较,只有当部分结果改变时才打印,并且只包含添加的新字符。
counter = 0
countinc = 2000/sample_rate
lastPR = ""
thisPR = ""
while True:
data = process.stdout.read(4000)
counter += 1
if len(data) == 0:
break
rec.AcceptWaveform(data)
thisPR = json.loads(rec.PartialResult())['partial']
if lastPR != thisPR:
print(counter*countinc,thisPR[len(lastPR):len(thisPR)])
lastPR = thisPR
https://stackoverflow.com/questions/69529170
复制相似问题