在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
百度云识别没有提供c#版本的sdk,下面给个c#的 1、打开网址http://developer.baidu.com/ 2、登陆 3、管理控制台》开发者服务管理 4、创建工程 5、输入名称,点击创建 6、点击左边【媒体云】》【语音识别】》申请开启语音服务,申请内容写的诚恳一点,一般2天就可以通过了,开通成功后右上角有消息提示,注意观察 7、申请通过后,就可以来弄了
可以自己先看下百度提供的文档
下面上代码 然后再详细解释 1 using System; 2 using System.Collections.Generic; 3 using System.ComponentModel; 4 using System.Data; 5 using System.Drawing; 6 using System.Text; 7 using System.Windows.Forms; 8 using System.Net; 9 using System.IO; 10 using System.Threading; 11 12 namespace TestBaiDuAudio2Txt 13 { 14 public partial class Form1 : Form 15 { 16 string token = ""; 17 string filename = Application.StartupPath + "\\3.wav"; 18 string apiKey = "";//对应百度云界面基本信息的API Key 19 string secretKey = "";//对应百度云界面基本信息的Secret Key 20 string cuid = "";//这个随便写 不过尽量写唯一的,比如自己创建个guid,或者你手机号码什么的都可以 21 string getTokenURL = ""; 22 string serverURL = "http://vop.baidu.com/server_api"; 23 public Form1() 24 { 25 InitializeComponent(); 26 cuid = Guid.NewGuid().ToString(); 27 // getToken(); 28 // Post(); 29 } 30 31 //这个方法得到一个密钥,这个密钥可以使用1个月,1个月之后要重新请求一次获得一个 32 private void getToken() 33 { 34 getTokenURL = "https://openapi.baidu.com/oauth/2.0/token?grant_type=client_credentials" + 35 "&client_id=" + apiKey + "&client_secret=" + secretKey; 36 token = GetValue("access_token"); 37 } 38 39 private string GetValue(string key) 40 { 41 HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(getTokenURL); 42 HttpWebResponse response = (HttpWebResponse)request.GetResponse(); 43 44 StreamReader reader1 = new StreamReader(response.GetResponseStream(), Encoding.UTF8); 45 string ssss = reader1.ReadToEnd().Replace("\"", "").Replace("{", "").Replace("}", "").Replace("\n", ""); 46 string[] indexs = ssss.Split(','); 47 foreach (string index in indexs) 48 { 49 string[] _indexs = index.Split(':'); 50 if (_indexs[0] == key) 51 return _indexs[1]; 52 } 53 return ""; 54 } 55 56 57 58 59 private void Post() 60 { 61 62 serverURL += "?lan=en&cuid=kwwwvagaa&token=" + token; 63 FileStream fs = new FileStream(filename, FileMode.Open); 64 byte[] voice = new byte[fs.Length]; 65 fs.Read(voice, 0, voice.Length); 66 fs.Close(); 67 fs.Dispose(); 68 69 HttpWebRequest request = null; 70 71 Uri uri = new Uri(serverURL); 72 request = (HttpWebRequest)WebRequest.Create(uri); 73 request.Timeout = 10000; 74 request.Method = "POST"; 75 request.ContentType = "audio/wav; rate=8000"; 76 request.ContentLength = voice.Length; 77 try 78 { 79 using (Stream writeStream = request.GetRequestStream()) 80 { 81 writeStream.Write(voice, 0, voice.Length); 82 writeStream.Close(); 83 writeStream.Dispose(); 84 } 85 } 86 catch 87 { 88 return; 89 } 90 string result = string.Empty; 91 using (HttpWebResponse response = (HttpWebResponse)request.GetResponse()) 92 { 93 using (Stream responseStream = response.GetResponseStream()) 94 { 95 using (StreamReader readStream = new StreamReader(responseStream, Encoding.UTF8)) 96 { 97 string line = string.Empty; 98 StringBuilder sb = new StringBuilder(); 99 while (!readStream.EndOfStream) 100 { 101 line = readStream.ReadLine(); 102 sb.Append(line); 103 sb.Append("\r"); 104 } 105 106 // result = readStream.ReadToEnd(); 107 108 result = sb.ToString(); 109 110 //message = result.Substring(result.IndexOf("utterance") + 12); 111 //message = message.Substring(0, message.IndexOf("\"")); 112 readStream.Close(); 113 readStream.Dispose(); 114 MessageBox.Show(result); 115 } 116 responseStream.Close(); 117 responseStream.Dispose(); 118 } 119 response.Close(); 120 } 121 122 this.textBox1.BeginInvoke(new MethodInvoker(delegate() 123 { 124 this.textBox1.Text = result; 125 })); 126 127 } 128 129 private void button1_Click(object sender, EventArgs e) 130 { 131 Thread th = new Thread(Post); 132 th.IsBackground = true; 133 th.Start(); 134 } 135 136 } 137 } getToken()方法不需要每次都执行,这个方法返回一个密钥字符串,可以使用30天,你自己进行判断,将要过期的时候再请求一次得到个新的就可以了 request.ContentType = "audio/wav; rate=8000"; 这个地方注意一下,你可以在文档中看到支持6种格式,rate支持8000和16000,不过我使用8000识别有问题,你们自己可试一下 注意下: 语音识别接口支持 POST 方式 还有一下其他的说明你可以看百度的文档,另外可以使用网络上的语音文件
还有不懂的地方 回帖问吧
|
2023-10-27
2022-08-15
2022-08-17
2022-09-23
2022-08-13
请发表评论