funasr-onnx-online-asr.cpp 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  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 <vector>
  15. #include <glog/logging.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. 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-vad", ' ', "1.0");
  41. 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");
  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> 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");
  44. cmd.add(model_dir);
  45. cmd.add(quantize);
  46. cmd.add(wav_path);
  47. cmd.parse(argc, argv);
  48. std::map<std::string, std::string> model_path;
  49. GetValue(model_dir, MODEL_DIR, model_path);
  50. GetValue(quantize, QUANTIZE, model_path);
  51. GetValue(wav_path, WAV_PATH, model_path);
  52. struct timeval start, end;
  53. gettimeofday(&start, NULL);
  54. int thread_num = 1;
  55. FUNASR_HANDLE asr_handle=FunASRInit(model_path, thread_num, ASR_ONLINE);
  56. if (!asr_handle)
  57. {
  58. LOG(ERROR) << "FunVad init failed";
  59. exit(-1);
  60. }
  61. gettimeofday(&end, NULL);
  62. long seconds = (end.tv_sec - start.tv_sec);
  63. long modle_init_micros = ((seconds * 1000000) + end.tv_usec) - (start.tv_usec);
  64. LOG(INFO) << "Model initialization takes " << (double)modle_init_micros / 1000000 << " s";
  65. // read wav_path
  66. vector<string> wav_list;
  67. vector<string> wav_ids;
  68. string default_id = "wav_default_id";
  69. string wav_path_ = model_path.at(WAV_PATH);
  70. if(is_target_file(wav_path_, "scp")){
  71. ifstream in(wav_path_);
  72. if (!in.is_open()) {
  73. LOG(ERROR) << "Failed to open file: " << model_path.at(WAV_SCP) ;
  74. return 0;
  75. }
  76. string line;
  77. while(getline(in, line))
  78. {
  79. istringstream iss(line);
  80. string column1, column2;
  81. iss >> column1 >> column2;
  82. wav_list.emplace_back(column2);
  83. wav_ids.emplace_back(column1);
  84. }
  85. in.close();
  86. }else{
  87. wav_list.emplace_back(wav_path_);
  88. wav_ids.emplace_back(default_id);
  89. }
  90. // init online features
  91. FUNASR_HANDLE online_handle=FunASROnlineInit(asr_handle);
  92. float snippet_time = 0.0f;
  93. long taking_micros = 0;
  94. for (int i = 0; i < wav_list.size(); i++) {
  95. auto& wav_file = wav_list[i];
  96. auto& wav_id = wav_ids[i];
  97. int32_t sampling_rate_ = -1;
  98. funasr::Audio audio(1);
  99. if(is_target_file(wav_file.c_str(), "wav")){
  100. if(!audio.LoadWav2Char(wav_file.c_str(), &sampling_rate_)){
  101. LOG(ERROR)<<"Failed to load "<< wav_file;
  102. exit(-1);
  103. }
  104. }else if(is_target_file(wav_file.c_str(), "pcm")){
  105. if (!audio.LoadPcmwav2Char(wav_file.c_str(), &sampling_rate_)){
  106. LOG(ERROR)<<"Failed to load "<< wav_file;
  107. exit(-1);
  108. }
  109. }else{
  110. if (!audio.FfmpegLoad(wav_file.c_str(), true)){
  111. LOG(ERROR)<<"Failed to load "<< wav_file;
  112. exit(-1);
  113. }
  114. }
  115. char* speech_buff = audio.GetSpeechChar();
  116. int buff_len = audio.GetSpeechLen()*2;
  117. int step = 9600*2;
  118. bool is_final = false;
  119. string final_res="";
  120. for (int sample_offset = 0; sample_offset < buff_len; sample_offset += std::min(step, buff_len - sample_offset)) {
  121. if (sample_offset + step >= buff_len - 1) {
  122. step = buff_len - sample_offset;
  123. is_final = true;
  124. } else {
  125. is_final = false;
  126. }
  127. gettimeofday(&start, NULL);
  128. FUNASR_RESULT result = FunASRInferBuffer(online_handle, speech_buff+sample_offset, step, RASR_NONE, NULL, is_final, 16000);
  129. gettimeofday(&end, NULL);
  130. seconds = (end.tv_sec - start.tv_sec);
  131. taking_micros += ((seconds * 1000000) + end.tv_usec) - (start.tv_usec);
  132. if (result)
  133. {
  134. string msg = FunASRGetResult(result, 0);
  135. final_res += msg;
  136. LOG(INFO)<< wav_id <<" : "<<msg;
  137. snippet_time += FunASRGetRetSnippetTime(result);
  138. FunASRFreeResult(result);
  139. }
  140. else
  141. {
  142. LOG(ERROR) << ("No return data!\n");
  143. }
  144. }
  145. LOG(INFO)<<"Final results " << wav_id <<" : "<<final_res;
  146. }
  147. LOG(INFO) << "Audio length: " << (double)snippet_time << " s";
  148. LOG(INFO) << "Model inference takes: " << (double)taking_micros / 1000000 <<" s";
  149. LOG(INFO) << "Model inference RTF: " << (double)taking_micros/ (snippet_time*1000000);
  150. FunASRUninit(asr_handle);
  151. FunASRUninit(online_handle);
  152. return 0;
  153. }