更新ocr图片识别
This commit is contained in:
parent
1c0482a76a
commit
7b9296039c
@ -166,97 +166,89 @@ class XunFeiController extends BaseApiController
|
|||||||
$iatHostUrl = "wss://iat-api.xfyun.cn/v2/iat";
|
$iatHostUrl = "wss://iat-api.xfyun.cn/v2/iat";
|
||||||
$iat = new IatClient($this->app_id, $this->api_key, $this->api_secret);
|
$iat = new IatClient($this->app_id, $this->api_key, $this->api_secret);
|
||||||
$client = new Client($iat->assembleAuthUrl($iatHostUrl));
|
$client = new Client($iat->assembleAuthUrl($iatHostUrl));
|
||||||
if ($client) {
|
$frameSize = 1280; //每一帧的音频大小
|
||||||
$frameSize = 1280; //每一帧的音频大小
|
$intervel = 20 * 1000; //发送音频间隔
|
||||||
$intervel = 20 * 1000; //发送音频间隔
|
$status = 0;
|
||||||
$status = 0;
|
while (true) {
|
||||||
while (true) {
|
$len = fread($audioFile, $frameSize);
|
||||||
$len = fread($audioFile, $frameSize);
|
if ($len === false) {
|
||||||
if ($len === false) {
|
break;
|
||||||
break;
|
|
||||||
}
|
|
||||||
if ($len === '') { //文件读取完了
|
|
||||||
$status = 2;
|
|
||||||
}
|
|
||||||
switch ($status) {
|
|
||||||
case 0: //发送第一帧音频,带business 参数
|
|
||||||
$frameData = array(
|
|
||||||
'common' => array(
|
|
||||||
'app_id' => $this->app_id //appid 必须带上,只需第一帧发送
|
|
||||||
),
|
|
||||||
'business' => array( //business 参数,只需一帧发送
|
|
||||||
'language' => 'zh_cn',
|
|
||||||
'domain' => 'iat',
|
|
||||||
'accent' => 'mandarin'
|
|
||||||
),
|
|
||||||
'data' => array(
|
|
||||||
'status' => 0,
|
|
||||||
'format' => 'audio/L16;rate=16000',
|
|
||||||
'audio' => base64_encode($len),
|
|
||||||
'encoding' => $encoding
|
|
||||||
)
|
|
||||||
);
|
|
||||||
$client->send(json_encode($frameData));
|
|
||||||
$status = 1;
|
|
||||||
break;
|
|
||||||
case 1:
|
|
||||||
$frameData = array(
|
|
||||||
'data' => array(
|
|
||||||
'status' => 1,
|
|
||||||
'format' => 'audio/L16;rate=16000',
|
|
||||||
'audio' => base64_encode($len),
|
|
||||||
'encoding' => $encoding
|
|
||||||
)
|
|
||||||
);
|
|
||||||
$client->send(json_encode($frameData));
|
|
||||||
break;
|
|
||||||
case 2:
|
|
||||||
$frameData = array(
|
|
||||||
'data' => array(
|
|
||||||
'status' => 2,
|
|
||||||
'format' => 'audio/L16;rate=16000',
|
|
||||||
'audio' => base64_encode($len),
|
|
||||||
'encoding' => $encoding
|
|
||||||
)
|
|
||||||
);
|
|
||||||
$client->send(json_encode($frameData));
|
|
||||||
break 2;
|
|
||||||
}
|
|
||||||
//模拟音频采样间隔
|
|
||||||
usleep($intervel);
|
|
||||||
}
|
}
|
||||||
while (true) {
|
if ($len === '') { //文件读取完了
|
||||||
$response = $client->receive();
|
$status = 2;
|
||||||
if ($response === null) {
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
$resp = json_decode($response, true);
|
|
||||||
$code = $resp['code'];
|
|
||||||
if ($code != 0) {
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
$message = $resp['message'];
|
|
||||||
$data = $resp['data'];
|
|
||||||
$result = $data['result'];
|
|
||||||
$status = $data['status'];
|
|
||||||
foreach($result['ws'] as $v) {
|
|
||||||
$words .= $v['cw'][0]['w'] ?? '';
|
|
||||||
}
|
|
||||||
if ($status === 2) {
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
} else {
|
switch ($status) {
|
||||||
// 删除临时音频文件
|
case 0: //发送第一帧音频,带business 参数
|
||||||
if (file_exists($file)) {
|
$frameData = array(
|
||||||
//unlink($file);
|
'common' => array(
|
||||||
|
'app_id' => $this->app_id //appid 必须带上,只需第一帧发送
|
||||||
|
),
|
||||||
|
'business' => array( //business 参数,只需一帧发送
|
||||||
|
'language' => 'zh_cn',
|
||||||
|
'domain' => 'iat',
|
||||||
|
'accent' => 'mandarin'
|
||||||
|
),
|
||||||
|
'data' => array(
|
||||||
|
'status' => 0,
|
||||||
|
'format' => 'audio/L16;rate=16000',
|
||||||
|
'audio' => base64_encode($len),
|
||||||
|
'encoding' => $encoding
|
||||||
|
)
|
||||||
|
);
|
||||||
|
$client->send(json_encode($frameData));
|
||||||
|
$status = 1;
|
||||||
|
break;
|
||||||
|
case 1:
|
||||||
|
$frameData = array(
|
||||||
|
'data' => array(
|
||||||
|
'status' => 1,
|
||||||
|
'format' => 'audio/L16;rate=16000',
|
||||||
|
'audio' => base64_encode($len),
|
||||||
|
'encoding' => $encoding
|
||||||
|
)
|
||||||
|
);
|
||||||
|
$client->send(json_encode($frameData));
|
||||||
|
break;
|
||||||
|
case 2:
|
||||||
|
$frameData = array(
|
||||||
|
'data' => array(
|
||||||
|
'status' => 2,
|
||||||
|
'format' => 'audio/L16;rate=16000',
|
||||||
|
'audio' => base64_encode($len),
|
||||||
|
'encoding' => $encoding
|
||||||
|
)
|
||||||
|
);
|
||||||
|
$client->send(json_encode($frameData));
|
||||||
|
break 2;
|
||||||
|
}
|
||||||
|
//模拟音频采样间隔
|
||||||
|
usleep($intervel);
|
||||||
|
}
|
||||||
|
while (true) {
|
||||||
|
$response = $client->receive();
|
||||||
|
if ($response === null) {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
$resp = json_decode($response, true);
|
||||||
|
$code = $resp['code'];
|
||||||
|
if ($code != 0) {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
$message = $resp['message'];
|
||||||
|
$data = $resp['data'];
|
||||||
|
$result = $data['result'];
|
||||||
|
$status = $data['status'];
|
||||||
|
foreach($result['ws'] as $v) {
|
||||||
|
$words .= $v['cw'][0]['w'] ?? '';
|
||||||
|
}
|
||||||
|
if ($status === 2) {
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
return $this->fail('无法连接到 WebSocket 服务器');
|
|
||||||
}
|
}
|
||||||
fclose($audioFile);
|
fclose($audioFile);
|
||||||
// 删除临时音频文件
|
// 删除临时音频文件
|
||||||
if (file_exists($file)) {
|
if (file_exists($file)) {
|
||||||
//unlink($file);
|
unlink($file);
|
||||||
}
|
}
|
||||||
} catch (\Exception $e) {
|
} catch (\Exception $e) {
|
||||||
if (file_exists($file)) {
|
if (file_exists($file)) {
|
||||||
@ -334,6 +326,10 @@ class XunFeiController extends BaseApiController
|
|||||||
}
|
}
|
||||||
$ext = pathinfo($file, PATHINFO_EXTENSION);
|
$ext = pathinfo($file, PATHINFO_EXTENSION);
|
||||||
$base64_image = base64_encode(file_get_contents($file));
|
$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);
|
$ocr = new OcrClient($this->app_id, $this->api_key, $this->api_secret);
|
||||||
$ocrHostUrl = $ocr->assembleAuthUrl($ocrHostUrl);
|
$ocrHostUrl = $ocr->assembleAuthUrl($ocrHostUrl);
|
||||||
$requestBody = [
|
$requestBody = [
|
||||||
@ -359,7 +355,7 @@ class XunFeiController extends BaseApiController
|
|||||||
]
|
]
|
||||||
]
|
]
|
||||||
];
|
];
|
||||||
$responseData = '';
|
$text = [];
|
||||||
try {
|
try {
|
||||||
$client = new GzClient(['timeout' => 2]);
|
$client = new GzClient(['timeout' => 2]);
|
||||||
$response = $client->request('POST', $ocrHostUrl, [
|
$response = $client->request('POST', $ocrHostUrl, [
|
||||||
@ -367,18 +363,20 @@ class XunFeiController extends BaseApiController
|
|||||||
'verify' => false
|
'verify' => false
|
||||||
]);
|
]);
|
||||||
$responseData = $response->getBody()->getContents();
|
$responseData = $response->getBody()->getContents();
|
||||||
if (file_exists($file)) {
|
$responseArray = json_decode($responseData, true);
|
||||||
// 删除临时文件
|
if (empty($responseArray['payload']['result']['text'])) {
|
||||||
unlink($file);
|
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) {
|
} catch (GuzzleException $e) {
|
||||||
if (file_exists($file)) {
|
|
||||||
// 删除临时文件
|
|
||||||
unlink($file);
|
|
||||||
}
|
|
||||||
return $this->fail($e->getMessage());
|
return $this->fail($e->getMessage());
|
||||||
}
|
}
|
||||||
return $this->data(['words' => (string)$responseData]);
|
return $this->data(['words' => $text]);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user