Spaces:
Running
Running
File size: 4,032 Bytes
26dfa9a 4793f39 26dfa9a 88a6c78 26dfa9a 4793f39 be9f49a 26dfa9a 3194abe d2b6f9b 3194abe 26dfa9a fc794b7 88a6c78 fc794b7 26dfa9a |
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 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 |
#!/usr/bin/python3
# -*- coding: utf-8 -*-
import argparse
import os
from pathlib import Path
import sys
import tempfile
pwd = os.path.abspath(os.path.dirname(__file__))
sys.path.append(os.path.join(pwd, "../../"))
import librosa
import numpy as np
import sherpa
from scipy.io import wavfile
import torch
import torchaudio
from project_settings import project_path, temp_directory
from toolbox.k2_sherpa.utils import audio_convert
from toolbox.k2_sherpa import decode, models
def get_args():
parser = argparse.ArgumentParser()
parser.add_argument(
"--model_dir",
default=(project_path / "pretrained_models/huggingface/csukuangfj/wenet-chinese-model").as_posix(),
type=str
)
parser.add_argument(
"--in_filename",
default=(project_path / "data/test_wavs/paraformer-zh/si_chuan_hua.wav").as_posix(),
type=str
)
parser.add_argument("--sample_rate", default=16000, type=int)
args = parser.parse_args()
return args
def load_sherpa_offline_recognizer(nn_model_file: str,
tokens_file: str,
sample_rate: int = 16000,
num_active_paths: int = 2,
decoding_method: str = "greedy_search",
num_mel_bins: int = 80,
frame_dither: int = 0,
):
feat_config = sherpa.FeatureConfig(normalize_samples=False)
feat_config.fbank_opts.frame_opts.samp_freq = sample_rate
feat_config.fbank_opts.mel_opts.num_bins = 80
feat_config.fbank_opts.frame_opts.dither = 0
config = sherpa.OfflineRecognizerConfig(
nn_model=nn_model_file,
tokens=tokens_file,
use_gpu=False,
feat_config=feat_config,
decoding_method="greedy_search",
num_active_paths=2,
)
recognizer = sherpa.OfflineRecognizer(config)
return recognizer
def main():
args = get_args()
# audio convert
in_filename = Path(args.in_filename)
out_filename = Path(tempfile.gettempdir()) / "asr" / in_filename.name
out_filename.parent.mkdir(parents=True, exist_ok=True)
audio_convert(in_filename=in_filename.as_posix(),
out_filename=out_filename.as_posix(),
)
# load recognizer
m_dict = models.model_map["Chinese"][0]
local_model_dir = Path(args.model_dir)
nn_model_file = local_model_dir / m_dict["nn_model_file"]
tokens_file = local_model_dir / m_dict["tokens_file"]
recognizer = load_sherpa_offline_recognizer(
nn_model_file=nn_model_file.as_posix(),
tokens_file=tokens_file.as_posix(),
decoding_method="greedy_search",
num_active_paths=2,
)
# recognizer = models.load_recognizer(
# repo_id=m_dict["repo_id"],
# nn_model_file=nn_model_file.as_posix(),
# tokens_file=tokens_file.as_posix(),
# sub_folder=m_dict["sub_folder"],
# local_model_dir=local_model_dir,
# recognizer_type=m_dict["recognizer_type"],
# decoding_method="greedy_search",
# num_active_paths=2,
# )
# feat_config = sherpa.FeatureConfig(normalize_samples=False)
# feat_config.fbank_opts.frame_opts.samp_freq = args.sample_rate
# feat_config.fbank_opts.mel_opts.num_bins = 80
# feat_config.fbank_opts.frame_opts.dither = 0
#
# config = sherpa.OfflineRecognizerConfig(
# nn_model=nn_model_file.as_posix(),
# tokens=tokens_file.as_posix(),
# use_gpu=False,
# feat_config=feat_config,
# decoding_method="greedy_search",
# num_active_paths=2,
# )
# recognizer = sherpa.OfflineRecognizer(config)
text = decode.decode_by_recognizer(recognizer=recognizer,
filename=out_filename.as_posix(),
)
print("text: {}".format(text))
return
if __name__ == "__main__":
main()
|