From 4c3e70baf2616cc9d78f7bc3d281c61b6fe36f02 Mon Sep 17 00:00:00 2001 From: yaooo <272523191@qq.com> Date: Tue, 10 Oct 2023 17:52:37 +0800 Subject: [PATCH] =?UTF-8?q?=E6=9B=B4=E6=96=B0=E8=AF=AD=E9=9F=B3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/api/controller/XunFeiController.php | 75 +++++++++++++++++++++-- extend/IFlytek/Xfyun/Speech/IatClient.php | 4 +- 2 files changed, 72 insertions(+), 7 deletions(-) diff --git a/app/api/controller/XunFeiController.php b/app/api/controller/XunFeiController.php index ad5903c36..36be6981e 100644 --- a/app/api/controller/XunFeiController.php +++ b/app/api/controller/XunFeiController.php @@ -17,7 +17,12 @@ namespace app\api\controller; use IFlytek\Xfyun\Speech\ChatClient; use IFlytek\Xfyun\Speech\IatClient; use IFlytek\Xfyun\Speech\TtsClient; +use IFlytek\Xfyun\Speech\OcrClient; 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 { - public array $notNeedLogin = ['chat', 'iat', 'tts']; + public array $notNeedLogin = ['chat', 'iat', 'tts', 'ocr']; private $app_id='2eda6c2e'; @@ -125,7 +130,7 @@ class XunFeiController extends BaseApiController { header('X-Accel-Buffering: no'); $file = request()->file('audio'); - if(empty($file)){ + if (empty($file)) { return $this->fail('未上传音频文件'); } // 上传音频临时文件 @@ -145,7 +150,7 @@ class XunFeiController extends BaseApiController $client = new Client($iat->assembleAuthUrl($iatHostUrl)); if ($client) { $frameSize = 1280; //每一帧的音频大小 - $intervel = 20 * 1000; //发送音频间隔 + $intervel = 40 * 1000; //发送音频间隔 $status = 0; while (true) { $len = fread($audioFile, $frameSize); @@ -237,7 +242,7 @@ class XunFeiController extends BaseApiController header('X-Accel-Buffering: no'); $ttsHostUrl = "wss://tts-api.xfyun.cn/v2/tts"; $text = request()->param('text'); - if(empty($text)){ + if (empty($text)) { return $this->fail('未上传文本参数'); } $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); file_put_contents($audioFile, $tts->request($text)->getBody()->getContents()); } 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]); } + //通用文字识别 + 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()); + } + } + } diff --git a/extend/IFlytek/Xfyun/Speech/IatClient.php b/extend/IFlytek/Xfyun/Speech/IatClient.php index f8e2eae29..d4d6a552e 100644 --- a/extend/IFlytek/Xfyun/Speech/IatClient.php +++ b/extend/IFlytek/Xfyun/Speech/IatClient.php @@ -46,7 +46,7 @@ class IatClient $this->client = new HttpClient([]); } - function assembleAuthUrl($addr,$method='GET') { + public function assembleAuthUrl($addr, $method='GET') { $apiKey=$this->apiKey; $apiSecret=$this->apiSecret; if ($apiKey == "" && $apiSecret == "") { // 不鉴权 @@ -68,7 +68,7 @@ class IatClient // 将签名字符串拼接成一个字符串 $sgin = implode("\n", $signString); // 对签名字符串进行HMAC-SHA256加密,得到签名结果 - $sha = hash_hmac('sha256', $sgin, $apiSecret,true); + $sha = hash_hmac('sha256', $sgin, $apiSecret, true); $signature_sha_base64 = base64_encode($sha);