mirror of
https://gitee.com/270580156/weiyu.git
synced 2026-05-16 04:07:51 +00:00
Sync from bytedesk-private: update
This commit is contained in:
54
modules/python/vendors/FunASR/runtime/onnxruntime/bin/CMakeLists.txt
vendored
Normal file
54
modules/python/vendors/FunASR/runtime/onnxruntime/bin/CMakeLists.txt
vendored
Normal file
@@ -0,0 +1,54 @@
|
||||
include_directories(${CMAKE_SOURCE_DIR}/include)
|
||||
|
||||
if(WIN32)
|
||||
add_compile_options("$<$<CXX_COMPILER_ID:MSVC>:/execution-charset:utf-8>")
|
||||
add_compile_options("$<$<CXX_COMPILER_ID:MSVC>:/source-charset:utf-8>")
|
||||
include_directories(${ONNXRUNTIME_DIR}/include)
|
||||
include_directories(${FFMPEG_DIR}/include)
|
||||
include_directories(${PROJECT_SOURCE_DIR}/third_party)
|
||||
SET(RELATION_SOURCE "../src/resample.cpp" "../src/util.cpp" "../src/alignedmem.cpp" "../src/encode_converter.cpp")
|
||||
endif()
|
||||
|
||||
add_executable(funasr-onnx-offline "funasr-onnx-offline.cpp" ${RELATION_SOURCE})
|
||||
target_link_options(funasr-onnx-offline PRIVATE "-Wl,--no-as-needed")
|
||||
target_link_libraries(funasr-onnx-offline PUBLIC funasr)
|
||||
|
||||
add_executable(funasr-onnx-offline-vad "funasr-onnx-offline-vad.cpp" ${RELATION_SOURCE})
|
||||
target_link_options(funasr-onnx-offline-vad PRIVATE "-Wl,--no-as-needed")
|
||||
target_link_libraries(funasr-onnx-offline-vad PUBLIC funasr)
|
||||
|
||||
add_executable(funasr-onnx-online-vad "funasr-onnx-online-vad.cpp" ${RELATION_SOURCE})
|
||||
target_link_options(funasr-onnx-online-vad PRIVATE "-Wl,--no-as-needed")
|
||||
target_link_libraries(funasr-onnx-online-vad PUBLIC funasr)
|
||||
|
||||
add_executable(funasr-onnx-online-asr "funasr-onnx-online-asr.cpp" ${RELATION_SOURCE})
|
||||
target_link_options(funasr-onnx-online-asr PRIVATE "-Wl,--no-as-needed")
|
||||
target_link_libraries(funasr-onnx-online-asr PUBLIC funasr)
|
||||
|
||||
add_executable(funasr-onnx-offline-punc "funasr-onnx-offline-punc.cpp" ${RELATION_SOURCE})
|
||||
target_link_options(funasr-onnx-offline-punc PRIVATE "-Wl,--no-as-needed")
|
||||
target_link_libraries(funasr-onnx-offline-punc PUBLIC funasr)
|
||||
|
||||
add_executable(funasr-onnx-online-punc "funasr-onnx-online-punc.cpp" ${RELATION_SOURCE})
|
||||
target_link_options(funasr-onnx-online-punc PRIVATE "-Wl,--no-as-needed")
|
||||
target_link_libraries(funasr-onnx-online-punc PUBLIC funasr)
|
||||
|
||||
add_executable(funasr-onnx-offline-rtf "funasr-onnx-offline-rtf.cpp" ${RELATION_SOURCE})
|
||||
target_link_options(funasr-onnx-offline-rtf PRIVATE "-Wl,--no-as-needed")
|
||||
target_link_libraries(funasr-onnx-offline-rtf PUBLIC funasr)
|
||||
|
||||
add_executable(funasr-onnx-2pass "funasr-onnx-2pass.cpp" ${RELATION_SOURCE})
|
||||
target_link_options(funasr-onnx-2pass PRIVATE "-Wl,--no-as-needed")
|
||||
target_link_libraries(funasr-onnx-2pass PUBLIC funasr)
|
||||
|
||||
add_executable(funasr-onnx-2pass-rtf "funasr-onnx-2pass-rtf.cpp" ${RELATION_SOURCE})
|
||||
target_link_options(funasr-onnx-2pass-rtf PRIVATE "-Wl,--no-as-needed")
|
||||
target_link_libraries(funasr-onnx-2pass-rtf PUBLIC funasr)
|
||||
|
||||
add_executable(funasr-onnx-online-rtf "funasr-onnx-online-rtf.cpp" ${RELATION_SOURCE})
|
||||
target_link_options(funasr-onnx-online-rtf PRIVATE "-Wl,--no-as-needed")
|
||||
target_link_libraries(funasr-onnx-online-rtf PUBLIC funasr)
|
||||
|
||||
# include_directories(${FFMPEG_DIR}/include)
|
||||
# add_executable(ff "ffmpeg.cpp")
|
||||
# target_link_libraries(ff PUBLIC avutil avcodec avformat swresample)
|
||||
369
modules/python/vendors/FunASR/runtime/onnxruntime/bin/funasr-onnx-2pass-rtf.cpp
vendored
Normal file
369
modules/python/vendors/FunASR/runtime/onnxruntime/bin/funasr-onnx-2pass-rtf.cpp
vendored
Normal file
@@ -0,0 +1,369 @@
|
||||
/**
|
||||
* Copyright FunASR (https://github.com/alibaba-damo-academy/FunASR). All Rights Reserved.
|
||||
* MIT License (https://opensource.org/licenses/MIT)
|
||||
*/
|
||||
|
||||
#ifndef _WIN32
|
||||
#include <sys/time.h>
|
||||
#else
|
||||
#include <win_func.h>
|
||||
#endif
|
||||
|
||||
#include <iostream>
|
||||
#include <fstream>
|
||||
#include <sstream>
|
||||
#include <map>
|
||||
#include <atomic>
|
||||
#include <mutex>
|
||||
#include <thread>
|
||||
#include <glog/logging.h>
|
||||
#include "util.h"
|
||||
#include "funasrruntime.h"
|
||||
#include "tclap/CmdLine.h"
|
||||
#include "com-define.h"
|
||||
#include "audio.h"
|
||||
|
||||
using namespace std;
|
||||
|
||||
std::atomic<int> wav_index(0);
|
||||
std::mutex mtx;
|
||||
|
||||
bool is_target_file(const std::string& filename, const std::string target) {
|
||||
std::size_t pos = filename.find_last_of(".");
|
||||
if (pos == std::string::npos) {
|
||||
return false;
|
||||
}
|
||||
std::string extension = filename.substr(pos + 1);
|
||||
return (extension == target);
|
||||
}
|
||||
|
||||
void GetValue(TCLAP::ValueArg<std::string>& value_arg, string key, std::map<std::string, std::string>& model_path)
|
||||
{
|
||||
model_path.insert({key, value_arg.getValue()});
|
||||
LOG(INFO)<< key << " : " << value_arg.getValue();
|
||||
}
|
||||
|
||||
void runReg(FUNASR_HANDLE tpass_handle, std::vector<int> chunk_size, vector<string> wav_list, vector<string> wav_ids, int audio_fs,
|
||||
float* total_length, long* total_time, int core_id, ASR_TYPE asr_mode_, string nn_hotwords_,
|
||||
float glob_beam, float lat_beam, float am_scale, int inc_bias, unordered_map<string, int> hws_map) {
|
||||
|
||||
struct timeval start, end;
|
||||
long seconds = 0;
|
||||
float n_total_length = 0.0f;
|
||||
long n_total_time = 0;
|
||||
|
||||
FUNASR_DEC_HANDLE decoder_handle = FunASRWfstDecoderInit(tpass_handle, ASR_TWO_PASS, glob_beam, lat_beam, am_scale);
|
||||
// load hotwords list and build graph
|
||||
FunWfstDecoderLoadHwsRes(decoder_handle, inc_bias, hws_map);
|
||||
|
||||
std::vector<std::vector<float>> hotwords_embedding = CompileHotwordEmbedding(tpass_handle, nn_hotwords_, ASR_TWO_PASS);
|
||||
|
||||
// init online features
|
||||
FUNASR_HANDLE tpass_online_handle=FunTpassOnlineInit(tpass_handle, chunk_size);
|
||||
|
||||
// warm up
|
||||
for (size_t i = 0; i < 2; i++)
|
||||
{
|
||||
int32_t sampling_rate_ = audio_fs;
|
||||
funasr::Audio audio(1);
|
||||
if(is_target_file(wav_list[0].c_str(), "wav")){
|
||||
if(!audio.LoadWav2Char(wav_list[0].c_str(), &sampling_rate_)){
|
||||
LOG(ERROR)<<"Failed to load "<< wav_list[0];
|
||||
exit(-1);
|
||||
}
|
||||
}else if(is_target_file(wav_list[0].c_str(), "pcm")){
|
||||
if (!audio.LoadPcmwav2Char(wav_list[0].c_str(), &sampling_rate_)){
|
||||
LOG(ERROR)<<"Failed to load "<< wav_list[0];
|
||||
exit(-1);
|
||||
}
|
||||
}else{
|
||||
if (!audio.FfmpegLoad(wav_list[0].c_str(), true)){
|
||||
LOG(ERROR)<<"Failed to load "<< wav_list[0];
|
||||
exit(-1);
|
||||
}
|
||||
}
|
||||
char* speech_buff = audio.GetSpeechChar();
|
||||
int buff_len = audio.GetSpeechLen()*2;
|
||||
|
||||
int step = 1600*2;
|
||||
bool is_final = false;
|
||||
std::vector<std::vector<string>> punc_cache(2);
|
||||
for (int sample_offset = 0; sample_offset < buff_len; sample_offset += std::min(step, buff_len - sample_offset)) {
|
||||
if (sample_offset + step >= buff_len - 1) {
|
||||
step = buff_len - sample_offset;
|
||||
is_final = true;
|
||||
} else {
|
||||
is_final = false;
|
||||
}
|
||||
FUNASR_RESULT result = FunTpassInferBuffer(tpass_handle, tpass_online_handle, speech_buff+sample_offset, step, punc_cache, is_final,
|
||||
sampling_rate_, "pcm", (ASR_TYPE)asr_mode_, hotwords_embedding, true, decoder_handle);
|
||||
if (result)
|
||||
{
|
||||
FunASRFreeResult(result);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
while (true) {
|
||||
// 使用原子变量获取索引并递增
|
||||
int i = wav_index.fetch_add(1);
|
||||
if (i >= wav_list.size()) {
|
||||
break;
|
||||
}
|
||||
int32_t sampling_rate_ = audio_fs;
|
||||
funasr::Audio audio(1);
|
||||
if(is_target_file(wav_list[i].c_str(), "wav")){
|
||||
if(!audio.LoadWav2Char(wav_list[i].c_str(), &sampling_rate_)){
|
||||
LOG(ERROR)<<"Failed to load "<< wav_list[i];
|
||||
exit(-1);
|
||||
}
|
||||
}else if(is_target_file(wav_list[i].c_str(), "pcm")){
|
||||
if (!audio.LoadPcmwav2Char(wav_list[i].c_str(), &sampling_rate_)){
|
||||
LOG(ERROR)<<"Failed to load "<< wav_list[i];
|
||||
exit(-1);
|
||||
}
|
||||
}else{
|
||||
if (!audio.FfmpegLoad(wav_list[i].c_str(), true)){
|
||||
LOG(ERROR)<<"Failed to load "<< wav_list[i];
|
||||
exit(-1);
|
||||
}
|
||||
}
|
||||
char* speech_buff = audio.GetSpeechChar();
|
||||
int buff_len = audio.GetSpeechLen()*2;
|
||||
|
||||
int step = 1600*2;
|
||||
bool is_final = false;
|
||||
string online_res="";
|
||||
string tpass_res="";
|
||||
string time_stamp_res="";
|
||||
std::vector<std::vector<string>> punc_cache(2);
|
||||
for (int sample_offset = 0; sample_offset < buff_len; sample_offset += std::min(step, buff_len - sample_offset)) {
|
||||
if (sample_offset + step >= buff_len - 1) {
|
||||
step = buff_len - sample_offset;
|
||||
is_final = true;
|
||||
} else {
|
||||
is_final = false;
|
||||
}
|
||||
gettimeofday(&start, nullptr);
|
||||
FUNASR_RESULT result = FunTpassInferBuffer(tpass_handle, tpass_online_handle, speech_buff+sample_offset, step, punc_cache, is_final,
|
||||
sampling_rate_, "pcm", (ASR_TYPE)asr_mode_, hotwords_embedding, true, decoder_handle);
|
||||
gettimeofday(&end, nullptr);
|
||||
seconds = (end.tv_sec - start.tv_sec);
|
||||
long taking_micros = ((seconds * 1000000) + end.tv_usec) - (start.tv_usec);
|
||||
n_total_time += taking_micros;
|
||||
|
||||
if (result)
|
||||
{
|
||||
string online_msg = FunASRGetResult(result, 0);
|
||||
online_res += online_msg;
|
||||
if(online_msg != ""){
|
||||
LOG(INFO) <<"Thread: " << this_thread::get_id() <<" " << wav_ids[i] <<" : "<<online_msg;
|
||||
}
|
||||
string tpass_msg = FunASRGetTpassResult(result, 0);
|
||||
tpass_res += tpass_msg;
|
||||
if(tpass_msg != ""){
|
||||
LOG(INFO) <<"Thread: " << this_thread::get_id() <<" " << wav_ids[i] <<" offline results : "<<tpass_msg;
|
||||
}
|
||||
string stamp = FunASRGetStamp(result);
|
||||
if(stamp !=""){
|
||||
LOG(INFO) <<"Thread: " << this_thread::get_id() <<" " << wav_ids[i] << " time stamp : " << stamp;
|
||||
if(time_stamp_res == ""){
|
||||
time_stamp_res += stamp;
|
||||
}else{
|
||||
time_stamp_res = time_stamp_res.erase(time_stamp_res.length()-1)
|
||||
+ "," + stamp.substr(1);
|
||||
}
|
||||
}
|
||||
float snippet_time = FunASRGetRetSnippetTime(result);
|
||||
n_total_length += snippet_time;
|
||||
FunASRFreeResult(result);
|
||||
}
|
||||
else
|
||||
{
|
||||
LOG(ERROR) << ("No return data!\n");
|
||||
}
|
||||
}
|
||||
if(asr_mode_ == 2){
|
||||
LOG(INFO) <<"Thread: " << this_thread::get_id() <<" " << wav_ids[i] << " Final online results "<<" : "<<online_res;
|
||||
}
|
||||
if(asr_mode_==1){
|
||||
LOG(INFO) <<"Thread: " << this_thread::get_id() <<" " << wav_ids[i] << " Final online results "<<" : "<<tpass_res;
|
||||
}
|
||||
if(asr_mode_ == 0 || asr_mode_==2){
|
||||
LOG(INFO) <<"Thread: " << this_thread::get_id() <<" " << wav_ids[i] << " Final offline results " <<" : "<<tpass_res;
|
||||
if(time_stamp_res!=""){
|
||||
LOG(INFO) <<"Thread: " << this_thread::get_id() <<" " << wav_ids[i] << " Final timestamp results " <<" : "<<time_stamp_res;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
{
|
||||
lock_guard<mutex> guard(mtx);
|
||||
*total_length += n_total_length;
|
||||
if(*total_time < n_total_time){
|
||||
*total_time = n_total_time;
|
||||
}
|
||||
}
|
||||
FunWfstDecoderUnloadHwsRes(decoder_handle);
|
||||
FunASRWfstDecoderUninit(decoder_handle);
|
||||
FunTpassOnlineUninit(tpass_online_handle);
|
||||
}
|
||||
|
||||
|
||||
int main(int argc, char** argv)
|
||||
{
|
||||
google::InitGoogleLogging(argv[0]);
|
||||
FLAGS_logtostderr = true;
|
||||
|
||||
TCLAP::CmdLine cmd("funasr-onnx-2pass", ' ', "1.0");
|
||||
TCLAP::ValueArg<std::string> offline_model_dir("", OFFLINE_MODEL_DIR, "the asr offline model path, which contains model.onnx, config.yaml, am.mvn", true, "", "string");
|
||||
TCLAP::ValueArg<std::string> online_model_dir("", ONLINE_MODEL_DIR, "the asr online model path, which contains model.onnx, decoder.onnx, config.yaml, am.mvn", true, "", "string");
|
||||
TCLAP::ValueArg<std::string> quantize("", QUANTIZE, "true (Default), load the model of model.onnx in model_dir. If set true, load the model of model_quant.onnx in model_dir", false, "true", "string");
|
||||
TCLAP::ValueArg<std::string> vad_dir("", VAD_DIR, "the vad online model path, which contains model.onnx, vad.yaml, vad.mvn", false, "", "string");
|
||||
TCLAP::ValueArg<std::string> vad_quant("", VAD_QUANT, "true (Default), load the model of model.onnx in vad_dir. If set true, load the model of model_quant.onnx in vad_dir", false, "true", "string");
|
||||
TCLAP::ValueArg<std::string> punc_dir("", PUNC_DIR, "the punc online model path, which contains model.onnx, punc.yaml", false, "", "string");
|
||||
TCLAP::ValueArg<std::string> punc_quant("", PUNC_QUANT, "true (Default), load the model of model.onnx in punc_dir. If set true, load the model of model_quant.onnx in punc_dir", false, "true", "string");
|
||||
TCLAP::ValueArg<std::string> itn_dir("", ITN_DIR, "the itn model(fst) path, which contains zh_itn_tagger.fst and zh_itn_verbalizer.fst", false, "", "string");
|
||||
TCLAP::ValueArg<std::string> lm_dir("", LM_DIR, "the lm model path, which contains compiled models: TLG.fst, config.yaml, lexicon.txt ", false, "", "string");
|
||||
TCLAP::ValueArg<float> global_beam("", GLOB_BEAM, "the decoding beam for beam searching ", false, 3.0, "float");
|
||||
TCLAP::ValueArg<float> lattice_beam("", LAT_BEAM, "the lattice generation beam for beam searching ", false, 3.0, "float");
|
||||
TCLAP::ValueArg<float> am_scale("", AM_SCALE, "the acoustic scale for beam searching ", false, 10.0, "float");
|
||||
TCLAP::ValueArg<std::int32_t> fst_inc_wts("", FST_INC_WTS, "the fst hotwords incremental bias", false, 20, "int32_t");
|
||||
|
||||
TCLAP::ValueArg<std::string> asr_mode("", ASR_MODE, "offline, online, 2pass", false, "2pass", "string");
|
||||
TCLAP::ValueArg<std::int32_t> onnx_thread("", "model-thread-num", "onnxruntime SetIntraOpNumThreads", false, 1, "int32_t");
|
||||
TCLAP::ValueArg<std::int32_t> thread_num_("", THREAD_NUM, "multi-thread num for rtf", false, 1, "int32_t");
|
||||
TCLAP::ValueArg<std::string> wav_path("", WAV_PATH, "the input could be: wav_path, e.g.: asr_example.wav; pcm_path, e.g.: asr_example.pcm; wav.scp, kaldi style wav list (wav_id \t wav_path)", true, "", "string");
|
||||
TCLAP::ValueArg<std::int32_t> audio_fs("", AUDIO_FS, "the sample rate of audio", false, 16000, "int32_t");
|
||||
TCLAP::ValueArg<std::string> hotword("", HOTWORD, "the hotword file, one hotword perline, Format: Hotword Weight (could be: 阿里巴巴 20)", false, "", "string");
|
||||
|
||||
cmd.add(offline_model_dir);
|
||||
cmd.add(online_model_dir);
|
||||
cmd.add(quantize);
|
||||
cmd.add(vad_dir);
|
||||
cmd.add(vad_quant);
|
||||
cmd.add(punc_dir);
|
||||
cmd.add(punc_quant);
|
||||
cmd.add(itn_dir);
|
||||
cmd.add(lm_dir);
|
||||
cmd.add(global_beam);
|
||||
cmd.add(lattice_beam);
|
||||
cmd.add(am_scale);
|
||||
cmd.add(fst_inc_wts);
|
||||
cmd.add(wav_path);
|
||||
cmd.add(audio_fs);
|
||||
cmd.add(asr_mode);
|
||||
cmd.add(onnx_thread);
|
||||
cmd.add(thread_num_);
|
||||
cmd.add(hotword);
|
||||
cmd.parse(argc, argv);
|
||||
|
||||
std::map<std::string, std::string> model_path;
|
||||
GetValue(offline_model_dir, OFFLINE_MODEL_DIR, model_path);
|
||||
GetValue(online_model_dir, ONLINE_MODEL_DIR, model_path);
|
||||
GetValue(quantize, QUANTIZE, model_path);
|
||||
GetValue(vad_dir, VAD_DIR, model_path);
|
||||
GetValue(vad_quant, VAD_QUANT, model_path);
|
||||
GetValue(punc_dir, PUNC_DIR, model_path);
|
||||
GetValue(punc_quant, PUNC_QUANT, model_path);
|
||||
GetValue(itn_dir, ITN_DIR, model_path);
|
||||
GetValue(lm_dir, LM_DIR, model_path);
|
||||
GetValue(wav_path, WAV_PATH, model_path);
|
||||
GetValue(asr_mode, ASR_MODE, model_path);
|
||||
|
||||
struct timeval start, end;
|
||||
gettimeofday(&start, nullptr);
|
||||
int thread_num = onnx_thread.getValue();
|
||||
int asr_mode_ = -1;
|
||||
if(model_path[ASR_MODE] == "offline"){
|
||||
asr_mode_ = 0;
|
||||
}else if(model_path[ASR_MODE] == "online"){
|
||||
asr_mode_ = 1;
|
||||
}else if(model_path[ASR_MODE] == "2pass"){
|
||||
asr_mode_ = 2;
|
||||
}else{
|
||||
LOG(ERROR) << "Wrong asr-mode : " << model_path[ASR_MODE];
|
||||
exit(-1);
|
||||
}
|
||||
FUNASR_HANDLE tpass_hanlde=FunTpassInit(model_path, thread_num);
|
||||
|
||||
if (!tpass_hanlde)
|
||||
{
|
||||
LOG(ERROR) << "FunTpassInit init failed";
|
||||
exit(-1);
|
||||
}
|
||||
float glob_beam = 3.0f;
|
||||
float lat_beam = 3.0f;
|
||||
float am_sc = 10.0f;
|
||||
if (lm_dir.isSet()) {
|
||||
glob_beam = global_beam.getValue();
|
||||
lat_beam = lattice_beam.getValue();
|
||||
am_sc = am_scale.getValue();
|
||||
}
|
||||
|
||||
gettimeofday(&end, nullptr);
|
||||
long seconds = (end.tv_sec - start.tv_sec);
|
||||
long modle_init_micros = ((seconds * 1000000) + end.tv_usec) - (start.tv_usec);
|
||||
LOG(INFO) << "Model initialization takes " << (double)modle_init_micros / 1000000 << " s";
|
||||
|
||||
// hotword file
|
||||
unordered_map<string, int> hws_map;
|
||||
std::string nn_hotwords_ = "";
|
||||
std::string hotword_path = hotword.getValue();
|
||||
LOG(INFO) << "hotword path: " << hotword_path;
|
||||
funasr::ExtractHws(hotword_path, hws_map, nn_hotwords_);
|
||||
|
||||
// read wav_path
|
||||
vector<string> wav_list;
|
||||
vector<string> wav_ids;
|
||||
string default_id = "wav_default_id";
|
||||
string wav_path_ = model_path.at(WAV_PATH);
|
||||
|
||||
if(is_target_file(wav_path_, "scp")){
|
||||
ifstream in(wav_path_);
|
||||
if (!in.is_open()) {
|
||||
LOG(ERROR) << "Failed to open file: " << model_path.at(WAV_SCP) ;
|
||||
return 0;
|
||||
}
|
||||
string line;
|
||||
while(getline(in, line))
|
||||
{
|
||||
istringstream iss(line);
|
||||
string column1, column2;
|
||||
iss >> column1 >> column2;
|
||||
wav_list.emplace_back(column2);
|
||||
wav_ids.emplace_back(column1);
|
||||
}
|
||||
in.close();
|
||||
}else{
|
||||
wav_list.emplace_back(wav_path_);
|
||||
wav_ids.emplace_back(default_id);
|
||||
}
|
||||
|
||||
std::vector<int> chunk_size = {5,10,5};
|
||||
// 多线程测试
|
||||
float total_length = 0.0f;
|
||||
long total_time = 0;
|
||||
std::vector<std::thread> threads;
|
||||
|
||||
int rtf_threds = thread_num_.getValue();
|
||||
for (int i = 0; i < rtf_threds; i++)
|
||||
{
|
||||
threads.emplace_back(thread(runReg, tpass_hanlde, chunk_size, wav_list, wav_ids, audio_fs.getValue(), &total_length, &total_time, i, (ASR_TYPE)asr_mode_, nn_hotwords_,
|
||||
glob_beam, lat_beam, am_sc, fst_inc_wts.getValue(), hws_map));
|
||||
}
|
||||
|
||||
for (auto& thread : threads)
|
||||
{
|
||||
thread.join();
|
||||
}
|
||||
|
||||
LOG(INFO) << "total_time_wav " << (long)(total_length * 1000) << " ms";
|
||||
LOG(INFO) << "total_time_comput " << total_time / 1000 << " ms";
|
||||
LOG(INFO) << "total_rtf " << (double)total_time/ (total_length*1000000);
|
||||
LOG(INFO) << "speedup " << 1.0/((double)total_time/ (total_length*1000000));
|
||||
|
||||
FunTpassUninit(tpass_hanlde);
|
||||
return 0;
|
||||
}
|
||||
|
||||
274
modules/python/vendors/FunASR/runtime/onnxruntime/bin/funasr-onnx-2pass.cpp
vendored
Normal file
274
modules/python/vendors/FunASR/runtime/onnxruntime/bin/funasr-onnx-2pass.cpp
vendored
Normal file
@@ -0,0 +1,274 @@
|
||||
/**
|
||||
* Copyright FunASR (https://github.com/alibaba-damo-academy/FunASR). All Rights Reserved.
|
||||
* MIT License (https://opensource.org/licenses/MIT)
|
||||
*/
|
||||
|
||||
#ifndef _WIN32
|
||||
#include <sys/time.h>
|
||||
#else
|
||||
#include <win_func.h>
|
||||
#endif
|
||||
|
||||
#include <iostream>
|
||||
#include <fstream>
|
||||
#include <sstream>
|
||||
#include <map>
|
||||
#include <glog/logging.h>
|
||||
#include "util.h"
|
||||
#include "funasrruntime.h"
|
||||
#include "tclap/CmdLine.h"
|
||||
#include "com-define.h"
|
||||
#include "audio.h"
|
||||
|
||||
using namespace std;
|
||||
|
||||
bool is_target_file(const std::string& filename, const std::string target) {
|
||||
std::size_t pos = filename.find_last_of(".");
|
||||
if (pos == std::string::npos) {
|
||||
return false;
|
||||
}
|
||||
std::string extension = filename.substr(pos + 1);
|
||||
return (extension == target);
|
||||
}
|
||||
|
||||
void GetValue(TCLAP::ValueArg<std::string>& value_arg, string key, std::map<std::string, std::string>& model_path)
|
||||
{
|
||||
model_path.insert({key, value_arg.getValue()});
|
||||
LOG(INFO)<< key << " : " << value_arg.getValue();
|
||||
}
|
||||
|
||||
int main(int argc, char** argv)
|
||||
{
|
||||
google::InitGoogleLogging(argv[0]);
|
||||
FLAGS_logtostderr = true;
|
||||
|
||||
TCLAP::CmdLine cmd("funasr-onnx-2pass", ' ', "1.0");
|
||||
TCLAP::ValueArg<std::string> offline_model_dir("", OFFLINE_MODEL_DIR, "the asr offline model path, which contains model.onnx, config.yaml, am.mvn", true, "", "string");
|
||||
TCLAP::ValueArg<std::string> online_model_dir("", ONLINE_MODEL_DIR, "the asr online model path, which contains model.onnx, decoder.onnx, config.yaml, am.mvn", true, "", "string");
|
||||
TCLAP::ValueArg<std::string> quantize("", QUANTIZE, "true (Default), load the model of model.onnx in model_dir. If set true, load the model of model_quant.onnx in model_dir", false, "true", "string");
|
||||
TCLAP::ValueArg<std::string> vad_dir("", VAD_DIR, "the vad online model path, which contains model.onnx, vad.yaml, vad.mvn", false, "", "string");
|
||||
TCLAP::ValueArg<std::string> vad_quant("", VAD_QUANT, "true (Default), load the model of model.onnx in vad_dir. If set true, load the model of model_quant.onnx in vad_dir", false, "true", "string");
|
||||
TCLAP::ValueArg<std::string> punc_dir("", PUNC_DIR, "the punc online model path, which contains model.onnx, punc.yaml", false, "", "string");
|
||||
TCLAP::ValueArg<std::string> punc_quant("", PUNC_QUANT, "true (Default), load the model of model.onnx in punc_dir. If set true, load the model of model_quant.onnx in punc_dir", false, "true", "string");
|
||||
TCLAP::ValueArg<std::string> itn_dir("", ITN_DIR, "the itn model(fst) path, which contains zh_itn_tagger.fst and zh_itn_verbalizer.fst", false, "", "string");
|
||||
TCLAP::ValueArg<std::string> lm_dir("", LM_DIR, "the lm model path, which contains compiled models: TLG.fst, config.yaml, lexicon.txt ", false, "", "string");
|
||||
TCLAP::ValueArg<float> global_beam("", GLOB_BEAM, "the decoding beam for beam searching ", false, 3.0, "float");
|
||||
TCLAP::ValueArg<float> lattice_beam("", LAT_BEAM, "the lattice generation beam for beam searching ", false, 3.0, "float");
|
||||
TCLAP::ValueArg<float> am_scale("", AM_SCALE, "the acoustic scale for beam searching ", false, 10.0, "float");
|
||||
TCLAP::ValueArg<std::int32_t> fst_inc_wts("", FST_INC_WTS, "the fst hotwords incremental bias", false, 20, "int32_t");
|
||||
TCLAP::ValueArg<std::string> asr_mode("", ASR_MODE, "offline, online, 2pass", false, "2pass", "string");
|
||||
TCLAP::ValueArg<std::int32_t> onnx_thread("", "model-thread-num", "onnxruntime SetIntraOpNumThreads", false, 1, "int32_t");
|
||||
|
||||
TCLAP::ValueArg<std::string> wav_path("", WAV_PATH, "the input could be: wav_path, e.g.: asr_example.wav; pcm_path, e.g.: asr_example.pcm; wav.scp, kaldi style wav list (wav_id \t wav_path)", true, "", "string");
|
||||
TCLAP::ValueArg<std::int32_t> audio_fs("", AUDIO_FS, "the sample rate of audio", false, 16000, "int32_t");
|
||||
TCLAP::ValueArg<std::string> hotword("", HOTWORD, "the hotword file, one hotword perline, Format: Hotword Weight (could be: 阿里巴巴 20)", false, "", "string");
|
||||
|
||||
cmd.add(offline_model_dir);
|
||||
cmd.add(online_model_dir);
|
||||
cmd.add(quantize);
|
||||
cmd.add(vad_dir);
|
||||
cmd.add(vad_quant);
|
||||
cmd.add(punc_dir);
|
||||
cmd.add(punc_quant);
|
||||
cmd.add(lm_dir);
|
||||
cmd.add(global_beam);
|
||||
cmd.add(lattice_beam);
|
||||
cmd.add(am_scale);
|
||||
cmd.add(fst_inc_wts);
|
||||
cmd.add(itn_dir);
|
||||
cmd.add(wav_path);
|
||||
cmd.add(audio_fs);
|
||||
cmd.add(asr_mode);
|
||||
cmd.add(onnx_thread);
|
||||
cmd.add(hotword);
|
||||
cmd.parse(argc, argv);
|
||||
|
||||
std::map<std::string, std::string> model_path;
|
||||
GetValue(offline_model_dir, OFFLINE_MODEL_DIR, model_path);
|
||||
GetValue(online_model_dir, ONLINE_MODEL_DIR, model_path);
|
||||
GetValue(quantize, QUANTIZE, model_path);
|
||||
GetValue(vad_dir, VAD_DIR, model_path);
|
||||
GetValue(vad_quant, VAD_QUANT, model_path);
|
||||
GetValue(punc_dir, PUNC_DIR, model_path);
|
||||
GetValue(punc_quant, PUNC_QUANT, model_path);
|
||||
GetValue(lm_dir, LM_DIR, model_path);
|
||||
GetValue(itn_dir, ITN_DIR, model_path);
|
||||
GetValue(wav_path, WAV_PATH, model_path);
|
||||
GetValue(asr_mode, ASR_MODE, model_path);
|
||||
|
||||
struct timeval start, end;
|
||||
gettimeofday(&start, nullptr);
|
||||
int thread_num = onnx_thread.getValue();
|
||||
int asr_mode_ = -1;
|
||||
if(model_path[ASR_MODE] == "offline"){
|
||||
asr_mode_ = 0;
|
||||
}else if(model_path[ASR_MODE] == "online"){
|
||||
asr_mode_ = 1;
|
||||
}else if(model_path[ASR_MODE] == "2pass"){
|
||||
asr_mode_ = 2;
|
||||
}else{
|
||||
LOG(ERROR) << "Wrong asr-mode : " << model_path[ASR_MODE];
|
||||
exit(-1);
|
||||
}
|
||||
FUNASR_HANDLE tpass_handle=FunTpassInit(model_path, thread_num);
|
||||
|
||||
if (!tpass_handle)
|
||||
{
|
||||
LOG(ERROR) << "FunTpassInit init failed";
|
||||
exit(-1);
|
||||
}
|
||||
float glob_beam = 3.0f;
|
||||
float lat_beam = 3.0f;
|
||||
float am_sc = 10.0f;
|
||||
if (lm_dir.isSet()) {
|
||||
glob_beam = global_beam.getValue();
|
||||
lat_beam = lattice_beam.getValue();
|
||||
am_sc = am_scale.getValue();
|
||||
}
|
||||
// init wfst decoder
|
||||
FUNASR_DEC_HANDLE decoder_handle = FunASRWfstDecoderInit(tpass_handle, ASR_TWO_PASS, glob_beam, lat_beam, am_sc);
|
||||
|
||||
gettimeofday(&end, nullptr);
|
||||
long seconds = (end.tv_sec - start.tv_sec);
|
||||
long modle_init_micros = ((seconds * 1000000) + end.tv_usec) - (start.tv_usec);
|
||||
LOG(INFO) << "Model initialization takes " << (double)modle_init_micros / 1000000 << " s";
|
||||
|
||||
// hotword file
|
||||
unordered_map<string, int> hws_map;
|
||||
std::string nn_hotwords_ = "";
|
||||
std::string hotword_path = hotword.getValue();
|
||||
LOG(INFO) << "hotword path: " << hotword_path;
|
||||
funasr::ExtractHws(hotword_path, hws_map, nn_hotwords_);
|
||||
|
||||
// read wav_path
|
||||
vector<string> wav_list;
|
||||
vector<string> wav_ids;
|
||||
string default_id = "wav_default_id";
|
||||
string wav_path_ = model_path.at(WAV_PATH);
|
||||
|
||||
if(is_target_file(wav_path_, "scp")){
|
||||
ifstream in(wav_path_);
|
||||
if (!in.is_open()) {
|
||||
LOG(ERROR) << "Failed to open file: " << model_path.at(WAV_SCP) ;
|
||||
return 0;
|
||||
}
|
||||
string line;
|
||||
while(getline(in, line))
|
||||
{
|
||||
istringstream iss(line);
|
||||
string column1, column2;
|
||||
iss >> column1 >> column2;
|
||||
wav_list.emplace_back(column2);
|
||||
wav_ids.emplace_back(column1);
|
||||
}
|
||||
in.close();
|
||||
}else{
|
||||
wav_list.emplace_back(wav_path_);
|
||||
wav_ids.emplace_back(default_id);
|
||||
}
|
||||
|
||||
// load hotwords list and build graph
|
||||
FunWfstDecoderLoadHwsRes(decoder_handle, fst_inc_wts.getValue(), hws_map);
|
||||
|
||||
std::vector<std::vector<float>> hotwords_embedding = CompileHotwordEmbedding(tpass_handle, nn_hotwords_, ASR_TWO_PASS);
|
||||
// init online features
|
||||
std::vector<int> chunk_size = {5,10,5};
|
||||
FUNASR_HANDLE tpass_online_handle=FunTpassOnlineInit(tpass_handle, chunk_size);
|
||||
float snippet_time = 0.0f;
|
||||
long taking_micros = 0;
|
||||
for (int i = 0; i < wav_list.size(); i++) {
|
||||
auto& wav_file = wav_list[i];
|
||||
auto& wav_id = wav_ids[i];
|
||||
|
||||
int32_t sampling_rate_ = audio_fs.getValue();
|
||||
funasr::Audio audio(1);
|
||||
if(is_target_file(wav_file.c_str(), "wav")){
|
||||
if(!audio.LoadWav2Char(wav_file.c_str(), &sampling_rate_)){
|
||||
LOG(ERROR)<<"Failed to load "<< wav_file;
|
||||
exit(-1);
|
||||
}
|
||||
}else if(is_target_file(wav_file.c_str(), "pcm")){
|
||||
if (!audio.LoadPcmwav2Char(wav_file.c_str(), &sampling_rate_)){
|
||||
LOG(ERROR)<<"Failed to load "<< wav_file;
|
||||
exit(-1);
|
||||
}
|
||||
}else{
|
||||
if (!audio.FfmpegLoad(wav_file.c_str(), true)){
|
||||
LOG(ERROR)<<"Failed to load "<< wav_file;
|
||||
exit(-1);
|
||||
}
|
||||
}
|
||||
char* speech_buff = audio.GetSpeechChar();
|
||||
int buff_len = audio.GetSpeechLen()*2;
|
||||
|
||||
int step = 800*2;
|
||||
bool is_final = false;
|
||||
string online_res="";
|
||||
string tpass_res="";
|
||||
string time_stamp_res="";
|
||||
std::vector<std::vector<string>> punc_cache(2);
|
||||
for (int sample_offset = 0; sample_offset < buff_len; sample_offset += std::min(step, buff_len - sample_offset)) {
|
||||
if (sample_offset + step >= buff_len - 1) {
|
||||
step = buff_len - sample_offset;
|
||||
is_final = true;
|
||||
} else {
|
||||
is_final = false;
|
||||
}
|
||||
gettimeofday(&start, nullptr);
|
||||
FUNASR_RESULT result = FunTpassInferBuffer(tpass_handle, tpass_online_handle,
|
||||
speech_buff+sample_offset, step, punc_cache, is_final, sampling_rate_, "pcm",
|
||||
(ASR_TYPE)asr_mode_, hotwords_embedding, true, decoder_handle);
|
||||
gettimeofday(&end, nullptr);
|
||||
seconds = (end.tv_sec - start.tv_sec);
|
||||
taking_micros += ((seconds * 1000000) + end.tv_usec) - (start.tv_usec);
|
||||
|
||||
if (result)
|
||||
{
|
||||
string online_msg = FunASRGetResult(result, 0);
|
||||
online_res += online_msg;
|
||||
if(online_msg != ""){
|
||||
LOG(INFO)<< wav_id <<" : "<<online_msg;
|
||||
}
|
||||
string tpass_msg = FunASRGetTpassResult(result, 0);
|
||||
tpass_res += tpass_msg;
|
||||
if(tpass_msg != ""){
|
||||
LOG(INFO)<< wav_id <<" offline results : "<<tpass_msg;
|
||||
}
|
||||
string stamp = FunASRGetStamp(result);
|
||||
if(stamp !=""){
|
||||
LOG(INFO) << wav_ids[i] << " time stamp : " << stamp;
|
||||
if(time_stamp_res == ""){
|
||||
time_stamp_res += stamp;
|
||||
}else{
|
||||
time_stamp_res = time_stamp_res.erase(time_stamp_res.length()-1)
|
||||
+ "," + stamp.substr(1);
|
||||
}
|
||||
}
|
||||
snippet_time += FunASRGetRetSnippetTime(result);
|
||||
FunASRFreeResult(result);
|
||||
}
|
||||
}
|
||||
if(asr_mode_==2){
|
||||
LOG(INFO) << wav_id << " Final online results "<<" : "<<online_res;
|
||||
}
|
||||
if(asr_mode_==1){
|
||||
LOG(INFO) << wav_id << " Final online results "<<" : "<<tpass_res;
|
||||
}
|
||||
if(asr_mode_==0 || asr_mode_==2){
|
||||
LOG(INFO) << wav_id << " Final offline results " <<" : "<<tpass_res;
|
||||
if(time_stamp_res!=""){
|
||||
LOG(INFO) << wav_id << " Final timestamp results " <<" : "<<time_stamp_res;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
FunWfstDecoderUnloadHwsRes(decoder_handle);
|
||||
LOG(INFO) << "Audio length: " << (double)snippet_time << " s";
|
||||
LOG(INFO) << "Model inference takes: " << (double)taking_micros / 1000000 <<" s";
|
||||
LOG(INFO) << "Model inference RTF: " << (double)taking_micros/ (snippet_time*1000000);
|
||||
FunASRWfstDecoderUninit(decoder_handle);
|
||||
FunTpassOnlineUninit(tpass_online_handle);
|
||||
FunTpassUninit(tpass_handle);
|
||||
return 0;
|
||||
}
|
||||
|
||||
100
modules/python/vendors/FunASR/runtime/onnxruntime/bin/funasr-onnx-offline-punc.cpp
vendored
Normal file
100
modules/python/vendors/FunASR/runtime/onnxruntime/bin/funasr-onnx-offline-punc.cpp
vendored
Normal file
@@ -0,0 +1,100 @@
|
||||
/**
|
||||
* Copyright FunASR (https://github.com/alibaba-damo-academy/FunASR). All Rights Reserved.
|
||||
* MIT License (https://opensource.org/licenses/MIT)
|
||||
*/
|
||||
|
||||
#ifndef _WIN32
|
||||
#include <sys/time.h>
|
||||
#else
|
||||
#include <win_func.h>
|
||||
#endif
|
||||
|
||||
#include <iostream>
|
||||
#include <fstream>
|
||||
#include <sstream>
|
||||
#include <map>
|
||||
#include <glog/logging.h>
|
||||
#include "funasrruntime.h"
|
||||
#include "tclap/CmdLine.h"
|
||||
#include "com-define.h"
|
||||
|
||||
using namespace std;
|
||||
|
||||
void GetValue(TCLAP::ValueArg<std::string>& value_arg, string key, std::map<std::string, std::string>& model_path)
|
||||
{
|
||||
if (value_arg.isSet()){
|
||||
model_path.insert({key, value_arg.getValue()});
|
||||
LOG(INFO)<< key << " : " << value_arg.getValue();
|
||||
}
|
||||
}
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
google::InitGoogleLogging(argv[0]);
|
||||
FLAGS_logtostderr = true;
|
||||
|
||||
TCLAP::CmdLine cmd("funasr-onnx-offline-punc", ' ', "1.0");
|
||||
TCLAP::ValueArg<std::string> model_dir("", MODEL_DIR, "the punc model path, which contains model.onnx, punc.yaml", true, "", "string");
|
||||
TCLAP::ValueArg<std::string> quantize("", QUANTIZE, "false (Default), load the model of model.onnx in model_dir. If set true, load the model of model_quant.onnx in model_dir", false, "true", "string");
|
||||
TCLAP::ValueArg<std::string> txt_path("", TXT_PATH, "txt file path, one sentence per line", true, "", "string");
|
||||
|
||||
cmd.add(model_dir);
|
||||
cmd.add(quantize);
|
||||
cmd.add(txt_path);
|
||||
cmd.parse(argc, argv);
|
||||
|
||||
std::map<std::string, std::string> model_path;
|
||||
GetValue(model_dir, MODEL_DIR, model_path);
|
||||
GetValue(quantize, QUANTIZE, model_path);
|
||||
GetValue(txt_path, TXT_PATH, model_path);
|
||||
|
||||
struct timeval start, end;
|
||||
gettimeofday(&start, nullptr);
|
||||
int thread_num = 1;
|
||||
FUNASR_HANDLE punc_hanlde=CTTransformerInit(model_path, thread_num);
|
||||
|
||||
if (!punc_hanlde)
|
||||
{
|
||||
LOG(ERROR) << "FunASR init failed";
|
||||
exit(-1);
|
||||
}
|
||||
|
||||
gettimeofday(&end, nullptr);
|
||||
long seconds = (end.tv_sec - start.tv_sec);
|
||||
long modle_init_micros = ((seconds * 1000000) + end.tv_usec) - (start.tv_usec);
|
||||
LOG(INFO) << "Model initialization takes " << (double)modle_init_micros / 1000000 << " s";
|
||||
|
||||
// read txt_path
|
||||
vector<string> txt_list;
|
||||
|
||||
if(model_path.find(TXT_PATH)!=model_path.end()){
|
||||
ifstream in(model_path.at(TXT_PATH));
|
||||
if (!in.is_open()) {
|
||||
LOG(ERROR) << "Failed to open file: " << model_path.at(TXT_PATH) ;
|
||||
return 0;
|
||||
}
|
||||
string line;
|
||||
while(getline(in, line))
|
||||
{
|
||||
txt_list.emplace_back(line);
|
||||
}
|
||||
in.close();
|
||||
}
|
||||
|
||||
long taking_micros = 0;
|
||||
for(auto& txt_str : txt_list){
|
||||
gettimeofday(&start, nullptr);
|
||||
FUNASR_RESULT result=CTTransformerInfer(punc_hanlde, txt_str.c_str(), RASR_NONE, nullptr);
|
||||
gettimeofday(&end, nullptr);
|
||||
seconds = (end.tv_sec - start.tv_sec);
|
||||
taking_micros += ((seconds * 1000000) + end.tv_usec) - (start.tv_usec);
|
||||
string msg = FunASRGetResult(result, 0);
|
||||
LOG(INFO)<<"Results: "<<msg;
|
||||
CTTransformerFreeResult(result);
|
||||
}
|
||||
|
||||
LOG(INFO) << "Model inference takes: " << (double)taking_micros / 1000000 <<" s";
|
||||
CTTransformerUninit(punc_hanlde);
|
||||
return 0;
|
||||
}
|
||||
|
||||
264
modules/python/vendors/FunASR/runtime/onnxruntime/bin/funasr-onnx-offline-rtf.cpp
vendored
Normal file
264
modules/python/vendors/FunASR/runtime/onnxruntime/bin/funasr-onnx-offline-rtf.cpp
vendored
Normal file
@@ -0,0 +1,264 @@
|
||||
/**
|
||||
* Copyright FunASR (https://github.com/alibaba-damo-academy/FunASR). All Rights Reserved.
|
||||
* MIT License (https://opensource.org/licenses/MIT)
|
||||
*/
|
||||
|
||||
#ifndef _WIN32
|
||||
#include <sys/time.h>
|
||||
#else
|
||||
#include <win_func.h>
|
||||
#endif
|
||||
|
||||
#include <glog/logging.h>
|
||||
#include "funasrruntime.h"
|
||||
#include "tclap/CmdLine.h"
|
||||
#include "com-define.h"
|
||||
|
||||
#include <iostream>
|
||||
#include <fstream>
|
||||
#include <sstream>
|
||||
#include <vector>
|
||||
#include <atomic>
|
||||
#include <mutex>
|
||||
#include <thread>
|
||||
#include <map>
|
||||
#include <unordered_map>
|
||||
#include "util.h"
|
||||
using namespace std;
|
||||
|
||||
std::atomic<int> wav_index(0);
|
||||
std::mutex mtx;
|
||||
|
||||
void runReg(FUNASR_HANDLE asr_handle, vector<string> wav_list, vector<string> wav_ids, int audio_fs,
|
||||
float* total_length, long* total_time, int core_id, float glob_beam = 3.0f, float lat_beam = 3.0f, float am_sc = 10.0f,
|
||||
int fst_inc_wts = 20, string hotword_path = "") {
|
||||
|
||||
struct timeval start, end;
|
||||
long seconds = 0;
|
||||
float n_total_length = 0.0f;
|
||||
long n_total_time = 0;
|
||||
|
||||
// init wfst decoder
|
||||
FUNASR_DEC_HANDLE decoder_handle = FunASRWfstDecoderInit(asr_handle, ASR_OFFLINE, glob_beam, lat_beam, am_sc);
|
||||
|
||||
// process fst hotwords list
|
||||
unordered_map<string, int> hws_map;
|
||||
string nn_hotwords_ = "";
|
||||
funasr::ExtractHws(hotword_path, hws_map, nn_hotwords_);
|
||||
|
||||
// load hotwords list and build graph
|
||||
FunWfstDecoderLoadHwsRes(decoder_handle, fst_inc_wts, hws_map);
|
||||
|
||||
std::vector<std::vector<float>> hotwords_embedding = CompileHotwordEmbedding(asr_handle, nn_hotwords_);
|
||||
|
||||
// warm up
|
||||
for (size_t i = 0; i < 1; i++)
|
||||
{
|
||||
FUNASR_RESULT result=FunOfflineInfer(asr_handle, wav_list[0].c_str(), RASR_NONE, nullptr, hotwords_embedding, audio_fs, true, decoder_handle);
|
||||
if(result){
|
||||
FunASRFreeResult(result);
|
||||
}
|
||||
}
|
||||
|
||||
while (true) {
|
||||
// 使用原子变量获取索引并递增
|
||||
int i = wav_index.fetch_add(1);
|
||||
if (i >= wav_list.size()) {
|
||||
break;
|
||||
}
|
||||
|
||||
gettimeofday(&start, nullptr);
|
||||
FUNASR_RESULT result=FunOfflineInfer(asr_handle, wav_list[i].c_str(), RASR_NONE, nullptr, hotwords_embedding, audio_fs, true, decoder_handle);
|
||||
|
||||
gettimeofday(&end, nullptr);
|
||||
seconds = (end.tv_sec - start.tv_sec);
|
||||
long taking_micros = ((seconds * 1000000) + end.tv_usec) - (start.tv_usec);
|
||||
n_total_time += taking_micros;
|
||||
|
||||
if(result){
|
||||
string msg = FunASRGetResult(result, 0);
|
||||
LOG(INFO) << "Thread: " << this_thread::get_id() << "," << wav_ids[i] << " : " << msg;
|
||||
string stamp = FunASRGetStamp(result);
|
||||
if(stamp !=""){
|
||||
LOG(INFO) << "Thread: " << this_thread::get_id() << "," << wav_ids[i] << " : " << stamp;
|
||||
}
|
||||
string stamp_sents = FunASRGetStampSents(result);
|
||||
if(stamp_sents !=""){
|
||||
LOG(INFO)<< wav_ids[i] <<" : "<<stamp_sents;
|
||||
}
|
||||
float snippet_time = FunASRGetRetSnippetTime(result);
|
||||
n_total_length += snippet_time;
|
||||
FunASRFreeResult(result);
|
||||
}else{
|
||||
LOG(ERROR) << wav_ids[i] << (": No return data!\n");
|
||||
}
|
||||
}
|
||||
{
|
||||
lock_guard<mutex> guard(mtx);
|
||||
*total_length += n_total_length;
|
||||
if(*total_time < n_total_time){
|
||||
*total_time = n_total_time;
|
||||
}
|
||||
}
|
||||
FunWfstDecoderUnloadHwsRes(decoder_handle);
|
||||
FunASRWfstDecoderUninit(decoder_handle);
|
||||
}
|
||||
|
||||
bool is_target_file(const std::string& filename, const std::string target) {
|
||||
std::size_t pos = filename.find_last_of(".");
|
||||
if (pos == std::string::npos) {
|
||||
return false;
|
||||
}
|
||||
std::string extension = filename.substr(pos + 1);
|
||||
return (extension == target);
|
||||
}
|
||||
|
||||
void GetValue(TCLAP::ValueArg<std::string>& value_arg, string key, std::map<std::string, std::string>& model_path)
|
||||
{
|
||||
model_path.insert({key, value_arg.getValue()});
|
||||
LOG(INFO)<< key << " : " << value_arg.getValue();
|
||||
}
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
google::InitGoogleLogging(argv[0]);
|
||||
FLAGS_logtostderr = true;
|
||||
|
||||
TCLAP::CmdLine cmd("funasr-onnx-offline-rtf", ' ', "1.0");
|
||||
TCLAP::ValueArg<std::string> model_dir("", MODEL_DIR, "the model path, which contains model.onnx, config.yaml, am.mvn", true, "", "string");
|
||||
TCLAP::ValueArg<std::string> quantize("", QUANTIZE, "true (Default), load the model of model.onnx in model_dir. If set true, load the model of model_quant.onnx in model_dir", false, "true", "string");
|
||||
TCLAP::ValueArg<std::string> bladedisc("", BLADEDISC, "true (Default), load the model of bladedisc in model_dir.", false, "true", "string");
|
||||
TCLAP::ValueArg<std::string> vad_dir("", VAD_DIR, "the vad model path, which contains model.onnx, vad.yaml, vad.mvn", false, "", "string");
|
||||
TCLAP::ValueArg<std::string> vad_quant("", VAD_QUANT, "true (Default), load the model of model.onnx in vad_dir. If set true, load the model of model_quant.onnx in vad_dir", false, "true", "string");
|
||||
TCLAP::ValueArg<std::string> punc_dir("", PUNC_DIR, "the punc model path, which contains model.onnx, punc.yaml", false, "", "string");
|
||||
TCLAP::ValueArg<std::string> punc_quant("", PUNC_QUANT, "true (Default), load the model of model.onnx in punc_dir. If set true, load the model of model_quant.onnx in punc_dir", false, "true", "string");
|
||||
TCLAP::ValueArg<std::string> lm_dir("", LM_DIR, "the lm model path, which contains compiled models: TLG.fst, config.yaml ", false, "", "string");
|
||||
TCLAP::ValueArg<float> global_beam("", GLOB_BEAM, "the decoding beam for beam searching ", false, 3.0, "float");
|
||||
TCLAP::ValueArg<float> lattice_beam("", LAT_BEAM, "the lattice generation beam for beam searching ", false, 3.0, "float");
|
||||
TCLAP::ValueArg<float> am_scale("", AM_SCALE, "the acoustic scale for beam searching ", false, 10.0, "float");
|
||||
TCLAP::ValueArg<std::int32_t> fst_inc_wts("", FST_INC_WTS, "the fst hotwords incremental bias", false, 20, "int32_t");
|
||||
TCLAP::ValueArg<std::string> itn_dir("", ITN_DIR, "the itn model(fst) path, which contains zh_itn_tagger.fst and zh_itn_verbalizer.fst", false, "", "string");
|
||||
|
||||
TCLAP::ValueArg<std::string> wav_path("", WAV_PATH, "the input could be: wav_path, e.g.: asr_example.wav; pcm_path, e.g.: asr_example.pcm; wav.scp, kaldi style wav list (wav_id \t wav_path)", true, "", "string");
|
||||
TCLAP::ValueArg<std::int32_t> audio_fs("", AUDIO_FS, "the sample rate of audio", false, 16000, "int32_t");
|
||||
TCLAP::ValueArg<std::int32_t> thread_num("", THREAD_NUM, "multi-thread num for rtf", false, 1, "int32_t");
|
||||
TCLAP::ValueArg<std::string> hotword("", HOTWORD, "the hotword file, one hotword perline, Format: Hotword Weight (could be: 阿里巴巴 20)", false, "", "string");
|
||||
TCLAP::SwitchArg use_gpu("", INFER_GPU, "Whether to use GPU for inference, default is false", false);
|
||||
TCLAP::ValueArg<std::int32_t> batch_size("", BATCHSIZE, "batch_size for ASR model when using GPU", false, 4, "int32_t");
|
||||
|
||||
cmd.add(model_dir);
|
||||
cmd.add(quantize);
|
||||
cmd.add(bladedisc);
|
||||
cmd.add(vad_dir);
|
||||
cmd.add(vad_quant);
|
||||
cmd.add(punc_dir);
|
||||
cmd.add(punc_quant);
|
||||
cmd.add(itn_dir);
|
||||
cmd.add(lm_dir);
|
||||
cmd.add(global_beam);
|
||||
cmd.add(lattice_beam);
|
||||
cmd.add(am_scale);
|
||||
cmd.add(hotword);
|
||||
cmd.add(fst_inc_wts);
|
||||
cmd.add(wav_path);
|
||||
cmd.add(audio_fs);
|
||||
cmd.add(thread_num);
|
||||
cmd.add(use_gpu);
|
||||
cmd.add(batch_size);
|
||||
cmd.parse(argc, argv);
|
||||
|
||||
std::map<std::string, std::string> model_path;
|
||||
GetValue(model_dir, MODEL_DIR, model_path);
|
||||
GetValue(quantize, QUANTIZE, model_path);
|
||||
GetValue(bladedisc, BLADEDISC, model_path);
|
||||
GetValue(vad_dir, VAD_DIR, model_path);
|
||||
GetValue(vad_quant, VAD_QUANT, model_path);
|
||||
GetValue(punc_dir, PUNC_DIR, model_path);
|
||||
GetValue(punc_quant, PUNC_QUANT, model_path);
|
||||
GetValue(itn_dir, ITN_DIR, model_path);
|
||||
GetValue(lm_dir, LM_DIR, model_path);
|
||||
GetValue(hotword, HOTWORD, model_path);
|
||||
GetValue(wav_path, WAV_PATH, model_path);
|
||||
|
||||
struct timeval start, end;
|
||||
gettimeofday(&start, nullptr);
|
||||
bool use_gpu_ = use_gpu.getValue();
|
||||
int batch_size_ = batch_size.getValue();
|
||||
FUNASR_HANDLE asr_handle=FunOfflineInit(model_path, 1, use_gpu_, batch_size_);
|
||||
|
||||
if (!asr_handle)
|
||||
{
|
||||
LOG(ERROR) << "FunASR init failed";
|
||||
exit(-1);
|
||||
}
|
||||
|
||||
gettimeofday(&end, nullptr);
|
||||
long seconds = (end.tv_sec - start.tv_sec);
|
||||
long modle_init_micros = ((seconds * 1000000) + end.tv_usec) - (start.tv_usec);
|
||||
LOG(INFO) << "Model initialization takes " << (double)modle_init_micros / 1000000 << " s";
|
||||
|
||||
// read wav_path
|
||||
vector<string> wav_list;
|
||||
vector<string> wav_ids;
|
||||
string default_id = "wav_default_id";
|
||||
string wav_path_ = model_path.at(WAV_PATH);
|
||||
if(is_target_file(wav_path_, "wav") || is_target_file(wav_path_, "pcm")){
|
||||
wav_list.emplace_back(wav_path_);
|
||||
wav_ids.emplace_back(default_id);
|
||||
}
|
||||
else if(is_target_file(wav_path_, "scp")){
|
||||
ifstream in(wav_path_);
|
||||
if (!in.is_open()) {
|
||||
LOG(ERROR) << "Failed to open file: " << model_path.at(WAV_SCP) ;
|
||||
return 0;
|
||||
}
|
||||
string line;
|
||||
while(getline(in, line))
|
||||
{
|
||||
istringstream iss(line);
|
||||
string column1, column2;
|
||||
iss >> column1 >> column2;
|
||||
wav_list.emplace_back(column2);
|
||||
wav_ids.emplace_back(column1);
|
||||
}
|
||||
in.close();
|
||||
}else{
|
||||
LOG(ERROR)<<"Please check the wav extension!";
|
||||
exit(-1);
|
||||
}
|
||||
|
||||
// 多线程测试
|
||||
float total_length = 0.0f;
|
||||
long total_time = 0;
|
||||
std::vector<std::thread> threads;
|
||||
|
||||
int rtf_threds = thread_num.getValue();
|
||||
std::string hotword_path = hotword.getValue();
|
||||
int value_bias = 20;
|
||||
value_bias = fst_inc_wts.getValue();
|
||||
float glob_beam = 3.0f;
|
||||
float lat_beam = 3.0f;
|
||||
float am_sc = 10.0f;
|
||||
if (lm_dir.isSet()) {
|
||||
glob_beam = global_beam.getValue();
|
||||
lat_beam = lattice_beam.getValue();
|
||||
am_sc = am_scale.getValue();
|
||||
}
|
||||
for (int i = 0; i < rtf_threds; i++)
|
||||
{
|
||||
threads.emplace_back(thread(runReg, asr_handle, wav_list, wav_ids, audio_fs.getValue(), &total_length, &total_time, i, glob_beam, lat_beam, am_sc, value_bias, hotword_path));
|
||||
}
|
||||
|
||||
for (auto& thread : threads)
|
||||
{
|
||||
thread.join();
|
||||
}
|
||||
|
||||
LOG(INFO) << "total_time_wav " << (long)(total_length * 1000) << " ms";
|
||||
LOG(INFO) << "total_time_comput " << total_time / 1000 << " ms";
|
||||
LOG(INFO) << "total_rtf " << (double)total_time/ (total_length*1000000);
|
||||
LOG(INFO) << "speedup " << 1.0/((double)total_time/ (total_length*1000000));
|
||||
|
||||
FunOfflineUninit(asr_handle);
|
||||
return 0;
|
||||
}
|
||||
160
modules/python/vendors/FunASR/runtime/onnxruntime/bin/funasr-onnx-offline-vad.cpp
vendored
Normal file
160
modules/python/vendors/FunASR/runtime/onnxruntime/bin/funasr-onnx-offline-vad.cpp
vendored
Normal file
@@ -0,0 +1,160 @@
|
||||
/**
|
||||
* Copyright FunASR (https://github.com/alibaba-damo-academy/FunASR). All Rights Reserved.
|
||||
* MIT License (https://opensource.org/licenses/MIT)
|
||||
*/
|
||||
|
||||
#ifndef _WIN32
|
||||
#include <sys/time.h>
|
||||
#else
|
||||
#include <win_func.h>
|
||||
#endif
|
||||
|
||||
#include <iostream>
|
||||
#include <fstream>
|
||||
#include <sstream>
|
||||
#include <map>
|
||||
#include <vector>
|
||||
#include <glog/logging.h>
|
||||
#include "funasrruntime.h"
|
||||
#include "tclap/CmdLine.h"
|
||||
#include "com-define.h"
|
||||
|
||||
using namespace std;
|
||||
|
||||
bool is_target_file(const std::string& filename, const std::string target) {
|
||||
std::size_t pos = filename.find_last_of(".");
|
||||
if (pos == std::string::npos) {
|
||||
return false;
|
||||
}
|
||||
std::string extension = filename.substr(pos + 1);
|
||||
return (extension == target);
|
||||
}
|
||||
|
||||
void GetValue(TCLAP::ValueArg<std::string>& value_arg, string key, std::map<std::string, std::string>& model_path)
|
||||
{
|
||||
if (value_arg.isSet()){
|
||||
model_path.insert({key, value_arg.getValue()});
|
||||
LOG(INFO)<< key << " : " << value_arg.getValue();
|
||||
}
|
||||
}
|
||||
|
||||
void print_segs(vector<vector<int>>* vec, string &wav_id) {
|
||||
string seg_out=wav_id + ": [";
|
||||
for (int i = 0; i < vec->size(); i++) {
|
||||
vector<int> inner_vec = (*vec)[i];
|
||||
seg_out += "[";
|
||||
for (int j = 0; j < inner_vec.size(); j++) {
|
||||
seg_out += to_string(inner_vec[j]);
|
||||
if (j != inner_vec.size() - 1) {
|
||||
seg_out += ",";
|
||||
}
|
||||
}
|
||||
seg_out += "]";
|
||||
if (i != vec->size() - 1) {
|
||||
seg_out += ",";
|
||||
}
|
||||
}
|
||||
seg_out += "]";
|
||||
LOG(INFO)<<seg_out;
|
||||
}
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
google::InitGoogleLogging(argv[0]);
|
||||
FLAGS_logtostderr = true;
|
||||
|
||||
TCLAP::CmdLine cmd("funasr-onnx-offline-vad", ' ', "1.0");
|
||||
TCLAP::ValueArg<std::string> model_dir("", MODEL_DIR, "the vad model path, which contains model.onnx, vad.yaml, vad.mvn", true, "", "string");
|
||||
TCLAP::ValueArg<std::string> quantize("", QUANTIZE, "true (Default), load the model of model.onnx in model_dir. If set true, load the model of model_quant.onnx in model_dir", false, "true", "string");
|
||||
|
||||
TCLAP::ValueArg<std::string> wav_path("", WAV_PATH, "the input could be: wav_path, e.g.: asr_example.wav; pcm_path, e.g.: asr_example.pcm; wav.scp, kaldi style wav list (wav_id \t wav_path)", true, "", "string");
|
||||
TCLAP::ValueArg<std::int32_t> audio_fs("", AUDIO_FS, "the sample rate of audio", false, 16000, "int32_t");
|
||||
|
||||
cmd.add(model_dir);
|
||||
cmd.add(quantize);
|
||||
cmd.add(wav_path);
|
||||
cmd.add(audio_fs);
|
||||
cmd.parse(argc, argv);
|
||||
|
||||
std::map<std::string, std::string> model_path;
|
||||
GetValue(model_dir, MODEL_DIR, model_path);
|
||||
GetValue(quantize, QUANTIZE, model_path);
|
||||
GetValue(wav_path, WAV_PATH, model_path);
|
||||
|
||||
struct timeval start, end;
|
||||
gettimeofday(&start, nullptr);
|
||||
int thread_num = 1;
|
||||
FUNASR_HANDLE vad_hanlde=FsmnVadInit(model_path, thread_num);
|
||||
|
||||
if (!vad_hanlde)
|
||||
{
|
||||
LOG(ERROR) << "FunVad init failed";
|
||||
exit(-1);
|
||||
}
|
||||
|
||||
gettimeofday(&end, nullptr);
|
||||
long seconds = (end.tv_sec - start.tv_sec);
|
||||
long modle_init_micros = ((seconds * 1000000) + end.tv_usec) - (start.tv_usec);
|
||||
LOG(INFO) << "Model initialization takes " << (double)modle_init_micros / 1000000 << " s";
|
||||
|
||||
// read wav_path
|
||||
vector<string> wav_list;
|
||||
vector<string> wav_ids;
|
||||
string default_id = "wav_default_id";
|
||||
string wav_path_ = model_path.at(WAV_PATH);
|
||||
if(is_target_file(wav_path_, "wav") || is_target_file(wav_path_, "pcm")){
|
||||
wav_list.emplace_back(wav_path_);
|
||||
wav_ids.emplace_back(default_id);
|
||||
}
|
||||
else if(is_target_file(wav_path_, "scp")){
|
||||
ifstream in(wav_path_);
|
||||
if (!in.is_open()) {
|
||||
LOG(ERROR) << "Failed to open file: " << model_path.at(WAV_SCP) ;
|
||||
return 0;
|
||||
}
|
||||
string line;
|
||||
while(getline(in, line))
|
||||
{
|
||||
istringstream iss(line);
|
||||
string column1, column2;
|
||||
iss >> column1 >> column2;
|
||||
wav_list.emplace_back(column2);
|
||||
wav_ids.emplace_back(column1);
|
||||
}
|
||||
in.close();
|
||||
}else{
|
||||
LOG(ERROR)<<"Please check the wav extension!";
|
||||
exit(-1);
|
||||
}
|
||||
|
||||
float snippet_time = 0.0f;
|
||||
long taking_micros = 0;
|
||||
for (int i = 0; i < wav_list.size(); i++) {
|
||||
auto& wav_file = wav_list[i];
|
||||
auto& wav_id = wav_ids[i];
|
||||
gettimeofday(&start, nullptr);
|
||||
FUNASR_RESULT result=FsmnVadInfer(vad_hanlde, wav_file.c_str(), nullptr, audio_fs.getValue());
|
||||
gettimeofday(&end, nullptr);
|
||||
seconds = (end.tv_sec - start.tv_sec);
|
||||
taking_micros += ((seconds * 1000000) + end.tv_usec) - (start.tv_usec);
|
||||
|
||||
if (result)
|
||||
{
|
||||
vector<std::vector<int>>* vad_segments = FsmnVadGetResult(result, 0);
|
||||
print_segs(vad_segments, wav_id);
|
||||
snippet_time += FsmnVadGetRetSnippetTime(result);
|
||||
FsmnVadFreeResult(result);
|
||||
}
|
||||
else
|
||||
{
|
||||
LOG(ERROR) << ("No return data!\n");
|
||||
}
|
||||
}
|
||||
|
||||
LOG(INFO) << "Audio length: " << (double)snippet_time << " s";
|
||||
LOG(INFO) << "Model inference takes: " << (double)taking_micros / 1000000 <<" s";
|
||||
LOG(INFO) << "Model inference RTF: " << (double)taking_micros/ (snippet_time*1000000);
|
||||
FsmnVadUninit(vad_hanlde);
|
||||
return 0;
|
||||
}
|
||||
|
||||
230
modules/python/vendors/FunASR/runtime/onnxruntime/bin/funasr-onnx-offline.cpp
vendored
Normal file
230
modules/python/vendors/FunASR/runtime/onnxruntime/bin/funasr-onnx-offline.cpp
vendored
Normal file
@@ -0,0 +1,230 @@
|
||||
/**
|
||||
* Copyright FunASR (https://github.com/alibaba-damo-academy/FunASR). All Rights Reserved.
|
||||
* MIT License (https://opensource.org/licenses/MIT)
|
||||
*/
|
||||
|
||||
#ifndef _WIN32
|
||||
#include <sys/time.h>
|
||||
#else
|
||||
#include <win_func.h>
|
||||
#endif
|
||||
|
||||
#include <iostream>
|
||||
#include <fstream>
|
||||
#include <sstream>
|
||||
#include <map>
|
||||
#include <glog/logging.h>
|
||||
#include "funasrruntime.h"
|
||||
#include "tclap/CmdLine.h"
|
||||
#include "com-define.h"
|
||||
#include <unordered_map>
|
||||
#include "util.h"
|
||||
#include "audio.h"
|
||||
using namespace std;
|
||||
|
||||
bool is_target_file(const std::string& filename, const std::string target) {
|
||||
std::size_t pos = filename.find_last_of(".");
|
||||
if (pos == std::string::npos) {
|
||||
return false;
|
||||
}
|
||||
std::string extension = filename.substr(pos + 1);
|
||||
return (extension == target);
|
||||
}
|
||||
|
||||
void GetValue(TCLAP::ValueArg<std::string>& value_arg, string key, std::map<std::string, std::string>& model_path)
|
||||
{
|
||||
model_path.insert({key, value_arg.getValue()});
|
||||
LOG(INFO)<< key << " : " << value_arg.getValue();
|
||||
}
|
||||
|
||||
int main(int argc, char** argv)
|
||||
{
|
||||
google::InitGoogleLogging(argv[0]);
|
||||
FLAGS_logtostderr = true;
|
||||
|
||||
TCLAP::CmdLine cmd("funasr-onnx-offline", ' ', "1.0");
|
||||
TCLAP::ValueArg<std::string> model_dir("", MODEL_DIR, "the asr model path, which contains model.onnx, config.yaml, am.mvn", true, "", "string");
|
||||
TCLAP::ValueArg<std::string> quantize("", QUANTIZE, "true (Default), load the model of model.onnx in model_dir. If set true, load the model of model_quant.onnx in model_dir", false, "true", "string");
|
||||
TCLAP::ValueArg<std::string> bladedisc("", BLADEDISC, "true (Default), load the model of bladedisc in model_dir.", false, "true", "string");
|
||||
TCLAP::ValueArg<std::string> vad_dir("", VAD_DIR, "the vad model path, which contains model.onnx, vad.yaml, vad.mvn", false, "", "string");
|
||||
TCLAP::ValueArg<std::string> vad_quant("", VAD_QUANT, "true (Default), load the model of model.onnx in vad_dir. If set true, load the model of model_quant.onnx in vad_dir", false, "true", "string");
|
||||
TCLAP::ValueArg<std::string> punc_dir("", PUNC_DIR, "the punc model path, which contains model.onnx, punc.yaml", false, "", "string");
|
||||
TCLAP::ValueArg<std::string> punc_quant("", PUNC_QUANT, "true (Default), load the model of model.onnx in punc_dir. If set true, load the model of model_quant.onnx in punc_dir", false, "true", "string");
|
||||
TCLAP::ValueArg<std::string> lm_dir("", LM_DIR, "the lm model path, which contains compiled models: TLG.fst, config.yaml, lexicon.txt ", false, "", "string");
|
||||
TCLAP::ValueArg<float> global_beam("", GLOB_BEAM, "the decoding beam for beam searching ", false, 3.0, "float");
|
||||
TCLAP::ValueArg<float> lattice_beam("", LAT_BEAM, "the lattice generation beam for beam searching ", false, 3.0, "float");
|
||||
TCLAP::ValueArg<float> am_scale("", AM_SCALE, "the acoustic scale for beam searching ", false, 10.0, "float");
|
||||
TCLAP::ValueArg<std::int32_t> fst_inc_wts("", FST_INC_WTS, "the fst hotwords incremental bias", false, 20, "int32_t");
|
||||
TCLAP::ValueArg<std::string> itn_dir("", ITN_DIR, "the itn model(fst) path, which contains zh_itn_tagger.fst and zh_itn_verbalizer.fst", false, "", "string");
|
||||
TCLAP::ValueArg<std::string> wav_path("", WAV_PATH, "the input could be: wav_path, e.g.: asr_example.wav; pcm_path, e.g.: asr_example.pcm; wav.scp, kaldi style wav list (wav_id \t wav_path)", true, "", "string");
|
||||
TCLAP::ValueArg<std::int32_t> audio_fs("", AUDIO_FS, "the sample rate of audio", false, 16000, "int32_t");
|
||||
TCLAP::ValueArg<std::string> hotword("", HOTWORD, "the hotword file, one hotword perline, Format: Hotword Weight (could be: 阿里巴巴 20)", false, "", "string");
|
||||
TCLAP::SwitchArg use_gpu("", INFER_GPU, "Whether to use GPU for inference, default is false", false);
|
||||
TCLAP::ValueArg<std::int32_t> batch_size("", BATCHSIZE, "batch_size for ASR model when using GPU", false, 4, "int32_t");
|
||||
|
||||
cmd.add(model_dir);
|
||||
cmd.add(quantize);
|
||||
cmd.add(bladedisc);
|
||||
cmd.add(vad_dir);
|
||||
cmd.add(vad_quant);
|
||||
cmd.add(punc_dir);
|
||||
cmd.add(punc_quant);
|
||||
cmd.add(itn_dir);
|
||||
cmd.add(lm_dir);
|
||||
cmd.add(global_beam);
|
||||
cmd.add(lattice_beam);
|
||||
cmd.add(am_scale);
|
||||
cmd.add(fst_inc_wts);
|
||||
cmd.add(wav_path);
|
||||
cmd.add(audio_fs);
|
||||
cmd.add(hotword);
|
||||
cmd.add(use_gpu);
|
||||
cmd.add(batch_size);
|
||||
cmd.parse(argc, argv);
|
||||
|
||||
std::map<std::string, std::string> model_path;
|
||||
GetValue(model_dir, MODEL_DIR, model_path);
|
||||
GetValue(quantize, QUANTIZE, model_path);
|
||||
GetValue(bladedisc, BLADEDISC, model_path);
|
||||
GetValue(vad_dir, VAD_DIR, model_path);
|
||||
GetValue(vad_quant, VAD_QUANT, model_path);
|
||||
GetValue(punc_dir, PUNC_DIR, model_path);
|
||||
GetValue(punc_quant, PUNC_QUANT, model_path);
|
||||
GetValue(itn_dir, ITN_DIR, model_path);
|
||||
GetValue(lm_dir, LM_DIR, model_path);
|
||||
GetValue(wav_path, WAV_PATH, model_path);
|
||||
|
||||
struct timeval start, end;
|
||||
gettimeofday(&start, nullptr);
|
||||
int thread_num = 1;
|
||||
bool use_gpu_ = use_gpu.getValue();
|
||||
int batch_size_ = batch_size.getValue();
|
||||
FUNASR_HANDLE asr_hanlde=FunOfflineInit(model_path, thread_num, use_gpu_, batch_size_);
|
||||
|
||||
if (!asr_hanlde)
|
||||
{
|
||||
LOG(ERROR) << "FunASR init failed";
|
||||
exit(-1);
|
||||
}
|
||||
float glob_beam = 3.0f;
|
||||
float lat_beam = 3.0f;
|
||||
float am_sc = 10.0f;
|
||||
if (lm_dir.isSet()) {
|
||||
glob_beam = global_beam.getValue();
|
||||
lat_beam = lattice_beam.getValue();
|
||||
am_sc = am_scale.getValue();
|
||||
}
|
||||
// init wfst decoder
|
||||
FUNASR_DEC_HANDLE decoder_handle = FunASRWfstDecoderInit(asr_hanlde, ASR_OFFLINE, glob_beam, lat_beam, am_sc);
|
||||
|
||||
// hotword file
|
||||
unordered_map<string, int> hws_map;
|
||||
std::string nn_hotwords_ = "";
|
||||
std::string hotword_path = hotword.getValue();
|
||||
LOG(INFO) << "hotword path: " << hotword_path;
|
||||
funasr::ExtractHws(hotword_path, hws_map, nn_hotwords_);
|
||||
|
||||
gettimeofday(&end, nullptr);
|
||||
long seconds = (end.tv_sec - start.tv_sec);
|
||||
long modle_init_micros = ((seconds * 1000000) + end.tv_usec) - (start.tv_usec);
|
||||
LOG(INFO) << "Model initialization takes " << (double)modle_init_micros / 1000000 << " s";
|
||||
|
||||
// read wav_path
|
||||
vector<string> wav_list;
|
||||
vector<string> wav_ids;
|
||||
string default_id = "wav_default_id";
|
||||
string wav_path_ = model_path.at(WAV_PATH);
|
||||
|
||||
if(is_target_file(wav_path_, "scp")){
|
||||
ifstream in(wav_path_);
|
||||
if (!in.is_open()) {
|
||||
LOG(ERROR) << "Failed to open file: " << model_path.at(WAV_SCP) ;
|
||||
return 0;
|
||||
}
|
||||
string line;
|
||||
while(getline(in, line))
|
||||
{
|
||||
istringstream iss(line);
|
||||
string column1, column2;
|
||||
iss >> column1 >> column2;
|
||||
wav_list.emplace_back(column2);
|
||||
wav_ids.emplace_back(column1);
|
||||
}
|
||||
in.close();
|
||||
}else{
|
||||
wav_list.emplace_back(wav_path_);
|
||||
wav_ids.emplace_back(default_id);
|
||||
}
|
||||
|
||||
float snippet_time = 0.0f;
|
||||
long taking_micros = 0;
|
||||
|
||||
// load hotwords list and build graph
|
||||
FunWfstDecoderLoadHwsRes(decoder_handle, fst_inc_wts.getValue(), hws_map);
|
||||
|
||||
std::vector<std::vector<float>> hotwords_embedding = CompileHotwordEmbedding(asr_hanlde, nn_hotwords_);
|
||||
for (int i = 0; i < wav_list.size(); i++) {
|
||||
auto& wav_file = wav_list[i];
|
||||
auto& wav_id = wav_ids[i];
|
||||
|
||||
// For debug:begin
|
||||
// int32_t sampling_rate_ = audio_fs.getValue();
|
||||
// funasr::Audio audio(1);
|
||||
// if(is_target_file(wav_file.c_str(), "wav")){
|
||||
// if(!audio.LoadWav2Char(wav_file.c_str(), &sampling_rate_)){
|
||||
// LOG(ERROR)<<"Failed to load "<< wav_file;
|
||||
// exit(-1);
|
||||
// }
|
||||
// }else if(is_target_file(wav_file.c_str(), "pcm")){
|
||||
// if (!audio.LoadPcmwav2Char(wav_file.c_str(), &sampling_rate_)){
|
||||
// LOG(ERROR)<<"Failed to load "<< wav_file;
|
||||
// exit(-1);
|
||||
// }
|
||||
// }else{
|
||||
// if (!audio.FfmpegLoad(wav_file.c_str(), true)){
|
||||
// LOG(ERROR)<<"Failed to load "<< wav_file;
|
||||
// exit(-1);
|
||||
// }
|
||||
// }
|
||||
// char* speech_buff = audio.GetSpeechChar();
|
||||
// int buff_len = audio.GetSpeechLen()*2;
|
||||
|
||||
// gettimeofday(&start, nullptr);
|
||||
// FUNASR_RESULT result=FunOfflineInferBuffer(asr_hanlde, speech_buff, buff_len, RASR_NONE, nullptr, hotwords_embedding, audio_fs.getValue(), "pcm", true, decoder_handle);
|
||||
// For debug:end
|
||||
|
||||
FUNASR_RESULT result=FunOfflineInfer(asr_hanlde, wav_file.c_str(), RASR_NONE, nullptr, hotwords_embedding, audio_fs.getValue(), true, decoder_handle);
|
||||
gettimeofday(&end, nullptr);
|
||||
seconds = (end.tv_sec - start.tv_sec);
|
||||
taking_micros += ((seconds * 1000000) + end.tv_usec) - (start.tv_usec);
|
||||
|
||||
if (result)
|
||||
{
|
||||
string msg = FunASRGetResult(result, 0);
|
||||
LOG(INFO)<< wav_id <<" : "<<msg;
|
||||
string stamp = FunASRGetStamp(result);
|
||||
if(stamp !=""){
|
||||
LOG(INFO)<< wav_id <<" : "<<stamp;
|
||||
}
|
||||
string stamp_sents = FunASRGetStampSents(result);
|
||||
if(stamp_sents !=""){
|
||||
LOG(INFO)<< wav_id <<" : "<<stamp_sents;
|
||||
}
|
||||
snippet_time += FunASRGetRetSnippetTime(result);
|
||||
FunASRFreeResult(result);
|
||||
}
|
||||
else
|
||||
{
|
||||
LOG(ERROR) << ("No return data!\n");
|
||||
}
|
||||
}
|
||||
FunWfstDecoderUnloadHwsRes(decoder_handle);
|
||||
LOG(INFO) << "Audio length: " << (double)snippet_time << " s";
|
||||
LOG(INFO) << "Model inference takes: " << (double)taking_micros / 1000000 <<" s";
|
||||
LOG(INFO) << "Model inference RTF: " << (double)taking_micros/ (snippet_time*1000000);
|
||||
|
||||
FunASRWfstDecoderUninit(decoder_handle);
|
||||
FunOfflineUninit(asr_hanlde);
|
||||
return 0;
|
||||
}
|
||||
|
||||
176
modules/python/vendors/FunASR/runtime/onnxruntime/bin/funasr-onnx-online-asr.cpp
vendored
Normal file
176
modules/python/vendors/FunASR/runtime/onnxruntime/bin/funasr-onnx-online-asr.cpp
vendored
Normal file
@@ -0,0 +1,176 @@
|
||||
/**
|
||||
* Copyright FunASR (https://github.com/alibaba-damo-academy/FunASR). All Rights Reserved.
|
||||
* MIT License (https://opensource.org/licenses/MIT)
|
||||
*/
|
||||
|
||||
#ifndef _WIN32
|
||||
#include <sys/time.h>
|
||||
#else
|
||||
#include <win_func.h>
|
||||
#endif
|
||||
|
||||
#include <iostream>
|
||||
#include <fstream>
|
||||
#include <sstream>
|
||||
#include <map>
|
||||
#include <vector>
|
||||
#include <glog/logging.h>
|
||||
#include "funasrruntime.h"
|
||||
#include "tclap/CmdLine.h"
|
||||
#include "com-define.h"
|
||||
#include "audio.h"
|
||||
|
||||
using namespace std;
|
||||
|
||||
bool is_target_file(const std::string& filename, const std::string target) {
|
||||
std::size_t pos = filename.find_last_of(".");
|
||||
if (pos == std::string::npos) {
|
||||
return false;
|
||||
}
|
||||
std::string extension = filename.substr(pos + 1);
|
||||
return (extension == target);
|
||||
}
|
||||
|
||||
void GetValue(TCLAP::ValueArg<std::string>& value_arg, string key, std::map<std::string, std::string>& model_path)
|
||||
{
|
||||
if (value_arg.isSet()){
|
||||
model_path.insert({key, value_arg.getValue()});
|
||||
LOG(INFO)<< key << " : " << value_arg.getValue();
|
||||
}
|
||||
}
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
google::InitGoogleLogging(argv[0]);
|
||||
FLAGS_logtostderr = true;
|
||||
|
||||
TCLAP::CmdLine cmd("funasr-onnx-offline-vad", ' ', "1.0");
|
||||
TCLAP::ValueArg<std::string> model_dir("", MODEL_DIR, "the asr online model path, which contains model.onnx, decoder.onnx, config.yaml, am.mvn", true, "", "string");
|
||||
TCLAP::ValueArg<std::string> quantize("", QUANTIZE, "true (Default), load the model of model.onnx in model_dir. If set true, load the model of model_quant.onnx in model_dir", false, "true", "string");
|
||||
|
||||
TCLAP::ValueArg<std::string> wav_path("", WAV_PATH, "the input could be: wav_path, e.g.: asr_example.wav; pcm_path, e.g.: asr_example.pcm; wav.scp, kaldi style wav list (wav_id \t wav_path)", true, "", "string");
|
||||
TCLAP::ValueArg<std::int32_t> audio_fs("", AUDIO_FS, "the sample rate of audio", false, 16000, "int32_t");
|
||||
|
||||
cmd.add(model_dir);
|
||||
cmd.add(quantize);
|
||||
cmd.add(wav_path);
|
||||
cmd.add(audio_fs);
|
||||
cmd.parse(argc, argv);
|
||||
|
||||
std::map<std::string, std::string> model_path;
|
||||
GetValue(model_dir, MODEL_DIR, model_path);
|
||||
GetValue(quantize, QUANTIZE, model_path);
|
||||
GetValue(wav_path, WAV_PATH, model_path);
|
||||
|
||||
struct timeval start, end;
|
||||
gettimeofday(&start, nullptr);
|
||||
int thread_num = 1;
|
||||
FUNASR_HANDLE asr_handle=FunASRInit(model_path, thread_num, ASR_ONLINE);
|
||||
|
||||
if (!asr_handle)
|
||||
{
|
||||
LOG(ERROR) << "FunVad init failed";
|
||||
exit(-1);
|
||||
}
|
||||
|
||||
gettimeofday(&end, nullptr);
|
||||
long seconds = (end.tv_sec - start.tv_sec);
|
||||
long modle_init_micros = ((seconds * 1000000) + end.tv_usec) - (start.tv_usec);
|
||||
LOG(INFO) << "Model initialization takes " << (double)modle_init_micros / 1000000 << " s";
|
||||
|
||||
// read wav_path
|
||||
vector<string> wav_list;
|
||||
vector<string> wav_ids;
|
||||
string default_id = "wav_default_id";
|
||||
string wav_path_ = model_path.at(WAV_PATH);
|
||||
if(is_target_file(wav_path_, "scp")){
|
||||
ifstream in(wav_path_);
|
||||
if (!in.is_open()) {
|
||||
LOG(ERROR) << "Failed to open file: " << model_path.at(WAV_SCP) ;
|
||||
return 0;
|
||||
}
|
||||
string line;
|
||||
while(getline(in, line))
|
||||
{
|
||||
istringstream iss(line);
|
||||
string column1, column2;
|
||||
iss >> column1 >> column2;
|
||||
wav_list.emplace_back(column2);
|
||||
wav_ids.emplace_back(column1);
|
||||
}
|
||||
in.close();
|
||||
}else{
|
||||
wav_list.emplace_back(wav_path_);
|
||||
wav_ids.emplace_back(default_id);
|
||||
}
|
||||
|
||||
// init online features
|
||||
FUNASR_HANDLE online_handle=FunASROnlineInit(asr_handle);
|
||||
float snippet_time = 0.0f;
|
||||
long taking_micros = 0;
|
||||
for (int i = 0; i < wav_list.size(); i++) {
|
||||
auto& wav_file = wav_list[i];
|
||||
auto& wav_id = wav_ids[i];
|
||||
|
||||
int32_t sampling_rate_ = audio_fs.getValue();
|
||||
funasr::Audio audio(1);
|
||||
if(is_target_file(wav_file.c_str(), "wav")){
|
||||
if(!audio.LoadWav2Char(wav_file.c_str(), &sampling_rate_)){
|
||||
LOG(ERROR)<<"Failed to load "<< wav_file;
|
||||
exit(-1);
|
||||
}
|
||||
}else if(is_target_file(wav_file.c_str(), "pcm")){
|
||||
if (!audio.LoadPcmwav2Char(wav_file.c_str(), &sampling_rate_)){
|
||||
LOG(ERROR)<<"Failed to load "<< wav_file;
|
||||
exit(-1);
|
||||
}
|
||||
}else{
|
||||
if (!audio.FfmpegLoad(wav_file.c_str(), true)){
|
||||
LOG(ERROR)<<"Failed to load "<< wav_file;
|
||||
exit(-1);
|
||||
}
|
||||
}
|
||||
char* speech_buff = audio.GetSpeechChar();
|
||||
int buff_len = audio.GetSpeechLen()*2;
|
||||
|
||||
int step = 9600*2;
|
||||
bool is_final = false;
|
||||
|
||||
string final_res="";
|
||||
for (int sample_offset = 0; sample_offset < buff_len; sample_offset += std::min(step, buff_len - sample_offset)) {
|
||||
if (sample_offset + step >= buff_len - 1) {
|
||||
step = buff_len - sample_offset;
|
||||
is_final = true;
|
||||
} else {
|
||||
is_final = false;
|
||||
}
|
||||
gettimeofday(&start, nullptr);
|
||||
FUNASR_RESULT result = FunASRInferBuffer(online_handle, speech_buff+sample_offset, step, RASR_NONE, nullptr, is_final, sampling_rate_);
|
||||
gettimeofday(&end, nullptr);
|
||||
seconds = (end.tv_sec - start.tv_sec);
|
||||
taking_micros += ((seconds * 1000000) + end.tv_usec) - (start.tv_usec);
|
||||
|
||||
if (result)
|
||||
{
|
||||
string msg = FunASRGetResult(result, 0);
|
||||
final_res += msg;
|
||||
LOG(INFO)<< wav_id <<" : "<<msg;
|
||||
snippet_time += FunASRGetRetSnippetTime(result);
|
||||
FunASRFreeResult(result);
|
||||
}
|
||||
else
|
||||
{
|
||||
LOG(ERROR) << ("No return data!\n");
|
||||
}
|
||||
}
|
||||
LOG(INFO)<<"Final results " << wav_id <<" : "<<final_res;
|
||||
}
|
||||
|
||||
LOG(INFO) << "Audio length: " << (double)snippet_time << " s";
|
||||
LOG(INFO) << "Model inference takes: " << (double)taking_micros / 1000000 <<" s";
|
||||
LOG(INFO) << "Model inference RTF: " << (double)taking_micros/ (snippet_time*1000000);
|
||||
FunASRUninit(asr_handle);
|
||||
FunASRUninit(online_handle);
|
||||
return 0;
|
||||
}
|
||||
|
||||
130
modules/python/vendors/FunASR/runtime/onnxruntime/bin/funasr-onnx-online-punc.cpp
vendored
Normal file
130
modules/python/vendors/FunASR/runtime/onnxruntime/bin/funasr-onnx-online-punc.cpp
vendored
Normal file
@@ -0,0 +1,130 @@
|
||||
/**
|
||||
* Copyright FunASR (https://github.com/alibaba-damo-academy/FunASR). All Rights Reserved.
|
||||
* MIT License (https://opensource.org/licenses/MIT)
|
||||
*/
|
||||
|
||||
#ifndef _WIN32
|
||||
#include <sys/time.h>
|
||||
#else
|
||||
#include <win_func.h>
|
||||
#endif
|
||||
|
||||
#include <iostream>
|
||||
#include <fstream>
|
||||
#include <sstream>
|
||||
#include <map>
|
||||
#include <glog/logging.h>
|
||||
#include "funasrruntime.h"
|
||||
#include "tclap/CmdLine.h"
|
||||
#include "com-define.h"
|
||||
|
||||
using namespace std;
|
||||
|
||||
void GetValue(TCLAP::ValueArg<std::string>& value_arg, string key, std::map<std::string, std::string>& model_path)
|
||||
{
|
||||
if (value_arg.isSet()){
|
||||
model_path.insert({key, value_arg.getValue()});
|
||||
LOG(INFO)<< key << " : " << value_arg.getValue();
|
||||
}
|
||||
}
|
||||
|
||||
void splitString(vector<string>& strings, const string& org_string, const string& seq) {
|
||||
string::size_type p1 = 0;
|
||||
string::size_type p2 = org_string.find(seq);
|
||||
|
||||
while (p2 != string::npos) {
|
||||
if (p2 == p1) {
|
||||
++p1;
|
||||
p2 = org_string.find(seq, p1);
|
||||
continue;
|
||||
}
|
||||
strings.push_back(org_string.substr(p1, p2 - p1));
|
||||
p1 = p2 + seq.size();
|
||||
p2 = org_string.find(seq, p1);
|
||||
}
|
||||
|
||||
if (p1 != org_string.size()) {
|
||||
strings.push_back(org_string.substr(p1));
|
||||
}
|
||||
}
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
google::InitGoogleLogging(argv[0]);
|
||||
FLAGS_logtostderr = true;
|
||||
|
||||
TCLAP::CmdLine cmd("funasr-onnx-online-punc", ' ', "1.0");
|
||||
TCLAP::ValueArg<std::string> model_dir("", MODEL_DIR, "the punc model path, which contains model.onnx, punc.yaml", true, "", "string");
|
||||
TCLAP::ValueArg<std::string> quantize("", QUANTIZE, "true (Default), load the model of model.onnx in model_dir. If set true, load the model of model_quant.onnx in model_dir", false, "true", "string");
|
||||
TCLAP::ValueArg<std::string> txt_path("", TXT_PATH, "txt file path, one sentence per line", true, "", "string");
|
||||
|
||||
cmd.add(model_dir);
|
||||
cmd.add(quantize);
|
||||
cmd.add(txt_path);
|
||||
cmd.parse(argc, argv);
|
||||
|
||||
std::map<std::string, std::string> model_path;
|
||||
GetValue(model_dir, MODEL_DIR, model_path);
|
||||
GetValue(quantize, QUANTIZE, model_path);
|
||||
GetValue(txt_path, TXT_PATH, model_path);
|
||||
|
||||
struct timeval start, end;
|
||||
gettimeofday(&start, nullptr);
|
||||
int thread_num = 1;
|
||||
FUNASR_HANDLE punc_hanlde=CTTransformerInit(model_path, thread_num, PUNC_ONLINE);
|
||||
|
||||
if (!punc_hanlde)
|
||||
{
|
||||
LOG(ERROR) << "FunASR init failed";
|
||||
exit(-1);
|
||||
}
|
||||
|
||||
gettimeofday(&end, nullptr);
|
||||
long seconds = (end.tv_sec - start.tv_sec);
|
||||
long modle_init_micros = ((seconds * 1000000) + end.tv_usec) - (start.tv_usec);
|
||||
LOG(INFO) << "Model initialization takes " << (double)modle_init_micros / 1000000 << " s";
|
||||
|
||||
// read txt_path
|
||||
vector<string> txt_list;
|
||||
|
||||
if(model_path.find(TXT_PATH)!=model_path.end()){
|
||||
ifstream in(model_path.at(TXT_PATH));
|
||||
if (!in.is_open()) {
|
||||
LOG(ERROR) << "Failed to open file: " << model_path.at(TXT_PATH) ;
|
||||
return 0;
|
||||
}
|
||||
string line;
|
||||
while(getline(in, line))
|
||||
{
|
||||
txt_list.emplace_back(line);
|
||||
}
|
||||
in.close();
|
||||
}
|
||||
|
||||
long taking_micros = 0;
|
||||
for(auto& txt_str : txt_list){
|
||||
vector<string> vad_strs;
|
||||
splitString(vad_strs, txt_str, "|");
|
||||
string str_out;
|
||||
FUNASR_RESULT result = nullptr;
|
||||
gettimeofday(&start, nullptr);
|
||||
for(auto& vad_str:vad_strs){
|
||||
result=CTTransformerInfer(punc_hanlde, vad_str.c_str(), RASR_NONE, nullptr, PUNC_ONLINE, result);
|
||||
if(result){
|
||||
string msg = CTTransformerGetResult(result, 0);
|
||||
str_out += msg;
|
||||
LOG(INFO)<<"Online result: "<<msg;
|
||||
}
|
||||
}
|
||||
gettimeofday(&end, nullptr);
|
||||
seconds = (end.tv_sec - start.tv_sec);
|
||||
taking_micros += ((seconds * 1000000) + end.tv_usec) - (start.tv_usec);
|
||||
LOG(INFO)<<"Results: "<<str_out;
|
||||
CTTransformerFreeResult(result);
|
||||
}
|
||||
|
||||
LOG(INFO) << "Model inference takes: " << (double)taking_micros / 1000000 <<" s";
|
||||
CTTransformerUninit(punc_hanlde);
|
||||
return 0;
|
||||
}
|
||||
|
||||
280
modules/python/vendors/FunASR/runtime/onnxruntime/bin/funasr-onnx-online-rtf.cpp
vendored
Normal file
280
modules/python/vendors/FunASR/runtime/onnxruntime/bin/funasr-onnx-online-rtf.cpp
vendored
Normal file
@@ -0,0 +1,280 @@
|
||||
/**
|
||||
* Copyright FunASR (https://github.com/alibaba-damo-academy/FunASR). All Rights Reserved.
|
||||
* MIT License (https://opensource.org/licenses/MIT)
|
||||
*/
|
||||
|
||||
#ifndef _WIN32
|
||||
#include <sys/time.h>
|
||||
#else
|
||||
#include <win_func.h>
|
||||
#endif
|
||||
|
||||
#include <glog/logging.h>
|
||||
#include "funasrruntime.h"
|
||||
#include "tclap/CmdLine.h"
|
||||
#include "com-define.h"
|
||||
|
||||
#include <iostream>
|
||||
#include <fstream>
|
||||
#include <sstream>
|
||||
#include <vector>
|
||||
#include <atomic>
|
||||
#include <mutex>
|
||||
#include <thread>
|
||||
#include <map>
|
||||
#include "audio.h"
|
||||
|
||||
using namespace std;
|
||||
|
||||
std::atomic<int> wav_index(0);
|
||||
std::mutex mtx;
|
||||
|
||||
bool is_target_file(const std::string& filename, const std::string target) {
|
||||
std::size_t pos = filename.find_last_of(".");
|
||||
if (pos == std::string::npos) {
|
||||
return false;
|
||||
}
|
||||
std::string extension = filename.substr(pos + 1);
|
||||
return (extension == target);
|
||||
}
|
||||
|
||||
void runReg(FUNASR_HANDLE asr_handle, vector<string> wav_list, vector<string> wav_ids, int audio_fs,
|
||||
float* total_length, long* total_time, int core_id) {
|
||||
|
||||
struct timeval start, end;
|
||||
long seconds = 0;
|
||||
float n_total_length = 0.0f;
|
||||
long n_total_time = 0;
|
||||
|
||||
// init online features
|
||||
FUNASR_HANDLE online_handle=FunASROnlineInit(asr_handle);
|
||||
|
||||
// warm up
|
||||
for (size_t i = 0; i < 10; i++)
|
||||
{
|
||||
int32_t sampling_rate_ = audio_fs;
|
||||
funasr::Audio audio(1);
|
||||
if(is_target_file(wav_list[0].c_str(), "wav")){
|
||||
if(!audio.LoadWav2Char(wav_list[0].c_str(), &sampling_rate_)){
|
||||
LOG(ERROR)<<"Failed to load "<< wav_list[0];
|
||||
exit(-1);
|
||||
}
|
||||
}else if(is_target_file(wav_list[0].c_str(), "pcm")){
|
||||
if (!audio.LoadPcmwav2Char(wav_list[0].c_str(), &sampling_rate_)){
|
||||
LOG(ERROR)<<"Failed to load "<< wav_list[0];
|
||||
exit(-1);
|
||||
}
|
||||
}else{
|
||||
if (!audio.FfmpegLoad(wav_list[0].c_str(), true)){
|
||||
LOG(ERROR)<<"Failed to load "<< wav_list[0];
|
||||
exit(-1);
|
||||
}
|
||||
}
|
||||
char* speech_buff = audio.GetSpeechChar();
|
||||
int buff_len = audio.GetSpeechLen()*2;
|
||||
|
||||
int step = 9600*2;
|
||||
bool is_final = false;
|
||||
|
||||
string final_res="";
|
||||
for (int sample_offset = 0; sample_offset < buff_len; sample_offset += std::min(step, buff_len - sample_offset)) {
|
||||
if (sample_offset + step >= buff_len - 1) {
|
||||
step = buff_len - sample_offset;
|
||||
is_final = true;
|
||||
} else {
|
||||
is_final = false;
|
||||
}
|
||||
FUNASR_RESULT result = FunASRInferBuffer(online_handle, speech_buff+sample_offset, step, RASR_NONE, nullptr, is_final, sampling_rate_);
|
||||
if (result)
|
||||
{
|
||||
FunASRFreeResult(result);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
while (true) {
|
||||
// 使用原子变量获取索引并递增
|
||||
int i = wav_index.fetch_add(1);
|
||||
if (i >= wav_list.size()) {
|
||||
break;
|
||||
}
|
||||
int32_t sampling_rate_ = audio_fs;
|
||||
funasr::Audio audio(1);
|
||||
if(is_target_file(wav_list[i].c_str(), "wav")){
|
||||
if(!audio.LoadWav2Char(wav_list[i].c_str(), &sampling_rate_)){
|
||||
LOG(ERROR)<<"Failed to load "<< wav_list[i];
|
||||
exit(-1);
|
||||
}
|
||||
}else if(is_target_file(wav_list[i].c_str(), "pcm")){
|
||||
if (!audio.LoadPcmwav2Char(wav_list[i].c_str(), &sampling_rate_)){
|
||||
LOG(ERROR)<<"Failed to load "<< wav_list[i];
|
||||
exit(-1);
|
||||
}
|
||||
}else{
|
||||
if (!audio.FfmpegLoad(wav_list[i].c_str(), true)){
|
||||
LOG(ERROR)<<"Failed to load "<< wav_list[i];
|
||||
exit(-1);
|
||||
}
|
||||
}
|
||||
char* speech_buff = audio.GetSpeechChar();
|
||||
int buff_len = audio.GetSpeechLen()*2;
|
||||
|
||||
int step = 9600*2;
|
||||
bool is_final = false;
|
||||
|
||||
string final_res="";
|
||||
for (int sample_offset = 0; sample_offset < buff_len; sample_offset += std::min(step, buff_len - sample_offset)) {
|
||||
if (sample_offset + step >= buff_len - 1) {
|
||||
step = buff_len - sample_offset;
|
||||
is_final = true;
|
||||
} else {
|
||||
is_final = false;
|
||||
}
|
||||
gettimeofday(&start, nullptr);
|
||||
FUNASR_RESULT result = FunASRInferBuffer(online_handle, speech_buff+sample_offset, step, RASR_NONE, nullptr, is_final, sampling_rate_);
|
||||
gettimeofday(&end, nullptr);
|
||||
seconds = (end.tv_sec - start.tv_sec);
|
||||
long taking_micros = ((seconds * 1000000) + end.tv_usec) - (start.tv_usec);
|
||||
n_total_time += taking_micros;
|
||||
|
||||
if (result)
|
||||
{
|
||||
string msg = FunASRGetResult(result, 0);
|
||||
final_res += msg;
|
||||
LOG(INFO) << "Thread: " << this_thread::get_id() << "," << wav_ids[i] << " : " << msg;
|
||||
float snippet_time = FunASRGetRetSnippetTime(result);
|
||||
n_total_length += snippet_time;
|
||||
FunASRFreeResult(result);
|
||||
}
|
||||
else
|
||||
{
|
||||
LOG(ERROR) << ("No return data!\n");
|
||||
}
|
||||
}
|
||||
LOG(INFO) << "Thread: " << this_thread::get_id() << ", Final results " << wav_ids[i] << " : " << final_res;
|
||||
|
||||
}
|
||||
{
|
||||
lock_guard<mutex> guard(mtx);
|
||||
*total_length += n_total_length;
|
||||
if(*total_time < n_total_time){
|
||||
*total_time = n_total_time;
|
||||
}
|
||||
}
|
||||
FunASRUninit(online_handle);
|
||||
}
|
||||
|
||||
void GetValue(TCLAP::ValueArg<std::string>& value_arg, string key, std::map<std::string, std::string>& model_path)
|
||||
{
|
||||
if (value_arg.isSet()){
|
||||
model_path.insert({key, value_arg.getValue()});
|
||||
LOG(INFO)<< key << " : " << value_arg.getValue();
|
||||
}
|
||||
}
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
google::InitGoogleLogging(argv[0]);
|
||||
FLAGS_logtostderr = true;
|
||||
|
||||
TCLAP::CmdLine cmd("funasr-onnx-online-rtf", ' ', "1.0");
|
||||
TCLAP::ValueArg<std::string> model_dir("", MODEL_DIR, "the model path, which contains model.onnx, config.yaml, am.mvn", true, "", "string");
|
||||
TCLAP::ValueArg<std::string> quantize("", QUANTIZE, "true (Default), load the model of model.onnx in model_dir. If set true, load the model of model_quant.onnx in model_dir", false, "true", "string");
|
||||
TCLAP::ValueArg<std::string> vad_dir("", VAD_DIR, "the vad model path, which contains model.onnx, vad.yaml, vad.mvn", false, "", "string");
|
||||
TCLAP::ValueArg<std::string> vad_quant("", VAD_QUANT, "true (Default), load the model of model.onnx in vad_dir. If set true, load the model of model_quant.onnx in vad_dir", false, "true", "string");
|
||||
TCLAP::ValueArg<std::string> punc_dir("", PUNC_DIR, "the punc model path, which contains model.onnx, punc.yaml", false, "", "string");
|
||||
TCLAP::ValueArg<std::string> punc_quant("", PUNC_QUANT, "true (Default), load the model of model.onnx in punc_dir. If set true, load the model of model_quant.onnx in punc_dir", false, "true", "string");
|
||||
|
||||
TCLAP::ValueArg<std::string> wav_path("", WAV_PATH, "the input could be: wav_path, e.g.: asr_example.wav; pcm_path, e.g.: asr_example.pcm; wav.scp, kaldi style wav list (wav_id \t wav_path)", true, "", "string");
|
||||
TCLAP::ValueArg<std::int32_t> audio_fs("", AUDIO_FS, "the sample rate of audio", false, 16000, "int32_t");
|
||||
TCLAP::ValueArg<std::int32_t> thread_num("", THREAD_NUM, "multi-thread num for rtf", true, 0, "int32_t");
|
||||
|
||||
cmd.add(model_dir);
|
||||
cmd.add(quantize);
|
||||
cmd.add(vad_dir);
|
||||
cmd.add(vad_quant);
|
||||
cmd.add(punc_dir);
|
||||
cmd.add(punc_quant);
|
||||
cmd.add(wav_path);
|
||||
cmd.add(audio_fs);
|
||||
cmd.add(thread_num);
|
||||
cmd.parse(argc, argv);
|
||||
|
||||
std::map<std::string, std::string> model_path;
|
||||
GetValue(model_dir, MODEL_DIR, model_path);
|
||||
GetValue(quantize, QUANTIZE, model_path);
|
||||
GetValue(vad_dir, VAD_DIR, model_path);
|
||||
GetValue(vad_quant, VAD_QUANT, model_path);
|
||||
GetValue(punc_dir, PUNC_DIR, model_path);
|
||||
GetValue(punc_quant, PUNC_QUANT, model_path);
|
||||
GetValue(wav_path, WAV_PATH, model_path);
|
||||
|
||||
struct timeval start, end;
|
||||
gettimeofday(&start, nullptr);
|
||||
FUNASR_HANDLE asr_handle=FunASRInit(model_path, 1, ASR_ONLINE);
|
||||
|
||||
if (!asr_handle)
|
||||
{
|
||||
LOG(ERROR) << "FunASR init failed";
|
||||
exit(-1);
|
||||
}
|
||||
|
||||
gettimeofday(&end, nullptr);
|
||||
long seconds = (end.tv_sec - start.tv_sec);
|
||||
long modle_init_micros = ((seconds * 1000000) + end.tv_usec) - (start.tv_usec);
|
||||
LOG(INFO) << "Model initialization takes " << (double)modle_init_micros / 1000000 << " s";
|
||||
|
||||
// read wav_path
|
||||
vector<string> wav_list;
|
||||
vector<string> wav_ids;
|
||||
string default_id = "wav_default_id";
|
||||
string wav_path_ = model_path.at(WAV_PATH);
|
||||
if(is_target_file(wav_path_, "wav") || is_target_file(wav_path_, "pcm")){
|
||||
wav_list.emplace_back(wav_path_);
|
||||
wav_ids.emplace_back(default_id);
|
||||
}
|
||||
else if(is_target_file(wav_path_, "scp")){
|
||||
ifstream in(wav_path_);
|
||||
if (!in.is_open()) {
|
||||
LOG(ERROR) << "Failed to open file: " << model_path.at(WAV_SCP) ;
|
||||
return 0;
|
||||
}
|
||||
string line;
|
||||
while(getline(in, line))
|
||||
{
|
||||
istringstream iss(line);
|
||||
string column1, column2;
|
||||
iss >> column1 >> column2;
|
||||
wav_list.emplace_back(column2);
|
||||
wav_ids.emplace_back(column1);
|
||||
}
|
||||
in.close();
|
||||
}else{
|
||||
LOG(ERROR)<<"Please check the wav extension!";
|
||||
exit(-1);
|
||||
}
|
||||
|
||||
// 多线程测试
|
||||
float total_length = 0.0f;
|
||||
long total_time = 0;
|
||||
std::vector<std::thread> threads;
|
||||
|
||||
int rtf_threds = thread_num.getValue();
|
||||
for (int i = 0; i < rtf_threds; i++)
|
||||
{
|
||||
threads.emplace_back(thread(runReg, asr_handle, wav_list, wav_ids, audio_fs.getValue(), &total_length, &total_time, i));
|
||||
}
|
||||
|
||||
for (auto& thread : threads)
|
||||
{
|
||||
thread.join();
|
||||
}
|
||||
|
||||
LOG(INFO) << "total_time_wav " << (long)(total_length * 1000) << " ms";
|
||||
LOG(INFO) << "total_time_comput " << total_time / 1000 << " ms";
|
||||
LOG(INFO) << "total_rtf " << (double)total_time/ (total_length*1000000);
|
||||
LOG(INFO) << "speedup " << 1.0/((double)total_time/ (total_length*1000000));
|
||||
|
||||
FunASRUninit(asr_handle);
|
||||
return 0;
|
||||
}
|
||||
200
modules/python/vendors/FunASR/runtime/onnxruntime/bin/funasr-onnx-online-vad.cpp
vendored
Normal file
200
modules/python/vendors/FunASR/runtime/onnxruntime/bin/funasr-onnx-online-vad.cpp
vendored
Normal file
@@ -0,0 +1,200 @@
|
||||
/**
|
||||
* Copyright FunASR (https://github.com/alibaba-damo-academy/FunASR). All Rights Reserved.
|
||||
* MIT License (https://opensource.org/licenses/MIT)
|
||||
*/
|
||||
|
||||
#ifndef _WIN32
|
||||
#include <sys/time.h>
|
||||
#else
|
||||
#include <win_func.h>
|
||||
#endif
|
||||
|
||||
#include <iostream>
|
||||
#include <fstream>
|
||||
#include <sstream>
|
||||
#include <map>
|
||||
#include <vector>
|
||||
#include <glog/logging.h>
|
||||
#include "funasrruntime.h"
|
||||
#include "tclap/CmdLine.h"
|
||||
#include "com-define.h"
|
||||
#include "audio.h"
|
||||
|
||||
using namespace std;
|
||||
|
||||
bool is_target_file(const std::string& filename, const std::string target) {
|
||||
std::size_t pos = filename.find_last_of(".");
|
||||
if (pos == std::string::npos) {
|
||||
return false;
|
||||
}
|
||||
std::string extension = filename.substr(pos + 1);
|
||||
return (extension == target);
|
||||
}
|
||||
|
||||
void GetValue(TCLAP::ValueArg<std::string>& value_arg, string key, std::map<std::string, std::string>& model_path)
|
||||
{
|
||||
if (value_arg.isSet()){
|
||||
model_path.insert({key, value_arg.getValue()});
|
||||
LOG(INFO)<< key << " : " << value_arg.getValue();
|
||||
}
|
||||
}
|
||||
|
||||
void print_segs(vector<vector<int>>* vec, string &wav_id) {
|
||||
if((*vec).size() == 0){
|
||||
return;
|
||||
}
|
||||
string seg_out=wav_id + ": [";
|
||||
for (int i = 0; i < vec->size(); i++) {
|
||||
vector<int> inner_vec = (*vec)[i];
|
||||
if(inner_vec.size() == 0){
|
||||
continue;
|
||||
}
|
||||
seg_out += "[";
|
||||
for (int j = 0; j < inner_vec.size(); j++) {
|
||||
seg_out += to_string(inner_vec[j]);
|
||||
if (j != inner_vec.size() - 1) {
|
||||
seg_out += ",";
|
||||
}
|
||||
}
|
||||
seg_out += "]";
|
||||
if (i != vec->size() - 1) {
|
||||
seg_out += ",";
|
||||
}
|
||||
}
|
||||
seg_out += "]";
|
||||
LOG(INFO)<<seg_out;
|
||||
}
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
google::InitGoogleLogging(argv[0]);
|
||||
FLAGS_logtostderr = true;
|
||||
|
||||
TCLAP::CmdLine cmd("funasr-onnx-offline-vad", ' ', "1.0");
|
||||
TCLAP::ValueArg<std::string> model_dir("", MODEL_DIR, "the vad model path, which contains model.onnx, vad.yaml, vad.mvn", true, "", "string");
|
||||
TCLAP::ValueArg<std::string> quantize("", QUANTIZE, "false (Default), load the model of model.onnx in model_dir. If set true, load the model of model_quant.onnx in model_dir", false, "true", "string");
|
||||
|
||||
TCLAP::ValueArg<std::string> wav_path("", WAV_PATH, "the input could be: wav_path, e.g.: asr_example.wav; pcm_path, e.g.: asr_example.pcm; wav.scp, kaldi style wav list (wav_id \t wav_path)", true, "", "string");
|
||||
TCLAP::ValueArg<std::int32_t> audio_fs("", AUDIO_FS, "the sample rate of audio", false, 16000, "int32_t");
|
||||
|
||||
cmd.add(model_dir);
|
||||
cmd.add(quantize);
|
||||
cmd.add(wav_path);
|
||||
cmd.add(audio_fs);
|
||||
cmd.parse(argc, argv);
|
||||
|
||||
std::map<std::string, std::string> model_path;
|
||||
GetValue(model_dir, MODEL_DIR, model_path);
|
||||
GetValue(quantize, QUANTIZE, model_path);
|
||||
GetValue(wav_path, WAV_PATH, model_path);
|
||||
|
||||
struct timeval start, end;
|
||||
gettimeofday(&start, nullptr);
|
||||
int thread_num = 1;
|
||||
FUNASR_HANDLE vad_hanlde=FsmnVadInit(model_path, thread_num);
|
||||
|
||||
if (!vad_hanlde)
|
||||
{
|
||||
LOG(ERROR) << "FunVad init failed";
|
||||
exit(-1);
|
||||
}
|
||||
|
||||
gettimeofday(&end, nullptr);
|
||||
long seconds = (end.tv_sec - start.tv_sec);
|
||||
long modle_init_micros = ((seconds * 1000000) + end.tv_usec) - (start.tv_usec);
|
||||
LOG(INFO) << "Model initialization takes " << (double)modle_init_micros / 1000000 << " s";
|
||||
|
||||
// read wav_path
|
||||
vector<string> wav_list;
|
||||
vector<string> wav_ids;
|
||||
string default_id = "wav_default_id";
|
||||
string wav_path_ = model_path.at(WAV_PATH);
|
||||
if(is_target_file(wav_path_, "wav") || is_target_file(wav_path_, "pcm")){
|
||||
wav_list.emplace_back(wav_path_);
|
||||
wav_ids.emplace_back(default_id);
|
||||
}
|
||||
else if(is_target_file(wav_path_, "scp")){
|
||||
ifstream in(wav_path_);
|
||||
if (!in.is_open()) {
|
||||
LOG(ERROR) << "Failed to open file: " << model_path.at(WAV_SCP) ;
|
||||
return 0;
|
||||
}
|
||||
string line;
|
||||
while(getline(in, line))
|
||||
{
|
||||
istringstream iss(line);
|
||||
string column1, column2;
|
||||
iss >> column1 >> column2;
|
||||
wav_list.emplace_back(column2);
|
||||
wav_ids.emplace_back(column1);
|
||||
}
|
||||
in.close();
|
||||
}else{
|
||||
LOG(ERROR)<<"Please check the wav extension!";
|
||||
exit(-1);
|
||||
}
|
||||
// init online features
|
||||
FUNASR_HANDLE online_hanlde=FsmnVadOnlineInit(vad_hanlde);
|
||||
float snippet_time = 0.0f;
|
||||
long taking_micros = 0;
|
||||
for (int i = 0; i < wav_list.size(); i++) {
|
||||
auto& wav_file = wav_list[i];
|
||||
auto& wav_id = wav_ids[i];
|
||||
|
||||
int32_t sampling_rate_ = audio_fs.getValue();
|
||||
funasr::Audio audio(1);
|
||||
if(is_target_file(wav_file.c_str(), "wav")){
|
||||
if(!audio.LoadWav2Char(wav_file.c_str(), &sampling_rate_)){
|
||||
LOG(ERROR)<<"Failed to load "<< wav_file;
|
||||
exit(-1);
|
||||
}
|
||||
}else if(is_target_file(wav_file.c_str(), "pcm")){
|
||||
if (!audio.LoadPcmwav2Char(wav_file.c_str(), &sampling_rate_)){
|
||||
LOG(ERROR)<<"Failed to load "<< wav_file;
|
||||
exit(-1);
|
||||
}
|
||||
}else{
|
||||
LOG(ERROR)<<"Wrong wav extension";
|
||||
exit(-1);
|
||||
}
|
||||
char* speech_buff = audio.GetSpeechChar();
|
||||
int buff_len = audio.GetSpeechLen()*2;
|
||||
|
||||
int step = 800*2;
|
||||
bool is_final = false;
|
||||
|
||||
for (int sample_offset = 0; sample_offset < buff_len; sample_offset += std::min(step, buff_len - sample_offset)) {
|
||||
if (sample_offset + step >= buff_len - 1) {
|
||||
step = buff_len - sample_offset;
|
||||
is_final = true;
|
||||
} else {
|
||||
is_final = false;
|
||||
}
|
||||
gettimeofday(&start, nullptr);
|
||||
FUNASR_RESULT result = FsmnVadInferBuffer(online_hanlde, speech_buff+sample_offset, step, nullptr, is_final, sampling_rate_);
|
||||
gettimeofday(&end, nullptr);
|
||||
seconds = (end.tv_sec - start.tv_sec);
|
||||
taking_micros += ((seconds * 1000000) + end.tv_usec) - (start.tv_usec);
|
||||
|
||||
if (result)
|
||||
{
|
||||
vector<std::vector<int>>* vad_segments = FsmnVadGetResult(result, 0);
|
||||
print_segs(vad_segments, wav_id);
|
||||
snippet_time += FsmnVadGetRetSnippetTime(result);
|
||||
FsmnVadFreeResult(result);
|
||||
}
|
||||
else
|
||||
{
|
||||
LOG(ERROR) << ("No return data!\n");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
LOG(INFO) << "Audio length: " << (double)snippet_time << " s";
|
||||
LOG(INFO) << "Model inference takes: " << (double)taking_micros / 1000000 <<" s";
|
||||
LOG(INFO) << "Model inference RTF: " << (double)taking_micros/ (snippet_time*1000000);
|
||||
FsmnVadUninit(online_hanlde);
|
||||
FsmnVadUninit(vad_hanlde);
|
||||
return 0;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user