funasr-onnx-offline-vad.cpp 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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. 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. if (value_arg.isSet()){
  31. model_path.insert({key, value_arg.getValue()});
  32. LOG(INFO)<< key << " : " << value_arg.getValue();
  33. }
  34. }
  35. void print_segs(vector<vector<int>>* vec, string &wav_id) {
  36. string seg_out=wav_id + ": [";
  37. for (int i = 0; i < vec->size(); i++) {
  38. vector<int> inner_vec = (*vec)[i];
  39. seg_out += "[";
  40. for (int j = 0; j < inner_vec.size(); j++) {
  41. seg_out += to_string(inner_vec[j]);
  42. if (j != inner_vec.size() - 1) {
  43. seg_out += ",";
  44. }
  45. }
  46. seg_out += "]";
  47. if (i != vec->size() - 1) {
  48. seg_out += ",";
  49. }
  50. }
  51. seg_out += "]";
  52. LOG(INFO)<<seg_out;
  53. }
  54. int main(int argc, char *argv[])
  55. {
  56. google::InitGoogleLogging(argv[0]);
  57. FLAGS_logtostderr = true;
  58. TCLAP::CmdLine cmd("funasr-onnx-offline-vad", ' ', "1.0");
  59. TCLAP::ValueArg<std::string> model_dir("", MODEL_DIR, "the vad model path, which contains model.onnx, vad.yaml, vad.mvn", true, "", "string");
  60. 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");
  61. 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");
  62. TCLAP::ValueArg<std::int32_t> audio_fs("", AUDIO_FS, "the sample rate of audio", false, 16000, "int32_t");
  63. cmd.add(model_dir);
  64. cmd.add(quantize);
  65. cmd.add(wav_path);
  66. cmd.add(audio_fs);
  67. cmd.parse(argc, argv);
  68. std::map<std::string, std::string> model_path;
  69. GetValue(model_dir, MODEL_DIR, model_path);
  70. GetValue(quantize, QUANTIZE, model_path);
  71. GetValue(wav_path, WAV_PATH, model_path);
  72. struct timeval start, end;
  73. gettimeofday(&start, nullptr);
  74. int thread_num = 1;
  75. FUNASR_HANDLE vad_hanlde=FsmnVadInit(model_path, thread_num);
  76. if (!vad_hanlde)
  77. {
  78. LOG(ERROR) << "FunVad init failed";
  79. exit(-1);
  80. }
  81. gettimeofday(&end, nullptr);
  82. long seconds = (end.tv_sec - start.tv_sec);
  83. long modle_init_micros = ((seconds * 1000000) + end.tv_usec) - (start.tv_usec);
  84. LOG(INFO) << "Model initialization takes " << (double)modle_init_micros / 1000000 << " s";
  85. // read wav_path
  86. vector<string> wav_list;
  87. vector<string> wav_ids;
  88. string default_id = "wav_default_id";
  89. string wav_path_ = model_path.at(WAV_PATH);
  90. if(is_target_file(wav_path_, "wav") || is_target_file(wav_path_, "pcm")){
  91. wav_list.emplace_back(wav_path_);
  92. wav_ids.emplace_back(default_id);
  93. }
  94. else if(is_target_file(wav_path_, "scp")){
  95. ifstream in(wav_path_);
  96. if (!in.is_open()) {
  97. LOG(ERROR) << "Failed to open file: " << model_path.at(WAV_SCP) ;
  98. return 0;
  99. }
  100. string line;
  101. while(getline(in, line))
  102. {
  103. istringstream iss(line);
  104. string column1, column2;
  105. iss >> column1 >> column2;
  106. wav_list.emplace_back(column2);
  107. wav_ids.emplace_back(column1);
  108. }
  109. in.close();
  110. }else{
  111. LOG(ERROR)<<"Please check the wav extension!";
  112. exit(-1);
  113. }
  114. float snippet_time = 0.0f;
  115. long taking_micros = 0;
  116. for (int i = 0; i < wav_list.size(); i++) {
  117. auto& wav_file = wav_list[i];
  118. auto& wav_id = wav_ids[i];
  119. gettimeofday(&start, nullptr);
  120. FUNASR_RESULT result=FsmnVadInfer(vad_hanlde, wav_file.c_str(), nullptr, audio_fs.getValue());
  121. gettimeofday(&end, nullptr);
  122. seconds = (end.tv_sec - start.tv_sec);
  123. taking_micros += ((seconds * 1000000) + end.tv_usec) - (start.tv_usec);
  124. if (result)
  125. {
  126. vector<std::vector<int>>* vad_segments = FsmnVadGetResult(result, 0);
  127. print_segs(vad_segments, wav_id);
  128. snippet_time += FsmnVadGetRetSnippetTime(result);
  129. FsmnVadFreeResult(result);
  130. }
  131. else
  132. {
  133. LOG(ERROR) << ("No return data!\n");
  134. }
  135. }
  136. LOG(INFO) << "Audio length: " << (double)snippet_time << " s";
  137. LOG(INFO) << "Model inference takes: " << (double)taking_micros / 1000000 <<" s";
  138. LOG(INFO) << "Model inference RTF: " << (double)taking_micros/ (snippet_time*1000000);
  139. FsmnVadUninit(vad_hanlde);
  140. return 0;
  141. }