File size: 1,227 Bytes
4d68c40 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
#!/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()
|