funasr-onnx-offline.cpp 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  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 <unordered_map>
  19. #include "util.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. if (value_arg.isSet()){
  32. model_path.insert({key, value_arg.getValue()});
  33. LOG(INFO)<< key << " : " << value_arg.getValue();
  34. }
  35. }
  36. int main(int argc, char** argv)
  37. {
  38. google::InitGoogleLogging(argv[0]);
  39. FLAGS_logtostderr = true;
  40. TCLAP::CmdLine cmd("funasr-onnx-offline", ' ', "1.0");
  41. TCLAP::ValueArg<std::string> model_dir("", MODEL_DIR, "the asr model path, which contains model.onnx, config.yaml, am.mvn", true, "", "string");
  42. 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");
  43. TCLAP::ValueArg<std::string> vad_dir("", VAD_DIR, "the vad model path, which contains model.onnx, vad.yaml, vad.mvn", false, "", "string");
  44. 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");
  45. TCLAP::ValueArg<std::string> punc_dir("", PUNC_DIR, "the punc model path, which contains model.onnx, punc.yaml", false, "", "string");
  46. 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");
  47. TCLAP::ValueArg<std::string> lm_dir("", LM_DIR, "the lm model path, which contains compiled models: TLG.fst, config.yaml ", false, "", "string");
  48. TCLAP::ValueArg<float> global_beam("", GLOB_BEAM, "the decoding beam for beam searching ", false, 3.0, "float");
  49. TCLAP::ValueArg<float> lattice_beam("", LAT_BEAM, "the lattice generation beam for beam searching ", false, 3.0, "float");
  50. TCLAP::ValueArg<float> am_scale("", AM_SCALE, "the acoustic scale for beam searching ", false, 10.0, "float");
  51. TCLAP::ValueArg<std::int32_t> fst_inc_wts("", FST_INC_WTS, "the fst hotwords incremental bias", false, 20, "int32_t");
  52. 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");
  53. 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");
  54. TCLAP::ValueArg<std::string> hotword("", HOTWORD, "the hotword file, one hotword perline, Format: Hotword Weight (could be: 阿里巴巴 20)", false, "", "string");
  55. cmd.add(model_dir);
  56. cmd.add(quantize);
  57. cmd.add(vad_dir);
  58. cmd.add(vad_quant);
  59. cmd.add(punc_dir);
  60. cmd.add(punc_quant);
  61. cmd.add(itn_dir);
  62. cmd.add(lm_dir);
  63. cmd.add(global_beam);
  64. cmd.add(lattice_beam);
  65. cmd.add(am_scale);
  66. cmd.add(fst_inc_wts);
  67. cmd.add(wav_path);
  68. cmd.add(hotword);
  69. cmd.parse(argc, argv);
  70. std::map<std::string, std::string> model_path;
  71. GetValue(model_dir, MODEL_DIR, model_path);
  72. GetValue(quantize, QUANTIZE, model_path);
  73. GetValue(vad_dir, VAD_DIR, model_path);
  74. GetValue(vad_quant, VAD_QUANT, model_path);
  75. GetValue(punc_dir, PUNC_DIR, model_path);
  76. GetValue(punc_quant, PUNC_QUANT, model_path);
  77. GetValue(itn_dir, ITN_DIR, model_path);
  78. GetValue(lm_dir, LM_DIR, model_path);
  79. GetValue(wav_path, WAV_PATH, model_path);
  80. struct timeval start, end;
  81. gettimeofday(&start, NULL);
  82. int thread_num = 1;
  83. FUNASR_HANDLE asr_hanlde=FunOfflineInit(model_path, thread_num);
  84. if (!asr_hanlde)
  85. {
  86. LOG(ERROR) << "FunASR init failed";
  87. exit(-1);
  88. }
  89. float glob_beam = 3.0f;
  90. float lat_beam = 3.0f;
  91. float am_sc = 10.0f;
  92. if (lm_dir.isSet()) {
  93. glob_beam = global_beam.getValue();
  94. lat_beam = lattice_beam.getValue();
  95. am_sc = am_scale.getValue();
  96. }
  97. // init wfst decoder
  98. FUNASR_DEC_HANDLE decoder_handle = FunASRWfstDecoderInit(asr_hanlde, ASR_OFFLINE, glob_beam, lat_beam, am_sc);
  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. gettimeofday(&end, NULL);
  106. long seconds = (end.tv_sec - start.tv_sec);
  107. long modle_init_micros = ((seconds * 1000000) + end.tv_usec) - (start.tv_usec);
  108. LOG(INFO) << "Model initialization takes " << (double)modle_init_micros / 1000000 << " s";
  109. // read wav_path
  110. vector<string> wav_list;
  111. vector<string> wav_ids;
  112. string default_id = "wav_default_id";
  113. string wav_path_ = model_path.at(WAV_PATH);
  114. if(is_target_file(wav_path_, "scp")){
  115. ifstream in(wav_path_);
  116. if (!in.is_open()) {
  117. LOG(ERROR) << "Failed to open file: " << model_path.at(WAV_SCP) ;
  118. return 0;
  119. }
  120. string line;
  121. while(getline(in, line))
  122. {
  123. istringstream iss(line);
  124. string column1, column2;
  125. iss >> column1 >> column2;
  126. wav_list.emplace_back(column2);
  127. wav_ids.emplace_back(column1);
  128. }
  129. in.close();
  130. }else{
  131. wav_list.emplace_back(wav_path_);
  132. wav_ids.emplace_back(default_id);
  133. }
  134. float snippet_time = 0.0f;
  135. long taking_micros = 0;
  136. // load hotwords list and build graph
  137. FunWfstDecoderLoadHwsRes(decoder_handle, fst_inc_wts.getValue(), hws_map);
  138. std::vector<std::vector<float>> hotwords_embedding = CompileHotwordEmbedding(asr_hanlde, nn_hotwords_);
  139. for (int i = 0; i < wav_list.size(); i++) {
  140. auto& wav_file = wav_list[i];
  141. auto& wav_id = wav_ids[i];
  142. gettimeofday(&start, NULL);
  143. FUNASR_RESULT result=FunOfflineInfer(asr_hanlde, wav_file.c_str(), RASR_NONE, NULL, hotwords_embedding, 16000, false, decoder_handle);
  144. gettimeofday(&end, NULL);
  145. seconds = (end.tv_sec - start.tv_sec);
  146. taking_micros += ((seconds * 1000000) + end.tv_usec) - (start.tv_usec);
  147. if (result)
  148. {
  149. string msg = FunASRGetResult(result, 0);
  150. LOG(INFO)<< wav_id <<" : "<<msg;
  151. string stamp = FunASRGetStamp(result);
  152. if(stamp !=""){
  153. LOG(INFO)<< wav_id <<" : "<<stamp;
  154. }
  155. snippet_time += FunASRGetRetSnippetTime(result);
  156. FunASRFreeResult(result);
  157. }
  158. else
  159. {
  160. LOG(ERROR) << ("No return data!\n");
  161. }
  162. }
  163. FunWfstDecoderUnloadHwsRes(decoder_handle);
  164. LOG(INFO) << "Audio length: " << (double)snippet_time << " s";
  165. LOG(INFO) << "Model inference takes: " << (double)taking_micros / 1000000 <<" s";
  166. LOG(INFO) << "Model inference RTF: " << (double)taking_micros/ (snippet_time*1000000);
  167. FunASRWfstDecoderUninit(decoder_handle);
  168. FunOfflineUninit(asr_hanlde);
  169. return 0;
  170. }