|
|
<?php
|
|
|
|
|
|
namespace tencent\asr;
|
|
|
|
|
|
use TencentCloud\Common\Credential;
|
|
|
use TencentCloud\Common\Profile\ClientProfile;
|
|
|
use TencentCloud\Common\Profile\HttpProfile;
|
|
|
use TencentCloud\Common\Exception\TencentCloudSDKException;
|
|
|
use TencentCloud\Asr\V20190614\AsrClient;
|
|
|
use TencentCloud\Asr\V20190614\Models\SentenceRecognitionRequest;
|
|
|
|
|
|
class ShortRecord extends Base
|
|
|
{
|
|
|
|
|
|
/**
|
|
|
* 一句话识别 (支持wav、mp3格式 对60秒之内的短音频文件进行识别)
|
|
|
* @param string $source_type 语音数据来源。0:语音 URL;1:语音数据(post body)
|
|
|
* @param string $content $source_type = 1语音的URL地址,需要公网可下载。长度小于2048字节,当 SourceType 值为 0 时须填写该字段,为 1 时不需要填写。注意:请确保录音文件时长在5个小时之内,否则可能识别失败。请保证文件的下载速度,否则可能下载失败。
|
|
|
* $source_type = 2语音数据,当SourceType 值为1时必须填写,为0可不写。要base64编码(采用python语言时注意读取文件应该为string而不是byte,以byte格式读取后要decode()。编码后的数据不可带有回车换行符)。音频数据要小于5MB
|
|
|
* @param string $user_audio_key 用户端对此任务的唯一标识,用户自助生成,用于用户查找识别结果。
|
|
|
* @param string $voice_format 识别音频的音频格式。mp3、wav。
|
|
|
* @param string $eng_ser_vice_type 引擎模型类型
|
|
|
* @param integer $data_length 数据长度,单位为字节。当 SourceType 值为1(本地语音数据上传)时必须填写,当 SourceType 值为0(语音 URL上传)可不写(此数据长度为数据未进行base64编码时的数据长度)
|
|
|
* @date 2021-02-18
|
|
|
*/
|
|
|
public function SentenceRecognition($source_type, $content, $user_audio_key, $voice_format = 'mp3', $eng_ser_vice_type = '16k_zh', $data_length = 0)
|
|
|
{
|
|
|
try {
|
|
|
|
|
|
$cred = new Credential($this->config['secret_id'], $this->config['secret_key']);
|
|
|
$httpProfile = new HttpProfile();
|
|
|
$httpProfile->setEndpoint("asr.tencentcloudapi.com");
|
|
|
|
|
|
$clientProfile = new ClientProfile();
|
|
|
$clientProfile->setHttpProfile($httpProfile);
|
|
|
$client = new AsrClient($cred, "", $clientProfile);
|
|
|
|
|
|
$req = new SentenceRecognitionRequest();
|
|
|
|
|
|
$params = array(
|
|
|
"ProjectId" => 0,
|
|
|
"SubServiceType" => 2,
|
|
|
"EngSerViceType" => $eng_ser_vice_type,
|
|
|
"SourceType" => $source_type,
|
|
|
"VoiceFormat" => $voice_format,
|
|
|
"UsrAudioKey" => $user_audio_key,
|
|
|
);
|
|
|
if ($source_type == 0) {
|
|
|
$params['Url'] = $content;
|
|
|
} else if ($source_type == 1) {
|
|
|
$params['Data'] = $content;
|
|
|
$params['DataLen'] = $data_length;
|
|
|
}
|
|
|
$req->fromJsonString(json_encode($params));
|
|
|
|
|
|
$resp = $client->SentenceRecognition($req);
|
|
|
|
|
|
$resp = $resp->toJsonString();
|
|
|
return json_decode($resp, true);
|
|
|
} catch (TencentCloudSDKException $e) {
|
|
|
echo $e;
|
|
|
}
|
|
|
}
|
|
|
|
|
|
}
|