科大讯飞和百度语音平台语音识别Java调用记录
生活随笔
收集整理的這篇文章主要介紹了
科大讯飞和百度语音平台语音识别Java调用记录
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
1、科大訊飛平臺:http://www.xfyun.cn/doccenter/java
? ? ?注冊并創建應用,下載SDK,選擇語音聽寫+Java+創建的應用(APPID);
? ? ?未提交審核的應用,服務量會有500次/日的限制,開發完成后提交審核解除限制;
? ? ?注冊并?創建應用,下載REST API的壓縮文檔,提供Http接口支持整段錄音文件的識別;
? ? ?原始語音的錄音格式目前支持評測 8k/16k 采樣率 16bit 位深的單聲道語音;
? ? ?壓縮格式支持:pcm(不壓縮)、wav、opus、speex、amr、x-flac;
? ? ?語音識別服務開通成功后即可獲得 50000次/日 的在線識別調用配額;
? ? ?注冊并創建應用,下載SDK,選擇語音聽寫+Java+創建的應用(APPID);
? ? ?未提交審核的應用,服務量會有500次/日的限制,開發完成后提交審核解除限制;
? ? ?SDK里面的Sample功能直接導入eclipse,編譯執行,支持粵語;
? ? ?注冊并?創建應用,下載REST API的壓縮文檔,提供Http接口支持整段錄音文件的識別;
? ? ?原始語音的錄音格式目前支持評測 8k/16k 采樣率 16bit 位深的單聲道語音;
? ? ?壓縮格式支持:pcm(不壓縮)、wav、opus、speex、amr、x-flac;
? ? ?語音識別服務開通成功后即可獲得 50000次/日 的在線識別調用配額;
? ? ?http://yuyin.baidu.com/docs/asr/57
? ? ?Sample代碼
? ??
package org.asr.baidu.speech.serviceapi;import java.io.BufferedReader; import java.io.DataOutputStream; import java.io.File; import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.net.HttpURLConnection; import java.net.URL;import javax.xml.bind.DatatypeConverter;import org.asr.json.JSONObject;public class Sample {private static final String serverURL = "http://vop.baidu.com/server_api";private static String token = "";//private static final String testFileName = "test.pcm";private static final String testFileName = "D:\\tmp\\1.wav";//put your own params hereprivate static final String apiKey = "";private static final String secretKey = "";private static final String cuid = "";public static void main(String[] args) throws Exception {getToken();//method1();method2();}private static void getToken() throws Exception {String getTokenURL = "https://openapi.baidu.com/oauth/2.0/token?grant_type=client_credentials" + "&client_id=" + apiKey + "&client_secret=" + secretKey;HttpURLConnection conn = (HttpURLConnection) new URL(getTokenURL).openConnection();token = new JSONObject(printResponse(conn)).getString("access_token");}private static void method1() throws Exception {File pcmFile = new File(testFileName);HttpURLConnection conn = (HttpURLConnection) new URL(serverURL).openConnection();// construct paramsJSONObject params = new JSONObject();params.put("format", "pcm");params.put("rate", 8000);params.put("channel", "1");params.put("token", token);params.put("cuid", cuid);params.put("len", pcmFile.length());params.put("speech", DatatypeConverter.printBase64Binary(loadFile(pcmFile)));// add request headerconn.setRequestMethod("POST");conn.setRequestProperty("Content-Type", "application/json; charset=utf-8");conn.setDoInput(true);conn.setDoOutput(true);// send requestDataOutputStream wr = new DataOutputStream(conn.getOutputStream());wr.writeBytes(params.toString());wr.flush();wr.close();printResponse(conn);}private static void method2() throws Exception {File pcmFile = new File(testFileName);HttpURLConnection conn = (HttpURLConnection) new URL(serverURL+ "?cuid=" + cuid + "&token=" + token).openConnection();// add request headerconn.setRequestMethod("POST");conn.setRequestProperty("Content-Type", "audio/pcm; rate=8000");conn.setDoInput(true);conn.setDoOutput(true);// send requestDataOutputStream wr = new DataOutputStream(conn.getOutputStream());wr.write(loadFile(pcmFile));wr.flush();wr.close();printResponse(conn);}private static String printResponse(HttpURLConnection conn) throws Exception {if (conn.getResponseCode() != 200) {// request errorreturn "";}InputStream is = conn.getInputStream();BufferedReader rd = new BufferedReader(new InputStreamReader(is));String line;StringBuffer response = new StringBuffer();while ((line = rd.readLine()) != null) {response.append(line);response.append('\r');}rd.close();System.out.println(new JSONObject(response.toString()).toString(4));return response.toString();}private static byte[] loadFile(File file) throws IOException {InputStream is = new FileInputStream(file);long length = file.length();byte[] bytes = new byte[(int) length];int offset = 0;int numRead = 0;while (offset < bytes.length&& (numRead = is.read(bytes, offset, bytes.length - offset)) >= 0) {offset += numRead;}if (offset < bytes.length) {is.close();throw new IOException("Could not completely read file " + file.getName());}is.close();return bytes;} }3)兩個平臺都提供相對詳細的開發者教程,Demo也可以直接使用,相對來說科大訊飛平臺支持方言和語音訓練,百度暫時沒發現有語音訓練功能。
總結
以上是生活随笔為你收集整理的科大讯飞和百度语音平台语音识别Java调用记录的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Java程序执行Linux命令调用Eas
- 下一篇: 计算机视觉库OpenCV初步了解