Add Android demo for real-time ASR with non-streaming ASR models. (#2214)
This commit is contained in:
@@ -31,6 +31,7 @@
|
||||
#include "sherpa-onnx/csrc/speaker-embedding-extractor.h"
|
||||
#include "sherpa-onnx/csrc/speaker-embedding-manager.h"
|
||||
#include "sherpa-onnx/csrc/spoken-language-identification.h"
|
||||
#include "sherpa-onnx/csrc/text-utils.h"
|
||||
#include "sherpa-onnx/csrc/voice-activity-detector.h"
|
||||
#include "sherpa-onnx/csrc/wave-reader.h"
|
||||
#include "sherpa-onnx/csrc/wave-writer.h"
|
||||
@@ -158,11 +159,14 @@ static sherpa_onnx::OnlineRecognizerConfig GetOnlineRecognizerConfig(
|
||||
recognizer_config.hr.rule_fsts = SHERPA_ONNX_OR(config->hr.rule_fsts, "");
|
||||
|
||||
if (config->model_config.debug) {
|
||||
auto str_vec = sherpa_onnx::SplitString(recognizer_config.ToString(), 128);
|
||||
for (const auto &s : str_vec) {
|
||||
#if __OHOS__
|
||||
SHERPA_ONNX_LOGE("%{public}s\n", recognizer_config.ToString().c_str());
|
||||
SHERPA_ONNX_LOGE("%{public}s\n", s.c_str());
|
||||
#else
|
||||
SHERPA_ONNX_LOGE("%s\n", recognizer_config.ToString().c_str());
|
||||
SHERPA_ONNX_LOGE("%s\n", s.c_str());
|
||||
#endif
|
||||
}
|
||||
}
|
||||
|
||||
return recognizer_config;
|
||||
@@ -503,11 +507,14 @@ static sherpa_onnx::OfflineRecognizerConfig GetOfflineRecognizerConfig(
|
||||
recognizer_config.hr.rule_fsts = SHERPA_ONNX_OR(config->hr.rule_fsts, "");
|
||||
|
||||
if (config->model_config.debug) {
|
||||
auto str_vec = sherpa_onnx::SplitString(recognizer_config.ToString(), 128);
|
||||
for (const auto &s : str_vec) {
|
||||
#if __OHOS__
|
||||
SHERPA_ONNX_LOGE("%{public}s\n", recognizer_config.ToString().c_str());
|
||||
SHERPA_ONNX_LOGE("%{public}s\n", s.c_str());
|
||||
#else
|
||||
SHERPA_ONNX_LOGE("%s\n", recognizer_config.ToString().c_str());
|
||||
SHERPA_ONNX_LOGE("%s\n", s.c_str());
|
||||
#endif
|
||||
}
|
||||
}
|
||||
|
||||
return recognizer_config;
|
||||
|
||||
@@ -707,4 +707,20 @@ bool EndsWith(const std::string &haystack, const std::string &needle) {
|
||||
return std::equal(needle.rbegin(), needle.rend(), haystack.rbegin());
|
||||
}
|
||||
|
||||
std::vector<std::string> SplitString(const std::string &s, int32_t chunk_size) {
|
||||
std::vector<std::string> ans;
|
||||
if (chunk_size < 1 || chunk_size > s.size()) {
|
||||
ans.push_back(s);
|
||||
} else {
|
||||
int32_t n = static_cast<int32_t>(s.size());
|
||||
int32_t i = 0;
|
||||
while (i < n) {
|
||||
int32_t end = std::min(i + chunk_size, n);
|
||||
ans.push_back(s.substr(i, end - i));
|
||||
i = end;
|
||||
}
|
||||
}
|
||||
return ans;
|
||||
}
|
||||
|
||||
} // namespace sherpa_onnx
|
||||
|
||||
@@ -147,6 +147,8 @@ std::string ToString(const std::wstring &s);
|
||||
|
||||
bool EndsWith(const std::string &haystack, const std::string &needle);
|
||||
|
||||
std::vector<std::string> SplitString(const std::string &s, int32_t chunk_size);
|
||||
|
||||
} // namespace sherpa_onnx
|
||||
|
||||
#endif // SHERPA_ONNX_CSRC_TEXT_UTILS_H_
|
||||
|
||||
@@ -5,6 +5,7 @@
|
||||
#include "sherpa-onnx/csrc/offline-recognizer.h"
|
||||
|
||||
#include "sherpa-onnx/csrc/macros.h"
|
||||
#include "sherpa-onnx/csrc/text-utils.h"
|
||||
#include "sherpa-onnx/jni/common.h"
|
||||
|
||||
namespace sherpa_onnx {
|
||||
@@ -327,7 +328,12 @@ Java_com_k2fsa_sherpa_onnx_OfflineRecognizer_newFromAsset(JNIEnv *env,
|
||||
}
|
||||
#endif
|
||||
auto config = sherpa_onnx::GetOfflineConfig(env, _config);
|
||||
SHERPA_ONNX_LOGE("config:\n%s", config.ToString().c_str());
|
||||
|
||||
// logcat truncates long strings, so we split the string into chunks
|
||||
auto str_vec = sherpa_onnx::SplitString(config.ToString(), 128);
|
||||
for (const auto &s : str_vec) {
|
||||
SHERPA_ONNX_LOGE("%s", s.c_str());
|
||||
}
|
||||
|
||||
auto model = new sherpa_onnx::OfflineRecognizer(
|
||||
#if __ANDROID_API__ >= 9
|
||||
@@ -344,7 +350,11 @@ Java_com_k2fsa_sherpa_onnx_OfflineRecognizer_newFromFile(JNIEnv *env,
|
||||
jobject /*obj*/,
|
||||
jobject _config) {
|
||||
auto config = sherpa_onnx::GetOfflineConfig(env, _config);
|
||||
SHERPA_ONNX_LOGE("config:\n%s", config.ToString().c_str());
|
||||
|
||||
auto str_vec = sherpa_onnx::SplitString(config.ToString(), 128);
|
||||
for (const auto &s : str_vec) {
|
||||
SHERPA_ONNX_LOGE("%s", s.c_str());
|
||||
}
|
||||
|
||||
if (!config.Validate()) {
|
||||
SHERPA_ONNX_LOGE("Errors found in config!");
|
||||
|
||||
@@ -5,6 +5,7 @@
|
||||
#include "sherpa-onnx/csrc/online-recognizer.h"
|
||||
|
||||
#include "sherpa-onnx/csrc/macros.h"
|
||||
#include "sherpa-onnx/csrc/text-utils.h"
|
||||
#include "sherpa-onnx/jni/common.h"
|
||||
|
||||
namespace sherpa_onnx {
|
||||
@@ -295,7 +296,10 @@ Java_com_k2fsa_sherpa_onnx_OnlineRecognizer_newFromAsset(JNIEnv *env,
|
||||
}
|
||||
#endif
|
||||
auto config = sherpa_onnx::GetConfig(env, _config);
|
||||
SHERPA_ONNX_LOGE("config:\n%s", config.ToString().c_str());
|
||||
auto str_vec = sherpa_onnx::SplitString(config.ToString(), 128);
|
||||
for (const auto &s : str_vec) {
|
||||
SHERPA_ONNX_LOGE("%s", s.c_str());
|
||||
}
|
||||
|
||||
auto recognizer = new sherpa_onnx::OnlineRecognizer(
|
||||
#if __ANDROID_API__ >= 9
|
||||
@@ -310,7 +314,11 @@ SHERPA_ONNX_EXTERN_C
|
||||
JNIEXPORT jlong JNICALL Java_com_k2fsa_sherpa_onnx_OnlineRecognizer_newFromFile(
|
||||
JNIEnv *env, jobject /*obj*/, jobject _config) {
|
||||
auto config = sherpa_onnx::GetConfig(env, _config);
|
||||
SHERPA_ONNX_LOGE("config:\n%s", config.ToString().c_str());
|
||||
|
||||
auto str_vec = sherpa_onnx::SplitString(config.ToString(), 128);
|
||||
for (const auto &s : str_vec) {
|
||||
SHERPA_ONNX_LOGE("%s", s.c_str());
|
||||
}
|
||||
|
||||
if (!config.Validate()) {
|
||||
SHERPA_ONNX_LOGE("Errors found in config!");
|
||||
|
||||
Reference in New Issue
Block a user