funasr-onnx-2pass.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  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::int32_t> audio_fs("", AUDIO_FS, "the sample rate of audio", false, 16000, "int32_t");
  51. TCLAP::ValueArg<std::string> hotword("", HOTWORD, "the hotword file, one hotword perline, Format: Hotword Weight (could be: 阿里巴巴 20)", false, "", "string");
  52. cmd.add(offline_model_dir);
  53. cmd.add(online_model_dir);
  54. cmd.add(quantize);
  55. cmd.add(vad_dir);
  56. cmd.add(vad_quant);
  57. cmd.add(punc_dir);
  58. cmd.add(punc_quant);
  59. cmd.add(itn_dir);
  60. cmd.add(wav_path);
  61. cmd.add(audio_fs);
  62. cmd.add(asr_mode);
  63. cmd.add(onnx_thread);
  64. cmd.add(hotword);
  65. cmd.parse(argc, argv);
  66. std::map<std::string, std::string> model_path;
  67. GetValue(offline_model_dir, OFFLINE_MODEL_DIR, model_path);
  68. GetValue(online_model_dir, ONLINE_MODEL_DIR, model_path);
  69. GetValue(quantize, QUANTIZE, model_path);
  70. GetValue(vad_dir, VAD_DIR, model_path);
  71. GetValue(vad_quant, VAD_QUANT, model_path);
  72. GetValue(punc_dir, PUNC_DIR, model_path);
  73. GetValue(punc_quant, PUNC_QUANT, model_path);
  74. GetValue(itn_dir, ITN_DIR, model_path);
  75. GetValue(wav_path, WAV_PATH, model_path);
  76. GetValue(asr_mode, ASR_MODE, model_path);
  77. struct timeval start, end;
  78. gettimeofday(&start, NULL);
  79. int thread_num = onnx_thread.getValue();
  80. int asr_mode_ = -1;
  81. if(model_path[ASR_MODE] == "offline"){
  82. asr_mode_ = 0;
  83. }else if(model_path[ASR_MODE] == "online"){
  84. asr_mode_ = 1;
  85. }else if(model_path[ASR_MODE] == "2pass"){
  86. asr_mode_ = 2;
  87. }else{
  88. LOG(ERROR) << "Wrong asr-mode : " << model_path[ASR_MODE];
  89. exit(-1);
  90. }
  91. FUNASR_HANDLE tpass_handle=FunTpassInit(model_path, thread_num);
  92. if (!tpass_handle)
  93. {
  94. LOG(ERROR) << "FunTpassInit init failed";
  95. exit(-1);
  96. }
  97. gettimeofday(&end, NULL);
  98. long seconds = (end.tv_sec - start.tv_sec);
  99. long modle_init_micros = ((seconds * 1000000) + end.tv_usec) - (start.tv_usec);
  100. LOG(INFO) << "Model initialization takes " << (double)modle_init_micros / 1000000 << " s";
  101. // hotword file
  102. unordered_map<string, int> hws_map;
  103. std::string nn_hotwords_ = "";
  104. std::string hotword_path = hotword.getValue();
  105. LOG(INFO) << "hotword path: " << hotword_path;
  106. funasr::ExtractHws(hotword_path, hws_map, nn_hotwords_);
  107. // read wav_path
  108. vector<string> wav_list;
  109. vector<string> wav_ids;
  110. string default_id = "wav_default_id";
  111. string wav_path_ = model_path.at(WAV_PATH);
  112. if(is_target_file(wav_path_, "scp")){
  113. ifstream in(wav_path_);
  114. if (!in.is_open()) {
  115. LOG(ERROR) << "Failed to open file: " << model_path.at(WAV_SCP) ;
  116. return 0;
  117. }
  118. string line;
  119. while(getline(in, line))
  120. {
  121. istringstream iss(line);
  122. string column1, column2;
  123. iss >> column1 >> column2;
  124. wav_list.emplace_back(column2);
  125. wav_ids.emplace_back(column1);
  126. }
  127. in.close();
  128. }else{
  129. wav_list.emplace_back(wav_path_);
  130. wav_ids.emplace_back(default_id);
  131. }
  132. std::vector<std::vector<float>> hotwords_embedding = CompileHotwordEmbedding(tpass_handle, nn_hotwords_, ASR_TWO_PASS);
  133. // init online features
  134. std::vector<int> chunk_size = {5,10,5};
  135. FUNASR_HANDLE tpass_online_handle=FunTpassOnlineInit(tpass_handle, chunk_size);
  136. float snippet_time = 0.0f;
  137. long taking_micros = 0;
  138. for (int i = 0; i < wav_list.size(); i++) {
  139. auto& wav_file = wav_list[i];
  140. auto& wav_id = wav_ids[i];
  141. int32_t sampling_rate_ = audio_fs.getValue();
  142. funasr::Audio audio(1);
  143. if(is_target_file(wav_file.c_str(), "wav")){
  144. if(!audio.LoadWav2Char(wav_file.c_str(), &sampling_rate_)){
  145. LOG(ERROR)<<"Failed to load "<< wav_file;
  146. exit(-1);
  147. }
  148. }else if(is_target_file(wav_file.c_str(), "pcm")){
  149. if (!audio.LoadPcmwav2Char(wav_file.c_str(), &sampling_rate_)){
  150. LOG(ERROR)<<"Failed to load "<< wav_file;
  151. exit(-1);
  152. }
  153. }else{
  154. if (!audio.FfmpegLoad(wav_file.c_str(), true)){
  155. LOG(ERROR)<<"Failed to load "<< wav_file;
  156. exit(-1);
  157. }
  158. }
  159. char* speech_buff = audio.GetSpeechChar();
  160. int buff_len = audio.GetSpeechLen()*2;
  161. int step = 800*2;
  162. bool is_final = false;
  163. string online_res="";
  164. string tpass_res="";
  165. string time_stamp_res="";
  166. std::vector<std::vector<string>> punc_cache(2);
  167. for (int sample_offset = 0; sample_offset < buff_len; sample_offset += std::min(step, buff_len - sample_offset)) {
  168. if (sample_offset + step >= buff_len - 1) {
  169. step = buff_len - sample_offset;
  170. is_final = true;
  171. } else {
  172. is_final = false;
  173. }
  174. gettimeofday(&start, NULL);
  175. 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);
  176. gettimeofday(&end, NULL);
  177. seconds = (end.tv_sec - start.tv_sec);
  178. taking_micros += ((seconds * 1000000) + end.tv_usec) - (start.tv_usec);
  179. if (result)
  180. {
  181. string online_msg = FunASRGetResult(result, 0);
  182. online_res += online_msg;
  183. if(online_msg != ""){
  184. LOG(INFO)<< wav_id <<" : "<<online_msg;
  185. }
  186. string tpass_msg = FunASRGetTpassResult(result, 0);
  187. tpass_res += tpass_msg;
  188. if(tpass_msg != ""){
  189. LOG(INFO)<< wav_id <<" offline results : "<<tpass_msg;
  190. }
  191. string stamp = FunASRGetStamp(result);
  192. if(stamp !=""){
  193. LOG(INFO) << wav_ids[i] << " time stamp : " << stamp;
  194. if(time_stamp_res == ""){
  195. time_stamp_res += stamp;
  196. }else{
  197. time_stamp_res = time_stamp_res.erase(time_stamp_res.length()-1)
  198. + "," + stamp.substr(1);
  199. }
  200. }
  201. snippet_time += FunASRGetRetSnippetTime(result);
  202. FunASRFreeResult(result);
  203. }
  204. }
  205. if(asr_mode_==2){
  206. LOG(INFO) << wav_id << " Final online results "<<" : "<<online_res;
  207. }
  208. if(asr_mode_==1){
  209. LOG(INFO) << wav_id << " Final online results "<<" : "<<tpass_res;
  210. }
  211. if(asr_mode_==0 || asr_mode_==2){
  212. LOG(INFO) << wav_id << " Final offline results " <<" : "<<tpass_res;
  213. if(time_stamp_res!=""){
  214. LOG(INFO) << wav_id << " Final timestamp results " <<" : "<<time_stamp_res;
  215. }
  216. }
  217. }
  218. LOG(INFO) << "Audio length: " << (double)snippet_time << " s";
  219. LOG(INFO) << "Model inference takes: " << (double)taking_micros / 1000000 <<" s";
  220. LOG(INFO) << "Model inference RTF: " << (double)taking_micros/ (snippet_time*1000000);
  221. FunTpassOnlineUninit(tpass_online_handle);
  222. FunTpassUninit(tpass_handle);
  223. return 0;
  224. }