funasr-onnx-2pass.cpp 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. /**
  2. * Copyright FunASR (https://github.com/alibaba-damo-academy/FunASR). All Rights Reserved.
  3. * MIT License (https://opensource.org/licenses/MIT)
  4. */
  5. #ifndef _WIN32
  6. #include <sys/time.h>
  7. #else
  8. #include <win_func.h>
  9. #endif
  10. #include <iostream>
  11. #include <fstream>
  12. #include <sstream>
  13. #include <map>
  14. #include <glog/logging.h>
  15. #include "util.h"
  16. #include "funasrruntime.h"
  17. #include "tclap/CmdLine.h"
  18. #include "com-define.h"
  19. #include "audio.h"
  20. using namespace std;
  21. bool is_target_file(const std::string& filename, const std::string target) {
  22. std::size_t pos = filename.find_last_of(".");
  23. if (pos == std::string::npos) {
  24. return false;
  25. }
  26. std::string extension = filename.substr(pos + 1);
  27. return (extension == target);
  28. }
  29. void GetValue(TCLAP::ValueArg<std::string>& value_arg, string key, std::map<std::string, std::string>& model_path)
  30. {
  31. model_path.insert({key, value_arg.getValue()});
  32. LOG(INFO)<< key << " : " << value_arg.getValue();
  33. }
  34. int main(int argc, char** argv)
  35. {
  36. google::InitGoogleLogging(argv[0]);
  37. FLAGS_logtostderr = true;
  38. TCLAP::CmdLine cmd("funasr-onnx-2pass", ' ', "1.0");
  39. 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");
  40. 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");
  41. 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");
  42. TCLAP::ValueArg<std::string> vad_dir("", VAD_DIR, "the vad online model path, which contains model.onnx, vad.yaml, vad.mvn", false, "", "string");
  43. 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");
  44. TCLAP::ValueArg<std::string> punc_dir("", PUNC_DIR, "the punc online model path, which contains model.onnx, punc.yaml", false, "", "string");
  45. 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");
  46. 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");
  47. TCLAP::ValueArg<std::string> asr_mode("", ASR_MODE, "offline, online, 2pass", false, "2pass", "string");
  48. TCLAP::ValueArg<std::int32_t> onnx_thread("", "model-thread-num", "onnxruntime SetIntraOpNumThreads", false, 1, "int32_t");
  49. 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");
  50. TCLAP::ValueArg<std::string> hotword("", HOTWORD, "the hotword file, one hotword perline, Format: Hotword Weight (could be: 阿里巴巴 20)", false, "", "string");
  51. cmd.add(offline_model_dir);
  52. cmd.add(online_model_dir);
  53. cmd.add(quantize);
  54. cmd.add(vad_dir);
  55. cmd.add(vad_quant);
  56. cmd.add(punc_dir);
  57. cmd.add(punc_quant);
  58. cmd.add(itn_dir);
  59. cmd.add(wav_path);
  60. cmd.add(asr_mode);
  61. cmd.add(onnx_thread);
  62. cmd.add(hotword);
  63. cmd.parse(argc, argv);
  64. std::map<std::string, std::string> model_path;
  65. GetValue(offline_model_dir, OFFLINE_MODEL_DIR, model_path);
  66. GetValue(online_model_dir, ONLINE_MODEL_DIR, model_path);
  67. GetValue(quantize, QUANTIZE, model_path);
  68. GetValue(vad_dir, VAD_DIR, model_path);
  69. GetValue(vad_quant, VAD_QUANT, model_path);
  70. GetValue(punc_dir, PUNC_DIR, model_path);
  71. GetValue(punc_quant, PUNC_QUANT, model_path);
  72. GetValue(itn_dir, ITN_DIR, model_path);
  73. GetValue(wav_path, WAV_PATH, model_path);
  74. GetValue(asr_mode, ASR_MODE, model_path);
  75. struct timeval start, end;
  76. gettimeofday(&start, NULL);
  77. int thread_num = onnx_thread.getValue();
  78. int asr_mode_ = -1;
  79. if(model_path[ASR_MODE] == "offline"){
  80. asr_mode_ = 0;
  81. }else if(model_path[ASR_MODE] == "online"){
  82. asr_mode_ = 1;
  83. }else if(model_path[ASR_MODE] == "2pass"){
  84. asr_mode_ = 2;
  85. }else{
  86. LOG(ERROR) << "Wrong asr-mode : " << model_path[ASR_MODE];
  87. exit(-1);
  88. }
  89. FUNASR_HANDLE tpass_handle=FunTpassInit(model_path, thread_num);
  90. if (!tpass_handle)
  91. {
  92. LOG(ERROR) << "FunTpassInit init failed";
  93. exit(-1);
  94. }
  95. gettimeofday(&end, NULL);
  96. long seconds = (end.tv_sec - start.tv_sec);
  97. long modle_init_micros = ((seconds * 1000000) + end.tv_usec) - (start.tv_usec);
  98. LOG(INFO) << "Model initialization takes " << (double)modle_init_micros / 1000000 << " s";
  99. // hotword file
  100. unordered_map<string, int> hws_map;
  101. std::string nn_hotwords_ = "";
  102. std::string hotword_path = hotword.getValue();
  103. LOG(INFO) << "hotword path: " << hotword_path;
  104. funasr::ExtractHws(hotword_path, hws_map, nn_hotwords_);
  105. // read wav_path
  106. vector<string> wav_list;
  107. vector<string> wav_ids;
  108. string default_id = "wav_default_id";
  109. string wav_path_ = model_path.at(WAV_PATH);
  110. if(is_target_file(wav_path_, "scp")){
  111. ifstream in(wav_path_);
  112. if (!in.is_open()) {
  113. LOG(ERROR) << "Failed to open file: " << model_path.at(WAV_SCP) ;
  114. return 0;
  115. }
  116. string line;
  117. while(getline(in, line))
  118. {
  119. istringstream iss(line);
  120. string column1, column2;
  121. iss >> column1 >> column2;
  122. wav_list.emplace_back(column2);
  123. wav_ids.emplace_back(column1);
  124. }
  125. in.close();
  126. }else{
  127. wav_list.emplace_back(wav_path_);
  128. wav_ids.emplace_back(default_id);
  129. }
  130. std::vector<std::vector<float>> hotwords_embedding = CompileHotwordEmbedding(tpass_handle, nn_hotwords_, ASR_TWO_PASS);
  131. // init online features
  132. std::vector<int> chunk_size = {5,10,5};
  133. FUNASR_HANDLE tpass_online_handle=FunTpassOnlineInit(tpass_handle, chunk_size);
  134. float snippet_time = 0.0f;
  135. long taking_micros = 0;
  136. for (int i = 0; i < wav_list.size(); i++) {
  137. auto& wav_file = wav_list[i];
  138. auto& wav_id = wav_ids[i];
  139. int32_t sampling_rate_ = 16000;
  140. funasr::Audio audio(1);
  141. if(is_target_file(wav_file.c_str(), "wav")){
  142. if(!audio.LoadWav2Char(wav_file.c_str(), &sampling_rate_)){
  143. LOG(ERROR)<<"Failed to load "<< wav_file;
  144. exit(-1);
  145. }
  146. }else if(is_target_file(wav_file.c_str(), "pcm")){
  147. if (!audio.LoadPcmwav2Char(wav_file.c_str(), &sampling_rate_)){
  148. LOG(ERROR)<<"Failed to load "<< wav_file;
  149. exit(-1);
  150. }
  151. }else{
  152. if (!audio.FfmpegLoad(wav_file.c_str(), true)){
  153. LOG(ERROR)<<"Failed to load "<< wav_file;
  154. exit(-1);
  155. }
  156. }
  157. char* speech_buff = audio.GetSpeechChar();
  158. int buff_len = audio.GetSpeechLen()*2;
  159. int step = 800*2;
  160. bool is_final = false;
  161. string online_res="";
  162. string tpass_res="";
  163. string time_stamp_res="";
  164. std::vector<std::vector<string>> punc_cache(2);
  165. for (int sample_offset = 0; sample_offset < buff_len; sample_offset += std::min(step, buff_len - sample_offset)) {
  166. if (sample_offset + step >= buff_len - 1) {
  167. step = buff_len - sample_offset;
  168. is_final = true;
  169. } else {
  170. is_final = false;
  171. }
  172. gettimeofday(&start, NULL);
  173. 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);
  174. gettimeofday(&end, NULL);
  175. seconds = (end.tv_sec - start.tv_sec);
  176. taking_micros += ((seconds * 1000000) + end.tv_usec) - (start.tv_usec);
  177. if (result)
  178. {
  179. string online_msg = FunASRGetResult(result, 0);
  180. online_res += online_msg;
  181. if(online_msg != ""){
  182. LOG(INFO)<< wav_id <<" : "<<online_msg;
  183. }
  184. string tpass_msg = FunASRGetTpassResult(result, 0);
  185. tpass_res += tpass_msg;
  186. if(tpass_msg != ""){
  187. LOG(INFO)<< wav_id <<" offline results : "<<tpass_msg;
  188. }
  189. string stamp = FunASRGetStamp(result);
  190. if(stamp !=""){
  191. LOG(INFO) << wav_ids[i] << " time stamp : " << stamp;
  192. if(time_stamp_res == ""){
  193. time_stamp_res += stamp;
  194. }else{
  195. time_stamp_res = time_stamp_res.erase(time_stamp_res.length()-1)
  196. + "," + stamp.substr(1);
  197. }
  198. }
  199. snippet_time += FunASRGetRetSnippetTime(result);
  200. FunASRFreeResult(result);
  201. }
  202. }
  203. if(asr_mode_==2){
  204. LOG(INFO) << wav_id << " Final online results "<<" : "<<online_res;
  205. }
  206. if(asr_mode_==1){
  207. LOG(INFO) << wav_id << " Final online results "<<" : "<<tpass_res;
  208. }
  209. if(asr_mode_==0 || asr_mode_==2){
  210. LOG(INFO) << wav_id << " Final offline results " <<" : "<<tpass_res;
  211. if(time_stamp_res!=""){
  212. LOG(INFO) << wav_id << " Final timestamp results " <<" : "<<time_stamp_res;
  213. }
  214. }
  215. }
  216. LOG(INFO) << "Audio length: " << (double)snippet_time << " s";
  217. LOG(INFO) << "Model inference takes: " << (double)taking_micros / 1000000 <<" s";
  218. LOG(INFO) << "Model inference RTF: " << (double)taking_micros/ (snippet_time*1000000);
  219. FunTpassOnlineUninit(tpass_online_handle);
  220. FunTpassUninit(tpass_handle);
  221. return 0;
  222. }