您的位置:首页 > 游戏 > 手游 > Windows 11部署FunASR离线语音识别系统

Windows 11部署FunASR离线语音识别系统

2024/10/16 19:59:10 来源:https://blog.csdn.net/qq_31042143/article/details/139585567  浏览:    关键词:Windows 11部署FunASR离线语音识别系统

Windows 11部署FunASR离线语音识别系统

官网连接

https://github.com/alibaba-damo-academy/FunASR/blob/main/runtime/docs/SDK_advanced_guide_online_zh.md

1-安装Docker

运行Docker Desktop Installer.exe安装Docker

2-Windows添加删除程序增加虚拟机和Linux子系统功能

Hyper-V(Windows 11可能不显示,通过命令systeminfo显示:Hyper-V 要求: 已检测到虚拟机监控程序。将不显示 Hyper-V 所需的功能。说明系统支持Hyper-V的)
适用于Linux的Windows子系统
虚拟机平台
Windows虚拟机监控程序平台

错误处理:
Windows 11运行Docker提示"Docker Engine stopped"错误
a-在系统服务中启动Docker Desktop Service(com.docker.service)
b-在系统服务中启动Hyper-V 主机计算服务(vmcompute)

3-升级Linux子系统(可选)

wsl --update

4-部署Docker

docker pull registry.cn-hangzhou.aliyuncs.com/funasr_repo/funasr:funasr-runtime-sdk-online-cpu-0.1.9
mkdir d://FunASR//model

5-启动Docker

docker run -p 10095:10095 -it --privileged=true -v d:/FunASR/model:/workspace/models registry.cn-hangzhou.aliyuncs.com/funasr_repo/funasr:funasr-runtime-sdk-online-cpu-0.1.9

6-启动FunASR

cd FunASR/runtime
nohup bash run_server_2pass.sh --certfile 0  --download-model-dir /workspace/models --vad-dir damo/speech_fsmn_vad_zh-cn-16k-common-onnx --model-dir damo/speech_paraformer-large-vad-punc_asr_nat-zh-cn-16k-common-vocab8404-onnx  --online-model-dir damo/speech_paraformer-large_asr_nat-zh-cn-16k-common-vocab8404-online-onnx  --punc-dir damo/punc_ct-transformer_zh-cn-common-vad_realtime-vocab272727-onnx --itn-dir thuduj12/fst_itn_zh > log.txt 2>&1 &

7-关闭FunASR

ps -x | grep funasr-wss-server-2pass
kill -9 PID

8-打开新的控制台查看执行日志

docker exec -it PID /bin/sh
tail -f nohup.out

9-批处理自动执行

@echo off
REM 设置CONTAINER_NAME变量为容器的PID
set CONTAINER_NAME=3991fdb6c269chcp 65001REM 等待 30 秒让容器完全启动
timeout /t 30docker start %CONTAINER_NAME%REM 进入 Docker 容器
docker exec -it %CONTAINER_NAME% bash -c "cd FunASR/runtime && nohup bash run_server_2pass.sh --certfile 0 --download-model-dir /workspace/models --vad-dir damo/speech_fsmn_vad_zh-cn-16k-common-onnx --model-dir damo/speech_paraformer-large-vad-punc_asr_nat-zh-cn-16k-common-vocab8404-onnx --online-model-dir damo/speech_paraformer-large_asr_nat-zh-cn-16k-common-vocab8404-online-onnx --punc-dir damo/punc_ct-transformer_zh-cn-common-vad_realtime-vocab272727-onnx --itn-dir thuduj12/fst_itn_zh > log.txt 2>&1 &"REM 实时查看日志
docker exec -it %CONTAINER_NAME% bash -c "tail -f FunASR/runtime/nohup.out":: 暂停,等待用户按下任意键
pause

10-C#客户端

运行容器时命令含参数包含了-certfile 0,意思为关闭SSL,客户端通过ws://127.0.0.1:10095连接系统(默认为wss://,安全WebSocket协议)

流程:
客户端 ----------发起WebSocket连接---------->服务器
客户端----------发送JSON握手协议---------->服务器
客户端----------发送PCM二进制数据帧---------->服务器
客户端<----------返回JSON数据----------服务器

握手协议:

{"chunk_size":[5,10,5],"wav_name":"h5","is_speaking":true,"chunk_interval":10,"itn":false,"mode":"2pass","hotwords":"{\"阿里巴巴\":20,\"hello world\":40}"}

返回JSON数据有两种
实时返回数据

{"is_final":false,"mode":"2pass-online","text":"小明","wav_name":"h5"}

返回断句数据

{"is_final":false,"mode":"2pass-offline","stamp_sents":[{"end":39950,"punc":"。","start":39670,"text_seg":"小 明","ts_list":[[39670,39850],[39850,39950]]},{"end":40465,"punc":"","start":39950,"text_seg":"小 明","ts_list":[[39950,40050],[40050,40465]]}],"text":"小明。小明","timestamp":"[[39670,39850],[39850,39950],[39950,40050],[40050,40465]]","wav_name":"h5"}

不同点在于mode是online还是offline。

主要代码

  /** 与FunASR建立WebSocket连接* */public async Task StartWebSocketSync(){// 全局忽略证书验证错误ServicePointManager.ServerCertificateValidationCallback += (sender, cert, chain, sslPolicyErrors) => true;string wssUrl = "ws://127.0.0.1:10095";webSocket = new ClientWebSocket();try{await webSocket.ConnectAsync(new Uri(wssUrl), CancellationToken.None);await SendInitialMessage(webSocket);Console.WriteLine("WEBSOCKET已连接...");bWebSocketReady = true;await Task.WhenAll(ReceiveMessagesSync(webSocket), SendAudioSync(webSocket));}catch (Exception ex){Console.WriteLine($"WEBSOCKET连接错误: {ex.Message}");}}/** 发送握手数据* */private async Task SendInitialMessage(ClientWebSocket webSocket){var request = new{chunk_size = new int[] { 5, 10, 5 },wav_name = "h5",is_speaking = true,chunk_interval = 10,itn = false,mode = "2pass"};string jsonRequest = JsonConvert.SerializeObject(request);byte[] bytes = Encoding.UTF8.GetBytes(jsonRequest);await webSocket.SendAsync(new ArraySegment<byte>(bytes), WebSocketMessageType.Text, true, CancellationToken.None);}/** 接收数据并处理* */private async Task ReceiveMessagesSync(ClientWebSocket ws){var buffer = new byte[1024 * 100];while (ws.State == WebSocketState.Open){var result = await ws.ReceiveAsync(new ArraySegment<byte>(buffer), CancellationToken.None);if (result.MessageType == WebSocketMessageType.Close){await ws.CloseAsync(WebSocketCloseStatus.NormalClosure, string.Empty, CancellationToken.None);Console.WriteLine("WEBSOCKET已关闭。");}else{string message = Encoding.UTF8.GetString(buffer, 0, result.Count);if (!string.IsNullOrEmpty(message)){// HandleMessage(message); // 收到为包含识别文本的JSON数据}}}}/** 开始监听* */private async Task StartListeningAsync(CancellationToken cancellationToken){var waveIn = new WaveInEvent{WaveFormat = new WaveFormat(16000, 1) // 使用16kHz单声道};waveIn.DataAvailable += (s, e) =>{lock (this){// 读取数据byte[] bytes = new byte[e.BytesRecorded];for (var i = 0; i < e.BytesRecorded; i++){bytes[i] = e.Buffer[i];}// 缓存数据_waveBuffer.Add(bytes);}};waveIn.StartRecording();Console.WriteLine("开始监听麦克风...");while (!cancellationToken.IsCancellationRequested){await StartWebSocketSync();await Task.Delay(100, cancellationToken); // 适当的延迟}waveIn.StopRecording();Console.WriteLine("停止监听麦克风。");}

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com