funasr-onnx-offline-vad.cpp 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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, "false (Default), load the model of model.onnx in model_dir. If set true, load the model of model_quant.onnx in model_dir", false, "false", "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. cmd.add(model_dir);
  63. cmd.add(quantize);
  64. cmd.add(wav_path);
  65. cmd.parse(argc, argv);
  66. std::map<std::string, std::string> model_path;
  67. GetValue(model_dir, MODEL_DIR, model_path);
  68. GetValue(quantize, QUANTIZE, model_path);
  69. GetValue(wav_path, WAV_PATH, model_path);
  70. struct timeval start, end;
  71. gettimeofday(&start, NULL);
  72. int thread_num = 1;
  73. FUNASR_HANDLE vad_hanlde=FsmnVadInit(model_path, thread_num);
  74. if (!vad_hanlde)
  75. {
  76. LOG(ERROR) << "FunVad init failed";
  77. exit(-1);
  78. }
  79. gettimeofday(&end, NULL);
  80. long seconds = (end.tv_sec - start.tv_sec);
  81. long modle_init_micros = ((seconds * 1000000) + end.tv_usec) - (start.tv_usec);
  82. LOG(INFO) << "Model initialization takes " << (double)modle_init_micros / 1000000 << " s";
  83. // read wav_path
  84. vector<string> wav_list;
  85. vector<string> wav_ids;
  86. string default_id = "wav_default_id";
  87. string wav_path_ = model_path.at(WAV_PATH);
  88. if(is_target_file(wav_path_, "wav") || is_target_file(wav_path_, "pcm")){
  89. wav_list.emplace_back(wav_path_);
  90. wav_ids.emplace_back(default_id);
  91. }
  92. else if(is_target_file(wav_path_, "scp")){
  93. ifstream in(wav_path_);
  94. if (!in.is_open()) {
  95. LOG(ERROR) << "Failed to open file: " << model_path.at(WAV_SCP) ;
  96. return 0;
  97. }
  98. string line;
  99. while(getline(in, line))
  100. {
  101. istringstream iss(line);
  102. string column1, column2;
  103. iss >> column1 >> column2;
  104. wav_list.emplace_back(column2);
  105. wav_ids.emplace_back(column1);
  106. }
  107. in.close();
  108. }else{
  109. LOG(ERROR)<<"Please check the wav extension!";
  110. exit(-1);
  111. }
  112. float snippet_time = 0.0f;
  113. long taking_micros = 0;
  114. for (int i = 0; i < wav_list.size(); i++) {
  115. auto& wav_file = wav_list[i];
  116. auto& wav_id = wav_ids[i];
  117. gettimeofday(&start, NULL);
  118. FUNASR_RESULT result=FsmnVadInfer(vad_hanlde, wav_file.c_str(), NULL, 16000);
  119. gettimeofday(&end, NULL);
  120. seconds = (end.tv_sec - start.tv_sec);
  121. taking_micros += ((seconds * 1000000) + end.tv_usec) - (start.tv_usec);
  122. if (result)
  123. {
  124. vector<std::vector<int>>* vad_segments = FsmnVadGetResult(result, 0);
  125. print_segs(vad_segments, wav_id);
  126. snippet_time += FsmnVadGetRetSnippetTime(result);
  127. FsmnVadFreeResult(result);
  128. }
  129. else
  130. {
  131. LOG(ERROR) << ("No return data!\n");
  132. }
  133. }
  134. LOG(INFO) << "Audio length: " << (double)snippet_time << " s";
  135. LOG(INFO) << "Model inference takes: " << (double)taking_micros / 1000000 <<" s";
  136. LOG(INFO) << "Model inference RTF: " << (double)taking_micros/ (snippet_time*1000000);
  137. FsmnVadUninit(vad_hanlde);
  138. return 0;
  139. }