AipImageClassify.php 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  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. require_once 'lib/AipBase.php';
  18. class AipImageClassify extends AipBase {
  19. /**
  20. * 通用物体识别 advanced_general api url
  21. * @var string
  22. */
  23. private $advancedGeneralUrl = 'https://aip.baidubce.com/rest/2.0/image-classify/v2/advanced_general';
  24. /**
  25. * 菜品识别 dish_detect api url
  26. * @var string
  27. */
  28. private $dishDetectUrl = 'https://aip.baidubce.com/rest/2.0/image-classify/v2/dish';
  29. /**
  30. * 车辆识别 car_detect api url
  31. * @var string
  32. */
  33. private $carDetectUrl = 'https://aip.baidubce.com/rest/2.0/image-classify/v1/car';
  34. /**
  35. * logo商标识别 logo_search api url
  36. * @var string
  37. */
  38. private $logoSearchUrl = 'https://aip.baidubce.com/rest/2.0/image-classify/v2/logo';
  39. /**
  40. * logo商标识别—添加 logo_add api url
  41. * @var string
  42. */
  43. private $logoAddUrl = 'https://aip.baidubce.com/rest/2.0/realtime_search/v1/logo/add';
  44. /**
  45. * logo商标识别—删除 logo_delete api url
  46. * @var string
  47. */
  48. private $logoDeleteUrl = 'https://aip.baidubce.com/rest/2.0/realtime_search/v1/logo/delete';
  49. /**
  50. * 动物识别 animal_detect api url
  51. * @var string
  52. */
  53. private $animalDetectUrl = 'https://aip.baidubce.com/rest/2.0/image-classify/v1/animal';
  54. /**
  55. * 植物识别 plant_detect api url
  56. * @var string
  57. */
  58. private $plantDetectUrl = 'https://aip.baidubce.com/rest/2.0/image-classify/v1/plant';
  59. /**
  60. * 图像主体检测 object_detect api url
  61. * @var string
  62. */
  63. private $objectDetectUrl = 'https://aip.baidubce.com/rest/2.0/image-classify/v1/object_detect';
  64. /**
  65. * 通用物体识别接口
  66. *
  67. * @param string $image - 图像数据,base64编码,要求base64编码后大小不超过4M,最短边至少15px,最长边最大4096px,支持jpg/png/bmp格式
  68. * @param array $options - 可选参数对象,key: value都为string类型
  69. * @description options列表:
  70. * baike_num 返回百科信息的结果数,默认不返回
  71. * @return array
  72. */
  73. public function advancedGeneral($image, $options=array()){
  74. $data = array();
  75. $data['image'] = base64_encode($image);
  76. $data = array_merge($data, $options);
  77. return $this->request($this->advancedGeneralUrl, $data);
  78. }
  79. /**
  80. * 菜品识别接口
  81. *
  82. * @param string $image - 图像数据,base64编码,要求base64编码后大小不超过4M,最短边至少15px,最长边最大4096px,支持jpg/png/bmp格式
  83. * @param array $options - 可选参数对象,key: value都为string类型
  84. * @description options列表:
  85. * top_num 返回预测得分top结果数,默认为5
  86. * filter_threshold 默认0.95,可以通过该参数调节识别效果,降低非菜识别率.
  87. * baike_num 返回百科信息的结果数,默认不返回
  88. * @return array
  89. */
  90. public function dishDetect($image, $options=array()){
  91. $data = array();
  92. $data['image'] = base64_encode($image);
  93. $data = array_merge($data, $options);
  94. return $this->request($this->dishDetectUrl, $data);
  95. }
  96. /**
  97. * 车辆识别接口
  98. *
  99. * @param string $image - 图像数据,base64编码,要求base64编码后大小不超过4M,最短边至少15px,最长边最大4096px,支持jpg/png/bmp格式
  100. * @param array $options - 可选参数对象,key: value都为string类型
  101. * @description options列表:
  102. * top_num 返回预测得分top结果数,默认为5
  103. * baike_num 返回百科信息的结果数,默认不返回
  104. * @return array
  105. */
  106. public function carDetect($image, $options=array()){
  107. $data = array();
  108. $data['image'] = base64_encode($image);
  109. $data = array_merge($data, $options);
  110. return $this->request($this->carDetectUrl, $data);
  111. }
  112. /**
  113. * logo商标识别接口
  114. *
  115. * @param string $image - 图像数据,base64编码,要求base64编码后大小不超过4M,最短边至少15px,最长边最大4096px,支持jpg/png/bmp格式
  116. * @param array $options - 可选参数对象,key: value都为string类型
  117. * @description options列表:
  118. * custom_lib 是否只使用自定义logo库的结果,默认false:返回自定义库+默认库的识别结果
  119. * @return array
  120. */
  121. public function logoSearch($image, $options=array()){
  122. $data = array();
  123. $data['image'] = base64_encode($image);
  124. $data = array_merge($data, $options);
  125. return $this->request($this->logoSearchUrl, $data);
  126. }
  127. /**
  128. * logo商标识别—添加接口
  129. *
  130. * @param string $image - 图像数据,base64编码,要求base64编码后大小不超过4M,最短边至少15px,最长边最大4096px,支持jpg/png/bmp格式
  131. * @param string $brief - brief,检索时带回。此处要传对应的name与code字段,name长度小于100B,code长度小于150B
  132. * @param array $options - 可选参数对象,key: value都为string类型
  133. * @description options列表:
  134. * @return array
  135. */
  136. public function logoAdd($image, $brief, $options=array()){
  137. $data = array();
  138. $data['image'] = base64_encode($image);
  139. $data['brief'] = $brief;
  140. $data = array_merge($data, $options);
  141. return $this->request($this->logoAddUrl, $data);
  142. }
  143. /**
  144. * logo商标识别—删除接口
  145. *
  146. * @param string $image - 图像数据,base64编码,要求base64编码后大小不超过4M,最短边至少15px,最长边最大4096px,支持jpg/png/bmp格式
  147. * @param array $options - 可选参数对象,key: value都为string类型
  148. * @description options列表:
  149. * @return array
  150. */
  151. public function logoDeleteByImage($image, $options=array()){
  152. $data = array();
  153. $data['image'] = base64_encode($image);
  154. $data = array_merge($data, $options);
  155. return $this->request($this->logoDeleteUrl, $data);
  156. }
  157. /**
  158. * logo商标识别—删除接口
  159. *
  160. * @param string $contSign - 图片签名(和image二选一,image优先级更高)
  161. * @param array $options - 可选参数对象,key: value都为string类型
  162. * @description options列表:
  163. * @return array
  164. */
  165. public function logoDeleteBySign($contSign, $options=array()){
  166. $data = array();
  167. $data['cont_sign'] = $contSign;
  168. $data = array_merge($data, $options);
  169. return $this->request($this->logoDeleteUrl, $data);
  170. }
  171. /**
  172. * 动物识别接口
  173. *
  174. * @param string $image - 图像数据,base64编码,要求base64编码后大小不超过4M,最短边至少15px,最长边最大4096px,支持jpg/png/bmp格式
  175. * @param array $options - 可选参数对象,key: value都为string类型
  176. * @description options列表:
  177. * top_num 返回预测得分top结果数,默认为6
  178. * baike_num 返回百科信息的结果数,默认不返回
  179. * @return array
  180. */
  181. public function animalDetect($image, $options=array()){
  182. $data = array();
  183. $data['image'] = base64_encode($image);
  184. $data = array_merge($data, $options);
  185. return $this->request($this->animalDetectUrl, $data);
  186. }
  187. /**
  188. * 植物识别接口
  189. *
  190. * @param string $image - 图像数据,base64编码,要求base64编码后大小不超过4M,最短边至少15px,最长边最大4096px,支持jpg/png/bmp格式
  191. * @param array $options - 可选参数对象,key: value都为string类型
  192. * @description options列表:
  193. * baike_num 返回百科信息的结果数,默认不返回
  194. * @return array
  195. */
  196. public function plantDetect($image, $options=array()){
  197. $data = array();
  198. $data['image'] = base64_encode($image);
  199. $data = array_merge($data, $options);
  200. return $this->request($this->plantDetectUrl, $data);
  201. }
  202. /**
  203. * 图像主体检测接口
  204. *
  205. * @param string $image - 图像数据,base64编码,要求base64编码后大小不超过4M,最短边至少15px,最长边最大4096px,支持jpg/png/bmp格式
  206. * @param array $options - 可选参数对象,key: value都为string类型
  207. * @description options列表:
  208. * with_face 如果检测主体是人,主体区域是否带上人脸部分,0-不带人脸区域,其他-带人脸区域,裁剪类需求推荐带人脸,检索/识别类需求推荐不带人脸。默认取1,带人脸。
  209. * @return array
  210. */
  211. public function objectDetect($image, $options=array()){
  212. $data = array();
  213. $data['image'] = base64_encode($image);
  214. $data = array_merge($data, $options);
  215. return $this->request($this->objectDetectUrl, $data);
  216. }
  217. }