vosk-model-small-ru / decode.py
nshmyrevgmail's picture
Import small model
4d68c40
#!/usr/bin/env python3
import wave
from pathlib import Path
from typing import Tuple
import sys
import numpy as np
import sherpa_onnx
def read_wave(wave_filename: str) -> Tuple[np.ndarray, int]:
with wave.open(wave_filename) as f:
assert f.getnchannels() == 1, f.getnchannels()
assert f.getsampwidth() == 2, f.getsampwidth() # it is in bytes
num_samples = f.getnframes()
samples = f.readframes(num_samples)
samples_int16 = np.frombuffer(samples, dtype=np.int16)
samples_float32 = samples_int16.astype(np.float32)
samples_float32 = samples_float32 / 32768
return samples_float32, f.getframerate()
def main():
recognizer = sherpa_onnx.OfflineRecognizer.from_transducer(
encoder="am/encoder.onnx",
decoder="am/decoder.onnx",
joiner="am/joiner.onnx",
tokens="lang/tokens.txt",
num_threads=4,
sample_rate=16000,
decoding_method="greedy_search")
samples, sample_rate = read_wave("test.wav")
s = recognizer.create_stream()
s.accept_waveform(sample_rate, samples)
recognizer.decode_stream(s)
print (s.result.text)
if __name__ == "__main__":
main()