csharp:百度语音识别

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
public string API_id = "8888"; //你的ID
       public string API_record = null;
       public string API_record_format = "wav";
       public string API_record_HZ = "16000";
       public string API_key = "geovindu"; //你的KEY
       public string API_secret_key = "55519"; //你的SECRRET_KEY
       public string API_language = "zh";
       public string API_access_token = null;
       public string strJSON = "";
 
       //录音
       [DllImport("winmm.dll", EntryPoint = "mciSendString", CharSet = CharSet.Auto)]
       public static extern int mciSendString(
        string lpstrCommand,
        string lpstrReturnString,
        int uReturnLength,
        int hwndCallback
       );
       /// <summary>
       ///
       /// </summary>
       public Form1()
       {
           InitializeComponent();
       }
 
       /// <summary>
       ///
       /// </summary>
       /// <param name="sender"></param>
       /// <param name="e"></param>
       private void Form1_Load(object sender, EventArgs e)
       {
           API_record=Application.StartupPath + "\\上班.wav";
           API_access_token = getStrAccess(API_key, API_secret_key);
 
       }
 
       /// <summary>
       ///
       /// </summary>
       /// <param name="sender"></param>
       /// <param name="e"></param>
       private void button1_Click(object sender, EventArgs e)
       {
           this.textBox2.Text = getStrText(API_id, API_access_token, API_language, API_record, API_record_format, API_record_HZ);
 
       }
       /// <summary>
       ///
       /// </summary>
       /// <param name="para_API_key">你的KEY</param>
       /// <param name="para_API_secret_key">你的SECRRET_KEY</param>
       /// <returns></returns>
       public string getStrAccess(string para_API_key, string para_API_secret_key)
       {
 
           //方法参数说明:            
           //para_API_key:API_key(你的KEY)            
           //para_API_secret_key(你的SECRRET_KEY)            
           //方法返回值说明:            
           //百度认证口令码,access_token            
           string access_html = null;
           string access_token = null;
           string getAccessUrl = "https://openapi.baidu.com/oauth/2.0/token?grant_type=client_credentials" +"&client_id=" + para_API_key + "&client_secret=" + para_API_secret_key;
           try
           {
               HttpWebRequest getAccessRequest = WebRequest.Create(getAccessUrl) as HttpWebRequest;
               //getAccessRequest.Proxy = null;                
               getAccessRequest.ContentType = "multipart/form-data";
               getAccessRequest.Accept = "*/*";
               getAccessRequest.UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 2.0.50727)";
               getAccessRequest.Timeout = 30000;//30秒连接不成功就中断                 
               getAccessRequest.Method = "post";
               HttpWebResponse response = getAccessRequest.GetResponse() as HttpWebResponse;
               using (StreamReader strHttpComback = new StreamReader(response.GetResponseStream(), Encoding.UTF8))
               {
                   access_html = strHttpComback.ReadToEnd();
               }
           }
           catch (WebException ex)
           {
               MessageBox.Show(ex.ToString());
           }
 
           JObject jo = JObject.Parse(access_html);
           access_token = jo["access_token"].ToString();//得到返回的toke            
           return access_token;
       }
       /// <summary>
       ///
       /// </summary>
       /// <param name="para_API_id"></param>
       /// <param name="para_API_access_token"></param>
       /// <param name="para_API_language"></param>
       /// <param name="para_API_record"></param>
       /// <param name="para_format"></param>
       /// <param name="para_Hz"></param>
       /// <returns></returns>
       public string getStrText(string para_API_id, string para_API_access_token, string para_API_language, string para_API_record, string para_format, string para_Hz)
       {
           //方法参数说明:            
           //para_API_id: API_id (你的ID)
           //para_API_access_token (getStrAccess(...)方法得到的access_token口令)
           //para_API_language (你要识别的语言,zh,en,ct)
           //para_API_record(语音文件的路径)
           //para_format(语音文件的格式)
           //para_Hz(语音文件的采样率 16000或者8000)
              
           //该方法返回值:            
           //该方法执行正确返回值是语音翻译的文本,错误是错误号,可以去看百度语音文档,查看对应错误            
           string strText = null;
           string error = null;
           FileInfo fi = new FileInfo(para_API_record);
           FileStream fs = new FileStream(para_API_record, FileMode.Open);
           byte[] voice = new byte[fs.Length];
           fs.Read(voice, 0, voice.Length);
           fs.Close();
  
           string getTextUrl = "http://vop.baidu.com/server_api?lan=" + para_API_language + "&cuid=" + para_API_id + "&token=" + para_API_access_token;
           HttpWebRequest getTextRequst = WebRequest.Create(getTextUrl) as HttpWebRequest;
  
          /* getTextRequst.Proxy = null;
           getTextRequst.ServicePoint.Expect100Continue = false;
           getTextRequst.ServicePoint.UseNagleAlgorithm = false;
           getTextRequst.ServicePoint.ConnectionLimit = 65500;
           getTextRequst.AllowWriteStreamBuffering = false;*/
  
           getTextRequst.ContentType = "audio /"+para_format+";rate="+para_Hz;
           getTextRequst.ContentLength = fi.Length;
           getTextRequst.Method = "post";
           getTextRequst.Accept = "*/*";
           getTextRequst.KeepAlive = true;
           getTextRequst.UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 2.0.50727)";
           getTextRequst.Timeout = 30000;//30秒连接不成功就中断             
           using (Stream writeStream = getTextRequst.GetRequestStream())
           {
               writeStream.Write(voice, 0, voice.Length);
           }
  
           HttpWebResponse getTextResponse = getTextRequst.GetResponse() as HttpWebResponse;       
           using (StreamReader strHttpText = new StreamReader(getTextResponse.GetResponseStream(), Encoding.UTF8))
           {
               strJSON = strHttpText.ReadToEnd();
           }
                
           JObject jsons = JObject.Parse(strJSON);//解析JSON 
               
           if (jsons["err_msg"].Value<string>() == "success.")
           {                 
                   strText = jsons["result"][0].ToString();
                   return strText;
           }
           else
           {
                   error = jsons["err_no"].Value<string>() + jsons["err_msg"].Value<string>();
                   return error;
           }
              
       }

  参考: http://yuyin.baidu.com/docs/asr/57 

API请求方式基本说明

  • 语音识别接口支持 POST 方式
  • 目前 API 仅支持整段语音识别的模式,即需要上传整段语音进行识别
  • 语音数据上传方式有两种:隐示发送和显示发送
  • 原始语音的录音格式目前只支持评测 8k/16k 采样率 16bit 位深的单声道语音
  • 压缩格式支持:pcm(不压缩)、wav、opus、speex、amr、x-flac
  • 系统支持语言种类:中文(zh)、粤语(ct)、英文(en)
  • 正式地址:http://vop.baidu.com/server_api

 

posted @   ®Geovin Du Dream Park™  阅读(1219)  评论(0编辑  收藏  举报
编辑推荐:
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
阅读排行:
· 25岁的心里话
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· 零经验选手,Compose 一天开发一款小游戏!
· 一起来玩mcp_server_sqlite,让AI帮你做增删改查!!
历史上的今天:
2010-11-25 颜色选择器 Color Picker,Internet Explorer,Firefox,Opera,Safar
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5
点击右上角即可分享
微信分享提示