| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444 |
- <?php
- /*
- * Copyright (c) 2017 Baidu.com, Inc. All Rights Reserved
- *
- * Licensed under the Apache License, Version 2.0 (the "License"); you may not
- * use this file except in compliance with the License. You may obtain a copy of
- * the License at
- *
- * Http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
- * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
- * License for the specific language governing permissions and limitations under
- * the License.
- */
- namespace PhpLife\Library;
- require_once 'BaiduVoice/AipBase.php';
- use AipBase;
- use AipImageUtil;
- /**
- * 文字OCR
- */
- class AipOcr extends AipBase{
- /**
- * idcard api url
- * @var string
- */
- private $idcardUrl = 'https://aip.baidubce.com/rest/2.0/ocr/v1/idcard';
-
- /**
- * bankcard api url
- * @var string
- */
- private $bankcardUrl = 'https://aip.baidubce.com/rest/2.0/ocr/v1/bankcard';
-
- /**
- * general api url
- * @var string
- */
- private $generalUrl = 'https://aip.baidubce.com/rest/2.0/ocr/v1/general';
- /**
- * basic general api url
- * @var string
- */
- private $basicGeneralUrl = 'https://aip.baidubce.com/rest/2.0/ocr/v1/general_basic';
- /**
- * web image url
- * @var string
- */
- private $webImageUrl = 'https://aip.baidubce.com/rest/2.0/ocr/v1/webimage';
- /**
- * enhanced general url
- * @var string
- */
- private $enhancedGeneralUrl = 'https://aip.baidubce.com/rest/2.0/ocr/v1/general_enhanced';
- /**
- * @var string
- */
- private $drivingLicenseUrl = 'https://aip.baidubce.com/rest/2.0/ocr/v1/driving_license';
- /**
- * @var string
- */
- private $vehicleLicenseUrl = 'https://aip.baidubce.com/rest/2.0/ocr/v1/vehicle_license';
- /**
- * @var string
- */
- private $tableRequestUrl = 'https://aip.baidubce.com/rest/2.0/solution/v1/form_ocr/request';
- /**
- * @var string
- */
- private $tableResultUrl = 'https://aip.baidubce.com/rest/2.0/solution/v1/form_ocr/get_request_result';
- /**
- * @var string
- */
- private $licensePlateUrl = 'https://aip.baidubce.com/rest/2.0/ocr/v1/license_plate';
- /**
- * @var string
- */
- private $accurateUrl = 'https://aip.baidubce.com/rest/2.0/ocr/v1/accurate';
- /**
- * @var string
- */
- private $basicAccurateUrl = 'https://aip.baidubce.com/rest/2.0/ocr/v1/accurate_basic';
- /**
- * @var string
- */
- private $receiptUrl = 'https://aip.baidubce.com/rest/2.0/ocr/v1/receipt';
- /**
- * @var string
- */
- private $businessLicenseUrl = 'https://aip.baidubce.com/rest/2.0/ocr/v1/business_license';
- /**
- * @param string $image 图像读取
- * @param bool $isFront 身份证是 true正面 false反面
- * @param array $options 可选参数
- * @return array
- */
- public function idcard($image, $isFront, $options=array()){
- $data = array();
- $data['image'] = $image;
- $data['id_card_side'] = $isFront ? 'front' : 'back';
- $data = array_merge($data, $options);
- return $this->request($this->idcardUrl, $data);
- }
-
- /**
- * @param string $image 图像读取
- * @return array
- */
- public function bankcard($image){
- $data = array();
- $data['image'] = $image;
- return $this->request($this->bankcardUrl, $data);
- }
- /**
- * @param string $image 图像读取
- * @param array $options 可选参数
- * @return array
- */
- public function general($image, $options=array()){
- $data = array();
- $data['image'] = $image;
- $data = array_merge($data, $options);
-
- return $this->request($this->generalUrl, $data);
- }
- /**
- * @param string $image 图像读取
- * @param array $options 可选参数
- * @return array
- */
- public function basicGeneral($image, $options=array()){
- $data = array();
- $data['image'] = $image;
- $data = array_merge($data, $options);
-
- return $this->request($this->basicGeneralUrl, $data);
- }
- /**
- * @param string $image 图像读取
- * @param options 接口可选参数
- * detect_direction : true/false
- * language_type :
- * <p>识别语言类型,若不传则默认为CHN_ENG。
- * 可选值包括:CHN_ENG - 中英文混合;
- * ENG - 英文;
- * POR - 葡萄牙语;
- * FRE - 法语;
- * GER - 德语;
- * ITA - 意大利语;
- * SPA - 西班牙语;
- * RUS - 俄语;
- * JAP - 日语</p>
- * mask : 表示mask区域的黑白灰度图片,白色代表选中, base64编码
- * detect_language: true/false
- * @return array
- */
- public function webImage($image, $options=array()){
- $data = array();
- $data['image'] = $image;
- $data = array_merge($data, $options);
-
- return $this->request($this->webImageUrl, $data);
- }
- /**
- * @param string $image 图像读取
- * @param options 接口可选参数
- * detect_direction : true/false
- * language_type :
- * <p>识别语言类型,若不传则默认为CHN_ENG。
- * 可选值包括:CHN_ENG - 中英文混合;
- * ENG - 英文;
- * POR - 葡萄牙语;
- * FRE - 法语;
- * GER - 德语;
- * ITA - 意大利语;
- * SPA - 西班牙语;
- * RUS - 俄语;
- * JAP - 日语</p>
- * mask : 表示mask区域的黑白灰度图片,白色代表选中, base64编码
- * detect_language: true/false
- * @return array
- */
- public function enhancedGeneral($image, $options=array()){
- $data = array();
- $data['image'] = $image;
- $data = array_merge($data, $options);
-
- return $this->request($this->enhancedGeneralUrl, $data);
- }
- /**
- * 行驶证
- * @param string $image 图像读取
- * @param options 接口可选参数
- * @return array
- */
- public function vehicleLicense($image, $options=array()){
- $data = array();
- $data['image'] = $image;
- $data = array_merge($data, $options);
- return $this->request($this->vehicleLicenseUrl, $data);
- }
- /**
- * 驾驶证
- * @param string $image 图像读取
- * @param options 接口可选参数
- * @return array
- */
- public function drivingLicense($image, $options=array()){
- $data = array();
- $data['image'] = $image;
- $data = array_merge($data, $options);
- return $this->request($this->drivingLicenseUrl, $data);
- }
- /**
- * 格式检查
- * @param string $url
- * @param array $data
- * @return array
- */
- protected function validate($url, &$data){
- if($url === $this->tableResultUrl){
- return true;
- }
- // 支持url
- if(preg_match('/^\w{1,128}:\/\//', $data['image'])){
- $data['url'] = $data['image'];
- unset($data['image']);
- return true;
- }
- $imageInfo = AipImageUtil::getImageInfo($data['image']);
- //图片格式检查
- if(!in_array($imageInfo['mime'], array('image/jpeg', 'image/png', 'image/bmp'))){
- return array(
- 'error_code' => 'SDK109',
- 'error_msg' => 'unsupported image format',
- );
- }
- //图片大小检查
- if($imageInfo['width'] < 15 || $imageInfo['width'] > 4096 || $imageInfo['height'] < 15 || $imageInfo['height'] > 4096){
- return array(
- 'error_code' => 'SDK101',
- 'error_msg' => 'image length error',
- );
- }
- $data['image'] = base64_encode($data['image']);
- //编码后小于4m
- if(strlen($data['image']) >= 4 * 1024 * 1024){
- return array(
- 'error_code' => 'SDK100',
- 'error_msg' => 'image size error',
- );
- }
- return true;
- }
- /**
- * 异步请求
- * @param string $image 图像读取
- * @param options 接口可选参数
- * @return array
- */
- public function tableRecognitionAsync($image, $options=array()){
- $data = array();
- $data['image'] = $image;
- $data = array_merge($data, $options);
- return $this->request($this->tableRequestUrl, $data);
- }
- /**
- * 获取结果
- * @param string $requestId 图像读取
- * @param options 接口可选参数
- * @return array
- */
- public function getTableRecognitionResult($requestId, $options=array()){
- $data = array();
- $data['request_id'] = $requestId;
- $data = array_merge($data, $options);
- return $this->request($this->tableResultUrl, $data);
- }
- /**
- * 同步请求
- * @param string $image 图像读取
- * @param options 接口可选参数
- * @return array
- */
- public function tableRecognition($image, $options=array(), $timeout=10000){
- $result = $this->tableRecognitionAsync($image);
- if(isset($result['error_code'])){
- return $result;
- }
- $requestId = $result['result'][0]['request_id'];
- $count = ceil($timeout / 1000);
- for($i=0; $i<$count; $i++){
- $result = $this->getTableRecognitionResult($requestId, $options);
- // 完成
- if($result['result']['ret_code'] == 3){
- break;
- }
- sleep(1);
- }
- return $result;
- }
- /**
- * 车牌
- * @param string $image 图像读取
- * @param options 接口可选参数
- * @return array
- */
- public function licensePlate($image, $options=array()){
- $data = array();
- $data['image'] = $image;
- $data = array_merge($data, $options);
- return $this->request($this->licensePlateUrl, $data);
- }
- /**
- * @param string $image 图像读取
- * @param array $options 可选参数
- * @return array
- */
- public function accurate($image, $options=array()){
- $data = array();
- $data['image'] = $image;
- $data = array_merge($data, $options);
-
- return $this->request($this->accurateUrl, $data);
- }
- /**
- * @param string $image 图像读取
- * @param array $options 可选参数
- * @return array
- */
- public function basicAccurate($image, $options=array()){
- $data = array();
- $data['image'] = $image;
- $data = array_merge($data, $options);
-
- return $this->request($this->basicAccurateUrl, $data);
- }
- /**
- * @param string $image 图像读取
- * @param array $options 可选参数
- * @return array
- */
- public function receipt($image, $options=array()){
- $data = array();
- $data['image'] = $image;
- $data = array_merge($data, $options);
-
- return $this->request($this->receiptUrl, $data);
- }
- /**
- * @param string $image 图像读取
- * @param array $options 可选参数
- * @return array
- */
- public function businessLicense($image, $options=array()){
- $data = array();
- $data['image'] = $image;
- $data = array_merge($data, $options);
-
- return $this->request($this->businessLicenseUrl, $data);
- }
- }
|