AipOcr.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444
  1. <?php
  2. /*
  3. * Copyright (c) 2017 Baidu.com, Inc. All Rights Reserved
  4. *
  5. * Licensed under the Apache License, Version 2.0 (the "License"); you may not
  6. * use this file except in compliance with the License. You may obtain a copy of
  7. * the License at
  8. *
  9. * Http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  13. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  14. * License for the specific language governing permissions and limitations under
  15. * the License.
  16. */
  17. namespace PhpLife\Library;
  18. require_once 'BaiduVoice/AipBase.php';
  19. use AipBase;
  20. use AipImageUtil;
  21. /**
  22. * 文字OCR
  23. */
  24. class AipOcr extends AipBase{
  25. /**
  26. * idcard api url
  27. * @var string
  28. */
  29. private $idcardUrl = 'https://aip.baidubce.com/rest/2.0/ocr/v1/idcard';
  30. /**
  31. * bankcard api url
  32. * @var string
  33. */
  34. private $bankcardUrl = 'https://aip.baidubce.com/rest/2.0/ocr/v1/bankcard';
  35. /**
  36. * general api url
  37. * @var string
  38. */
  39. private $generalUrl = 'https://aip.baidubce.com/rest/2.0/ocr/v1/general';
  40. /**
  41. * basic general api url
  42. * @var string
  43. */
  44. private $basicGeneralUrl = 'https://aip.baidubce.com/rest/2.0/ocr/v1/general_basic';
  45. /**
  46. * web image url
  47. * @var string
  48. */
  49. private $webImageUrl = 'https://aip.baidubce.com/rest/2.0/ocr/v1/webimage';
  50. /**
  51. * enhanced general url
  52. * @var string
  53. */
  54. private $enhancedGeneralUrl = 'https://aip.baidubce.com/rest/2.0/ocr/v1/general_enhanced';
  55. /**
  56. * @var string
  57. */
  58. private $drivingLicenseUrl = 'https://aip.baidubce.com/rest/2.0/ocr/v1/driving_license';
  59. /**
  60. * @var string
  61. */
  62. private $vehicleLicenseUrl = 'https://aip.baidubce.com/rest/2.0/ocr/v1/vehicle_license';
  63. /**
  64. * @var string
  65. */
  66. private $tableRequestUrl = 'https://aip.baidubce.com/rest/2.0/solution/v1/form_ocr/request';
  67. /**
  68. * @var string
  69. */
  70. private $tableResultUrl = 'https://aip.baidubce.com/rest/2.0/solution/v1/form_ocr/get_request_result';
  71. /**
  72. * @var string
  73. */
  74. private $licensePlateUrl = 'https://aip.baidubce.com/rest/2.0/ocr/v1/license_plate';
  75. /**
  76. * @var string
  77. */
  78. private $accurateUrl = 'https://aip.baidubce.com/rest/2.0/ocr/v1/accurate';
  79. /**
  80. * @var string
  81. */
  82. private $basicAccurateUrl = 'https://aip.baidubce.com/rest/2.0/ocr/v1/accurate_basic';
  83. /**
  84. * @var string
  85. */
  86. private $receiptUrl = 'https://aip.baidubce.com/rest/2.0/ocr/v1/receipt';
  87. /**
  88. * @var string
  89. */
  90. private $businessLicenseUrl = 'https://aip.baidubce.com/rest/2.0/ocr/v1/business_license';
  91. /**
  92. * @param string $image 图像读取
  93. * @param bool $isFront 身份证是 true正面 false反面
  94. * @param array $options 可选参数
  95. * @return array
  96. */
  97. public function idcard($image, $isFront, $options=array()){
  98. $data = array();
  99. $data['image'] = $image;
  100. $data['id_card_side'] = $isFront ? 'front' : 'back';
  101. $data = array_merge($data, $options);
  102. return $this->request($this->idcardUrl, $data);
  103. }
  104. /**
  105. * @param string $image 图像读取
  106. * @return array
  107. */
  108. public function bankcard($image){
  109. $data = array();
  110. $data['image'] = $image;
  111. return $this->request($this->bankcardUrl, $data);
  112. }
  113. /**
  114. * @param string $image 图像读取
  115. * @param array $options 可选参数
  116. * @return array
  117. */
  118. public function general($image, $options=array()){
  119. $data = array();
  120. $data['image'] = $image;
  121. $data = array_merge($data, $options);
  122. return $this->request($this->generalUrl, $data);
  123. }
  124. /**
  125. * @param string $image 图像读取
  126. * @param array $options 可选参数
  127. * @return array
  128. */
  129. public function basicGeneral($image, $options=array()){
  130. $data = array();
  131. $data['image'] = $image;
  132. $data = array_merge($data, $options);
  133. return $this->request($this->basicGeneralUrl, $data);
  134. }
  135. /**
  136. * @param string $image 图像读取
  137. * @param options 接口可选参数
  138. * detect_direction : true/false
  139. * language_type :
  140. * <p>识别语言类型,若不传则默认为CHN_ENG。
  141. * 可选值包括:CHN_ENG - 中英文混合;
  142. * ENG - 英文;
  143. * POR - 葡萄牙语;
  144. * FRE - 法语;
  145. * GER - 德语;
  146. * ITA - 意大利语;
  147. * SPA - 西班牙语;
  148. * RUS - 俄语;
  149. * JAP - 日语</p>
  150. * mask : 表示mask区域的黑白灰度图片,白色代表选中, base64编码
  151. * detect_language: true/false
  152. * @return array
  153. */
  154. public function webImage($image, $options=array()){
  155. $data = array();
  156. $data['image'] = $image;
  157. $data = array_merge($data, $options);
  158. return $this->request($this->webImageUrl, $data);
  159. }
  160. /**
  161. * @param string $image 图像读取
  162. * @param options 接口可选参数
  163. * detect_direction : true/false
  164. * language_type :
  165. * <p>识别语言类型,若不传则默认为CHN_ENG。
  166. * 可选值包括:CHN_ENG - 中英文混合;
  167. * ENG - 英文;
  168. * POR - 葡萄牙语;
  169. * FRE - 法语;
  170. * GER - 德语;
  171. * ITA - 意大利语;
  172. * SPA - 西班牙语;
  173. * RUS - 俄语;
  174. * JAP - 日语</p>
  175. * mask : 表示mask区域的黑白灰度图片,白色代表选中, base64编码
  176. * detect_language: true/false
  177. * @return array
  178. */
  179. public function enhancedGeneral($image, $options=array()){
  180. $data = array();
  181. $data['image'] = $image;
  182. $data = array_merge($data, $options);
  183. return $this->request($this->enhancedGeneralUrl, $data);
  184. }
  185. /**
  186. * 行驶证
  187. * @param string $image 图像读取
  188. * @param options 接口可选参数
  189. * @return array
  190. */
  191. public function vehicleLicense($image, $options=array()){
  192. $data = array();
  193. $data['image'] = $image;
  194. $data = array_merge($data, $options);
  195. return $this->request($this->vehicleLicenseUrl, $data);
  196. }
  197. /**
  198. * 驾驶证
  199. * @param string $image 图像读取
  200. * @param options 接口可选参数
  201. * @return array
  202. */
  203. public function drivingLicense($image, $options=array()){
  204. $data = array();
  205. $data['image'] = $image;
  206. $data = array_merge($data, $options);
  207. return $this->request($this->drivingLicenseUrl, $data);
  208. }
  209. /**
  210. * 格式检查
  211. * @param string $url
  212. * @param array $data
  213. * @return array
  214. */
  215. protected function validate($url, &$data){
  216. if($url === $this->tableResultUrl){
  217. return true;
  218. }
  219. // 支持url
  220. if(preg_match('/^\w{1,128}:\/\//', $data['image'])){
  221. $data['url'] = $data['image'];
  222. unset($data['image']);
  223. return true;
  224. }
  225. $imageInfo = AipImageUtil::getImageInfo($data['image']);
  226. //图片格式检查
  227. if(!in_array($imageInfo['mime'], array('image/jpeg', 'image/png', 'image/bmp'))){
  228. return array(
  229. 'error_code' => 'SDK109',
  230. 'error_msg' => 'unsupported image format',
  231. );
  232. }
  233. //图片大小检查
  234. if($imageInfo['width'] < 15 || $imageInfo['width'] > 4096 || $imageInfo['height'] < 15 || $imageInfo['height'] > 4096){
  235. return array(
  236. 'error_code' => 'SDK101',
  237. 'error_msg' => 'image length error',
  238. );
  239. }
  240. $data['image'] = base64_encode($data['image']);
  241. //编码后小于4m
  242. if(strlen($data['image']) >= 4 * 1024 * 1024){
  243. return array(
  244. 'error_code' => 'SDK100',
  245. 'error_msg' => 'image size error',
  246. );
  247. }
  248. return true;
  249. }
  250. /**
  251. * 异步请求
  252. * @param string $image 图像读取
  253. * @param options 接口可选参数
  254. * @return array
  255. */
  256. public function tableRecognitionAsync($image, $options=array()){
  257. $data = array();
  258. $data['image'] = $image;
  259. $data = array_merge($data, $options);
  260. return $this->request($this->tableRequestUrl, $data);
  261. }
  262. /**
  263. * 获取结果
  264. * @param string $requestId 图像读取
  265. * @param options 接口可选参数
  266. * @return array
  267. */
  268. public function getTableRecognitionResult($requestId, $options=array()){
  269. $data = array();
  270. $data['request_id'] = $requestId;
  271. $data = array_merge($data, $options);
  272. return $this->request($this->tableResultUrl, $data);
  273. }
  274. /**
  275. * 同步请求
  276. * @param string $image 图像读取
  277. * @param options 接口可选参数
  278. * @return array
  279. */
  280. public function tableRecognition($image, $options=array(), $timeout=10000){
  281. $result = $this->tableRecognitionAsync($image);
  282. if(isset($result['error_code'])){
  283. return $result;
  284. }
  285. $requestId = $result['result'][0]['request_id'];
  286. $count = ceil($timeout / 1000);
  287. for($i=0; $i<$count; $i++){
  288. $result = $this->getTableRecognitionResult($requestId, $options);
  289. // 完成
  290. if($result['result']['ret_code'] == 3){
  291. break;
  292. }
  293. sleep(1);
  294. }
  295. return $result;
  296. }
  297. /**
  298. * 车牌
  299. * @param string $image 图像读取
  300. * @param options 接口可选参数
  301. * @return array
  302. */
  303. public function licensePlate($image, $options=array()){
  304. $data = array();
  305. $data['image'] = $image;
  306. $data = array_merge($data, $options);
  307. return $this->request($this->licensePlateUrl, $data);
  308. }
  309. /**
  310. * @param string $image 图像读取
  311. * @param array $options 可选参数
  312. * @return array
  313. */
  314. public function accurate($image, $options=array()){
  315. $data = array();
  316. $data['image'] = $image;
  317. $data = array_merge($data, $options);
  318. return $this->request($this->accurateUrl, $data);
  319. }
  320. /**
  321. * @param string $image 图像读取
  322. * @param array $options 可选参数
  323. * @return array
  324. */
  325. public function basicAccurate($image, $options=array()){
  326. $data = array();
  327. $data['image'] = $image;
  328. $data = array_merge($data, $options);
  329. return $this->request($this->basicAccurateUrl, $data);
  330. }
  331. /**
  332. * @param string $image 图像读取
  333. * @param array $options 可选参数
  334. * @return array
  335. */
  336. public function receipt($image, $options=array()){
  337. $data = array();
  338. $data['image'] = $image;
  339. $data = array_merge($data, $options);
  340. return $this->request($this->receiptUrl, $data);
  341. }
  342. /**
  343. * @param string $image 图像读取
  344. * @param array $options 可选参数
  345. * @return array
  346. */
  347. public function businessLicense($image, $options=array()){
  348. $data = array();
  349. $data['image'] = $image;
  350. $data = array_merge($data, $options);
  351. return $this->request($this->businessLicenseUrl, $data);
  352. }
  353. }