cooldoomsday
Committed by GitHub

Return timestamp info and tokens in offline ASR

Co-authored-by: zhangbaofeng@npnets.com <41259@Zbf>
@@ -8,7 +8,7 @@ @@ -8,7 +8,7 @@
8 8
9 #include <algorithm> 9 #include <algorithm>
10 #include <cmath> 10 #include <cmath>
11 - 11 +#include "nlohmann/json.hpp"
12 #include "kaldi-native-fbank/csrc/online-feature.h" 12 #include "kaldi-native-fbank/csrc/online-feature.h"
13 #include "sherpa-onnx/csrc/macros.h" 13 #include "sherpa-onnx/csrc/macros.h"
14 #include "sherpa-onnx/csrc/offline-recognizer.h" 14 #include "sherpa-onnx/csrc/offline-recognizer.h"
@@ -214,5 +214,12 @@ void OfflineStream::SetResult(const OfflineRecognitionResult &r) { @@ -214,5 +214,12 @@ void OfflineStream::SetResult(const OfflineRecognitionResult &r) {
214 const OfflineRecognitionResult &OfflineStream::GetResult() const { 214 const OfflineRecognitionResult &OfflineStream::GetResult() const {
215 return impl_->GetResult(); 215 return impl_->GetResult();
216 } 216 }
  217 +std::string OfflineRecognitionResult::AsJsonString() const {
  218 + nlohmann::json j;
  219 + j["text"] = text;
  220 + j["tokens"] = tokens;
  221 + j["timestamps"] = timestamps;
217 222
  223 + return j.dump();
  224 +}
218 } // namespace sherpa_onnx 225 } // namespace sherpa_onnx
@@ -27,6 +27,8 @@ struct OfflineRecognitionResult { @@ -27,6 +27,8 @@ struct OfflineRecognitionResult {
27 /// timestamps.size() == tokens.size() 27 /// timestamps.size() == tokens.size()
28 /// timestamps[i] records the time in seconds when tokens[i] is decoded. 28 /// timestamps[i] records the time in seconds when tokens[i] is decoded.
29 std::vector<float> timestamps; 29 std::vector<float> timestamps;
  30 +
  31 + std::string AsJsonString() const;
30 }; 32 };
31 33
32 struct OfflineFeatureExtractorConfig { 34 struct OfflineFeatureExtractorConfig {
@@ -100,10 +100,11 @@ void OfflineWebsocketDecoder::Decode() { @@ -100,10 +100,11 @@ void OfflineWebsocketDecoder::Decode() {
100 for (int32_t i = 0; i != size; ++i) { 100 for (int32_t i = 0; i != size; ++i) {
101 connection_hdl hdl = handles[i]; 101 connection_hdl hdl = handles[i];
102 asio::post(server_->GetConnectionContext(), 102 asio::post(server_->GetConnectionContext(),
103 - [this, hdl, text = ss[i]->GetResult().text]() { 103 + [this, hdl, result = ss[i]->GetResult()]() {
104 websocketpp::lib::error_code ec; 104 websocketpp::lib::error_code ec;
105 server_->GetServer().send( 105 server_->GetServer().send(
106 - hdl, text, websocketpp::frame::opcode::text, ec); 106 + hdl, result.AsJsonString(),
  107 + websocketpp::frame::opcode::text, ec);
107 if (ec) { 108 if (ec) {
108 server_->GetServer().get_alog().write( 109 server_->GetServer().get_alog().write(
109 websocketpp::log::alevel::app, ec.message()); 110 websocketpp::log::alevel::app, ec.message());
@@ -101,7 +101,7 @@ for a list of pre-trained models to download. @@ -101,7 +101,7 @@ for a list of pre-trained models to download.
101 fprintf(stderr, "Done!\n\n"); 101 fprintf(stderr, "Done!\n\n");
102 for (int32_t i = 1; i <= po.NumArgs(); ++i) { 102 for (int32_t i = 1; i <= po.NumArgs(); ++i) {
103 fprintf(stderr, "%s\n%s\n----\n", po.GetArg(i).c_str(), 103 fprintf(stderr, "%s\n%s\n----\n", po.GetArg(i).c_str(),
104 - ss[i - 1]->GetResult().text.c_str()); 104 + ss[i - 1]->GetResult().AsJsonString().c_str());
105 } 105 }
106 106
107 float elapsed_seconds = 107 float elapsed_seconds =