Toggle navigation
Toggle navigation
此项目
正在载入...
Sign in
xuning
/
sherpaonnx
转到一个项目
Toggle navigation
项目
群组
代码片段
帮助
Toggle navigation pinning
Project
Activity
Repository
Pipelines
Graphs
Issues
0
Merge Requests
0
Wiki
Network
Create a new issue
Builds
Commits
Authored by
Fangjun Kuang
2023-02-26 20:33:16 +0800
Browse Files
Options
Browse Files
Download
Email Patches
Plain Diff
Committed by
GitHub
2023-02-26 20:33:16 +0800
Commit
343e732ccb546c4ee2cf398f9210728c0471f72a
343e732c
1 parent
5a8c3a6d
Refactor python examples (#67)
显示空白字符变更
内嵌
并排对比
正在显示
7 个修改的文件
包含
186 行增加
和
22 行删除
.github/scripts/test-python.sh
.gitignore
CMakeLists.txt
python-api-examples/decode-file.py
python-api-examples/speech-recognition-from-microphone-with-endpoint-detection.py
python-api-examples/speech-recognition-from-microphone.py
sherpa-onnx/csrc/onnx-utils.cc
.github/scripts/test-python.sh
查看文件 @
343e732
...
...
@@ -9,7 +9,7 @@ log() {
}
repo_url
=
https://huggingface.co/csukuangfj/sherpa-onnx-
lstm-en-2023-02-17
repo_url
=
https://huggingface.co/csukuangfj/sherpa-onnx-
streaming-zipformer-bilingual-zh-en-2023-02-20
log
"Start testing
${
repo_url
}
"
repo
=
$(
basename
$repo_url
)
...
...
@@ -30,4 +30,9 @@ ls -lh
ls -lh
$repo
python3 python-api-examples/decode-file.py
python3 ./python-api-examples/decode-file.py
\
--tokens
=
$repo
/tokens.txt
\
--encoder
=
$repo
/encoder-epoch-99-avg-1.onnx
\
--decoder
=
$repo
/decoder-epoch-99-avg-1.onnx
\
--joiner
=
$repo
/joiner-epoch-99-avg-1.onnx
\
--wave-filename
=
$repo
/test_wavs/4.wav
...
...
.gitignore
查看文件 @
343e732
...
...
@@ -33,3 +33,4 @@ decode-file
*.dylib
tokens.txt
*.onnx
log.txt
...
...
CMakeLists.txt
查看文件 @
343e732
cmake_minimum_required
(
VERSION 3.13 FATAL_ERROR
)
project
(
sherpa-onnx
)
set
(
SHERPA_ONNX_VERSION
"1.
1
"
)
set
(
SHERPA_ONNX_VERSION
"1.
2
"
)
# Disable warning about
#
...
...
python-api-examples/decode-file.py
100644 → 100755
查看文件 @
343e732
...
...
@@ -9,27 +9,83 @@ https://k2-fsa.github.io/sherpa/onnx/index.html
to install sherpa-onnx and to download the pre-trained models
used in this file.
"""
import
wav
e
import
argpars
e
import
time
import
wave
from
pathlib
import
Path
import
numpy
as
np
import
sherpa_onnx
def
assert_file_exists
(
filename
:
str
):
assert
Path
(
filename
)
.
is_file
(),
f
"{filename} does not exist!
\n
Please refer to https://k2-fsa.github.io/sherpa/onnx/pretrained_models/index.html to download it"
def
get_args
():
parser
=
argparse
.
ArgumentParser
(
formatter_class
=
argparse
.
ArgumentDefaultsHelpFormatter
)
parser
.
add_argument
(
"--tokens"
,
type
=
str
,
help
=
"Path to tokens.txt"
,
)
parser
.
add_argument
(
"--encoder"
,
type
=
str
,
help
=
"Path to the encoder model"
,
)
parser
.
add_argument
(
"--decoder"
,
type
=
str
,
help
=
"Path to the decoder model"
,
)
parser
.
add_argument
(
"--joiner"
,
type
=
str
,
help
=
"Path to the joiner model"
,
)
parser
.
add_argument
(
"--wave-filename"
,
type
=
str
,
help
=
"""Path to the wave filename. Must be 16 kHz,
mono with 16-bit samples"""
,
)
return
parser
.
parse_args
()
def
main
():
sample_rate
=
16000
num_threads
=
4
num_threads
=
2
args
=
get_args
()
assert_file_exists
(
args
.
encoder
)
assert_file_exists
(
args
.
decoder
)
assert_file_exists
(
args
.
joiner
)
assert_file_exists
(
args
.
tokens
)
if
not
Path
(
args
.
wave_filename
)
.
is_file
():
print
(
f
"{args.wave_filename} does not exist!"
)
return
recognizer
=
sherpa_onnx
.
OnlineRecognizer
(
tokens
=
"./sherpa-onnx-lstm-en-2023-02-17/tokens.txt"
,
encoder
=
"./sherpa-onnx-lstm-en-2023-02-17/encoder-epoch-99-avg-1.onnx"
,
decoder
=
"./sherpa-onnx-lstm-en-2023-02-17/decoder-epoch-99-avg-1.onnx"
,
joiner
=
"./sherpa-onnx-lstm-en-2023-02-17/joiner-epoch-99-avg-1.onnx"
,
tokens
=
args
.
tokens
,
encoder
=
args
.
encoder
,
decoder
=
args
.
decoder
,
joiner
=
args
.
joiner
,
num_threads
=
num_threads
,
sample_rate
=
sample_rate
,
feature_dim
=
80
,
)
filename
=
"./sherpa-onnx-lstm-en-2023-02-17/test_wavs/1089-134686-0001.wav"
with
wave
.
open
(
filename
)
as
f
:
with
wave
.
open
(
args
.
wave_filename
)
as
f
:
assert
f
.
getframerate
()
==
sample_rate
,
f
.
getframerate
()
assert
f
.
getnchannels
()
==
1
,
f
.
getnchannels
()
assert
f
.
getsampwidth
()
==
2
,
f
.
getsampwidth
()
# it is in bytes
...
...
python-api-examples/speech-recognition-from-microphone-with-endpoint-detection.py
100644 → 100755
查看文件 @
343e732
...
...
@@ -7,7 +7,9 @@
# https://k2-fsa.github.io/sherpa/onnx/pretrained_models/index.html
# to download pre-trained models
import
argparse
import
sys
from
pathlib
import
Path
try
:
import
sounddevice
as
sd
...
...
@@ -22,18 +24,65 @@ except ImportError as e:
import
sherpa_onnx
def
assert_file_exists
(
filename
:
str
):
assert
Path
(
filename
)
.
is_file
(),
f
"{filename} does not exist!
\n
Please refer to https://k2-fsa.github.io/sherpa/onnx/pretrained_models/index.html to download it"
def
get_args
():
parser
=
argparse
.
ArgumentParser
(
formatter_class
=
argparse
.
ArgumentDefaultsHelpFormatter
)
parser
.
add_argument
(
"--tokens"
,
type
=
str
,
help
=
"Path to tokens.txt"
,
)
parser
.
add_argument
(
"--encoder"
,
type
=
str
,
help
=
"Path to the encoder model"
,
)
parser
.
add_argument
(
"--decoder"
,
type
=
str
,
help
=
"Path to the decoder model"
,
)
parser
.
add_argument
(
"--joiner"
,
type
=
str
,
help
=
"Path to the joiner model"
,
)
parser
.
add_argument
(
"--wave-filename"
,
type
=
str
,
help
=
"""Path to the wave filename. Must be 16 kHz,
mono with 16-bit samples"""
,
)
return
parser
.
parse_args
()
def
create_recognizer
():
args
=
get_args
()
assert_file_exists
(
args
.
encoder
)
assert_file_exists
(
args
.
decoder
)
assert_file_exists
(
args
.
joiner
)
assert_file_exists
(
args
.
tokens
)
# Please replace the model files if needed.
# See https://k2-fsa.github.io/sherpa/onnx/pretrained_models/index.html
# for download links.
recognizer
=
sherpa_onnx
.
OnlineRecognizer
(
tokens
=
"./sherpa-onnx-streaming-zipformer-bilingual-zh-en-2023-02-20/tokens.txt"
,
encoder
=
"./sherpa-onnx-streaming-zipformer-bilingual-zh-en-2023-02-20/encoder-epoch-99-avg-1.onnx"
,
decoder
=
"./sherpa-onnx-streaming-zipformer-bilingual-zh-en-2023-02-20/decoder-epoch-99-avg-1.onnx"
,
joiner
=
"./sherpa-onnx-streaming-zipformer-bilingual-zh-en-2023-02-20/joiner-epoch-99-avg-1.onnx"
,
num_threads
=
4
,
sample_rate
=
16000
,
feature_dim
=
80
,
tokens
=
args
.
tokens
,
encoder
=
args
.
encoder
,
decoder
=
args
.
decoder
,
joiner
=
args
.
joiner
,
enable_endpoint_detection
=
True
,
rule1_min_trailing_silence
=
2.4
,
rule2_min_trailing_silence
=
1.2
,
...
...
python-api-examples/speech-recognition-from-microphone.py
100644 → 100755
查看文件 @
343e732
...
...
@@ -6,7 +6,9 @@
# https://k2-fsa.github.io/sherpa/onnx/pretrained_models/index.html
# to download pre-trained models
import
argparse
import
sys
from
pathlib
import
Path
try
:
import
sounddevice
as
sd
...
...
@@ -21,15 +23,65 @@ except ImportError as e:
import
sherpa_onnx
def
assert_file_exists
(
filename
:
str
):
assert
Path
(
filename
)
.
is_file
(),
f
"{filename} does not exist!
\n
Please refer to https://k2-fsa.github.io/sherpa/onnx/pretrained_models/index.html to download it"
def
get_args
():
parser
=
argparse
.
ArgumentParser
(
formatter_class
=
argparse
.
ArgumentDefaultsHelpFormatter
)
parser
.
add_argument
(
"--tokens"
,
type
=
str
,
help
=
"Path to tokens.txt"
,
)
parser
.
add_argument
(
"--encoder"
,
type
=
str
,
help
=
"Path to the encoder model"
,
)
parser
.
add_argument
(
"--decoder"
,
type
=
str
,
help
=
"Path to the decoder model"
,
)
parser
.
add_argument
(
"--joiner"
,
type
=
str
,
help
=
"Path to the joiner model"
,
)
parser
.
add_argument
(
"--wave-filename"
,
type
=
str
,
help
=
"""Path to the wave filename. Must be 16 kHz,
mono with 16-bit samples"""
,
)
return
parser
.
parse_args
()
def
create_recognizer
():
args
=
get_args
()
assert_file_exists
(
args
.
encoder
)
assert_file_exists
(
args
.
decoder
)
assert_file_exists
(
args
.
joiner
)
assert_file_exists
(
args
.
tokens
)
# Please replace the model files if needed.
# See https://k2-fsa.github.io/sherpa/onnx/pretrained_models/index.html
# for download links.
recognizer
=
sherpa_onnx
.
OnlineRecognizer
(
tokens
=
"./sherpa-onnx-lstm-en-2023-02-17/tokens.txt"
,
encoder
=
"./sherpa-onnx-lstm-en-2023-02-17/encoder-epoch-99-avg-1.onnx"
,
decoder
=
"./sherpa-onnx-lstm-en-2023-02-17/decoder-epoch-99-avg-1.onnx"
,
joiner
=
"./sherpa-onnx-lstm-en-2023-02-17/joiner-epoch-99-avg-1.onnx"
,
tokens
=
args
.
tokens
,
encoder
=
args
.
encoder
,
decoder
=
args
.
decoder
,
joiner
=
args
.
joiner
,
num_threads
=
4
,
sample_rate
=
16000
,
feature_dim
=
80
,
...
...
sherpa-onnx/csrc/onnx-utils.cc
查看文件 @
343e732
...
...
@@ -3,6 +3,7 @@
// Copyright (c) 2023 Xiaomi Corporation
#include "sherpa-onnx/csrc/onnx-utils.h"
#include <algorithm>
#include <fstream>
#include <string>
#include <vector>
...
...
请
注册
或
登录
后发表评论