MATLAB使用百度API-语音转文字

前面用MATLAB利用百度API进行人脸识别,给颜值打分,点这里看

今天用百度API,主要是免费吧,实现语音转文字的一个小功能。先看一个截图。

 

Access_Token这个参数怎么获取,我在前一个博客里面已经写了,所以就不重新写了。不会的点这里

(如果大家实在没有我已经在CSDN上传了我的代码,里面有一个公共的id和密码,大家可以直接用)

API里的参数设置,大家可以参考官方给的一个文档,python文档里面

然后MATLAB里面也是差不多该设置的都设置了就行了,然后就是像爬虫一样用webwrite这个函数,大家看MATLAB帮助里面有一个API的案例可以参考。下面我给出我的MATLAB主代码:

clear;clc
% Doc:
% https://github.com/Baidu-AIP/speech-demo
% https://ai.baidu.com/ai-doc/SPEECH/Vk38lxily
% code: 
% client_id = 'your id';
% client_secret = 'your secret';
Host = webread( 'https://openapi.baidu.com/oauth/2.0/token?grant_type=client_credentials&client_id=*写你的*&client_secret=*写你的*' );
Access_Token = Host.access_token;
request_url = 'http://vop.baidu.com/server_api';

[ Data, Freq] = audioread('Audio_test.aac');
newFreq = 16000;
[ P, Q ] = rat( newFreq/Freq );
Data = resample( Data, P, Q );

wavFilename = 'WavFile.wav';
audiowrite(wavFilename, Data, newFreq);

[base64string, base64len]= base64file('WavFile.wav');
options = weboptions('RequestMethod', 'post','HeaderFields',{ 'Content-Type','application/json'});
options.Timeout = 10;
Webpar = struct;
Webpar.format = 'wav';
Webpar.token = Access_Token;
Webpar.len = base64len;
Webpar.rate = 16000;
Webpar.speech = base64string;
Webpar.cuid = 'CUID';
Webpar.channel = 1;
Content = webwrite(request_url,Webpar,options);
disp(Content.result{:})


另外还有一个小函数,这个是Base64file的一个转化,来自一个网上的大神

function [base64string,lens]= base64file(file)
%BASE64FILE encode a file in base64
%
% base64 = base64file(filename) returns the file's contents as a
%  base64-encoded string
%
% This file uses the base64 encoder from the Apache Commons Codec, 
% http://commons.apache.org/codec/ and distrubed with MATLAB under the
% Apache License http://commons.apache.org/license.html

% Copyright 2009 The MathWorks, Inc.

fid = fopen(file,'rb');
bytes = fread(fid);
fclose(fid);
lens = size(bytes,1);
encoder = org.apache.commons.codec.binary.Base64;
base64string = char(encoder.encode(bytes))';

大家学废了嘛。

 

可以玩玩,毕竟免费,拜了个拜。

 

  • 5
    点赞
  • 46
    收藏
    觉得还不错? 一键收藏
  • 7
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值