使用PHP SDK图片OCR接口识别图片中的文字或卡证信息,并同步返回识别出来的结果。
背景信息
通用OCR除了能够识别普通图片中的文字,还能识别结构化的卡证上的文字,具体请参见图片OCR检测API说明。
准备工作
在进行具体的服务调用之前,请完成以下准备工作:
- 创建阿里云AccessKey。具体请参见创建AccessKey。
- 安装PHP依赖。具体请参见安装PHP依赖。
提交图片同步检测任务
接口 | 描述 | 支持的Region |
---|---|---|
ImageSyncScanRequest | 提交图片OCR同步识别任务,对图片中的文字进行识别(scene=ocr)。 |
|
示例代码
- 传图片URL进行检测
<?php require_once 'aliyuncs/aliyun-oss-php-sdk/autoload.php'; include_once 'aliyuncs/aliyun-php-sdk-core/Config.php'; use Green\Request\V20180509 as Green; // 请替换成您的AccessKey信息。 $iClientProfile = DefaultProfile::getProfile("cn-shanghai", "yourAccessKeyId", "yourAccessKeySecret"); DefaultProfile::addEndpoint("cn-shanghai", "cn-shanghai", "Green", "green.cn-shanghai.aliyuncs.com"); $client = new DefaultAcsClient($iClientProfile); $request = new Green\ImageSyncScanRequest(); $request->setMethod("POST"); $request->setAcceptFormat("JSON"); $task1 = array('dataId' => uniqid(), 'url' => 'https://身份证图片.jpg' ); $extras = array('card' => 'id-card-front'); // 设置待检测的图片,一张图片对应一个检测任务。 // 多张图片同时检测时,处理时间由最后一张处理完的图片决定。 // 通常情况下批量检测的平均响应时间比单张检测要长。一次批量提交的图片数越多,响应时间被拉长的概率越高。 // 代码中以单张图片检测作为示例,如果需要批量检测多张图片,请自行构建多个检测任务。 // OCR检测按照实际检测的图片张数*检测的卡证类型单价计费。 $request->setContent(json_encode(array( "tasks" => array($task1), "scenes" => array("ocr"), "extras" => array($extras) ))); try { $response = $client->getAcsResponse($request); # print_r($response); if(200 == $response->code){ $taskResults = $response->data; foreach ($taskResults as $taskResult) { if (200 == $taskResult->code) { $sceneResults = $taskResult->results; print_r($sceneResults); } } }else{ print_r("detect not success. code:" + $response->code); } } catch (Exception $e) { print_r($e); }
- 传本地图片文件进行检测
<?php require_once 'aliyuncs/aliyun-oss-php-sdk/autoload.php'; include_once 'aliyuncs/aliyun-php-sdk-core/Config.php'; use Green\Request\V20180509 as Green; use Green\Request\Extension\ClientUploader; // 请替换成您的AccessKey信息。 $iClientProfile = DefaultProfile::getProfile("cn-shanghai", "yourAccessKeyId", "yourAccessKeySecret"); DefaultProfile::addEndpoint("cn-shanghai", "cn-shanghai", "Green", "green.cn-shanghai.aliyuncs.com"); $client = new DefaultAcsClient($iClientProfile); $request = new Green\ImageSyncScanRequest(); $request->setMethod("POST"); $request->setAcceptFormat("JSON"); $uploader = ClientUploader::getImageClientUploader($client); $url = $uploader->uploadFile("d:/身份证图片.png"); $task1 = array('dataId' => uniqid(), 'url' => $url ); $extras = array('card' => 'id-card-front'); // 设置待检测的图片,一张图片对应一个检测任务。 // 多张图片同时检测时,处理时间由最后一张处理完的图片决定。 // 通常情况下批量检测的平均响应时间比单张检测要长。一次批量提交的图片数越多,响应时间被拉长的概率越高。 // 代码中以单张图片检测作为示例,如果需要批量检测多张图片,请自行构建多个检测任务。 // OCR检测按照实际检测的图片张数*检测的卡证类型单价计费。 $request->setContent(json_encode(array( "tasks" => array($task1), "scenes" => array("ocr"), "extras" => array($extras) ))); try { $response = $client->getAcsResponse($request); # print_r($response); if(200 == $response->code){ $taskResults = $response->data; foreach ($taskResults as $taskResult) { if (200 == $taskResult->code) { $sceneResults = $taskResult->results; print_r($sceneResults); } } }else{ print_r("detect not success. code:" + $response->code); } } catch (Exception $e) { print_r($e); }
- 传图片二进制字节组数据进行检测
<?php require_once 'aliyuncs/aliyun-oss-php-sdk/autoload.php'; include_once 'aliyuncs/aliyun-php-sdk-core/Config.php'; use Green\Request\V20180509 as Green; use Green\Request\Extension\ClientUploader; // 请替换成您的AccessKey信息。 $iClientProfile = DefaultProfile::getProfile("cn-shanghai", "yourAccessKeyId", "yourAccessKeySecret"); DefaultProfile::addEndpoint("cn-shanghai", "cn-shanghai", "Green", "green.cn-shanghai.aliyuncs.com"); $client = new DefaultAcsClient($iClientProfile); $request = new Green\ImageSyncScanRequest(); $request->setMethod("POST"); $request->setAcceptFormat("JSON"); $bytes = file_get_contents("d:/身份证图片.jpg"); $uploader = ClientUploader::getImageClientUploader($client); $url = $uploader->uploadBytes($bytes); $task1 = array('dataId' => uniqid(), 'url' => $url ); $extras = array('card' => 'id-card-front'); // 设置待检测的图片,一张图片对应一个检测任务。 // 多张图片同时检测时,处理时间由最后一张处理完的图片决定。 // 通常情况下批量检测的平均响应时间比单张检测要长。一次批量提交的图片数越多,响应时间被拉长的概率越高。 // 代码中以单张图片检测作为示例,如果需要批量检测多张图片,请自行构建多个检测任务。 // OCR检测按照实际检测的图片张数*检测的卡证类型单价计费。 $request->setContent(json_encode(array( "tasks" => array($task1), "scenes" => array("ocr"), "extras" => array($extras) ))); try { $response = $client->getAcsResponse($request); # print_r($response); if(200 == $response->code){ $taskResults = $response->data; foreach ($taskResults as $taskResult) { if (200 == $taskResult->code) { $sceneResults = $taskResult->results; print_r($sceneResults); } } }else{ print_r("detect not success. code:" + $response->code); } } catch (Exception $e) { print_r($e); }
在文档使用中是否遇到以下问题
更多建议
匿名提交