更新语音
This commit is contained in:
parent
b575585ea3
commit
4c3e70baf2
@ -17,7 +17,12 @@ namespace app\api\controller;
|
|||||||
use IFlytek\Xfyun\Speech\ChatClient;
|
use IFlytek\Xfyun\Speech\ChatClient;
|
||||||
use IFlytek\Xfyun\Speech\IatClient;
|
use IFlytek\Xfyun\Speech\IatClient;
|
||||||
use IFlytek\Xfyun\Speech\TtsClient;
|
use IFlytek\Xfyun\Speech\TtsClient;
|
||||||
|
use IFlytek\Xfyun\Speech\OcrClient;
|
||||||
use WebSocket\Client;
|
use WebSocket\Client;
|
||||||
|
use GuzzleHttp\Client as GzClient;
|
||||||
|
use GuzzleHttp\Psr7\Request;
|
||||||
|
use GuzzleHttp\Exception\GuzzleException;
|
||||||
|
use Guzzle\Http\Exception\RequestException;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 讯飞
|
* 讯飞
|
||||||
@ -25,7 +30,7 @@ use WebSocket\Client;
|
|||||||
*/
|
*/
|
||||||
class XunFeiController extends BaseApiController
|
class XunFeiController extends BaseApiController
|
||||||
{
|
{
|
||||||
public array $notNeedLogin = ['chat', 'iat', 'tts'];
|
public array $notNeedLogin = ['chat', 'iat', 'tts', 'ocr'];
|
||||||
|
|
||||||
private $app_id='2eda6c2e';
|
private $app_id='2eda6c2e';
|
||||||
|
|
||||||
@ -125,7 +130,7 @@ class XunFeiController extends BaseApiController
|
|||||||
{
|
{
|
||||||
header('X-Accel-Buffering: no');
|
header('X-Accel-Buffering: no');
|
||||||
$file = request()->file('audio');
|
$file = request()->file('audio');
|
||||||
if(empty($file)){
|
if (empty($file)) {
|
||||||
return $this->fail('未上传音频文件');
|
return $this->fail('未上传音频文件');
|
||||||
}
|
}
|
||||||
// 上传音频临时文件
|
// 上传音频临时文件
|
||||||
@ -145,7 +150,7 @@ class XunFeiController extends BaseApiController
|
|||||||
$client = new Client($iat->assembleAuthUrl($iatHostUrl));
|
$client = new Client($iat->assembleAuthUrl($iatHostUrl));
|
||||||
if ($client) {
|
if ($client) {
|
||||||
$frameSize = 1280; //每一帧的音频大小
|
$frameSize = 1280; //每一帧的音频大小
|
||||||
$intervel = 20 * 1000; //发送音频间隔
|
$intervel = 40 * 1000; //发送音频间隔
|
||||||
$status = 0;
|
$status = 0;
|
||||||
while (true) {
|
while (true) {
|
||||||
$len = fread($audioFile, $frameSize);
|
$len = fread($audioFile, $frameSize);
|
||||||
@ -237,7 +242,7 @@ class XunFeiController extends BaseApiController
|
|||||||
header('X-Accel-Buffering: no');
|
header('X-Accel-Buffering: no');
|
||||||
$ttsHostUrl = "wss://tts-api.xfyun.cn/v2/tts";
|
$ttsHostUrl = "wss://tts-api.xfyun.cn/v2/tts";
|
||||||
$text = request()->param('text');
|
$text = request()->param('text');
|
||||||
if(empty($text)){
|
if (empty($text)) {
|
||||||
return $this->fail('未上传文本参数');
|
return $this->fail('未上传文本参数');
|
||||||
}
|
}
|
||||||
$file_name = date('YmdHis', time()) . mt_rand(1000, 9999) . '.mp3';
|
$file_name = date('YmdHis', time()) . mt_rand(1000, 9999) . '.mp3';
|
||||||
@ -260,9 +265,69 @@ class XunFeiController extends BaseApiController
|
|||||||
$tts = new TtsClient($this->app_id, $this->api_key, $this->api_secret, $business);
|
$tts = new TtsClient($this->app_id, $this->api_key, $this->api_secret, $business);
|
||||||
file_put_contents($audioFile, $tts->request($text)->getBody()->getContents());
|
file_put_contents($audioFile, $tts->request($text)->getBody()->getContents());
|
||||||
} catch (\Exception $e) {
|
} catch (\Exception $e) {
|
||||||
return $this->fail($e->getMessage().'');
|
return $this->fail($e->getMessage());
|
||||||
}
|
}
|
||||||
return $this->data(['audio_file' => request()->domain() . '/uploads/audio/' . $date_path . '/' . $file_name]);
|
return $this->data(['audio_file' => request()->domain() . '/uploads/audio/' . $date_path . '/' . $file_name]);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//通用文字识别
|
||||||
|
public function ocr()
|
||||||
|
{
|
||||||
|
$ocrHostUrl = "https://api.xf-yun.com/v1/private/sf8e6aca1";
|
||||||
|
$base64_image = request()->param('base64_image');
|
||||||
|
$file = request()->file('image');
|
||||||
|
if (empty($file)) {
|
||||||
|
return $this->fail('未上传图片文件');
|
||||||
|
}
|
||||||
|
// 上传图片临时文件
|
||||||
|
$savename = \think\facade\Filesystem::putFile('ocr', $file);
|
||||||
|
$file = app()->getRootPath() . '/runtime/storage/' . $savename;
|
||||||
|
if (!file_exists($file)) {
|
||||||
|
return $this->fail('未上传图片文件');
|
||||||
|
}
|
||||||
|
$filesize = filesize($file);
|
||||||
|
if ($filesize > 4 * 1024 * 1024) {
|
||||||
|
return $this->fail('图片文件不能超过4M');
|
||||||
|
}
|
||||||
|
$ext = pathinfo($file, PATHINFO_EXTENSION);
|
||||||
|
$base64_image = base64_encode(file_get_contents($file));
|
||||||
|
$ocr = new OcrClient($this->app_id, $this->api_key, $this->api_secret);
|
||||||
|
$authorization = $ocr->assembleAuthorization($ocrHostUrl);
|
||||||
|
$requestBody = "{
|
||||||
|
'header': {
|
||||||
|
'app_id': '{$this->app_id}',
|
||||||
|
'status': 3
|
||||||
|
},
|
||||||
|
'parameter': {
|
||||||
|
'sf8e6aca1': {
|
||||||
|
'category': 'ch_en_public_cloud',
|
||||||
|
'result': {
|
||||||
|
'encoding': 'utf8',
|
||||||
|
'compress': 'raw',
|
||||||
|
'format': 'json'
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
'payload': {
|
||||||
|
'sf8e6aca1_data_1': {
|
||||||
|
'encoding': '{$ext}',
|
||||||
|
'status': 3,
|
||||||
|
'image': '{$base64_image}'
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}";
|
||||||
|
$ocrHostUrl .= '?authorization=' . $authorization;
|
||||||
|
try {
|
||||||
|
$client = new GzClient(['timeout' => 2]);
|
||||||
|
$response = $client->request('POST', $ocrHostUrl, [
|
||||||
|
'json' => json_decode($requestBody, true),
|
||||||
|
'verify' => false
|
||||||
|
]);
|
||||||
|
$responseData = $response->getBody()->getContents();
|
||||||
|
return $this->data(['words' => (string)$responseData]);
|
||||||
|
} catch (GuzzleException $e) {
|
||||||
|
return $this->fail($e->getMessage());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -46,7 +46,7 @@ class IatClient
|
|||||||
$this->client = new HttpClient([]);
|
$this->client = new HttpClient([]);
|
||||||
}
|
}
|
||||||
|
|
||||||
function assembleAuthUrl($addr,$method='GET') {
|
public function assembleAuthUrl($addr, $method='GET') {
|
||||||
$apiKey=$this->apiKey;
|
$apiKey=$this->apiKey;
|
||||||
$apiSecret=$this->apiSecret;
|
$apiSecret=$this->apiSecret;
|
||||||
if ($apiKey == "" && $apiSecret == "") { // 不鉴权
|
if ($apiKey == "" && $apiSecret == "") { // 不鉴权
|
||||||
@ -68,7 +68,7 @@ class IatClient
|
|||||||
// 将签名字符串拼接成一个字符串
|
// 将签名字符串拼接成一个字符串
|
||||||
$sgin = implode("\n", $signString);
|
$sgin = implode("\n", $signString);
|
||||||
// 对签名字符串进行HMAC-SHA256加密,得到签名结果
|
// 对签名字符串进行HMAC-SHA256加密,得到签名结果
|
||||||
$sha = hash_hmac('sha256', $sgin, $apiSecret,true);
|
$sha = hash_hmac('sha256', $sgin, $apiSecret, true);
|
||||||
|
|
||||||
$signature_sha_base64 = base64_encode($sha);
|
$signature_sha_base64 = base64_encode($sha);
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user