mirror of
https://github.com/alphacep/vosk-api.git
synced 2026-01-09 07:32:34 +08:00
36 lines
834 B
Python
Executable File
36 lines
834 B
Python
Executable File
#!/usr/bin/env python3
|
|
|
|
import wave
|
|
import sys
|
|
|
|
from vosk import Model, KaldiRecognizer, SetLogLevel
|
|
|
|
# You can set log level to -1 to disable debug messages
|
|
SetLogLevel(0)
|
|
|
|
wf = wave.open(sys.argv[1], "rb")
|
|
if wf.getnchannels() != 1 or wf.getsampwidth() != 2 or wf.getcomptype() != "NONE":
|
|
print("Audio file must be WAV format mono PCM.")
|
|
sys.exit(1)
|
|
|
|
model = Model(lang="en-us")
|
|
|
|
# You can also init model by name or with a folder path
|
|
# model = Model(model_name="vosk-model-en-us-0.21")
|
|
# model = Model("models/en")
|
|
|
|
rec = KaldiRecognizer(model, wf.getframerate())
|
|
rec.SetWords(True)
|
|
rec.SetPartialWords(True)
|
|
|
|
while True:
|
|
data = wf.readframes(4000)
|
|
if len(data) == 0:
|
|
break
|
|
if rec.AcceptWaveform(data):
|
|
print(rec.Result())
|
|
else:
|
|
print(rec.PartialResult())
|
|
|
|
print(rec.FinalResult())
|