diff --git a/sherpa-onnx/csrc/offline-stream.cc b/sherpa-onnx/csrc/offline-stream.cc index 67781cf3..9beb8669 100644 --- a/sherpa-onnx/csrc/offline-stream.cc +++ b/sherpa-onnx/csrc/offline-stream.cc @@ -8,7 +8,7 @@ #include #include - +#include "nlohmann/json.hpp" #include "kaldi-native-fbank/csrc/online-feature.h" #include "sherpa-onnx/csrc/macros.h" #include "sherpa-onnx/csrc/offline-recognizer.h" @@ -214,5 +214,12 @@ void OfflineStream::SetResult(const OfflineRecognitionResult &r) { const OfflineRecognitionResult &OfflineStream::GetResult() const { return impl_->GetResult(); } +std::string OfflineRecognitionResult::AsJsonString() const { + nlohmann::json j; + j["text"] = text; + j["tokens"] = tokens; + j["timestamps"] = timestamps; + return j.dump(); +} } // namespace sherpa_onnx diff --git a/sherpa-onnx/csrc/offline-stream.h b/sherpa-onnx/csrc/offline-stream.h index 99fbd43c..b1bed47e 100644 --- a/sherpa-onnx/csrc/offline-stream.h +++ b/sherpa-onnx/csrc/offline-stream.h @@ -27,6 +27,8 @@ struct OfflineRecognitionResult { /// timestamps.size() == tokens.size() /// timestamps[i] records the time in seconds when tokens[i] is decoded. std::vector timestamps; + + std::string AsJsonString() const; }; struct OfflineFeatureExtractorConfig { diff --git a/sherpa-onnx/csrc/offline-websocket-server-impl.cc b/sherpa-onnx/csrc/offline-websocket-server-impl.cc index 88130316..d3f9310a 100644 --- a/sherpa-onnx/csrc/offline-websocket-server-impl.cc +++ b/sherpa-onnx/csrc/offline-websocket-server-impl.cc @@ -100,10 +100,11 @@ void OfflineWebsocketDecoder::Decode() { for (int32_t i = 0; i != size; ++i) { connection_hdl hdl = handles[i]; asio::post(server_->GetConnectionContext(), - [this, hdl, text = ss[i]->GetResult().text]() { + [this, hdl, result = ss[i]->GetResult()]() { websocketpp::lib::error_code ec; server_->GetServer().send( - hdl, text, websocketpp::frame::opcode::text, ec); + hdl, result.AsJsonString(), + websocketpp::frame::opcode::text, ec); if (ec) { server_->GetServer().get_alog().write( websocketpp::log::alevel::app, ec.message()); diff --git a/sherpa-onnx/csrc/sherpa-onnx-offline.cc b/sherpa-onnx/csrc/sherpa-onnx-offline.cc index b6d8916f..662a1bc6 100644 --- a/sherpa-onnx/csrc/sherpa-onnx-offline.cc +++ b/sherpa-onnx/csrc/sherpa-onnx-offline.cc @@ -101,7 +101,7 @@ for a list of pre-trained models to download. fprintf(stderr, "Done!\n\n"); for (int32_t i = 1; i <= po.NumArgs(); ++i) { fprintf(stderr, "%s\n%s\n----\n", po.GetArg(i).c_str(), - ss[i - 1]->GetResult().text.c_str()); + ss[i - 1]->GetResult().AsJsonString().c_str()); } float elapsed_seconds =