基于腾讯api 语言识别

功能: 录音文件的识别

准备: https://cloud.tencent.com/document/product/1093/35800 注册帐号, 申请 SECTET_Id, SECRET_KEY

1. 发送请求

import org.apache.commons.codec.binary.Base64;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.ByteArrayEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import javax.crypto.Mac;
import javax.crypto.spec.SecretKeySpec;
import java.io.*;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.text.MessageFormat;
import java.util.HashMap;
/*
https://cloud.tencent.com/document/product/1093/35800
 */
public class TengXunYuYinToTxt {
    private static final String HMAC_SHA1_ALGORITHM = "HmacSHA1";

    private static final String APP_ID = "xxxxxxx";
    private static final String SECTET_Id = "xxxxx";
    private static final String SECRET_KEY = "xxxxxxx";

    private static final String CALLBACK_URL = "http://域名或公网ip/tengxu/voice/callback2";

    private static String genHMAC(String data) {
        byte[] result = null;
        try {
            SecretKeySpec signinKey = new SecretKeySpec(SECRET_KEY.getBytes(), HMAC_SHA1_ALGORITHM);
            Mac mac = Mac.getInstance(HMAC_SHA1_ALGORITHM);
            mac.init(signinKey);
            byte[] rawHmac = mac.doFinal(data.getBytes());
            result = Base64.encodeBase64(rawHmac);
        } catch (NoSuchAlgorithmException e) {
            System.err.println(e.getMessage());
        } catch (InvalidKeyException e) {
            System.err.println(e.getMessage());
        }
        if (null != result) {
            return new String(result);
        } else {
            return null;
        }
    }

    /*
    参数参考: https://cloud.tencent.com/document/product/1093/35800#sign
     */
    private static String packageUrl(HashMap<String, String> params){
        StringBuilder sb = new StringBuilder("aai.qcloud.com/asr/v1/");
        sb.append(APP_ID);
        // key 要按照字典排序
        sb.append(MessageFormat.format("?callback_url={0}", params.get("callback_url")));
        sb.append(MessageFormat.format("&channel_num={0}", params.get("channel_num")));
        sb.append(MessageFormat.format("&engine_model_type={0}", params.get("engine_model_type")));
        sb.append(MessageFormat.format("&expired={0}", params.get("expired")));
        sb.append(MessageFormat.format("&nonce={0}", params.get("nonce")));
        sb.append(MessageFormat.format("&projectid={0}", params.get("projectid")));
        sb.append(MessageFormat.format("&res_text_format={0}", params.get("res_text_format")));
        sb.append(MessageFormat.format("&res_type={0}", params.get("res_type")));
        sb.append(MessageFormat.format("&secretid={0}", params.get("secretid")));
        sb.append(MessageFormat.format("&source_type={0}", params.get("source_type")));
        sb.append(MessageFormat.format("&sub_service_type={0}", params.get("sub_service_type")));
        sb.append(MessageFormat.format("&timestamp={0}", params.get("timestamp")));

        return sb.toString();
    }

    public static String postVoice(String requestUrl, String host, String authorization, String contentType, int contentLength, byte[] voice){
        org.apache.http.client.HttpClient client = new DefaultHttpClient();
        HttpPost post = new HttpPost(requestUrl);
        String result = "";
        try {
            post.setHeader("Host", host);
            post.setHeader("Authorization", authorization);
            post.setHeader("Content-Type", contentType);
            // post.setHeader("Content-Length", String.valueOf(contentLength));
            post.setEntity(new ByteArrayEntity(voice));
            org.apache.http.HttpResponse httpResponse = client.execute(post);
            InputStream inStream = httpResponse.getEntity().getContent();
            BufferedReader reader = new BufferedReader(new InputStreamReader(inStream, "utf-8"));
            StringBuilder strber = new StringBuilder();
            String line = null;

            while ((line = reader.readLine()) != null)
                strber.append(line);
            inStream.close();

            result = strber.toString();
            System.out.println(result);

            if (httpResponse.getStatusLine().getStatusCode() == org.apache.http.HttpStatus.SC_OK) {
                System.out.println("请求服务器成功,做相应处理");
            } else {
                System.out.println("请求服务端失败");
            }

        } catch (Exception e) {
            System.out.println("请求异常");
            throw new RuntimeException(e);
        }
        return result;
    }

    public static void main(String args[]) throws IOException{
        HashMap<String, String> params = new HashMap();
        params.put("projectid", "0");
        params.put("secretid", SECTET_Id);
        params.put("sub_service_type", "0");
        params.put("engine_model_type", "8k_0");
        params.put("res_text_format", "0");
        params.put("res_type", "0");
        params.put("callback_url", CALLBACK_URL);
        params.put("channel_num", "1");
        params.put("source_type", "1");
        // params.put("url", "https://xuhai2-1255824371.cos.ap-chengdu.myqcloud.com/test.wav");
        params.put("timestamp", String.valueOf(System.currentTimeMillis()/1000));
        params.put("expired", String.valueOf(System.currentTimeMillis()/1000 + 1000));
        params.put("nonce", "3456");

        String host = "aai.qcloud.com";
        String requestUrl = "https://" + packageUrl(params);
        String authorization = genHMAC("POST" + packageUrl(params));
        System.out.println("requestUrl:"+requestUrl);
        String contentType = "application/octet-stream";

        // File file = new File("/home/eiffel/下载/15210211282_1555127898776.mp3");
        File file = new File("/home/eiffel/下载/test.wav");
        int contentLength = (int)file.length();
        FileInputStream fileInputStream = new FileInputStream(file);
        DataInputStream dataInputStream = new DataInputStream(fileInputStream);
        byte[] voice = new byte[contentLength];
        dataInputStream.read(voice);
        postVoice(requestUrl, host, authorization, contentType, contentLength, voice);
    }
}

2. 回调等待

import com.alibaba.fastjson.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*;
import java.net.URLDecoder;

@RestController
public class TengxuController {
    private static Logger logger = LoggerFactory.getLogger(TengxuController.class.getName());

    private static JSONObject returnReult = new JSONObject();
    static {
        returnReult.put("code", 0);
        returnReult.put("message", "ok");
    }

    @PostMapping("/tengxu/voice/callback2")
    public JSONObject callBack2(@RequestParam(name = "code", required = false) String code,
                            @RequestParam(name = "message", required = false) String message,
                            @RequestParam(name = "requestId", required = false) String requestId,
                            @RequestParam(name = "appid", required = false) String appid,
                            @RequestParam(name = "projectid", required = false) String projectid,
                            @RequestParam(name = "audioUrl", required = false) String audioUrl,
                            @RequestParam(name = "text", required = false) String text,
                            @RequestParam(name = "audioTime", required = false) String audioTime) throws Exception{
        logger.info(code);
        logger.info(message);
        logger.info(requestId);
        logger.info(appid);
        logger.info(projectid);
        logger.info(audioUrl);
        logger.info("text:" + text);
        logger.info("text utg-8:" + URLDecoder.decode(text, "UTF-8"));
        logger.info(audioTime);
        return returnReult;
    }
}

注意事项!!!:

1.计算签名的时候, 对应的 key 要按照字典顺序排序;

2. 回传的文本可能会有乱码, 这是清查看对应的编码res_txt_format, 这个编码要和回调服务默认的编码一直, 要不然需要单独解码处理

3. 腾讯的api, 识别率还是可以的

 

补充: 回调地址可以使用:http://api.online-service.vip/tengxu/voice/callback

可以通过 requestId appid 查询结果: ex: http://api.online-service.vip/tengxu/voice/get?requestId=5119058dd&appid=1233

结果一旦查询成功将会删除, 不支持二次查询。

 

weixin:13257734257

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值