Return timestamp info and tokens in offline ASR
Co-authored-by: zhangbaofeng@npnets.com <41259@Zbf>
This commit is contained in:
@@ -8,7 +8,7 @@
|
||||
|
||||
#include <algorithm>
|
||||
#include <cmath>
|
||||
|
||||
#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
|
||||
|
||||
@@ -27,6 +27,8 @@ struct OfflineRecognitionResult {
|
||||
/// timestamps.size() == tokens.size()
|
||||
/// timestamps[i] records the time in seconds when tokens[i] is decoded.
|
||||
std::vector<float> timestamps;
|
||||
|
||||
std::string AsJsonString() const;
|
||||
};
|
||||
|
||||
struct OfflineFeatureExtractorConfig {
|
||||
|
||||
@@ -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());
|
||||
|
||||
@@ -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 =
|
||||
|
||||
Reference in New Issue
Block a user