funasr-onnx-offline.cpp 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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. using namespace std;
  19. bool is_target_file(const std::string& filename, const std::string target) {
  20. std::size_t pos = filename.find_last_of(".");
  21. if (pos == std::string::npos) {
  22. return false;
  23. }
  24. std::string extension = filename.substr(pos + 1);
  25. return (extension == target);
  26. }
  27. void GetValue(TCLAP::ValueArg<std::string>& value_arg, string key, std::map<std::string, std::string>& model_path)
  28. {
  29. if (value_arg.isSet()){
  30. model_path.insert({key, value_arg.getValue()});
  31. LOG(INFO)<< key << " : " << value_arg.getValue();
  32. }
  33. }
  34. int main(int argc, char** argv)
  35. {
  36. google::InitGoogleLogging(argv[0]);
  37. FLAGS_logtostderr = true;
  38. TCLAP::CmdLine cmd("funasr-onnx-offline", ' ', "1.0");
  39. TCLAP::ValueArg<std::string> model_dir("", MODEL_DIR, "the asr model path, which contains model.onnx, config.yaml, am.mvn", true, "", "string");
  40. 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");
  41. TCLAP::ValueArg<std::string> vad_dir("", VAD_DIR, "the vad model path, which contains model.onnx, vad.yaml, vad.mvn", false, "", "string");
  42. TCLAP::ValueArg<std::string> vad_quant("", VAD_QUANT, "false (Default), load the model of model.onnx in vad_dir. If set true, load the model of model_quant.onnx in vad_dir", false, "false", "string");
  43. TCLAP::ValueArg<std::string> punc_dir("", PUNC_DIR, "the punc model path, which contains model.onnx, punc.yaml", false, "", "string");
  44. TCLAP::ValueArg<std::string> punc_quant("", PUNC_QUANT, "false (Default), load the model of model.onnx in punc_dir. If set true, load the model of model_quant.onnx in punc_dir", false, "false", "string");
  45. 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");
  46. cmd.add(model_dir);
  47. cmd.add(quantize);
  48. cmd.add(vad_dir);
  49. cmd.add(vad_quant);
  50. cmd.add(punc_dir);
  51. cmd.add(punc_quant);
  52. cmd.add(wav_path);
  53. cmd.parse(argc, argv);
  54. std::map<std::string, std::string> model_path;
  55. GetValue(model_dir, MODEL_DIR, model_path);
  56. GetValue(quantize, QUANTIZE, model_path);
  57. GetValue(vad_dir, VAD_DIR, model_path);
  58. GetValue(vad_quant, VAD_QUANT, model_path);
  59. GetValue(punc_dir, PUNC_DIR, model_path);
  60. GetValue(punc_quant, PUNC_QUANT, model_path);
  61. GetValue(wav_path, WAV_PATH, model_path);
  62. struct timeval start, end;
  63. gettimeofday(&start, NULL);
  64. int thread_num = 1;
  65. FUNASR_HANDLE asr_hanlde=FunOfflineInit(model_path, thread_num);
  66. if (!asr_hanlde)
  67. {
  68. LOG(ERROR) << "FunASR init failed";
  69. exit(-1);
  70. }
  71. gettimeofday(&end, NULL);
  72. long seconds = (end.tv_sec - start.tv_sec);
  73. long modle_init_micros = ((seconds * 1000000) + end.tv_usec) - (start.tv_usec);
  74. LOG(INFO) << "Model initialization takes " << (double)modle_init_micros / 1000000 << " s";
  75. // read wav_path
  76. vector<string> wav_list;
  77. vector<string> wav_ids;
  78. string default_id = "wav_default_id";
  79. string wav_path_ = model_path.at(WAV_PATH);
  80. if(is_target_file(wav_path_, "scp")){
  81. ifstream in(wav_path_);
  82. if (!in.is_open()) {
  83. LOG(ERROR) << "Failed to open file: " << model_path.at(WAV_SCP) ;
  84. return 0;
  85. }
  86. string line;
  87. while(getline(in, line))
  88. {
  89. istringstream iss(line);
  90. string column1, column2;
  91. iss >> column1 >> column2;
  92. wav_list.emplace_back(column2);
  93. wav_ids.emplace_back(column1);
  94. }
  95. in.close();
  96. }else{
  97. wav_list.emplace_back(wav_path_);
  98. wav_ids.emplace_back(default_id);
  99. }
  100. float snippet_time = 0.0f;
  101. long taking_micros = 0;
  102. for (int i = 0; i < wav_list.size(); i++) {
  103. auto& wav_file = wav_list[i];
  104. auto& wav_id = wav_ids[i];
  105. gettimeofday(&start, NULL);
  106. FUNASR_RESULT result=FunOfflineInfer(asr_hanlde, wav_file.c_str(), RASR_NONE, NULL, 16000);
  107. gettimeofday(&end, NULL);
  108. seconds = (end.tv_sec - start.tv_sec);
  109. taking_micros += ((seconds * 1000000) + end.tv_usec) - (start.tv_usec);
  110. if (result)
  111. {
  112. string msg = FunASRGetResult(result, 0);
  113. LOG(INFO)<< wav_id <<" : "<<msg;
  114. snippet_time += FunASRGetRetSnippetTime(result);
  115. FunASRFreeResult(result);
  116. }
  117. else
  118. {
  119. LOG(ERROR) << ("No return data!\n");
  120. }
  121. }
  122. LOG(INFO) << "Audio length: " << (double)snippet_time << " s";
  123. LOG(INFO) << "Model inference takes: " << (double)taking_micros / 1000000 <<" s";
  124. LOG(INFO) << "Model inference RTF: " << (double)taking_micros/ (snippet_time*1000000);
  125. FunOfflineUninit(asr_hanlde);
  126. return 0;
  127. }