使用System.Speech转换MP3文件为文本 [英] Using System.Speech to convert mp3 file to text

查看:256
本文介绍了使用System.Speech转换MP3文件为文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试使用语音识别在.net中认识到播客的演讲MP3文件并得到结果的字符串。所有我见过的例子是与使用的麦克风,但我不希望使用的麦克风,并提供了一​​个MP3文件作为我的音频源。任何人都可以指出我的任何资源或张贴的例子。

修改 -

我转换音频文件到 WAV 文件,并试图在其上该code。但只提取前68个单词。

 公共类MyRecognizer {
    公共字符串ReadAudio(){
        SpeechRecognitionEngine SRE =新SpeechRecognitionEngine();
        语法克=新DictationGrammar();
        sre.LoadGrammar(克);
        sre.SetInputToWaveFile(C:\\用户\\ Soham达斯古普塔\\ \\下载播客\\ Engadget_Podcast_353.wav);
        sre.BabbleTimeout =新的时间跨度(Int32.MaxValue);
        sre.InitialSilenceTimeout =新的时间跨度(Int32.MaxValue);
        sre.EndSilenceTimeout =新的时间跨度(亿);
        sre.EndSilenceTimeoutAmbiguous =新的时间跨度(亿);
        RecognitionResult结果= sre.Recognize(新的TimeSpan(Int32.MaxValue));
        返回result.Text;
    }
}
 

解决方案

尝试阅读它在一个循环。

  SpeechRecognitionEngine斯雷=新SpeechRecognitionEngine();
语法克=新DictationGrammar();
sre.LoadGrammar(克);
sre.SetInputToWaveFile(C:\\用户\\ Soham达斯古普塔\\ \\下载播客\\ Engadget_Podcast_353.wav);
sre.BabbleTimeout =新的时间跨度(Int32.MaxValue);
sre.InitialSilenceTimeout =新的时间跨度(Int32.MaxValue);
sre.EndSilenceTimeout =新的时间跨度(亿);
sre.EndSilenceTimeoutAmbiguous =新的时间跨度(亿);

StringBuilder的SB =新的StringBuilder();
而(真)
{
    尝试
    {
        变种recText = sre.Recognize();
        如果(recText == NULL)
        {
            打破;
        }

        sb.Append(recText.Text);
    }
    赶上(例外前)
    {
        //处理异常
        // ...

        打破;
    }
}
返回sb.ToString();
 

如果你已经在Windows窗体或WPF应用程序,在一个单独的线程中运行该code,否则阻塞UI线程。

I'm trying to use the speech recognition in .net to recognize the speech of a podcast in an mp3 file and get the result as string. All the examples I've seen are related to using microphone but I don't want to use the microphone and provide a sample mp3 file as my audio source. Can anyone point me to any resource or post an example.

EDIT -

I converted the audio file to wav file and tried this code on it. But it only extracts the first 68 words.

public class MyRecognizer {
    public string ReadAudio() {
        SpeechRecognitionEngine sre = new SpeechRecognitionEngine();
        Grammar gr = new DictationGrammar();
        sre.LoadGrammar(gr);
        sre.SetInputToWaveFile("C:\\Users\\Soham Dasgupta\\Downloads\\Podcasts\\Engadget_Podcast_353.wav");
        sre.BabbleTimeout = new TimeSpan(Int32.MaxValue);
        sre.InitialSilenceTimeout = new TimeSpan(Int32.MaxValue);
        sre.EndSilenceTimeout = new TimeSpan(100000000);
        sre.EndSilenceTimeoutAmbiguous = new TimeSpan(100000000);
        RecognitionResult result = sre.Recognize(new TimeSpan(Int32.MaxValue));
        return result.Text;
    }
}

解决方案

Try reading it in a loop.

SpeechRecognitionEngine sre = new SpeechRecognitionEngine();
Grammar gr = new DictationGrammar();
sre.LoadGrammar(gr);
sre.SetInputToWaveFile("C:\\Users\\Soham Dasgupta\\Downloads\\Podcasts\\Engadget_Podcast_353.wav");
sre.BabbleTimeout = new TimeSpan(Int32.MaxValue);
sre.InitialSilenceTimeout = new TimeSpan(Int32.MaxValue);
sre.EndSilenceTimeout = new TimeSpan(100000000);
sre.EndSilenceTimeoutAmbiguous = new TimeSpan(100000000); 

StringBuilder sb = new StringBuilder();
while (true)
{
    try
    {
        var recText = sre.Recognize();
        if (recText == null)
        {               
            break;
        }

        sb.Append(recText.Text);
    }
    catch (Exception ex)
    {   
        //handle exception      
        //...

        break;
    }
}
return sb.ToString();

If you've a Windows Forms or WPF application, run this code in a seperate thread, otherwise it blocks the UI thread.

这篇关于使用System.Speech转换MP3文件为文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆