funasr-onnx-2pass.cpp 8.4 KB

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