C#使用whisper.net实现语音识别(语音转文本)

目录

介绍

效果

输出信息

项目

代码

下载


介绍

github地址:https://github.com/sandrohanea/whisper.net

Whisper.net. Speech to text made simple using Whisper Models

模型下载地址:https://huggingface.co/sandrohanea/whisper.net/tree/main/classic

效果

输出信息

whisper_init_from_file_no_state: loading model from 'ggml-small.bin'

whisper_model_load: loading model

whisper_model_load: n_vocab = 51865

whisper_model_load: n_audio_ctx = 1500

whisper_model_load: n_audio_state = 768

whisper_model_load: n_audio_head = 12

whisper_model_load: n_audio_layer = 12

whisper_model_load: n_text_ctx = 448

whisper_model_load: n_text_state = 768

whisper_model_load: n_text_head = 12

whisper_model_load: n_text_layer = 12

whisper_model_load: n_mels = 80

whisper_model_load: ftype = 1

whisper_model_load: qntvr = 0

whisper_model_load: type = 3

whisper_model_load: mem required = 743.00 MB (+ 16.00 MB per decoder)

whisper_model_load: adding 1608 extra tokens

whisper_model_load: model ctx = 464.68 MB

whisper_model_load: model size = 464.44 MB

whisper_init_state: kv self size = 15.75 MB

whisper_init_state: kv cross size = 52.73 MB

00:00:00->00:00:20: 皇鶴楼,崔昊,西人已成皇鶴去,此地空于皇鶴楼,皇鶴一去不复返,白云千载空悠悠。

00:00:20->00:00:39: 青川莉莉汉阳树,方草七七英五周,日暮相关何处事,燕泊江上世人愁。

项目

代码

using System;

using System.Collections.Generic;

using System.ComponentModel;

using System.Data;

using System.Drawing;

using System.IO;

using System.Linq;

using System.Text;

using System.Threading.Tasks;

using System.Windows.Forms;

using Whisper.net;

using static System.Net.Mime.MediaTypeNames;

namespace C_使用whisper.net实现语音转文本

{

public partial class Form1 : Form

{

public Form1()

{

InitializeComponent();

}

string fileFilter = "*.wav|*.wav";

string wavFileName = "";

WhisperFactory whisperFactory;

WhisperProcessor processor;

private async void button2_Click(object sender, EventArgs e)

{

if (wavFileName == "")

{

return;

}

try

{

button2.Enabled = false;

using var fileStream = File.OpenRead(wavFileName);

await foreach (var result in processor.ProcessAsync(fileStream))

{

Console.WriteLine($"{result.Start}->{result.End}: {result.Text}\r\n");

txtResult.Text += $"{result.Start}->{result.End}: {result.Text}\r\n";

}

}

catch (Exception ex)

{

MessageBox.Show(ex.Message);

}

finally

{

button2.Enabled = true;

}

}

private void Form1_Load(object sender, EventArgs e)

{

whisperFactory = WhisperFactory.FromPath("ggml-small.bin");

processor = whisperFactory.CreateBuilder()

.WithLanguage("zh")//.WithLanguage("auto")

.Build();

wavFileName = "085黄鹤楼.wav";

txtFileName.Text = wavFileName;

}

private void button1_Click(object sender, EventArgs e)

{

OpenFileDialog ofd = new OpenFileDialog();

ofd.Filter = fileFilter;

if (ofd.ShowDialog() != DialogResult.OK) return;

txtResult.Text = "";

wavFileName = ofd.FileName;

txtFileName.Text = wavFileName;

}

}

}

复制代码
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Whisper.net;
using static System.Net.Mime.MediaTypeNames;

namespace C_使用whisper.net实现语音转文本
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        string fileFilter = "*.wav|*.wav";
        string wavFileName = "";
        WhisperFactory whisperFactory;
        WhisperProcessor processor;
        private async void button2_Click(object sender, EventArgs e)
        {
            if (wavFileName == "")
            {
                return;
            }

            try
            {
                button2.Enabled = false;
                using var fileStream = File.OpenRead(wavFileName);
                await foreach (var result in processor.ProcessAsync(fileStream))
                {
                    Console.WriteLine($"{result.Start}->{result.End}: {result.Text}\r\n");
                    txtResult.Text += $"{result.Start}->{result.End}: {result.Text}\r\n";
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            finally
            {
                button2.Enabled = true;
            }

        }

        private void Form1_Load(object sender, EventArgs e)
        {
            whisperFactory = WhisperFactory.FromPath("ggml-small.bin");

            processor = whisperFactory.CreateBuilder()
               .WithLanguage("zh")//.WithLanguage("auto")
               .Build();

            wavFileName = "085黄鹤楼.wav";
            txtFileName.Text = wavFileName;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            OpenFileDialog ofd = new OpenFileDialog();
            ofd.Filter = fileFilter;
            if (ofd.ShowDialog() != DialogResult.OK) return;

            txtResult.Text = "";

            wavFileName = ofd.FileName;
            txtFileName.Text = wavFileName;
        }
    }
}

下载

源码下载

相关推荐
不懂嵌入式4 分钟前
基于深度学习的水果识别系统设计
人工智能·深度学习
江小皮不皮13 分钟前
为何选择MCP?自建流程与Anthropic MCP的对比分析
人工智能·llm·nlp·aigc·sse·mcp·fastmcp
GIS数据转换器27 分钟前
当三维地理信息遇上气象预警:电网安全如何实现“先知先觉”?
人工智能·科技·安全·gis·智慧城市·交互
网易易盾27 分钟前
AIGC时代的内容安全:AI检测技术如何应对新型风险挑战?
人工智能·安全·aigc
工头阿乐31 分钟前
PyTorch中的nn.Embedding应用详解
人工智能·pytorch·embedding
alpszero35 分钟前
YOLO11解决方案之物体模糊探索
人工智能·python·opencv·计算机视觉·yolo11
Alessio Micheli37 分钟前
基于几何布朗运动的股价预测模型构建与分析
线性代数·机器学习·概率论
vlln42 分钟前
适应性神经树:当深度学习遇上决策树的“生长法则”
人工智能·深度学习·算法·决策树·机器学习
奋斗者1号1 小时前
机器学习之决策树与决策森林:机器学习中的强大工具
人工智能·决策树·机器学习
多巴胺与内啡肽.1 小时前
OpenCV进阶操作:风格迁移以及DNN模块解析
人工智能·opencv·dnn