AipOcr.php 44 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156
  1. <?php
  2. namespace addons\cms\library\aip;
  3. /*
  4. * Copyright (c) 2017 Baidu.com, Inc. All Rights Reserved
  5. *
  6. * Licensed under the Apache License, Version 2.0 (the "License"); you may not
  7. * use this file except in compliance with the License. You may obtain a copy of
  8. * the License at
  9. *
  10. * Http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing, software
  13. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  14. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  15. * License for the specific language governing permissions and limitations under
  16. * the License.
  17. */
  18. use addons\cms\library\aip\lib\AipBase;
  19. class AipOcr extends AipBase
  20. {
  21. /**
  22. * 通用文字识别 general_basic api url
  23. * @var string
  24. */
  25. private $generalBasicUrl = 'https://aip.baidubce.com/rest/2.0/ocr/v1/general_basic';
  26. /**
  27. * 通用文字识别(高精度版) accurate_basic api url
  28. * @var string
  29. */
  30. private $accurateBasicUrl = 'https://aip.baidubce.com/rest/2.0/ocr/v1/accurate_basic';
  31. /**
  32. * 通用文字识别(含位置信息版) general api url
  33. * @var string
  34. */
  35. private $generalUrl = 'https://aip.baidubce.com/rest/2.0/ocr/v1/general';
  36. /**
  37. * 通用文字识别(含位置高精度版) accurate api url
  38. * @var string
  39. */
  40. private $accurateUrl = 'https://aip.baidubce.com/rest/2.0/ocr/v1/accurate';
  41. /**
  42. * 通用文字识别(含生僻字版) general_enhanced api url
  43. * @var string
  44. */
  45. private $generalEnhancedUrl = 'https://aip.baidubce.com/rest/2.0/ocr/v1/general_enhanced';
  46. /**
  47. * 网络图片文字识别 web_image api url
  48. * @var string
  49. */
  50. private $webImageUrl = 'https://aip.baidubce.com/rest/2.0/ocr/v1/webimage';
  51. /**
  52. * 身份证识别 idcard api url
  53. * @var string
  54. */
  55. private $idcardUrl = 'https://aip.baidubce.com/rest/2.0/ocr/v1/idcard';
  56. /**
  57. * 银行卡识别 bankcard api url
  58. * @var string
  59. */
  60. private $bankcardUrl = 'https://aip.baidubce.com/rest/2.0/ocr/v1/bankcard';
  61. /**
  62. * 驾驶证识别 driving_license api url
  63. * @var string
  64. */
  65. private $drivingLicenseUrl = 'https://aip.baidubce.com/rest/2.0/ocr/v1/driving_license';
  66. /**
  67. * 行驶证识别 vehicle_license api url
  68. * @var string
  69. */
  70. private $vehicleLicenseUrl = 'https://aip.baidubce.com/rest/2.0/ocr/v1/vehicle_license';
  71. /**
  72. * 车牌识别 license_plate api url
  73. * @var string
  74. */
  75. private $licensePlateUrl = 'https://aip.baidubce.com/rest/2.0/ocr/v1/license_plate';
  76. /**
  77. * 营业执照识别 business_license api url
  78. * @var string
  79. */
  80. private $businessLicenseUrl = 'https://aip.baidubce.com/rest/2.0/ocr/v1/business_license';
  81. /**
  82. * 通用票据识别 receipt api url
  83. * @var string
  84. */
  85. private $receiptUrl = 'https://aip.baidubce.com/rest/2.0/ocr/v1/receipt';
  86. /**
  87. * 火车票识别 train_ticket api url
  88. * @var string
  89. */
  90. private $trainTicketUrl = 'https://aip.baidubce.com/rest/2.0/ocr/v1/train_ticket';
  91. /**
  92. * 出租车票识别 taxi_receipt api url
  93. * @var string
  94. */
  95. private $taxiReceiptUrl = 'https://aip.baidubce.com/rest/2.0/ocr/v1/taxi_receipt';
  96. /**
  97. * 表格文字识别同步接口 form api url
  98. * @var string
  99. */
  100. private $formUrl = 'https://aip.baidubce.com/rest/2.0/ocr/v1/form';
  101. /**
  102. * 表格文字识别 table_recognize api url
  103. * @var string
  104. */
  105. private $tableRecognizeUrl = 'https://aip.baidubce.com/rest/2.0/solution/v1/form_ocr/request';
  106. /**
  107. * 表格识别结果 table_result_get api url
  108. * @var string
  109. */
  110. private $tableResultGetUrl = 'https://aip.baidubce.com/rest/2.0/solution/v1/form_ocr/get_request_result';
  111. /**
  112. * VIN码识别 vin_code api url
  113. * @var string
  114. */
  115. private $vinCodeUrl = 'https://aip.baidubce.com/rest/2.0/ocr/v1/vin_code';
  116. /**
  117. * 定额发票识别 quota_invoice api url
  118. * @var string
  119. */
  120. private $quotaInvoiceUrl = 'https://aip.baidubce.com/rest/2.0/ocr/v1/quota_invoice';
  121. /**
  122. * 户口本识别 household_register api url
  123. * @var string
  124. */
  125. private $householdRegisterUrl = 'https://aip.baidubce.com/rest/2.0/ocr/v1/household_register';
  126. /**
  127. * 港澳通行证识别 HK_Macau_exitentrypermit api url
  128. * @var string
  129. */
  130. private $HKMacauExitentrypermitUrl = 'https://aip.baidubce.com/rest/2.0/ocr/v1/HK_Macau_exitentrypermit';
  131. /**
  132. * 台湾通行证识别 taiwan_exitentrypermit api url
  133. * @var string
  134. */
  135. private $taiwanExitentrypermitUrl = 'https://aip.baidubce.com/rest/2.0/ocr/v1/taiwan_exitentrypermit';
  136. /**
  137. * 出生医学证明识别 birth_certificate api url
  138. * @var string
  139. */
  140. private $birthCertificateUrl = 'https://aip.baidubce.com/rest/2.0/ocr/v1/birth_certificate';
  141. /**
  142. * 机动车销售发票识别 vehicle_invoice api url
  143. * @var string
  144. */
  145. private $vehicleInvoiceUrl = 'https://aip.baidubce.com/rest/2.0/ocr/v1/vehicle_invoice';
  146. /**
  147. * 车辆合格证识别 vehicle_certificate api url
  148. * @var string
  149. */
  150. private $vehicleCertificateUrl = 'https://aip.baidubce.com/rest/2.0/ocr/v1/vehicle_certificate';
  151. /**
  152. * 税务局通用机打发票识别 invoice api url
  153. * @var string
  154. */
  155. private $invoiceUrl = 'https://aip.baidubce.com/rest/2.0/ocr/v1/invoice';
  156. /**
  157. * 行程单识别 air_ticket api url
  158. * @var string
  159. */
  160. private $airTicketUrl = 'https://aip.baidubce.com/rest/2.0/ocr/v1/air_ticket';
  161. /**
  162. * 保单识别 insurance_documents api url
  163. * @var string
  164. */
  165. private $insuranceDocumentsUrl = 'https://aip.baidubce.com/rest/2.0/ocr/v1/insurance_documents';
  166. /**
  167. * 增值税发票识别 vat_invoice api url
  168. * @var string
  169. */
  170. private $vatInvoiceUrl = 'https://aip.baidubce.com/rest/2.0/ocr/v1/vat_invoice';
  171. /**
  172. * 二维码识别 qrcode api url
  173. * @var string
  174. */
  175. private $qrcodeUrl = 'https://aip.baidubce.com/rest/2.0/ocr/v1/qrcode';
  176. /**
  177. * 数字识别 numbers api url
  178. * @var string
  179. */
  180. private $numbersUrl = 'https://aip.baidubce.com/rest/2.0/ocr/v1/numbers';
  181. /**
  182. * 彩票识别 lottery api url
  183. * @var string
  184. */
  185. private $lotteryUrl = 'https://aip.baidubce.com/rest/2.0/ocr/v1/lottery';
  186. /**
  187. * 护照识别 passport api url
  188. * @var string
  189. */
  190. private $passportUrl = 'https://aip.baidubce.com/rest/2.0/ocr/v1/passport';
  191. /**
  192. * 名片识别 business_card api url
  193. * @var string
  194. */
  195. private $businessCardUrl = 'https://aip.baidubce.com/rest/2.0/ocr/v1/business_card';
  196. /**
  197. * 手写文字识别 handwriting api url
  198. * @var string
  199. */
  200. private $handwritingUrl = 'https://aip.baidubce.com/rest/2.0/ocr/v1/handwriting';
  201. /**
  202. * 自定义模板文字识别 custom api url
  203. * @var string
  204. */
  205. private $customUrl = 'https://aip.baidubce.com/rest/2.0/solution/v1/iocr/recognise';
  206. /**
  207. * 通用文字识别接口
  208. *
  209. * @param string $image - 图像数据,base64编码,要求base64编码后大小不超过4M,最短边至少15px,最长边最大4096px,支持jpg/png/bmp格式
  210. * @param array $options - 可选参数对象,key: value都为string类型
  211. * @description options列表:
  212. * language_type 识别语言类型,默认为CHN_ENG。可选值包括:<br>- CHN_ENG:中英文混合;<br>- ENG:英文;<br>- POR:葡萄牙语;<br>- FRE:法语;<br>- GER:德语;<br>- ITA:意大利语;<br>- SPA:西班牙语;<br>- RUS:俄语;<br>- JAP:日语;<br>- KOR:韩语;
  213. * detect_direction 是否检测图像朝向,默认不检测,即:false。朝向是指输入图像是正常方向、逆时针旋转90/180/270度。可选值包括:<br>- true:检测朝向;<br>- false:不检测朝向。
  214. * detect_language 是否检测语言,默认不检测。当前支持(中文、英语、日语、韩语)
  215. * probability 是否返回识别结果中每一行的置信度
  216. * @return array
  217. */
  218. public function basicGeneral($image, $options = array())
  219. {
  220. $data = array();
  221. $data['image'] = base64_encode($image);
  222. $data = array_merge($data, $options);
  223. return $this->request($this->generalBasicUrl, $data);
  224. }
  225. /**
  226. * 通用文字识别接口
  227. *
  228. * @param string $url - 图片完整URL,URL长度不超过1024字节,URL对应的图片base64编码后大小不超过4M,最短边至少15px,最长边最大4096px,支持jpg/png/bmp格式,当image字段存在时url字段失效
  229. * @param array $options - 可选参数对象,key: value都为string类型
  230. * @description options列表:
  231. * language_type 识别语言类型,默认为CHN_ENG。可选值包括:<br>- CHN_ENG:中英文混合;<br>- ENG:英文;<br>- POR:葡萄牙语;<br>- FRE:法语;<br>- GER:德语;<br>- ITA:意大利语;<br>- SPA:西班牙语;<br>- RUS:俄语;<br>- JAP:日语;<br>- KOR:韩语;
  232. * detect_direction 是否检测图像朝向,默认不检测,即:false。朝向是指输入图像是正常方向、逆时针旋转90/180/270度。可选值包括:<br>- true:检测朝向;<br>- false:不检测朝向。
  233. * detect_language 是否检测语言,默认不检测。当前支持(中文、英语、日语、韩语)
  234. * probability 是否返回识别结果中每一行的置信度
  235. * @return array
  236. */
  237. public function basicGeneralUrl($url, $options = array())
  238. {
  239. $data = array();
  240. $data['url'] = $url;
  241. $data = array_merge($data, $options);
  242. return $this->request($this->generalBasicUrl, $data);
  243. }
  244. /**
  245. * 通用文字识别(高精度版)接口
  246. *
  247. * @param string $image - 图像数据,base64编码,要求base64编码后大小不超过4M,最短边至少15px,最长边最大4096px,支持jpg/png/bmp格式
  248. * @param array $options - 可选参数对象,key: value都为string类型
  249. * @description options列表:
  250. * detect_direction 是否检测图像朝向,默认不检测,即:false。朝向是指输入图像是正常方向、逆时针旋转90/180/270度。可选值包括:<br>- true:检测朝向;<br>- false:不检测朝向。
  251. * probability 是否返回识别结果中每一行的置信度
  252. * @return array
  253. */
  254. public function basicAccurate($image, $options = array())
  255. {
  256. $data = array();
  257. $data['image'] = base64_encode($image);
  258. $data = array_merge($data, $options);
  259. return $this->request($this->accurateBasicUrl, $data);
  260. }
  261. /**
  262. * 通用文字识别(含位置信息版)接口
  263. *
  264. * @param string $image - 图像数据,base64编码,要求base64编码后大小不超过4M,最短边至少15px,最长边最大4096px,支持jpg/png/bmp格式
  265. * @param array $options - 可选参数对象,key: value都为string类型
  266. * @description options列表:
  267. * recognize_granularity 是否定位单字符位置,big:不定位单字符位置,默认值;small:定位单字符位置
  268. * language_type 识别语言类型,默认为CHN_ENG。可选值包括:<br>- CHN_ENG:中英文混合;<br>- ENG:英文;<br>- POR:葡萄牙语;<br>- FRE:法语;<br>- GER:德语;<br>- ITA:意大利语;<br>- SPA:西班牙语;<br>- RUS:俄语;<br>- JAP:日语;<br>- KOR:韩语;
  269. * detect_direction 是否检测图像朝向,默认不检测,即:false。朝向是指输入图像是正常方向、逆时针旋转90/180/270度。可选值包括:<br>- true:检测朝向;<br>- false:不检测朝向。
  270. * detect_language 是否检测语言,默认不检测。当前支持(中文、英语、日语、韩语)
  271. * vertexes_location 是否返回文字外接多边形顶点位置,不支持单字位置。默认为false
  272. * probability 是否返回识别结果中每一行的置信度
  273. * @return array
  274. */
  275. public function general($image, $options = array())
  276. {
  277. $data = array();
  278. $data['image'] = base64_encode($image);
  279. $data = array_merge($data, $options);
  280. return $this->request($this->generalUrl, $data);
  281. }
  282. /**
  283. * 通用文字识别(含位置信息版)接口
  284. *
  285. * @param string $url - 图片完整URL,URL长度不超过1024字节,URL对应的图片base64编码后大小不超过4M,最短边至少15px,最长边最大4096px,支持jpg/png/bmp格式,当image字段存在时url字段失效
  286. * @param array $options - 可选参数对象,key: value都为string类型
  287. * @description options列表:
  288. * recognize_granularity 是否定位单字符位置,big:不定位单字符位置,默认值;small:定位单字符位置
  289. * language_type 识别语言类型,默认为CHN_ENG。可选值包括:<br>- CHN_ENG:中英文混合;<br>- ENG:英文;<br>- POR:葡萄牙语;<br>- FRE:法语;<br>- GER:德语;<br>- ITA:意大利语;<br>- SPA:西班牙语;<br>- RUS:俄语;<br>- JAP:日语;<br>- KOR:韩语;
  290. * detect_direction 是否检测图像朝向,默认不检测,即:false。朝向是指输入图像是正常方向、逆时针旋转90/180/270度。可选值包括:<br>- true:检测朝向;<br>- false:不检测朝向。
  291. * detect_language 是否检测语言,默认不检测。当前支持(中文、英语、日语、韩语)
  292. * vertexes_location 是否返回文字外接多边形顶点位置,不支持单字位置。默认为false
  293. * probability 是否返回识别结果中每一行的置信度
  294. * @return array
  295. */
  296. public function generalUrl($url, $options = array())
  297. {
  298. $data = array();
  299. $data['url'] = $url;
  300. $data = array_merge($data, $options);
  301. return $this->request($this->generalUrl, $data);
  302. }
  303. /**
  304. * 通用文字识别(含位置高精度版)接口
  305. *
  306. * @param string $image - 图像数据,base64编码,要求base64编码后大小不超过4M,最短边至少15px,最长边最大4096px,支持jpg/png/bmp格式
  307. * @param array $options - 可选参数对象,key: value都为string类型
  308. * @description options列表:
  309. * recognize_granularity 是否定位单字符位置,big:不定位单字符位置,默认值;small:定位单字符位置
  310. * detect_direction 是否检测图像朝向,默认不检测,即:false。朝向是指输入图像是正常方向、逆时针旋转90/180/270度。可选值包括:<br>- true:检测朝向;<br>- false:不检测朝向。
  311. * vertexes_location 是否返回文字外接多边形顶点位置,不支持单字位置。默认为false
  312. * probability 是否返回识别结果中每一行的置信度
  313. * @return array
  314. */
  315. public function accurate($image, $options = array())
  316. {
  317. $data = array();
  318. $data['image'] = base64_encode($image);
  319. $data = array_merge($data, $options);
  320. return $this->request($this->accurateUrl, $data);
  321. }
  322. /**
  323. * 通用文字识别(含生僻字版)接口
  324. *
  325. * @param string $image - 图像数据,base64编码,要求base64编码后大小不超过4M,最短边至少15px,最长边最大4096px,支持jpg/png/bmp格式
  326. * @param array $options - 可选参数对象,key: value都为string类型
  327. * @description options列表:
  328. * language_type 识别语言类型,默认为CHN_ENG。可选值包括:<br>- CHN_ENG:中英文混合;<br>- ENG:英文;<br>- POR:葡萄牙语;<br>- FRE:法语;<br>- GER:德语;<br>- ITA:意大利语;<br>- SPA:西班牙语;<br>- RUS:俄语;<br>- JAP:日语;<br>- KOR:韩语;
  329. * detect_direction 是否检测图像朝向,默认不检测,即:false。朝向是指输入图像是正常方向、逆时针旋转90/180/270度。可选值包括:<br>- true:检测朝向;<br>- false:不检测朝向。
  330. * detect_language 是否检测语言,默认不检测。当前支持(中文、英语、日语、韩语)
  331. * probability 是否返回识别结果中每一行的置信度
  332. * @return array
  333. */
  334. public function enhancedGeneral($image, $options = array())
  335. {
  336. $data = array();
  337. $data['image'] = base64_encode($image);
  338. $data = array_merge($data, $options);
  339. return $this->request($this->generalEnhancedUrl, $data);
  340. }
  341. /**
  342. * 通用文字识别(含生僻字版)接口
  343. *
  344. * @param string $url - 图片完整URL,URL长度不超过1024字节,URL对应的图片base64编码后大小不超过4M,最短边至少15px,最长边最大4096px,支持jpg/png/bmp格式,当image字段存在时url字段失效
  345. * @param array $options - 可选参数对象,key: value都为string类型
  346. * @description options列表:
  347. * language_type 识别语言类型,默认为CHN_ENG。可选值包括:<br>- CHN_ENG:中英文混合;<br>- ENG:英文;<br>- POR:葡萄牙语;<br>- FRE:法语;<br>- GER:德语;<br>- ITA:意大利语;<br>- SPA:西班牙语;<br>- RUS:俄语;<br>- JAP:日语;<br>- KOR:韩语;
  348. * detect_direction 是否检测图像朝向,默认不检测,即:false。朝向是指输入图像是正常方向、逆时针旋转90/180/270度。可选值包括:<br>- true:检测朝向;<br>- false:不检测朝向。
  349. * detect_language 是否检测语言,默认不检测。当前支持(中文、英语、日语、韩语)
  350. * probability 是否返回识别结果中每一行的置信度
  351. * @return array
  352. */
  353. public function enhancedGeneralUrl($url, $options = array())
  354. {
  355. $data = array();
  356. $data['url'] = $url;
  357. $data = array_merge($data, $options);
  358. return $this->request($this->generalEnhancedUrl, $data);
  359. }
  360. /**
  361. * 网络图片文字识别接口
  362. *
  363. * @param string $image - 图像数据,base64编码,要求base64编码后大小不超过4M,最短边至少15px,最长边最大4096px,支持jpg/png/bmp格式
  364. * @param array $options - 可选参数对象,key: value都为string类型
  365. * @description options列表:
  366. * detect_direction 是否检测图像朝向,默认不检测,即:false。朝向是指输入图像是正常方向、逆时针旋转90/180/270度。可选值包括:<br>- true:检测朝向;<br>- false:不检测朝向。
  367. * detect_language 是否检测语言,默认不检测。当前支持(中文、英语、日语、韩语)
  368. * @return array
  369. */
  370. public function webImage($image, $options = array())
  371. {
  372. $data = array();
  373. $data['image'] = base64_encode($image);
  374. $data = array_merge($data, $options);
  375. return $this->request($this->webImageUrl, $data);
  376. }
  377. /**
  378. * 网络图片文字识别接口
  379. *
  380. * @param string $url - 图片完整URL,URL长度不超过1024字节,URL对应的图片base64编码后大小不超过4M,最短边至少15px,最长边最大4096px,支持jpg/png/bmp格式,当image字段存在时url字段失效
  381. * @param array $options - 可选参数对象,key: value都为string类型
  382. * @description options列表:
  383. * detect_direction 是否检测图像朝向,默认不检测,即:false。朝向是指输入图像是正常方向、逆时针旋转90/180/270度。可选值包括:<br>- true:检测朝向;<br>- false:不检测朝向。
  384. * detect_language 是否检测语言,默认不检测。当前支持(中文、英语、日语、韩语)
  385. * @return array
  386. */
  387. public function webImageUrl($url, $options = array())
  388. {
  389. $data = array();
  390. $data['url'] = $url;
  391. $data = array_merge($data, $options);
  392. return $this->request($this->webImageUrl, $data);
  393. }
  394. /**
  395. * 身份证识别接口
  396. *
  397. * @param string $image - 图像数据,base64编码,要求base64编码后大小不超过4M,最短边至少15px,最长边最大4096px,支持jpg/png/bmp格式
  398. * @param string $idCardSide - front:身份证含照片的一面;back:身份证带国徽的一面
  399. * @param array $options - 可选参数对象,key: value都为string类型
  400. * @description options列表:
  401. * detect_direction 是否检测图像朝向,默认不检测,即:false。朝向是指输入图像是正常方向、逆时针旋转90/180/270度。可选值包括:<br>- true:检测朝向;<br>- false:不检测朝向。
  402. * detect_risk 是否开启身份证风险类型(身份证复印件、临时身份证、身份证翻拍、修改过的身份证)功能,默认不开启,即:false。可选值:true-开启;false-不开启
  403. * @return array
  404. */
  405. public function idcard($image, $idCardSide, $options = array())
  406. {
  407. $data = array();
  408. $data['image'] = base64_encode($image);
  409. $data['id_card_side'] = $idCardSide;
  410. $data = array_merge($data, $options);
  411. return $this->request($this->idcardUrl, $data);
  412. }
  413. /**
  414. * 银行卡识别接口
  415. *
  416. * @param string $image - 图像数据,base64编码,要求base64编码后大小不超过4M,最短边至少15px,最长边最大4096px,支持jpg/png/bmp格式
  417. * @param array $options - 可选参数对象,key: value都为string类型
  418. * @description options列表:
  419. * @return array
  420. */
  421. public function bankcard($image, $options = array())
  422. {
  423. $data = array();
  424. $data['image'] = base64_encode($image);
  425. $data = array_merge($data, $options);
  426. return $this->request($this->bankcardUrl, $data);
  427. }
  428. /**
  429. * 驾驶证识别接口
  430. *
  431. * @param string $image - 图像数据,base64编码,要求base64编码后大小不超过4M,最短边至少15px,最长边最大4096px,支持jpg/png/bmp格式
  432. * @param array $options - 可选参数对象,key: value都为string类型
  433. * @description options列表:
  434. * detect_direction 是否检测图像朝向,默认不检测,即:false。朝向是指输入图像是正常方向、逆时针旋转90/180/270度。可选值包括:<br>- true:检测朝向;<br>- false:不检测朝向。
  435. * @return array
  436. */
  437. public function drivingLicense($image, $options = array())
  438. {
  439. $data = array();
  440. $data['image'] = base64_encode($image);
  441. $data = array_merge($data, $options);
  442. return $this->request($this->drivingLicenseUrl, $data);
  443. }
  444. /**
  445. * 行驶证识别接口
  446. *
  447. * @param string $image - 图像数据,base64编码,要求base64编码后大小不超过4M,最短边至少15px,最长边最大4096px,支持jpg/png/bmp格式
  448. * @param array $options - 可选参数对象,key: value都为string类型
  449. * @description options列表:
  450. * detect_direction 是否检测图像朝向,默认不检测,即:false。朝向是指输入图像是正常方向、逆时针旋转90/180/270度。可选值包括:<br>- true:检测朝向;<br>- false:不检测朝向。
  451. * accuracy normal 使用快速服务,1200ms左右时延;缺省或其它值使用高精度服务,1600ms左右时延
  452. * @return array
  453. */
  454. public function vehicleLicense($image, $options = array())
  455. {
  456. $data = array();
  457. $data['image'] = base64_encode($image);
  458. $data = array_merge($data, $options);
  459. return $this->request($this->vehicleLicenseUrl, $data);
  460. }
  461. /**
  462. * 车牌识别接口
  463. *
  464. * @param string $image - 图像数据,base64编码,要求base64编码后大小不超过4M,最短边至少15px,最长边最大4096px,支持jpg/png/bmp格式
  465. * @param array $options - 可选参数对象,key: value都为string类型
  466. * @description options列表:
  467. * multi_detect 是否检测多张车牌,默认为false,当置为true的时候可以对一张图片内的多张车牌进行识别
  468. * @return array
  469. */
  470. public function licensePlate($image, $options = array())
  471. {
  472. $data = array();
  473. $data['image'] = base64_encode($image);
  474. $data = array_merge($data, $options);
  475. return $this->request($this->licensePlateUrl, $data);
  476. }
  477. /**
  478. * 营业执照识别接口
  479. *
  480. * @param string $image - 图像数据,base64编码,要求base64编码后大小不超过4M,最短边至少15px,最长边最大4096px,支持jpg/png/bmp格式
  481. * @param array $options - 可选参数对象,key: value都为string类型
  482. * @description options列表:
  483. * @return array
  484. */
  485. public function businessLicense($image, $options = array())
  486. {
  487. $data = array();
  488. $data['image'] = base64_encode($image);
  489. $data = array_merge($data, $options);
  490. return $this->request($this->businessLicenseUrl, $data);
  491. }
  492. /**
  493. * 通用票据识别接口
  494. *
  495. * @param string $image - 图像数据,base64编码,要求base64编码后大小不超过4M,最短边至少15px,最长边最大4096px,支持jpg/png/bmp格式
  496. * @param array $options - 可选参数对象,key: value都为string类型
  497. * @description options列表:
  498. * recognize_granularity 是否定位单字符位置,big:不定位单字符位置,默认值;small:定位单字符位置
  499. * probability 是否返回识别结果中每一行的置信度
  500. * accuracy normal 使用快速服务,1200ms左右时延;缺省或其它值使用高精度服务,1600ms左右时延
  501. * detect_direction 是否检测图像朝向,默认不检测,即:false。朝向是指输入图像是正常方向、逆时针旋转90/180/270度。可选值包括:<br>- true:检测朝向;<br>- false:不检测朝向。
  502. * @return array
  503. */
  504. public function receipt($image, $options = array())
  505. {
  506. $data = array();
  507. $data['image'] = base64_encode($image);
  508. $data = array_merge($data, $options);
  509. return $this->request($this->receiptUrl, $data);
  510. }
  511. /**
  512. * 火车票识别接口
  513. *
  514. * @param string $image - 图像数据,base64编码,要求base64编码后大小不超过4M,最短边至少15px,最长边最大4096px,支持jpg/png/bmp格式
  515. * @param array $options - 可选参数对象,key: value都为string类型
  516. * @description options列表:
  517. * @return array
  518. */
  519. public function trainTicket($image, $options = array())
  520. {
  521. $data = array();
  522. $data['image'] = base64_encode($image);
  523. $data = array_merge($data, $options);
  524. return $this->request($this->trainTicketUrl, $data);
  525. }
  526. /**
  527. * 出租车票识别接口
  528. *
  529. * @param string $image - 图像数据,base64编码,要求base64编码后大小不超过4M,最短边至少15px,最长边最大4096px,支持jpg/png/bmp格式
  530. * @param array $options - 可选参数对象,key: value都为string类型
  531. * @description options列表:
  532. * @return array
  533. */
  534. public function taxiReceipt($image, $options = array())
  535. {
  536. $data = array();
  537. $data['image'] = base64_encode($image);
  538. $data = array_merge($data, $options);
  539. return $this->request($this->taxiReceiptUrl, $data);
  540. }
  541. /**
  542. * 表格文字识别同步接口接口
  543. *
  544. * @param string $image - 图像数据,base64编码,要求base64编码后大小不超过4M,最短边至少15px,最长边最大4096px,支持jpg/png/bmp格式
  545. * @param array $options - 可选参数对象,key: value都为string类型
  546. * @description options列表:
  547. * @return array
  548. */
  549. public function form($image, $options = array())
  550. {
  551. $data = array();
  552. $data['image'] = base64_encode($image);
  553. $data = array_merge($data, $options);
  554. return $this->request($this->formUrl, $data);
  555. }
  556. /**
  557. * 表格文字识别接口
  558. *
  559. * @param string $image - 图像数据,base64编码,要求base64编码后大小不超过4M,最短边至少15px,最长边最大4096px,支持jpg/png/bmp格式
  560. * @param array $options - 可选参数对象,key: value都为string类型
  561. * @description options列表:
  562. * @return array
  563. */
  564. public function tableRecognitionAsync($image, $options = array())
  565. {
  566. $data = array();
  567. $data['image'] = base64_encode($image);
  568. $data = array_merge($data, $options);
  569. return $this->request($this->tableRecognizeUrl, $data);
  570. }
  571. /**
  572. * 表格识别结果接口
  573. *
  574. * @param string $requestId - 发送表格文字识别请求时返回的request id
  575. * @param array $options - 可选参数对象,key: value都为string类型
  576. * @description options列表:
  577. * result_type 期望获取结果的类型,取值为“excel”时返回xls文件的地址,取值为“json”时返回json格式的字符串,默认为”excel”
  578. * @return array
  579. */
  580. public function getTableRecognitionResult($requestId, $options = array())
  581. {
  582. $data = array();
  583. $data['request_id'] = $requestId;
  584. $data = array_merge($data, $options);
  585. return $this->request($this->tableResultGetUrl, $data);
  586. }
  587. /**
  588. * VIN码识别接口
  589. *
  590. * @param string $image - 图像数据,base64编码,要求base64编码后大小不超过4M,最短边至少15px,最长边最大4096px,支持jpg/png/bmp格式
  591. * @param array $options - 可选参数对象,key: value都为string类型
  592. * @description options列表:
  593. * @return array
  594. */
  595. public function vinCode($image, $options = array())
  596. {
  597. $data = array();
  598. $data['image'] = base64_encode($image);
  599. $data = array_merge($data, $options);
  600. return $this->request($this->vinCodeUrl, $data);
  601. }
  602. /**
  603. * 定额发票识别接口
  604. *
  605. * @param string $image - 图像数据,base64编码,要求base64编码后大小不超过4M,最短边至少15px,最长边最大4096px,支持jpg/png/bmp格式
  606. * @param array $options - 可选参数对象,key: value都为string类型
  607. * @description options列表:
  608. * @return array
  609. */
  610. public function quotaInvoice($image, $options = array())
  611. {
  612. $data = array();
  613. $data['image'] = base64_encode($image);
  614. $data = array_merge($data, $options);
  615. return $this->request($this->quotaInvoiceUrl, $data);
  616. }
  617. /**
  618. * 户口本识别接口
  619. *
  620. * @param string $image - 图像数据,base64编码,要求base64编码后大小不超过4M,最短边至少15px,最长边最大4096px,支持jpg/png/bmp格式
  621. * @param array $options - 可选参数对象,key: value都为string类型
  622. * @description options列表:
  623. * @return array
  624. */
  625. public function householdRegister($image, $options = array())
  626. {
  627. $data = array();
  628. $data['image'] = base64_encode($image);
  629. $data = array_merge($data, $options);
  630. return $this->request($this->householdRegisterUrl, $data);
  631. }
  632. /**
  633. * 港澳通行证识别接口
  634. *
  635. * @param string $image - 图像数据,base64编码,要求base64编码后大小不超过4M,最短边至少15px,最长边最大4096px,支持jpg/png/bmp格式
  636. * @param array $options - 可选参数对象,key: value都为string类型
  637. * @description options列表:
  638. * @return array
  639. */
  640. public function HKMacauExitentrypermit($image, $options = array())
  641. {
  642. $data = array();
  643. $data['image'] = base64_encode($image);
  644. $data = array_merge($data, $options);
  645. return $this->request($this->HKMacauExitentrypermitUrl, $data);
  646. }
  647. /**
  648. * 台湾通行证识别接口
  649. *
  650. * @param string $image - 图像数据,base64编码,要求base64编码后大小不超过4M,最短边至少15px,最长边最大4096px,支持jpg/png/bmp格式
  651. * @param array $options - 可选参数对象,key: value都为string类型
  652. * @description options列表:
  653. * @return array
  654. */
  655. public function taiwanExitentrypermit($image, $options = array())
  656. {
  657. $data = array();
  658. $data['image'] = base64_encode($image);
  659. $data = array_merge($data, $options);
  660. return $this->request($this->taiwanExitentrypermitUrl, $data);
  661. }
  662. /**
  663. * 出生医学证明识别接口
  664. *
  665. * @param string $image - 图像数据,base64编码,要求base64编码后大小不超过4M,最短边至少15px,最长边最大4096px,支持jpg/png/bmp格式
  666. * @param array $options - 可选参数对象,key: value都为string类型
  667. * @description options列表:
  668. * @return array
  669. */
  670. public function birthCertificate($image, $options = array())
  671. {
  672. $data = array();
  673. $data['image'] = base64_encode($image);
  674. $data = array_merge($data, $options);
  675. return $this->request($this->birthCertificateUrl, $data);
  676. }
  677. /**
  678. * 机动车销售发票识别接口
  679. *
  680. * @param string $image - 图像数据,base64编码,要求base64编码后大小不超过4M,最短边至少15px,最长边最大4096px,支持jpg/png/bmp格式
  681. * @param array $options - 可选参数对象,key: value都为string类型
  682. * @description options列表:
  683. * @return array
  684. */
  685. public function vehicleInvoice($image, $options = array())
  686. {
  687. $data = array();
  688. $data['image'] = base64_encode($image);
  689. $data = array_merge($data, $options);
  690. return $this->request($this->vehicleInvoiceUrl, $data);
  691. }
  692. /**
  693. * 车辆合格证识别接口
  694. *
  695. * @param string $image - 图像数据,base64编码,要求base64编码后大小不超过4M,最短边至少15px,最长边最大4096px,支持jpg/png/bmp格式
  696. * @param array $options - 可选参数对象,key: value都为string类型
  697. * @description options列表:
  698. * @return array
  699. */
  700. public function vehicleCertificate($image, $options = array())
  701. {
  702. $data = array();
  703. $data['image'] = base64_encode($image);
  704. $data = array_merge($data, $options);
  705. return $this->request($this->vehicleCertificateUrl, $data);
  706. }
  707. /**
  708. * 税务局通用机打发票识别接口
  709. *
  710. * @param string $image - 图像数据,base64编码,要求base64编码后大小不超过4M,最短边至少15px,最长边最大4096px,支持jpg/png/bmp格式
  711. * @param array $options - 可选参数对象,key: value都为string类型
  712. * @description options列表:
  713. * location 是否输出位置信息,true:输出位置信息,false:不输出位置信息,默认false
  714. * @return array
  715. */
  716. public function invoice($image, $options = array())
  717. {
  718. $data = array();
  719. $data['image'] = base64_encode($image);
  720. $data = array_merge($data, $options);
  721. return $this->request($this->invoiceUrl, $data);
  722. }
  723. /**
  724. * 行程单识别接口
  725. *
  726. * @param string $image - 图像数据,base64编码,要求base64编码后大小不超过4M,最短边至少15px,最长边最大4096px,支持jpg/png/bmp格式
  727. * @param array $options - 可选参数对象,key: value都为string类型
  728. * @description options列表:
  729. * location 是否输出位置信息,true:输出位置信息,false:不输出位置信息,默认false
  730. * @return array
  731. */
  732. public function airTicket($image, $options = array())
  733. {
  734. $data = array();
  735. $data['image'] = base64_encode($image);
  736. $data = array_merge($data, $options);
  737. return $this->request($this->airTicketUrl, $data);
  738. }
  739. /**
  740. * 保单识别接口
  741. *
  742. * @param string $image - 图像数据,base64编码,要求base64编码后大小不超过4M,最短边至少15px,最长边最大4096px,支持jpg/png/bmp格式
  743. * @param array $options - 可选参数对象,key: value都为string类型
  744. * @description options列表:
  745. * rkv_business 是否进行商业逻辑处理,rue:进行商业逻辑处理,false:不进行商业逻辑处理,默认true
  746. * @return array
  747. */
  748. public function insuranceDocuments($image, $options = array())
  749. {
  750. $data = array();
  751. $data['image'] = base64_encode($image);
  752. $data = array_merge($data, $options);
  753. return $this->request($this->insuranceDocumentsUrl, $data);
  754. }
  755. /**
  756. * 增值税发票识别接口
  757. *
  758. * @param string $image - 图像数据,base64编码,要求base64编码后大小不超过4M,最短边至少15px,最长边最大4096px,支持jpg/png/bmp格式
  759. * @param array $options - 可选参数对象,key: value都为string类型
  760. * @description options列表:
  761. * @return array
  762. */
  763. public function vatInvoice($image, $options = array())
  764. {
  765. $data = array();
  766. $data['image'] = base64_encode($image);
  767. $data = array_merge($data, $options);
  768. return $this->request($this->vatInvoiceUrl, $data);
  769. }
  770. /**
  771. * 二维码识别接口
  772. *
  773. * @param string $image - 图像数据,base64编码,要求base64编码后大小不超过4M,最短边至少15px,最长边最大4096px,支持jpg/png/bmp格式
  774. * @param array $options - 可选参数对象,key: value都为string类型
  775. * @description options列表:
  776. * @return array
  777. */
  778. public function qrcode($image, $options = array())
  779. {
  780. $data = array();
  781. $data['image'] = base64_encode($image);
  782. $data = array_merge($data, $options);
  783. return $this->request($this->qrcodeUrl, $data);
  784. }
  785. /**
  786. * 数字识别接口
  787. *
  788. * @param string $image - 图像数据,base64编码,要求base64编码后大小不超过4M,最短边至少15px,最长边最大4096px,支持jpg/png/bmp格式
  789. * @param array $options - 可选参数对象,key: value都为string类型
  790. * @description options列表:
  791. * recognize_granularity 是否定位单字符位置,big:不定位单字符位置,默认值;small:定位单字符位置
  792. * detect_direction 是否检测图像朝向,默认不检测,即:false。朝向是指输入图像是正常方向、逆时针旋转90/180/270度。可选值包括:<br>- true:检测朝向;<br>- false:不检测朝向。
  793. * @return array
  794. */
  795. public function numbers($image, $options = array())
  796. {
  797. $data = array();
  798. $data['image'] = base64_encode($image);
  799. $data = array_merge($data, $options);
  800. return $this->request($this->numbersUrl, $data);
  801. }
  802. /**
  803. * 彩票识别接口
  804. *
  805. * @param string $image - 图像数据,base64编码,要求base64编码后大小不超过4M,最短边至少15px,最长边最大4096px,支持jpg/png/bmp格式
  806. * @param array $options - 可选参数对象,key: value都为string类型
  807. * @description options列表:
  808. * recognize_granularity 是否定位单字符位置,big:不定位单字符位置,默认值;small:定位单字符位置
  809. * @return array
  810. */
  811. public function lottery($image, $options = array())
  812. {
  813. $data = array();
  814. $data['image'] = base64_encode($image);
  815. $data = array_merge($data, $options);
  816. return $this->request($this->lotteryUrl, $data);
  817. }
  818. /**
  819. * 护照识别接口
  820. *
  821. * @param string $image - 图像数据,base64编码,要求base64编码后大小不超过4M,最短边至少15px,最长边最大4096px,支持jpg/png/bmp格式
  822. * @param array $options - 可选参数对象,key: value都为string类型
  823. * @description options列表:
  824. * @return array
  825. */
  826. public function passport($image, $options = array())
  827. {
  828. $data = array();
  829. $data['image'] = base64_encode($image);
  830. $data = array_merge($data, $options);
  831. return $this->request($this->passportUrl, $data);
  832. }
  833. /**
  834. * 名片识别接口
  835. *
  836. * @param string $image - 图像数据,base64编码,要求base64编码后大小不超过4M,最短边至少15px,最长边最大4096px,支持jpg/png/bmp格式
  837. * @param array $options - 可选参数对象,key: value都为string类型
  838. * @description options列表:
  839. * @return array
  840. */
  841. public function businessCard($image, $options = array())
  842. {
  843. $data = array();
  844. $data['image'] = base64_encode($image);
  845. $data = array_merge($data, $options);
  846. return $this->request($this->businessCardUrl, $data);
  847. }
  848. /**
  849. * 手写文字识别接口
  850. *
  851. * @param string $image - 图像数据,base64编码,要求base64编码后大小不超过4M,最短边至少15px,最长边最大4096px,支持jpg/png/bmp格式
  852. * @param array $options - 可选参数对象,key: value都为string类型
  853. * @description options列表:
  854. * recognize_granularity 是否定位单字符位置,big:不定位单字符位置,默认值;small:定位单字符位置
  855. * @return array
  856. */
  857. public function handwriting($image, $options = array())
  858. {
  859. $data = array();
  860. $data['image'] = base64_encode($image);
  861. $data = array_merge($data, $options);
  862. return $this->request($this->handwritingUrl, $data);
  863. }
  864. /**
  865. * 自定义模板文字识别接口
  866. *
  867. * @param string $image - 图像数据,base64编码,要求base64编码后大小不超过4M,最短边至少15px,最长边最大4096px,支持jpg/png/bmp格式
  868. * @param array $options - 可选参数对象,key: value都为string类型
  869. * @description options列表:
  870. * templateSign 您在自定义文字识别平台制作的模板的ID
  871. * classifierId 分类器Id。这个参数和templateSign至少存在一个,优先使用templateSign。存在templateSign时,表示使用指定模板;如果没有templateSign而有classifierId,表示使用分类器去判断使用哪个模板
  872. * @return array
  873. */
  874. public function custom($image, $options = array())
  875. {
  876. $data = array();
  877. $data['image'] = base64_encode($image);
  878. $data = array_merge($data, $options);
  879. return $this->request($this->customUrl, $data);
  880. }
  881. /**
  882. * 同步请求
  883. * @param string $image 图像读取
  884. * @param options 接口可选参数
  885. * @return array
  886. */
  887. public function tableRecognition($image, $options = array(), $timeout = 10000)
  888. {
  889. $result = $this->tableRecognitionAsync($image);
  890. if (isset($result['error_code'])) {
  891. return $result;
  892. }
  893. $requestId = $result['result'][0]['request_id'];
  894. $count = ceil($timeout / 1000);
  895. for ($i = 0; $i < $count; $i++) {
  896. $result = $this->getTableRecognitionResult($requestId, $options);
  897. // 完成
  898. if ($result['result']['ret_code'] == 3) {
  899. break;
  900. }
  901. sleep(1);
  902. }
  903. return $result;
  904. }
  905. }