您尚未登录。

楼主 #1 2018-04-30 23:21:01

晕哥
管理员
所在地: 微信 whycan_cn
注册时间: 2017-09-06
已发帖子: 9,223
积分: 9197

简单说一说 baidu 语音合成与识别 api 使用流程

刚刚使用了一下 乐鑫 ESP-ADF 的 baidu 语音合成 demo

https://github.com/espressif/esp-adf/blob/master/examples/cloud_services/pipeline_baidu_speech_mp3/main/play_baidu_speech_mp3_example.c

然后结合网上例程,
梳理一下调用过程

1. 第一步,根据 在 baidu 申请的 API Key 和 Secret Key 获取 tocken (令牌)

API Key: FvNYWABLHgYC3aMMgaO7j5Qh
Secret Key: uc1IRoK4yMBqvAnmizT0ZZ7nR5OgYREU

合成如下面的链接:
https://openapi.baidu.com/oauth/2.0/token?grant_type=client_credentials&client_id=FvNYWABLHgYC3aMMgaO7j5Qh&client_secret=uc1IRoK4yMBqvAnmizT0ZZ7nR5OgYREU

上面的 token 24.1d62c2210aae7e03f3987b4d4579e8d3.2592000.1527812681.282335-11175544 有效期是 一个月(30天)

2. 第二步, 填入上面获取的令牌 和 txt 文字, 生成语音文件。
http://tsn.baidu.com/text2audio?tex=%E6 … 5-11175544


上面的测试大概1个月会失效, 失效后可以用第一步产生的 token 代入 第二步的链接的 tok 参数。




2018-07-10 更新:
------------------------------------------------------
播放: 挖坑网,我的最爱





离线

楼主 #2 2018-04-30 23:26:40

晕哥
管理员
所在地: 微信 whycan_cn
注册时间: 2017-09-06
已发帖子: 9,223
积分: 9197

Re: 简单说一说 baidu 语音合成与识别 api 使用流程

乐鑫的demo其实就是分两步完成上面的动作。





离线

楼主 #3 2018-04-30 23:29:09

晕哥
管理员
所在地: 微信 whycan_cn
注册时间: 2017-09-06
已发帖子: 9,223
积分: 9197

Re: 简单说一说 baidu 语音合成与识别 api 使用流程

QQ20180430232852.png





离线

楼主 #4 2018-05-01 23:14:27

晕哥
管理员
所在地: 微信 whycan_cn
注册时间: 2017-09-06
已发帖子: 9,223
积分: 9197

Re: 简单说一说 baidu 语音合成与识别 api 使用流程

哈, 搞定一个php 语音识别!

参考这个: https://blog.csdn.net/weixin_36429334/article/details/53893824

首先要安装curl:

sudo apt-get install php-curl

语音文件可以从这里下载: http://yuyin.baidu.com/docs/asr/54
http://speech-doc.gz.bcebos.com/rest-api-asr/public_audio/16k.wav

代码里面采样率改成 16000

我改的代码:

root@ubuntu:~# cat test.php
<?php
define('AUDIO_FILE', "./text2audio_1.wav"); //语音文件地址,值支持本地

$url = "http://vop.baidu.com/server_api";

//put your params here
$cuid = "addd";
$apiKey = "FvNYWABLHgYC3aMMgaO7j5Qh";
$secretKey = "uc1IRoK4yMBqvAnmizT0ZZ7nR5OgYREU";

$auth_url = "https://openapi.baidu.com/oauth/2.0/token?grant_type=client_credentials&client_id=".$apiKey."&client_secret=".$secretKey;
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $auth_url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5);
$response = curl_exec($ch);
if(curl_errno($ch))
{
    print curl_error($ch);
}
curl_close($ch);
$response = json_decode($response, true);
$token = $response['access_token'];

$audio = file_get_contents(AUDIO_FILE);
$base_data = base64_encode($audio);
$array = array(
        "format" => "wav",
        "rate" => 16000,
        "channel" => 1,
//        "lan" => "zh",
        "token" => $token,
        "cuid"=> $cuid,
        //"url" => "http://www.xxx.com/sample.pcm",
        //"callback" => "http://www.xxx.com/audio/callback",
        "len" => filesize(AUDIO_FILE),
        "speech" => $base_data,
        );
$json_array = json_encode($array);
$content_len = "Content-Length: ".strlen($json_array);
$header = array ($content_len, 'Content-Type: application/json; charset=utf-8');

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 30);
curl_setopt($ch, CURLOPT_TIMEOUT, 30);
curl_setopt($ch, CURLOPT_POSTFIELDS, $json_array);
$response = curl_exec($ch);
if(curl_errno($ch))
{
    print curl_error($ch);
}
curl_close($ch);
echo $response;
$response = json_decode($response, true);
var_dump($response);

php test.php 运行结果:

root@ubuntu:~# php test.php
{"corpus_no":"6550630004628230343","err_msg":"success.","err_no":0,"result":["北京科技馆,"],"sn":"949063508651525187400"}
array(5) {
  ["corpus_no"]=>
  string(19) "6550630004628230343"
  ["err_msg"]=>
  string(8) "success."
  ["err_no"]=>
  int(0)
  ["result"]=>
  array(1) {
    [0]=>
    string(18) "北京科技馆,"
  }
  ["sn"]=>
  string(21) "949063508651525187400"
}

QQ20180501231414.png
看起来效果不错.





离线

#5 2018-05-02 09:57:30

mkseven32
会员
注册时间: 2018-04-24
已发帖子: 57
积分: 57

Re: 简单说一说 baidu 语音合成与识别 api 使用流程

晕哥 说:

刚刚使用了一下 乐鑫 ESP-ADF 的 baidu 语音合成 demo

https://github.com/espressif/esp-adf/blob/master/examples/cloud_services/pipeline_baidu_speech_mp3/main/play_baidu_speech_mp3_example.c

然后结合网上例程,
梳理一下调用过程

1. 第一步,根据 在 baidu 申请的 API Key 和 Secret Key 获取 tocken (令牌)

API Key: FvNYWABLHgYC3aMMgaO7j5Qh
Secret Key: uc1IRoK4yMBqvAnmizT0ZZ7nR5OgYREU

合成如下面的链接:
https://openapi.baidu.com/oauth/2.0/token?grant_type=client_credentials&client_id=FvNYWABLHgYC3aMMgaO7j5Qh&client_secret=uc1IRoK4yMBqvAnmizT0ZZ7nR5OgYREU

上面的 token 24.1d62c2210aae7e03f3987b4d4579e8d3.2592000.1527812681.282335-11175544 有效期是 一个月(30天)

2. 第二步, 填入上面获取的令牌 和 txt 文字, 生成语音文件。
http://tsn.baidu.com/text2audio?tex=%E6 … 5-11175544


上面的测试大概1个月会失效, 失效后可以用第一步产生的 token 代入 第二步的链接的 tok 参数。


晕哥,API Key,Secret Key 都拿到了。 ESP-ADF 的例程 在make menuconfig 也将其填进去了,

你说到 合成如下下面的链接,  这个 是什么意思? 没看懂? 
如何合成, 合成之后 这个链接有什么用?
需要填到 menuconfig 里面吗?

另外token 是在哪里的?  需要将它放在哪里去吗?

第2步 生成语音文件 是在menuconfig 那里设置? 还是?? 工程中的readme 说得模模糊糊的, 具体步骤 只说到 填 key ~~~~

到了后面 你又做了一个什么php 的 链接 =====》 php 语音识别!  这个又是 什么呢? 对于ESP32 这个文件貌似 和ESP32没有关系~~~ 是服务器端的程序吗?

离线

楼主 #6 2018-05-02 10:01:24

晕哥
管理员
所在地: 微信 whycan_cn
注册时间: 2017-09-06
已发帖子: 9,223
积分: 9197

Re: 简单说一说 baidu 语音合成与识别 api 使用流程

API Key,Secret Key 的目的是用来产生 token,
这两个demo都是分两步走:

1. 通过 API Key,Secret Key 获取 token, 相当于拿到一把钥匙.
2. 通过 token 去百度语音网关 申请服务 比如 语音合成 与 语音识别





离线

楼主 #7 2018-05-02 10:02:49

晕哥
管理员
所在地: 微信 whycan_cn
注册时间: 2017-09-06
已发帖子: 9,223
积分: 9197

Re: 简单说一说 baidu 语音合成与识别 api 使用流程

php 那个demo就当他是一个 esp32开发板就行了, 与网页服务器没有关系, 因为刚好看到网上有一个语音合成的php 代码,我也有一个linux主机,趁着时间缝隙测试了一下,果然没有问题。

php 在上面的 demo 中相当于 python,
只是我python不熟,
所以就直接用php了。





离线

楼主 #8 2018-05-02 17:42:49

晕哥
管理员
所在地: 微信 whycan_cn
注册时间: 2017-09-06
已发帖子: 9,223
积分: 9197

Re: 简单说一说 baidu 语音合成与识别 api 使用流程

刚刚有网友传了一个文件 b.wav, 结果识别效果惨不忍睹.

源码:

<?php
define('AUDIO_FILE', "./b.wav"); //语音文件地址,值支持本地

$url = "http://vop.baidu.com/server_api";

//put your params here
$cuid = "addd";
$apiKey = "FvNYWABLHgYC3aMMgaO7j5Qh";
$secretKey = "uc1IRoK4yMBqvAnmizT0ZZ7nR5OgYREU";

$auth_url = "https://openapi.baidu.com/oauth/2.0/token?grant_type=client_credentials&client_id=".$apiKey."&client_secret=".$secretKey;
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $auth_url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5);
$response = curl_exec($ch);
if(curl_errno($ch))
{
    print curl_error($ch);
}
curl_close($ch);
$response = json_decode($response, true);
$token = $response['access_token'];

$audio = file_get_contents(AUDIO_FILE);
$base_data = base64_encode($audio);
$array = array(
        "format" => "wav",
        "rate" => 8000,
        "channel" => 1,
//        "lan" => "zh",
        "token" => $token,
        "cuid"=> $cuid,
        //"url" => "http://www.xxx.com/sample.pcm",
        //"callback" => "http://www.xxx.com/audio/callback",
        "len" => filesize(AUDIO_FILE),
        "speech" => $base_data,
        );
$json_array = json_encode($array);
$content_len = "Content-Length: ".strlen($json_array);
$header = array ($content_len, 'Content-Type: application/json; charset=utf-8');

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 30);
curl_setopt($ch, CURLOPT_TIMEOUT, 30);
curl_setopt($ch, CURLOPT_POSTFIELDS, $json_array);
$response = curl_exec($ch);
if(curl_errno($ch))
{
    print curl_error($ch);
}
curl_close($ch);
echo $response;
$response = json_decode($response, true);
var_dump($response);

执行结果:
root@ubuntu:~# php test2.php

{"corpus_no":"6550913871967495989","err_msg":"success.","err_no":0,"result":["米,米,哎你好,你好你是网上说的大扎吧,宝贝,我是那个人的爱的,哦你说唉你好吴先生联系就给他呗学生打过四天该你还的上眼点六克的实力不是特别的想你又没给他都说的不想去检查看一下,哦没有,你咋恁闲下午开家长会的时候我没那个医院为咱们学校做了一个免费的公益的感慨像不单压你倒是可以带你飞都说给你个也感慨下我看一下,嗯好的嗯还来的不打给她的人呢好的谢谢再见再减,"],"sn":"976533065391525253493"}
array(5) {
  ["corpus_no"]=>
  string(19) "6550913871967495989"
  ["err_msg"]=>
  string(8) "success."
  ["err_no"]=>
  int(0)
  ["result"]=>
  array(1) {
    [0]=>
    string(531) "米,米,哎你好,你好你是网上说的大扎吧,宝贝,我是那个人的爱的,哦你说唉你好吴先生联系就给他呗学生打过四天该你还的上眼点六克的实力不是特别的想你又没给他都说的不想去检查看一下,哦没有,你咋恁闲下午开家长会的时候我没那个医院为咱们学校做了一个免费的公益的感慨像不单压你倒是可以带你飞都说给你个也感慨下我看一下,嗯好的嗯还来的不打给她的人呢好的谢谢再见再减,"
  }
  ["sn"]=>
  string(21) "976533065391525253493"
}





离线

#9 2018-05-22 20:19:47

zhuacai
会员
注册时间: 2017-12-16
已发帖子: 234
积分: 233.5

Re: 简单说一说 baidu 语音合成与识别 api 使用流程

晕哥,esp8266用什么芯片录音呢

离线

楼主 #10 2018-05-22 20:28:22

晕哥
管理员
所在地: 微信 whycan_cn
注册时间: 2017-09-06
已发帖子: 9,223
积分: 9197

Re: 简单说一说 baidu 语音合成与识别 api 使用流程

通过带录音功能的I2S的声卡芯片, 比如WM8978, ES8388等,
也可以通过spi的数字麦克风芯片。





离线

楼主 #11 2018-05-24 11:24:57

晕哥
管理员
所在地: 微信 whycan_cn
注册时间: 2017-09-06
已发帖子: 9,223
积分: 9197

Re: 简单说一说 baidu 语音合成与识别 api 使用流程

QQ20180524113118.png

<?php
require_once 'AipSpeech.php';

const APP_ID = 'whycan.cn';
const API_KEY = 'FvNYWABLHgYC3aMMgaO7j5Qh';
const SECRET_KEY = 'uc1IRoK4yMBqvAnmizT0ZZ7nR5OgYREU';

$client= new AipSpeech(APP_ID, API_KEY, SECRET_KEY);


$result = $client->synthesis('欢迎来到挖坑网', 'zh', 1, array('vol' => 8,));

// 识别正确返回语音二进制 错误则返回json 参照下面错误码
if(!is_array($result)){
    file_put_contents('/var/www/html/files/audio.mp3', $result);
}

?>

参考网址: 百度语音 接口说明

百度语音合成在线体验: http://yuyin.baidu.com/#try
php sdk 下载地址: https://github.com/Baidu-AIP/php-sdk
php sdk 下载命令: git clone https://github.com/Baidu-AIP/php-sdk.git
执行: php test.php
自动生成 /var/www/html/audio.mp3
试听地址: https://whycan.cn/files/audio.mp3





离线

楼主 #12 2018-05-24 11:54:21

晕哥
管理员
所在地: 微信 whycan_cn
注册时间: 2017-09-06
已发帖子: 9,223
积分: 9197

Re: 简单说一说 baidu 语音合成与识别 api 使用流程

http://yuyin.baidu.com/docs/tts/136

从上面的文档看,
百度语音合成只支持mp3格式文件输出。
不能指定格式输出。





离线

楼主 #13 2018-05-24 16:01:59

晕哥
管理员
所在地: 微信 whycan_cn
注册时间: 2017-09-06
已发帖子: 9,223
积分: 9197

Re: 简单说一说 baidu 语音合成与识别 api 使用流程

<?php
require_once 'AipSpeech.php';

const APP_ID = 'whycan.cn';
const API_KEY = 'FvNYWABLHgYC3aMMgaO7j5Qh';
const SECRET_KEY = 'uc1IRoK4yMBqvAnmizT0ZZ7nR5OgYREU';
const AUDIO_FILE = '/var/www/html/files/audio.mp3';

$client= new AipSpeech(APP_ID, API_KEY, SECRET_KEY);

//printf($_GET["Text"]);
unlink(AUDIO_FILE);
$result = $client->synthesis($_GET['Text'], 'zh', 1, array('vol' => 8,));

// 识别正确返回语音二进制 错误则返回json 参照下面错误码
if(!is_array($result)){
    file_put_contents(AUDIO_FILE, $result);
}

printf('<video width="320" height="240" controls>
  <source src="/files/audio.mp3?%s" type="audio/mpeg">
Your browser does not support audio in video tag.
</video><br /><br /><br />', time());
printf('download: <a href="/files/audio.mp3">audio.mp3</a>');
?>

做了一个简单的测试: 欢迎来到挖坑网的世界

本地命令行测试:

php-cgi -f test1.php "Text=时代"





离线

楼主 #14 2018-05-24 16:41:18

晕哥
管理员
所在地: 微信 whycan_cn
注册时间: 2017-09-06
已发帖子: 9,223
积分: 9197

Re: 简单说一说 baidu 语音合成与识别 api 使用流程

<?php
require_once 'AipSpeech.php';

const APP_ID = 'whycan.cn';
const API_KEY = 'FvNYWABLHgYC3aMMgaO7j5Qh';
const SECRET_KEY = 'uc1IRoK4yMBqvAnmizT0ZZ7nR5OgYREU';
const AUDIO_FILE = '/var/www/html/files/audio.mp3';

$client= new AipSpeech(APP_ID, API_KEY, SECRET_KEY);

//printf($_GET["Text"]);
//printf($_POST["Text"]);

if(isset($_GET["Text"]))
{
        $Text = $_GET["Text"];
}
else if(isset($_POST["Text"]))
{
        $Text = $_POST["Text"];
}

unlink(AUDIO_FILE);
$result = $client->synthesis($Text, 'zh', 1, array('vol' => 8,));

// 识别正确返回语音二进制 错误则返回json 参照下面错误码
if(!is_array($result)){
    file_put_contents(AUDIO_FILE, $result);
}

printf('<video width="320" height="240" controls>
  <source src="/files/audio.mp3?%s" type="audio/mpeg">
Your browser does not support audio in video tag.
</video><br /><br /><br />', time());
printf('download: <a href="/files/audio.mp3">audio.mp3</a>');
?>


<form action="test1.php" method="post">
   <p>Input your text: <input type="text" name="Text" value=<?php echo $Text; ?> /></p>
   <input type="submit" name="submit" value="Submit" />
</form>

QQ20180524164352.png

单行输入框搞定.





离线

#15 2018-05-25 21:40:17

chenqy2018
会员
注册时间: 2018-05-25
已发帖子: 2
积分: 1.5

Re: 简单说一说 baidu 语音合成与识别 api 使用流程

不错

离线

楼主 #16 2018-05-25 21:43:31

晕哥
管理员
所在地: 微信 whycan_cn
注册时间: 2017-09-06
已发帖子: 9,223
积分: 9197

Re: 简单说一说 baidu 语音合成与识别 api 使用流程

http://ai.whycan.cn/

新的测试地址


用百度AI 做了一个文字转语音的小demo, 可以下载语音文件。





离线

#17 2018-07-10 08:46:10

mkseven32
会员
注册时间: 2018-04-24
已发帖子: 57
积分: 57

Re: 简单说一说 baidu 语音合成与识别 api 使用流程

晕哥,make menuconfig 然后 填入 WIFI SSID 和密码  还有下面信息。

API Key: FvNYWABLHgYC3aMMgaO7j5Qh
Secret Key: uc1IRoK4yMBqvAnmizT0ZZ7nR5OgYREU

然后make flash monitor 。

启动ESP32 之后 会有什么效果呢?


另外你的第2步  是什么意思???

2. 第二步, 填入上面获取的令牌 和 txt 文字, 生成语音文件。
http://tsn.baidu.com/text2audio?tex=%E6 … 5-11175544


第二步  具体要做什么? 还是ESP32启动后自己完成了?

离线

楼主 #18 2018-07-10 08:54:48

晕哥
管理员
所在地: 微信 whycan_cn
注册时间: 2017-09-06
已发帖子: 9,223
积分: 9197

Re: 简单说一说 baidu 语音合成与识别 api 使用流程

API Key: FvNYWABLHgYC3aMMgaO7j5Qh
Secret Key: uc1IRoK4yMBqvAnmizT0ZZ7nR5OgYREU

然后make flash monitor 。

启动ESP32 之后 会有什么效果呢?

连上WIFI之后会与baidu的 api 交互,根据你的文字生成mp3文件, esp32播放出来,插上耳机或者接上喇叭可以听到.
第二个问题我得想一下,一段时间没弄了,要恢复一下堆栈。





离线

楼主 #19 2018-07-10 09:00:32

晕哥
管理员
所在地: 微信 whycan_cn
注册时间: 2017-09-06
已发帖子: 9,223
积分: 9197

Re: 简单说一说 baidu 语音合成与识别 api 使用流程

播放: 挖坑网,我的最爱

一楼链接可能系统保存的时候出问题了, 你重新打开上面的。
一楼的演示是方便看到esp32文字转语音的过程,如果跟踪esp32代码反过来推算如何生成的会很迷茫。





离线

#20 2018-08-01 09:21:45

abc3240660
会员
注册时间: 2018-07-31
已发帖子: 100
积分: 100

Re: 简单说一说 baidu 语音合成与识别 api 使用流程

可以不联网 本地用吗

离线

#21 2021-08-04 21:54:28

来日方长
会员
注册时间: 2019-10-25
已发帖子: 24
积分: 20

Re: 简单说一说 baidu 语音合成与识别 api 使用流程

abc3240660 说:

可以不联网 本地用吗

这个ADF自带的例子貌似不能
不过乐鑫这个芯片里边可以支持离线识别 但是句子长了可能不好说
离线语音合成的话 上次看好像是没有的

离线

#22 2021-08-05 15:12:21

szchen2006
会员
注册时间: 2019-10-09
已发帖子: 216
积分: 166.5

Re: 简单说一说 baidu 语音合成与识别 api 使用流程

该评论内容与本帖子无关,鼓励各位坑友积极发言讨论与帖子有关的内容!

离线

  • 不通过:其他

页脚

工信部备案:粤ICP备20025096号 Powered by FluxBB

感谢为中文互联网持续输出优质内容的各位老铁们。 QQ: 516333132, 微信(wechat): whycan_cn (哇酷网/挖坑网/填坑网) service@whycan.cn