更新ocr图片识别

This commit is contained in:
yaooo 2023-10-11 15:04:34 +08:00
parent 1c0482a76a
commit 7b9296039c

View File

@ -166,7 +166,6 @@ class XunFeiController extends BaseApiController
$iatHostUrl = "wss://iat-api.xfyun.cn/v2/iat";
$iat = new IatClient($this->app_id, $this->api_key, $this->api_secret);
$client = new Client($iat->assembleAuthUrl($iatHostUrl));
if ($client) {
$frameSize = 1280; //每一帧的音频大小
$intervel = 20 * 1000; //发送音频间隔
$status = 0;
@ -246,17 +245,10 @@ class XunFeiController extends BaseApiController
break;
}
}
} else {
// 删除临时音频文件
if (file_exists($file)) {
//unlink($file);
}
return $this->fail('无法连接到 WebSocket 服务器');
}
fclose($audioFile);
// 删除临时音频文件
if (file_exists($file)) {
//unlink($file);
unlink($file);
}
} catch (\Exception $e) {
if (file_exists($file)) {
@ -334,6 +326,10 @@ class XunFeiController extends BaseApiController
}
$ext = pathinfo($file, PATHINFO_EXTENSION);
$base64_image = base64_encode(file_get_contents($file));
if (file_exists($file)) {
// 删除临时文件
unlink($file);
}
$ocr = new OcrClient($this->app_id, $this->api_key, $this->api_secret);
$ocrHostUrl = $ocr->assembleAuthUrl($ocrHostUrl);
$requestBody = [
@ -359,7 +355,7 @@ class XunFeiController extends BaseApiController
]
]
];
$responseData = '';
$text = [];
try {
$client = new GzClient(['timeout' => 2]);
$response = $client->request('POST', $ocrHostUrl, [
@ -367,18 +363,20 @@ class XunFeiController extends BaseApiController
'verify' => false
]);
$responseData = $response->getBody()->getContents();
if (file_exists($file)) {
// 删除临时文件
unlink($file);
$responseArray = json_decode($responseData, true);
if (empty($responseArray['payload']['result']['text'])) {
return $this->fail('json解析错误');
}
$encodeText = $responseArray['payload']['result']['text'];
$textArray = json_decode(base64_decode($encodeText), true);
$lineArray = $textArray['pages'][0]['lines'] ?? [];
foreach($lineArray as $item) {
$text[] = $item['words'][0]['content'] ?? '';
}
} catch (GuzzleException $e) {
if (file_exists($file)) {
// 删除临时文件
unlink($file);
}
return $this->fail($e->getMessage());
}
return $this->data(['words' => (string)$responseData]);
return $this->data(['words' => $text]);
}
}