tester.cpp 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. #ifndef _WIN32
  2. #include <sys/time.h>
  3. #else
  4. #include <win_func.h>
  5. #endif
  6. #include "librapidasrapi.h"
  7. #include <iostream>
  8. #include <fstream>
  9. #include <sstream>
  10. using namespace std;
  11. int main(int argc, char *argv[])
  12. {
  13. if (argc < 4)
  14. {
  15. printf("Usage: %s /path/to/model_dir /path/to/wav/file quantize(true or false) \n", argv[0]);
  16. exit(-1);
  17. }
  18. struct timeval start, end;
  19. gettimeofday(&start, NULL);
  20. int nThreadNum = 4;
  21. // is quantize
  22. bool quantize = false;
  23. istringstream(argv[3]) >> boolalpha >> quantize;
  24. RPASR_HANDLE AsrHanlde=RapidAsrInit(argv[1], nThreadNum, quantize);
  25. if (!AsrHanlde)
  26. {
  27. printf("Cannot load ASR Model from: %s, there must be files model.onnx and vocab.txt", argv[1]);
  28. exit(-1);
  29. }
  30. gettimeofday(&end, NULL);
  31. long seconds = (end.tv_sec - start.tv_sec);
  32. long modle_init_micros = ((seconds * 1000000) + end.tv_usec) - (start.tv_usec);
  33. printf("Model initialization takes %lfs.\n", (double)modle_init_micros / 1000000);
  34. gettimeofday(&start, NULL);
  35. float snippet_time = 0.0f;
  36. RPASR_RESULT Result=RapidAsrRecogFile(AsrHanlde, argv[2], RASR_NONE, NULL);
  37. gettimeofday(&end, NULL);
  38. if (Result)
  39. {
  40. string msg = RapidAsrGetResult(Result, 0);
  41. setbuf(stdout, NULL);
  42. printf("Result: %s \n", msg.c_str());
  43. snippet_time = RapidAsrGetRetSnippetTime(Result);
  44. RapidAsrFreeResult(Result);
  45. }
  46. else
  47. {
  48. cout <<"no return data!";
  49. }
  50. printf("Audio length %lfs.\n", (double)snippet_time);
  51. seconds = (end.tv_sec - start.tv_sec);
  52. long taking_micros = ((seconds * 1000000) + end.tv_usec) - (start.tv_usec);
  53. printf("Model inference takes %lfs.\n", (double)taking_micros / 1000000);
  54. printf("Model inference RTF: %04lf.\n", (double)taking_micros/ (snippet_time*1000000));
  55. RapidAsrUninit(AsrHanlde);
  56. return 0;
  57. }