package com.supervision.util; import cn.hutool.core.codec.Base64; import cn.hutool.core.util.ObjectUtil; import cn.hutool.http.HttpUtil; import cn.hutool.json.JSONUtil; import com.fasterxml.jackson.core.type.TypeReference; import com.fasterxml.jackson.databind.ObjectMapper; import com.supervision.exception.BusinessException; import com.supervision.pojo.paddlespeech.req.AsrReqDTO; import com.supervision.pojo.paddlespeech.res.AsrResultDTO; import com.supervision.pojo.paddlespeech.res.PaddleSpeechResDTO; import org.springframework.core.env.Environment; public class AsrUtil { private static final String ASR_URL = SpringBeanUtil.getBean(Environment.class).getProperty("paddle-speech.asr"); private static final ObjectMapper objectMapper = new ObjectMapper(); public static String asrTransformByBytes(byte[] bytes) { // 首先编码为base64编码 String encode = Base64.encode(bytes); return asrTransformByBytes(encode); } public static String asrTransformByBytes(String voiceBase64){ // 这里调用Python的接口,将文字转换为语音 String post = HttpUtil.post(ASR_URL, JSONUtil.toJsonStr(new AsrReqDTO(voiceBase64))); try { PaddleSpeechResDTO response = objectMapper.readValue(post, new TypeReference>() { }); if (!response.getSuccess() || ObjectUtil.isEmpty(response.getResult())) { throw new BusinessException("语音转换文字失败"); } return response.getResult().getTranscription(); } catch (Exception e) { throw new BusinessException("语音转换文字失败"); } } }