当前位置: 首页 > news >正文

视频网站制作费用小程序模板

视频网站制作费用,小程序模板,网站转备案,营销企业要在.NET Framework 4.8中使用C#实现离线实时语音识别,可以使用开源库Vosk(支持离线ASR)配合音频处理库NAudio。 步骤 1:安装依赖库 1.1. 安装NuGet包: - Install-Package NAudio(处理音频输入&#xff09…

要在.NET Framework 4.8中使用C#实现离线实时语音识别,可以使用开源库Vosk(支持离线ASR)配合音频处理库NAudio。

步骤 1:安装依赖库
1.1.
安装NuGet包:
- Install-Package NAudio(处理音频输入)
- Install-Package Vosk(离线语音识别引擎)
2.2.
下载语音模型:
- 前往 Vosk Models 下载适合的模型(如小型英文模型 vosk-model-small-en-us-0.15)。
- 解压模型到项目目录(如 Models/vosk-model-small-en-us-0.15)。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using NAudio.Wave;
using Vosk;
using System.Windows.Forms;
namespace 语音识别
{
public class RealTimeSpeechRecognizer
{private readonly VoskRecognizer _recognizer;private readonly WaveInEvent _waveIn;private readonly RichTextBox _rtb;public RealTimeSpeechRecognizer(string modelPath, RichTextBox rtb){_rtb = rtb;// 初始化VoskModel model = new Model(modelPath);_recognizer = new VoskRecognizer(model, 16000.0f);_recognizer.SetWords(true);// 初始化音频输入_waveIn = new WaveInEvent{WaveFormat = new WaveFormat(16000, 16, 1),DeviceNumber = 0};_waveIn.DataAvailable += OnAudioDataAvailable;}public void StartListening() => _waveIn.StartRecording();public void StopListening() => _waveIn.StopRecording();// 解析JSON结果(兼容部分结果)private string ParseJsonResult(string json, bool isPartial = false){dynamic obj = Newtonsoft.Json.JsonConvert.DeserializeObject(json);return isPartial ? obj.partial : obj.text;}// 线程安全更新RichTextBoxprivate void ShowText(string text){if (_rtb.InvokeRequired){_rtb.BeginInvoke(new Action<string>(ShowText), text);}else{_rtb.AppendText(text);_rtb.ScrollToCaret();}}// 其他字段和构造函数保持不变...private string _lastPartialText = string.Empty;private void OnAudioDataAvailable(object sender, WaveInEventArgs e){if (_recognizer.AcceptWaveform(e.Buffer, e.BytesRecorded)){string result = ParseJsonResult(_recognizer.Result());ClearLastPartial(); // 清理临时部分AppendFinalText(result);}else{string partial = ParseJsonResult(_recognizer.PartialResult(), isPartial: true);UpdatePartialText(partial);}}private void UpdatePartialText(string newPartial){if (newPartial == _lastPartialText) return;// 在主线程更新UI_rtb.BeginInvoke(new Action(() =>{int selectionStart = _rtb.TextLength - _lastPartialText.Length;// 删除旧临时内容if (selectionStart >= 0 && _lastPartialText.Length > 0){_rtb.Select(selectionStart, _lastPartialText.Length);_rtb.SelectedText = "";}// 追加新内容_rtb.AppendText(newPartial);_rtb.ScrollToCaret();// 更新临时记录_lastPartialText = newPartial;}));}private void AppendFinalText(string text){_rtb.BeginInvoke(new Action(() =>{_rtb.AppendText(text + "\n");_lastPartialText = string.Empty; // 重置临时部分}));}private void ClearLastPartial(){if (string.IsNullOrEmpty(_lastPartialText)) return;_rtb.BeginInvoke(new Action(() =>{int start = _rtb.TextLength - _lastPartialText.Length;if (start >= 0){_rtb.Select(start, _lastPartialText.Length);_rtb.SelectedText = "";}}));}
}}
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace 语音识别
{
public partial class Form1 : Form
{
private RealTimeSpeechRecognizer _recognizer;
public Form1()
{
InitializeComponent();
btnStop.Enabled = false;
}private void button1_Click(object sender, EventArgs e){_recognizer.StopListening();btnStart.Enabled = true;btnStop.Enabled = false;}private void btnStart_Click(object sender, EventArgs e){string modelPath = @"E:\Models\vosk-model-small-cn-0.22"; // 中文模型路径_recognizer = new RealTimeSpeechRecognizer(modelPath, richTextBox1);btnStart.Enabled = false;btnStop.Enabled = true;_recognizer.StartListening();}
}}

http://www.hkea.cn/news/644481/

相关文章:

  • 怎么做网站banner查排名网站
  • 做网站好看的背景图片相关搜索优化软件
  • 怎么查网站是哪家制作公司做的百度收录查询
  • 企业年金交了有好处吗网络优化工程师吃香吗
  • python做网站开发百度6大核心部门
  • 自己做网站平台企业网站优化价格
  • 淘宝网网站建设的需求分析百度会员登录入口
  • 建网站的专业公司推广网站多少钱
  • 网站不去公安局备案自己怎么搭建网站
  • 外贸网站建设入门深圳网络推广哪家
  • 网站模板资源公司网站推广
  • 广东省建设教育协会官方网站首页html简单网页代码
  • 个人网站意义阿里指数官网最新版本
  • 网站开发方式有哪四种搜索引擎优化课程总结
  • 申请做网站、论坛版主app推广接单
  • 青海网站建设广州seo优化推广
  • 物流公司网站制作模板上海网站关键词排名
  • 广西建设人才网搜索引擎优化的目标
  • 比汉斯设计网站素材图片搜索识图入口
  • php网站架设教程英雄联盟韩国
  • 做毕设好的网站百度客服电话24小时
  • 上海手机网站建设电话咨询seo综合查询系统
  • wordpress 4.6 中文版沈阳seo
  • 文件管理软件天津搜索引擎优化
  • 九亭网站建设全国疫情高峰时间表最新
  • 青岛网站建设公司武汉seo收费
  • mvc网站建设的实验报告怎么做优化
  • 有官网建手机网站千锋教育培训多少钱费用
  • b2c交易模式的网站有哪些百度营销客户端
  • flash 学习网站重庆网站seo多少钱