+
选择asr模型模式:
+
-
+
+
+
+
+
+
+
+
+
+
+
+
语音识别结果显示:
diff --git a/funasr/runtime/html5/static/main.js b/funasr/runtime/html5/static/main.js
index 22f53c152..35e533a43 100644
--- a/funasr/runtime/html5/static/main.js
+++ b/funasr/runtime/html5/static/main.js
@@ -32,15 +32,121 @@ btnStart.disabled = true;
btnConnect= document.getElementById('btnConnect');
btnConnect.onclick = start;
-var rec_text="";
-var offline_text="";
+var rec_text=""; // for online rec asr result
+var offline_text=""; // for offline rec asr result
var info_div = document.getElementById('info_div');
-//var now_ipaddress=window.location.href;
-//now_ipaddress=now_ipaddress.replace("https://","wss://");
-//now_ipaddress=now_ipaddress.replace("static/index.html","");
-//document.getElementById('wssip').value=now_ipaddress;
+var upfile = document.getElementById('upfile');
+
+
+var isfilemode=false; // if it is in file mode
+var file_data_array; // array to save file data
+var isconnected=0; // for file rec, 0 is not begin, 1 is connected, -1 is error
+var totalsend=0;
+
+upfile.onchange = function () {
+ var len = this.files.length;
+ for(let i = 0; i < len; i++) {
+ let fileAudio = new FileReader();
+ fileAudio.readAsArrayBuffer(this.files[i]);
+ fileAudio.onload = function() {
+ var audioblob= fileAudio.result;
+ file_data_array=audioblob;
+ console.log(audioblob);
+ btnConnect.disabled = false;
+ info_div.innerHTML='请点击连接进行识别';
+
+ }
+ fileAudio.onerror = function(e) {
+ console.log('error' + e);
+ }
+ }
+ }
+
+function play_file()
+{
+ var audioblob=new Blob( [ new Uint8Array(file_data_array)] , {type :"audio/wav"});
+ var audio_record = document.getElementById('audio_record');
+ audio_record.src = (window.URL||webkitURL).createObjectURL(audioblob);
+ audio_record.controls=true;
+ audio_record.play();
+}
+function start_file_send()
+{
+ sampleBuf=new Int16Array( file_data_array );
+
+ var chunk_size=960; // for asr chunk_size [5, 10, 5]
+
+
+
+
+
+ while(sampleBuf.length>=chunk_size){
+
+ sendBuf=sampleBuf.slice(0,chunk_size);
+ totalsend=totalsend+sampleBuf.length;
+ sampleBuf=sampleBuf.slice(chunk_size,sampleBuf.length);
+ wsconnecter.wsSend(sendBuf,false);
+
+
+ }
+
+ stop();
+
+
+
+}
+function start_file_offline()
+{
+ console.log("start_file_offline",isconnected);
+ if(isconnected==-1)
+ {
+ return;
+ }
+ if(isconnected==0){
+
+ setTimeout(start_file_offline, 1000);
+ return;
+ }
+ start_file_send();
+
+
+
+
+}
+
+function on_recoder_mode_change()
+{
+ var item = null;
+ var obj = document.getElementsByName("recoder_mode");
+ for (var i = 0; i < obj.length; i++) { //遍历Radio
+ if (obj[i].checked) {
+ item = obj[i].value;
+ break;
+ }
+
+
+ }
+ if(item=="mic")
+ {
+ document.getElementById("mic_mode_div").style.display = 'block';
+ document.getElementById("rec_mode_div").style.display = 'none';
+
+ btnConnect.disabled=false;
+ isfilemode=false;
+ }
+ else
+ {
+ document.getElementById("mic_mode_div").style.display = 'none';
+ document.getElementById("rec_mode_div").style.display = 'block';
+ btnConnect.disabled = true;
+ isfilemode=true;
+ info_div.innerHTML='请点击选择文件';
+
+
+ }
+}
function getAsrMode(){
var item = null;
@@ -53,7 +159,12 @@ function getAsrMode(){
}
+ if(isfilemode)
+ {
+ item= "offline";
+ }
console.log("asr mode"+item);
+
return item;
}
@@ -78,6 +189,18 @@ function getJsonMessage( jsonMsg ) {
varArea.value=rec_text;
console.log( "offline_text: " + asrmodel+","+offline_text);
console.log( "rec_text: " + rec_text);
+ if (isfilemode==true){
+ console.log("call stop ws!");
+ play_file();
+ wsconnecter.wsStop();
+
+ info_div.innerHTML="请点击连接";
+ isconnected=0;
+ btnStart.disabled = true;
+ btnStop.disabled = true;
+ btnConnect.disabled=false;
+ }
+
}
@@ -86,14 +209,11 @@ function getJsonMessage( jsonMsg ) {
function getConnState( connState ) {
if ( connState === 0 ) {
- //rec.open( function(){
- // rec.start();
- // console.log("开始录音");
- //});
- btnStart.disabled = false;
- btnConnect.disabled = true;
info_div.innerHTML='连接成功!请点击开始';
+ if (isfilemode==true){
+ info_div.innerHTML='请耐心等待,大文件等待时间更长';
+ }
} else if ( connState === 1 ) {
//stop();
} else if ( connState === 2 ) {
@@ -102,36 +222,52 @@ function getConnState( connState ) {
alert("连接地址"+document.getElementById('wssip').value+"失败,请检查asr地址和端口,并确保h5服务和asr服务在同一个域内。或换个浏览器试试。");
btnStart.disabled = true;
-
+ isconnected=0;
+
info_div.innerHTML='请点击连接';
}
}
function record()
{
+
rec.open( function(){
rec.start();
console.log("开始");
btnStart.disabled = true;
});
+
}
+
+
+
// 识别启动、停止、清空操作
function start() {
// 清除显示
clear();
//控件状态更新
-
+ console.log("isfilemode"+isfilemode+","+isconnected);
info_div.innerHTML="正在连接asr服务器,请等待...";
//启动连接
var ret=wsconnecter.wsStart();
if(ret==1){
isRec = true;
- btnStart.disabled = true;
+ btnStart.disabled = false;
btnStop.disabled = false;
btnConnect.disabled=true;
-
+ if (isfilemode)
+ {
+ console.log("start file now");
+ start_file_offline();
+
+ btnStart.disabled = true;
+ btnStop.disabled = true;
+ btnConnect.disabled = true;
+ }
+ return 1;
}
+ return 0;
}
@@ -152,21 +288,26 @@ function stop() {
}
wsconnecter.wsSend( JSON.stringify(request) ,false);
+
+
-
-
-
+ //isconnected=0;
// 控件状态更新
+
isRec = false;
- info_div.innerHTML="请等候...";
- btnStop.disabled = true;
- setTimeout(function(){
- console.log("call stop ws!");
- wsconnecter.wsStop();
+ info_div.innerHTML="发送完数据,请等候,正在识别...";
+
+ if(isfilemode==false){
+ btnStop.disabled = true;
btnStart.disabled = true;
btnConnect.disabled=false;
+ setTimeout(function(){
+ console.log("call stop ws!");
+ wsconnecter.wsStop();
+ isconnected=0;
info_div.innerHTML="请点击连接";}, 3000 );
+
rec.stop(function(blob,duration){
console.log(blob);
@@ -189,8 +330,9 @@ function stop() {
},function(errMsg){
console.log("errMsg: " + errMsg);
});
+ }
// 停止连接
-
+
}
diff --git a/funasr/runtime/html5/static/wsconnecter.js b/funasr/runtime/html5/static/wsconnecter.js
index 676a94ae5..b9098bb5a 100644
--- a/funasr/runtime/html5/static/wsconnecter.js
+++ b/funasr/runtime/html5/static/wsconnecter.js
@@ -15,8 +15,7 @@ function WebSocketConnectMethod( config ) { //定义socket连接方法类
this.wsStart = function () {
var Uri = document.getElementById('wssip').value; //"wss://111.205.137.58:5821/wss/" //设置wss asr online接口地址 如 wss://X.X.X.X:port/wss/
-
- if(Uri.match(/wss:\S*/))
+ if(Uri.match(/wss:\S*|ws:\S*/))
{
console.log("Uri"+Uri);
}
@@ -25,6 +24,7 @@ function WebSocketConnectMethod( config ) { //定义socket连接方法类
alert("请检查wss地址正确性");
return 0;
}
+
if ( 'WebSocket' in window ) {
speechSokt = new WebSocket( Uri ); // 定义socket连接对象
speechSokt.onopen = function(e){onOpen(e);}; // 定义响应函数
@@ -80,6 +80,7 @@ function WebSocketConnectMethod( config ) { //定义socket连接方法类
speechSokt.send( JSON.stringify(request) );
console.log("连接成功");
stateHandle(0);
+ isconnected=1;
}
function onClose( e ) {
@@ -92,9 +93,11 @@ function WebSocketConnectMethod( config ) { //定义socket连接方法类
}
function onError( e ) {
+ isconnected=-1;
info_div.innerHTML="连接"+e;
console.log(e);
stateHandle(2);
+
}
diff --git a/funasr/runtime/websocket/funasr-wss-client.cpp b/funasr/runtime/websocket/funasr-wss-client.cpp
index 8b59000fc..53301253c 100644
--- a/funasr/runtime/websocket/funasr-wss-client.cpp
+++ b/funasr/runtime/websocket/funasr-wss-client.cpp
@@ -277,13 +277,14 @@ class WebsocketClient {
};
int main(int argc, char* argv[]) {
+
google::InitGoogleLogging(argv[0]);
FLAGS_logtostderr = true;
TCLAP::CmdLine cmd("funasr-ws-client", ' ', "1.0");
TCLAP::ValueArg
server_ip_("", "server-ip", "server-ip", true,
"127.0.0.1", "string");
- TCLAP::ValueArg port_("", "port", "port", true, "8889", "string");
+ TCLAP::ValueArg port_("", "port", "port", true, "10095", "string");
TCLAP::ValueArg wav_path_("", "wav-path",
"the input could be: wav_path, e.g.: asr_example.wav; pcm_path, e.g.: asr_example.pcm; wav.scp, kaldi style wav list (wav_id \t wav_path)",
true, "", "string");
diff --git a/funasr/runtime/websocket/funasr-wss-server.cpp b/funasr/runtime/websocket/funasr-wss-server.cpp
index 5f2af5ce0..a6fa2e137 100644
--- a/funasr/runtime/websocket/funasr-wss-server.cpp
+++ b/funasr/runtime/websocket/funasr-wss-server.cpp
@@ -11,6 +11,7 @@
// [--vad-quant ] [--vad-dir ] [--quantize
// ] --model-dir [--] [--version] [-h]
#include "websocket-server.h"
+#include
using namespace std;
void GetValue(TCLAP::ValueArg& value_arg, string key,
@@ -20,10 +21,15 @@ void GetValue(TCLAP::ValueArg& value_arg, string key,
}
int main(int argc, char* argv[]) {
try {
+
google::InitGoogleLogging(argv[0]);
FLAGS_logtostderr = true;
TCLAP::CmdLine cmd("funasr-ws-server", ' ', "1.0");
+ TCLAP::ValueArg download_model_dir(
+ "", "download-model-dir",
+ "Download model from Modelscope to download_model_dir",
+ false, "", "string");
TCLAP::ValueArg model_dir(
"", MODEL_DIR,
"default: /workspace/models/asr, the asr model path, which contains model.onnx, config.yaml, am.mvn",
@@ -53,15 +59,15 @@ int main(int argc, char* argv[]) {
"true, load the model of model_quant.onnx in punc_dir",
false, "true", "string");
- TCLAP::ValueArg listen_ip("", "listen_ip", "listen_ip", false,
+ TCLAP::ValueArg listen_ip("", "listen-ip", "listen ip", false,
"0.0.0.0", "string");
- TCLAP::ValueArg port("", "port", "port", false, 8889, "int");
- TCLAP::ValueArg io_thread_num("", "io_thread_num", "io_thread_num",
+ TCLAP::ValueArg port("", "port", "port", false, 10095, "int");
+ TCLAP::ValueArg io_thread_num("", "io-thread-num", "io thread num",
false, 8, "int");
TCLAP::ValueArg decoder_thread_num(
- "", "decoder_thread_num", "decoder_thread_num", false, 8, "int");
- TCLAP::ValueArg model_thread_num("", "model_thread_num",
- "model_thread_num", false, 1, "int");
+ "", "decoder-thread-num", "decoder thread num", false, 8, "int");
+ TCLAP::ValueArg model_thread_num("", "model-thread-num",
+ "model thread num", false, 1, "int");
TCLAP::ValueArg certfile("", "certfile",
"default: ../../../ssl_key/server.crt, path of certficate for WSS connection. if it is empty, it will be in WS mode.",
@@ -73,6 +79,7 @@ int main(int argc, char* argv[]) {
cmd.add(certfile);
cmd.add(keyfile);
+ cmd.add(download_model_dir);
cmd.add(model_dir);
cmd.add(quantize);
cmd.add(vad_dir);
@@ -95,6 +102,71 @@ int main(int argc, char* argv[]) {
GetValue(punc_dir, PUNC_DIR, model_path);
GetValue(punc_quant, PUNC_QUANT, model_path);
+ // Download model form Modelscope
+ try{
+ std::string s_download_model_dir = download_model_dir.getValue();
+ if(download_model_dir.isSet() && !s_download_model_dir.empty()){
+ if (access(s_download_model_dir.c_str(), F_OK) != 0){
+ LOG(ERROR) << s_download_model_dir << " do not exists.";
+ exit(-1);
+ }
+ std::string s_vad_path = model_path[VAD_DIR];
+ std::string s_asr_path = model_path[MODEL_DIR];
+ std::string s_punc_path = model_path[PUNC_DIR];
+ std::string python_cmd = "python -m funasr.export.export_model --type onnx --quantize True ";
+ if(vad_dir.isSet() && !s_vad_path.empty()){
+ std::string python_cmd_vad = python_cmd + " --model-name " + s_vad_path + " --export-dir " + s_download_model_dir;
+ LOG(INFO) << "Download model: " << s_vad_path << " from modelscope: ";
+ system(python_cmd_vad.c_str());
+ std::string down_vad_path = s_download_model_dir+"/"+s_vad_path;
+ std::string down_vad_model = s_download_model_dir+"/"+s_vad_path+"/model_quant.onnx";
+ if (access(down_vad_model.c_str(), F_OK) != 0){
+ LOG(ERROR) << down_vad_model << " do not exists.";
+ exit(-1);
+ }else{
+ model_path[VAD_DIR]=down_vad_path;
+ LOG(INFO) << "Set " << VAD_DIR << " : " << model_path[VAD_DIR];
+ }
+ }else{
+ LOG(INFO) << "VAD model is not set, use default.";
+ }
+ if(model_dir.isSet() && !s_asr_path.empty()){
+ std::string python_cmd_asr = python_cmd + " --model-name " + s_asr_path + " --export-dir " + s_download_model_dir;
+ LOG(INFO) << "Download model: " << s_asr_path << " from modelscope: ";
+ system(python_cmd_asr.c_str());
+ std::string down_asr_path = s_download_model_dir+"/"+s_asr_path;
+ std::string down_asr_model = s_download_model_dir+"/"+s_asr_path+"/model_quant.onnx";
+ if (access(down_asr_model.c_str(), F_OK) != 0){
+ LOG(ERROR) << down_asr_model << " do not exists.";
+ exit(-1);
+ }else{
+ model_path[MODEL_DIR]=down_asr_path;
+ LOG(INFO) << "Set " << MODEL_DIR << " : " << model_path[MODEL_DIR];
+ }
+ }else{
+ LOG(INFO) << "ASR model is not set, use default.";
+ }
+ if(punc_dir.isSet() && !s_punc_path.empty()){
+ std::string python_cmd_punc = python_cmd + " --model-name " + s_punc_path + " --export-dir " + s_download_model_dir;
+ LOG(INFO) << "Download model: " << s_punc_path << " from modelscope: ";
+ system(python_cmd_punc.c_str());
+ std::string down_punc_path = s_download_model_dir+"/"+s_punc_path;
+ std::string down_punc_model = s_download_model_dir+"/"+s_punc_path+"/model_quant.onnx";
+ if (access(down_punc_model.c_str(), F_OK) != 0){
+ LOG(ERROR) << down_punc_model << " do not exists.";
+ exit(-1);
+ }else{
+ model_path[PUNC_DIR]=down_punc_path;
+ LOG(INFO) << "Set " << PUNC_DIR << " : " << model_path[PUNC_DIR];
+ }
+ }else{
+ LOG(INFO) << "PUNC model is not set, use default.";
+ }
+ }
+ } catch (std::exception const& e) {
+ LOG(ERROR) << "Error: " << e.what();
+ }
+
std::string s_listen_ip = listen_ip.getValue();
int s_port = port.getValue();
int s_io_thread_num = io_thread_num.getValue();