AipImageClassify.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407
  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. * 地标识别 landmark api url
  66. * @var string
  67. */
  68. private $landmarkUrl = 'https://aip.baidubce.com/rest/2.0/image-classify/v1/landmark';
  69. /**
  70. * 花卉识别 flower api url
  71. * @var string
  72. */
  73. private $flowerUrl = 'https://aip.baidubce.com/rest/2.0/image-classify/v1/flower';
  74. /**
  75. * 食材识别 ingredient api url
  76. * @var string
  77. */
  78. private $ingredientUrl = 'https://aip.baidubce.com/rest/2.0/image-classify/v1/classify/ingredient';
  79. /**
  80. * 红酒识别 redwine api url
  81. * @var string
  82. */
  83. private $redwineUrl = 'https://aip.baidubce.com/rest/2.0/image-classify/v1/redwine';
  84. /**
  85. * 货币识别 currency api url
  86. * @var string
  87. */
  88. private $currencyUrl = 'https://aip.baidubce.com/rest/2.0/image-classify/v1/currency';
  89. /**
  90. * 通用物体识别接口
  91. *
  92. * @param string $image - 图像数据,base64编码,要求base64编码后大小不超过4M,最短边至少15px,最长边最大4096px,支持jpg/png/bmp格式
  93. * @param array $options - 可选参数对象,key: value都为string类型
  94. * @description options列表:
  95. * baike_num 返回百科信息的结果数,默认不返回
  96. * @return array
  97. */
  98. public function advancedGeneral($image, $options=array()){
  99. $data = array();
  100. $data['image'] = base64_encode($image);
  101. $data = array_merge($data, $options);
  102. return $this->request($this->advancedGeneralUrl, $data);
  103. }
  104. /**
  105. * 菜品识别接口
  106. *
  107. * @param string $image - 图像数据,base64编码,要求base64编码后大小不超过4M,最短边至少15px,最长边最大4096px,支持jpg/png/bmp格式
  108. * @param array $options - 可选参数对象,key: value都为string类型
  109. * @description options列表:
  110. * top_num 返回预测得分top结果数,默认为5
  111. * filter_threshold 默认0.95,可以通过该参数调节识别效果,降低非菜识别率.
  112. * baike_num 返回百科信息的结果数,默认不返回
  113. * @return array
  114. */
  115. public function dishDetect($image, $options=array()){
  116. $data = array();
  117. $data['image'] = base64_encode($image);
  118. $data = array_merge($data, $options);
  119. return $this->request($this->dishDetectUrl, $data);
  120. }
  121. /**
  122. * 车辆识别接口
  123. *
  124. * @param string $image - 图像数据,base64编码,要求base64编码后大小不超过4M,最短边至少15px,最长边最大4096px,支持jpg/png/bmp格式
  125. * @param array $options - 可选参数对象,key: value都为string类型
  126. * @description options列表:
  127. * top_num 返回预测得分top结果数,默认为5
  128. * baike_num 返回百科信息的结果数,默认不返回
  129. * @return array
  130. */
  131. public function carDetect($image, $options=array()){
  132. $data = array();
  133. $data['image'] = base64_encode($image);
  134. $data = array_merge($data, $options);
  135. return $this->request($this->carDetectUrl, $data);
  136. }
  137. /**
  138. * logo商标识别接口
  139. *
  140. * @param string $image - 图像数据,base64编码,要求base64编码后大小不超过4M,最短边至少15px,最长边最大4096px,支持jpg/png/bmp格式
  141. * @param array $options - 可选参数对象,key: value都为string类型
  142. * @description options列表:
  143. * custom_lib 是否只使用自定义logo库的结果,默认false:返回自定义库+默认库的识别结果
  144. * @return array
  145. */
  146. public function logoSearch($image, $options=array()){
  147. $data = array();
  148. $data['image'] = base64_encode($image);
  149. $data = array_merge($data, $options);
  150. return $this->request($this->logoSearchUrl, $data);
  151. }
  152. /**
  153. * logo商标识别—添加接口
  154. *
  155. * @param string $image - 图像数据,base64编码,要求base64编码后大小不超过4M,最短边至少15px,最长边最大4096px,支持jpg/png/bmp格式
  156. * @param string $brief - brief,检索时带回。此处要传对应的name与code字段,name长度小于100B,code长度小于150B
  157. * @param array $options - 可选参数对象,key: value都为string类型
  158. * @description options列表:
  159. * @return array
  160. */
  161. public function logoAdd($image, $brief, $options=array()){
  162. $data = array();
  163. $data['image'] = base64_encode($image);
  164. $data['brief'] = $brief;
  165. $data = array_merge($data, $options);
  166. return $this->request($this->logoAddUrl, $data);
  167. }
  168. /**
  169. * logo商标识别—删除接口
  170. *
  171. * @param string $image - 图像数据,base64编码,要求base64编码后大小不超过4M,最短边至少15px,最长边最大4096px,支持jpg/png/bmp格式
  172. * @param array $options - 可选参数对象,key: value都为string类型
  173. * @description options列表:
  174. * @return array
  175. */
  176. public function logoDeleteByImage($image, $options=array()){
  177. $data = array();
  178. $data['image'] = base64_encode($image);
  179. $data = array_merge($data, $options);
  180. return $this->request($this->logoDeleteUrl, $data);
  181. }
  182. /**
  183. * logo商标识别—删除接口
  184. *
  185. * @param string $contSign - 图片签名(和image二选一,image优先级更高)
  186. * @param array $options - 可选参数对象,key: value都为string类型
  187. * @description options列表:
  188. * @return array
  189. */
  190. public function logoDeleteBySign($contSign, $options=array()){
  191. $data = array();
  192. $data['cont_sign'] = $contSign;
  193. $data = array_merge($data, $options);
  194. return $this->request($this->logoDeleteUrl, $data);
  195. }
  196. /**
  197. * 动物识别接口
  198. *
  199. * @param string $image - 图像数据,base64编码,要求base64编码后大小不超过4M,最短边至少15px,最长边最大4096px,支持jpg/png/bmp格式
  200. * @param array $options - 可选参数对象,key: value都为string类型
  201. * @description options列表:
  202. * top_num 返回预测得分top结果数,默认为6
  203. * baike_num 返回百科信息的结果数,默认不返回
  204. * @return array
  205. */
  206. public function animalDetect($image, $options=array()){
  207. $data = array();
  208. $data['image'] = base64_encode($image);
  209. $data = array_merge($data, $options);
  210. return $this->request($this->animalDetectUrl, $data);
  211. }
  212. /**
  213. * 植物识别接口
  214. *
  215. * @param string $image - 图像数据,base64编码,要求base64编码后大小不超过4M,最短边至少15px,最长边最大4096px,支持jpg/png/bmp格式
  216. * @param array $options - 可选参数对象,key: value都为string类型
  217. * @description options列表:
  218. * baike_num 返回百科信息的结果数,默认不返回
  219. * @return array
  220. */
  221. public function plantDetect($image, $options=array()){
  222. $data = array();
  223. $data['image'] = base64_encode($image);
  224. $data = array_merge($data, $options);
  225. return $this->request($this->plantDetectUrl, $data);
  226. }
  227. /**
  228. * 图像主体检测接口
  229. *
  230. * @param string $image - 图像数据,base64编码,要求base64编码后大小不超过4M,最短边至少15px,最长边最大4096px,支持jpg/png/bmp格式
  231. * @param array $options - 可选参数对象,key: value都为string类型
  232. * @description options列表:
  233. * with_face 如果检测主体是人,主体区域是否带上人脸部分,0-不带人脸区域,其他-带人脸区域,裁剪类需求推荐带人脸,检索/识别类需求推荐不带人脸。默认取1,带人脸。
  234. * @return array
  235. */
  236. public function objectDetect($image, $options=array()){
  237. $data = array();
  238. $data['image'] = base64_encode($image);
  239. $data = array_merge($data, $options);
  240. return $this->request($this->objectDetectUrl, $data);
  241. }
  242. /**
  243. * 地标识别接口
  244. *
  245. * @param string $image - 图像数据,base64编码,要求base64编码后大小不超过4M,最短边至少15px,最长边最大4096px,支持jpg/png/bmp格式
  246. * @param array $options - 可选参数对象,key: value都为string类型
  247. * @description options列表:
  248. * @return array
  249. */
  250. public function landmark($image, $options=array()){
  251. $data = array();
  252. $data['image'] = base64_encode($image);
  253. $data = array_merge($data, $options);
  254. return $this->request($this->landmarkUrl, $data);
  255. }
  256. /**
  257. * 花卉识别接口
  258. *
  259. * @param string $image - 图像数据,base64编码,要求base64编码后大小不超过4M,最短边至少15px,最长边最大4096px,支持jpg/png/bmp格式
  260. * @param array $options - 可选参数对象,key: value都为string类型
  261. * @description options列表:
  262. * top_num 返回预测得分top结果数,默认为5
  263. * baike_num 返回百科信息的结果数,默认不返回
  264. * @return array
  265. */
  266. public function flower($image, $options=array()){
  267. $data = array();
  268. $data['image'] = base64_encode($image);
  269. $data = array_merge($data, $options);
  270. return $this->request($this->flowerUrl, $data);
  271. }
  272. /**
  273. * 食材识别接口
  274. *
  275. * @param string $image - 图像数据,base64编码,要求base64编码后大小不超过4M,最短边至少15px,最长边最大4096px,支持jpg/png/bmp格式
  276. * @param array $options - 可选参数对象,key: value都为string类型
  277. * @description options列表:
  278. * top_num 返回预测得分top结果数,如果为空或小于等于0默认为5;如果大于20默认20
  279. * @return array
  280. */
  281. public function ingredient($image, $options=array()){
  282. $data = array();
  283. $data['image'] = base64_encode($image);
  284. $data = array_merge($data, $options);
  285. return $this->request($this->ingredientUrl, $data);
  286. }
  287. /**
  288. * 红酒识别接口
  289. *
  290. * @param string $image - 图像数据,base64编码,要求base64编码后大小不超过4M,最短边至少15px,最长边最大4096px,支持jpg/png/bmp格式
  291. * @param array $options - 可选参数对象,key: value都为string类型
  292. * @description options列表:
  293. * @return array
  294. */
  295. public function redwine($image, $options=array()){
  296. $data = array();
  297. $data['image'] = base64_encode($image);
  298. $data = array_merge($data, $options);
  299. return $this->request($this->redwineUrl, $data);
  300. }
  301. /**
  302. * 货币识别接口
  303. *
  304. * @param string $image - 图像数据,base64编码,要求base64编码后大小不超过4M,最短边至少15px,最长边最大4096px,支持jpg/png/bmp格式
  305. * @param array $options - 可选参数对象,key: value都为string类型
  306. * @description options列表:
  307. * @return array
  308. */
  309. public function currency($image, $options=array()){
  310. $data = array();
  311. $data['image'] = base64_encode($image);
  312. $data = array_merge($data, $options);
  313. return $this->request($this->currencyUrl, $data);
  314. }
  315. }