当前位置: 首页 > news >正文

Java调用百度云语音识别【音频转写】

百度云文档  ttps://ai.baidu.com/ai-doc/SPEECH/Bk5difx01

示例代码: 

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import lombok.extern.slf4j.Slf4j;
import okhttp3.*;
import org.json.JSONObject;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.concurrent.TimeUnit;/*** 语音识别*/
@Slf4j
@Component
public class AasrUtils {public static final String API_KEY = "U92RRV****ag9xZv";public static final String SECRET_KEY = "SU05xD****0ziDkM";static final OkHttpClient HTTP_CLIENT = new OkHttpClient.Builder().connectTimeout(60, TimeUnit.SECONDS) // Set the connection timeout.readTimeout(300, TimeUnit.SECONDS)    // Set the read timeout.build();/*** 音频转写** @param speechUrl  音频url, 音频大小不超过500MB* @return* @throws IOException*/private static String createAasr(String speechUrl) throws IOException {MediaType mediaType = MediaType.parse("application/json");RequestBody body = RequestBody.create(mediaType, "{\n" +"    \"speech_url\": \"" + speechUrl + "\",\n" +"    \"format\": \"mp3\",\n" +"    \"pid\": 80001,\n" +"    \"rate\": 16000\n" +"}");Request request = new Request.Builder().url("https://aip.baidubce.com/rpc/2.0/aasr/v1/create?access_token=" + getAccessToken()).post(body).addHeader("Content-Type", "application/json").addHeader("Accept", "application/json").build();try (Response response = HTTP_CLIENT.newCall(request).execute()) {if (response.isSuccessful()) {String responseBody = response.body().string();log.info("创建ASR任务响应: {}", responseBody);String taskId = JSON.parseObject(responseBody).getString("task_id");return taskId;} else {log.info("创建ASR任务失败: {}", response.code());return null;}}}private static String checkASRStatus(String taskId) throws IOException {if (StringUtils.isEmpty(taskId)) {return "";}String taskStatus = "";while (!("Success".equals(taskStatus) || "Failed".equals(taskStatus))) {// Add a delay before the next querytry {Thread.sleep(3000); // Sleep for 3 seconds (adjust as needed)} catch (InterruptedException e) {e.printStackTrace();}HashMap<String, Object> map = new HashMap<>();ArrayList<Object> list = new ArrayList<>();list.add(taskId);map.put("task_ids", list);JSONObject jsonObject = new JSONObject(map);String toJSON = jsonObject.toString();MediaType mediaType = MediaType.parse("application/json");RequestBody body = RequestBody.create(mediaType, toJSON);Request request = new Request.Builder().url("https://aip.baidubce.com/rpc/2.0/aasr/v1/query?access_token=" + getAccessToken()).method("POST", body).addHeader("Content-Type", "application/json").addHeader("Accept", "application/json").build();try (Response response = HTTP_CLIENT.newCall(request).execute()) {if (response.isSuccessful()) {String responseBody = response.body().string();log.info("创建AACR任务响应: {}", responseBody);JSONArray tasksInfo = JSON.parseObject(responseBody).getJSONArray("tasks_info");if (tasksInfo.size() > 0) {com.alibaba.fastjson.JSONObject taskInfo = tasksInfo.getJSONObject(0);taskStatus = taskInfo.getString("task_status");if ("Success".equals(taskStatus)) {String resultText = taskInfo.getJSONObject("task_result").getString("result");log.info("语音转文字: {}", resultText);return resultText;} else if ("Failed".equals(taskStatus)) {log.info("任务失败: {}", taskStatus);}} else {log.info("未找到任务信息");}} else {log.info("检查任务状态失败: {}", response.code());}} catch (IOException e) {e.printStackTrace();}}return "";}/*** 从用户的AK,SK生成鉴权签名(Access Token)** @return 鉴权签名(Access Token)* @throws IOException IO异常*/static String getAccessToken() throws IOException {MediaType mediaType = MediaType.parse("application/x-www-form-urlencoded");RequestBody body = RequestBody.create(mediaType, "grant_type=client_credentials&client_id=" + API_KEY+ "&client_secret=" + SECRET_KEY);Request request = new Request.Builder().url("https://aip.baidubce.com/oauth/2.0/token").method("POST", body).addHeader("Content-Type", "application/x-www-form-urlencoded").build();Response response = HTTP_CLIENT.newCall(request).execute();String access_token = new JSONObject(response.body().string()).getString("access_token");return access_token;}public static void main(String[] args) throws IOException {checkASRStatus(createAasr("https://openai-demo-1319322874.cos.na-siliconvalley.myqcloud.com/audio/2023-12-05/1701764504763878.mp3"));checkASRStatus(createAasr("https://aipe-speech.bj.bcebos.com/text_to_speech/2024-01-04/65966e5370172800014d6524/speech/0.mp3?authorization=bce-auth-v1%2F8a6ca9b78c124d89bb6bca18c6fc5944%2F2024-01-04T08%3A38%3A04Z%2F259200%2F%2F7ca69d814bacad356114a0ff7573734c5ceb20b363dee6c2e971b16617787ac4"));}}

http://www.lryc.cn/news/273473.html

相关文章:

  • pyparamvalidate 项目背景和需求分析
  • Docker Linux快速安装及Nginx部署
  • Mac M1 Parallels CentOS7.9 Install Parallels Tools
  • 计算机网络物理层 习题答案及解析
  • 【解决】Unity 设置跨设备分辨率表现
  • 基于单片机的智能衣柜设计
  • HttpSession的使用
  • 人工智能在金融领域的应用存在的4大挑战
  • EasyExcel写出包含多个sheet页的Excel
  • 分类预测 | Matlab实现RP-CNN-LSTM-Attention递归图优化卷积长短期记忆神经网络注意力机制的数据分类预测【24年新算法】
  • 【教学类-09-04】20240102《游戏棋N*N》数字填写,制作棋子和骰子
  • 【flink番外篇】9、Flink Table API 支持的操作示例(14)- 时态表的join(java版本)
  • 【leetcode100-30】【链表】两两交换链表节点
  • 小秋SLAM入门实战ubuntu所有文章汇总
  • 深度学习课程实验二深层神经网络搭建及优化
  • Elasticsearch:Serarch tutorial - 使用 Python 进行搜索 (二)
  • 力扣labuladong——一刷day84
  • Linux环境vscode clang-format格式化:vscode clang format command is not available
  • 【KingbaseES】实现MySql函数WEEKS_BETWEEN
  • @Scheduled定时任务现状与改进
  • python+selenium爬虫笔记
  • 【LMM 009】MiniGPT-4:使用 Vicuna 增强视觉语言理解能力的多模态大模型
  • SpringBoot学习(三)-整合JDBC、Druid、MyBatis
  • 如何选择合适的语音呼叫中心?
  • 使用qtquick调用python程序
  • 【Axure高保真原型】树形表格_多选效果
  • 【Filament】加载obj和fbx模型
  • [USACO04OPEN] The Cow Lineup
  • 软件工具集合
  • C#利用openvino部署PP-TinyPose人体姿态识别