百度语音识别的springboot应用

1、pom依赖

<dependency>

<groupId>com.baidu.aip</groupId>

<artifactId>java-sdk</artifactId>

<version>4.16.18</version>

</dependency>

2、测试的demo

创建语音识别应用

百度智能云-管理中心 (baidu.com)

代码中要配置

复制代码
package com.zbIntel.integration.utils;

import com.baidu.aip.speech.AipSpeech;
import com.baidu.aip.util.Util;
import org.json.JSONObject;

import java.io.IOException;

public class Sample {
    //设置APPID/AK/SK
    public static final String APP_ID = "";
    public static final String API_KEY = "";
    public static final String SECRET_KEY = "";

    private static final String FILENAME = "D:\\project\\speech-demo-master\\rest-api-asr\\java\\16k.wav";

    public static void main(String[] args) throws IOException {
        // 初始化一个AipSpeech
        AipSpeech client = new AipSpeech(APP_ID, API_KEY, SECRET_KEY);

        // 可选:设置网络连接参数
        client.setConnectionTimeoutInMillis(2000);
        client.setSocketTimeoutInMillis(60000);

        // 可选:设置代理服务器地址, http和socket二选一,或者均不设置
        // client.setHttpProxy("proxy_host", proxy_port);  // 设置http代理
        // client.setSocketProxy("proxy_host", proxy_port);  // 设置socket代理

        // 可选:设置log4j日志输出格式,若不设置,则使用默认配置
        // 也可以直接通过jvm启动参数设置此环境变量
        System.setProperty("aip.log4j.conf", "path/to/your/log4j.properties");

        // 调用接口
        JSONObject res = client.asr("D:\\project\\mygpt\\src\\main\\resources\\iat\\16k_10.pcm", "pcm", 16000, null);
        System.out.println(res.toString(2));


        // 对本地语音文件进行识别

        String path = "D:\\project\\mygpt\\src\\main\\resources\\iat\\16k_10.pcm";
        JSONObject asrRes = client.asr(FILENAME, "pcm", 16000, null);
        System.out.println(asrRes);

        // 对语音二进制数据进行识别
        byte[] data = Util.readFileByBytes(path);     //readFileByBytes仅为获取二进制数据示例
        JSONObject asrRes2 = client.asr(data, "pcm", 16000, null);
        System.out.println(asrRes2);

        
    }
}

3、创建Service 支持语音转文字

复制代码
package com.zbIntel.integration.yuyin;

import com.baidu.aip.speech.AipSpeech;
import org.json.JSONObject;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

@Service
public class BaiduSpeechService {

    public static final String APP_ID = "";
    public static final String API_KEY = "";
    public static final String SECRET_KEY = "";

    private AipSpeech client;
 
    public BaiduSpeechService() {
        // 设置APPID/API Key/Secret Key
        client = new AipSpeech(APP_ID, API_KEY, SECRET_KEY);
    }
 
    public Map<String, Object> recognize(String filePath) {
        // 调用百度语音识别接口

        client.setConnectionTimeoutInMillis(2000);
        client.setSocketTimeoutInMillis(60000);

        HashMap<String, Object> options = new HashMap<>();
        options.put("dev_pid", 1537);
        JSONObject res = client.asr(filePath, "wav", 16000, options);
        Map<String, Object> resultMap = new HashMap<>();
        if (res.get("result") != null) {
            resultMap.put("result", res.get("result"));
            return resultMap;
        }
        return resultMap;
    }
}

3、创建controller 支持上传音频文件

复制代码
package com.zbIntel.integration.controller;

import com.zbIntel.integration.utils.ReturnResult;
import com.zbIntel.integration.yuyin.BaiduSpeechService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.Map;

@RestController
public class SpeechController {
 
    @Autowired
    private BaiduSpeechService speechService;
 

    private static final String UPLOAD_DIR = "/tmp/upload/directory"; // 指定上传文件的保存目录

    @PostMapping("/speech/recognize")
    public ReturnResult recognizeSpeech(@RequestParam("file") MultipartFile file) throws IOException {
        // 将上传的文件保存到服务器,调用语音识别服务
        String filePath = saveFile(file);
        try {
            Map<String, Object> text = speechService.recognize(filePath);
            return ReturnResult.ok().data(text);
        } finally {
            // 确保即使识别过程中出现异常,文件也能被删除
            deleteFile(filePath);
        }
    }

    private String saveFile(MultipartFile file) throws IOException {
        String fileName = file.getOriginalFilename();
        String filePath = UPLOAD_DIR + File.separator + fileName; // 构建完整的文件路径
        File dest = new File(filePath);
        // 确保目录存在
        if (!dest.getParentFile().exists()) {
            dest.getParentFile().mkdirs();
        }
        try (FileOutputStream outputStream = new FileOutputStream(dest)) {
            outputStream.write(file.getBytes());
        }
        return filePath;
    }

    private void deleteFile(String filePath) {
        try {
            Files.delete(Paths.get(filePath));
        } catch (IOException e) {
            // 记录日志或者处理删除失败的情况
            System.err.println("Failed to delete file: " + filePath);
            e.printStackTrace();
        }
    }

}

4、创建页面 支持 录音 上报 音频文件 等 待开展

相关推荐
BFT白芙堂几秒前
Franka Research 3 进阶应用:基于神经网络的 ORACLE 交互控制策略深度解析
人工智能·深度学习·神经网络·oracle·机器人·人机交互·vr
智算菩萨2 分钟前
自然语言处理常用Python库:spaCy使用全解
人工智能·python·自然语言处理
Katecat996634 分钟前
【工业视觉检测】基于YOLOv8的皮带输送机关键部件检测与识别系统完整实现
人工智能·yolo·视觉检测
2401_841495645 分钟前
【自然语言处理】自然语言处理(NLP)的全景应用:从生活便利到产业革新的全维度渗透
人工智能·自然语言处理·大语言模型·多模态融合·统计学习·规则驱动·通用语言智能
deephub6 分钟前
ONNX Runtime Python 推理性能优化:8 个低延迟工程实践
开发语言·人工智能·python·神经网络·性能优化·onnx
AdMergeX7 分钟前
AdMergeX旗下 Funlink SDK通过中国信通院双端安全专项检验
大数据·人工智能·安全·saas·广告saas·流量变现
大模型实验室Lab4AI7 分钟前
Qwen-Video-8B与LLaMA-Factory联动实现垂类视频理解
人工智能·音视频·llama
AI营销资讯站9 分钟前
原圈科技引领AI营销内容生产平台革新,技术与行业高度适配
人工智能
艾莉丝努力练剑10 分钟前
【Linux进程(四)】深入理解 Linux O(1) 调度器:双队列轮转与进程优先级机制——如何避免进程饥饿,实现公平且高效的进程调度
java·大数据·linux·运维·服务器·人工智能·安全
智驱力人工智能11 分钟前
守护生命的水上之眼 无人机人员落水检测系统的技术攻坚与应用实践 无人机溺水识别 山区水库无人机落水检测系统 水域安全无人机部署指南
大数据·人工智能·算法·安全·无人机·边缘计算