AipImageClassify.php 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634
  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. * 车辆检测 vehicle_detect api url
  36. * @var string
  37. */
  38. private $vehicleDetectUrl = 'https://aip.baidubce.com/rest/2.0/image-classify/v1/vehicle_detect';
  39. /**
  40. * 车辆外观损伤识别 vehicle_damage api url
  41. * @var string
  42. */
  43. private $vehicleDamageUrl = 'https://aip.baidubce.com/rest/2.0/image-classify/v1/vehicle_damage';
  44. /**
  45. * logo商标识别 logo_search api url
  46. * @var string
  47. */
  48. private $logoSearchUrl = 'https://aip.baidubce.com/rest/2.0/image-classify/v2/logo';
  49. /**
  50. * logo商标识别—添加 logo_add api url
  51. * @var string
  52. */
  53. private $logoAddUrl = 'https://aip.baidubce.com/rest/2.0/realtime_search/v1/logo/add';
  54. /**
  55. * logo商标识别—删除 logo_delete api url
  56. * @var string
  57. */
  58. private $logoDeleteUrl = 'https://aip.baidubce.com/rest/2.0/realtime_search/v1/logo/delete';
  59. /**
  60. * 动物识别 animal_detect api url
  61. * @var string
  62. */
  63. private $animalDetectUrl = 'https://aip.baidubce.com/rest/2.0/image-classify/v1/animal';
  64. /**
  65. * 植物识别 plant_detect api url
  66. * @var string
  67. */
  68. private $plantDetectUrl = 'https://aip.baidubce.com/rest/2.0/image-classify/v1/plant';
  69. /**
  70. * 图像主体检测 object_detect api url
  71. * @var string
  72. */
  73. private $objectDetectUrl = 'https://aip.baidubce.com/rest/2.0/image-classify/v1/object_detect';
  74. /**
  75. * 地标识别 landmark api url
  76. * @var string
  77. */
  78. private $landmarkUrl = 'https://aip.baidubce.com/rest/2.0/image-classify/v1/landmark';
  79. /**
  80. * 花卉识别 flower api url
  81. * @var string
  82. */
  83. private $flowerUrl = 'https://aip.baidubce.com/rest/2.0/image-classify/v1/flower';
  84. /**
  85. * 食材识别 ingredient api url
  86. * @var string
  87. */
  88. private $ingredientUrl = 'https://aip.baidubce.com/rest/2.0/image-classify/v1/classify/ingredient';
  89. /**
  90. * 红酒识别 redwine api url
  91. * @var string
  92. */
  93. private $redwineUrl = 'https://aip.baidubce.com/rest/2.0/image-classify/v1/redwine';
  94. /**
  95. * 货币识别 currency api url
  96. * @var string
  97. */
  98. private $currencyUrl = 'https://aip.baidubce.com/rest/2.0/image-classify/v1/currency';
  99. /**
  100. * 菜品识别-添加
  101. * @var string
  102. */
  103. private $customDishAddUrl = "https://aip.baidubce.com/rest/2.0/image-classify/v1/realtime_search/dish/add";
  104. /**
  105. * 菜品识别-搜索
  106. * @var string
  107. */
  108. private $customDishSearchUrl = "https://aip.baidubce.com/rest/2.0/image-classify/v1/realtime_search/dish/search";
  109. /**
  110. * 菜品识别-删除
  111. * @var string
  112. */
  113. private $customDishDeleteUrl = "https://aip.baidubce.com/rest/2.0/image-classify/v1/realtime_search/dish/delete";
  114. /**
  115. * 多目标识别
  116. * @var string
  117. */
  118. private $multiObjectDetectUrl = "https://aip.baidubce.com/rest/2.0/image-classify/v1/multi_object_detect";
  119. /**
  120. * 组合接口
  121. * @var string
  122. */
  123. private $combinationUrl = "https://aip.baidubce.com/api/v1/solution/direct/imagerecognition/combination";
  124. /**
  125. * 通用物体识别接口
  126. *
  127. * @param string $image - 图像数据,base64编码,要求base64编码后大小不超过4M,最短边至少15px,最长边最大4096px,支持jpg/png/bmp格式
  128. * @param array $options - 可选参数对象,key: value都为string类型
  129. * @description options列表:
  130. * baike_num 返回百科信息的结果数,默认不返回
  131. * @return array
  132. */
  133. public function advancedGeneral($image, $options=array()){
  134. $data = array();
  135. $data['image'] = base64_encode($image);
  136. $data = array_merge($data, $options);
  137. return $this->request($this->advancedGeneralUrl, $data);
  138. }
  139. /**
  140. * 菜品识别接口
  141. *
  142. * @param string $image - 图像数据,base64编码,要求base64编码后大小不超过4M,最短边至少15px,最长边最大4096px,支持jpg/png/bmp格式
  143. * @param array $options - 可选参数对象,key: value都为string类型
  144. * @description options列表:
  145. * top_num 返回预测得分top结果数,默认为5
  146. * filter_threshold 默认0.95,可以通过该参数调节识别效果,降低非菜识别率.
  147. * baike_num 返回百科信息的结果数,默认不返回
  148. * @return array
  149. */
  150. public function dishDetect($image, $options=array()){
  151. $data = array();
  152. $data['image'] = base64_encode($image);
  153. $data = array_merge($data, $options);
  154. return $this->request($this->dishDetectUrl, $data);
  155. }
  156. /**
  157. * 车辆识别接口
  158. *
  159. * @param string $image - 图像数据,base64编码,要求base64编码后大小不超过4M,最短边至少15px,最长边最大4096px,支持jpg/png/bmp格式
  160. * @param array $options - 可选参数对象,key: value都为string类型
  161. * @description options列表:
  162. * top_num 返回预测得分top结果数,默认为5
  163. * baike_num 返回百科信息的结果数,默认不返回
  164. * @return array
  165. */
  166. public function carDetect($image, $options=array()){
  167. $data = array();
  168. $data['image'] = base64_encode($image);
  169. $data = array_merge($data, $options);
  170. return $this->request($this->carDetectUrl, $data);
  171. }
  172. /**
  173. * 车辆检测接口
  174. *
  175. * @param string $image - 图像数据,base64编码,要求base64编码后大小不超过4M,最短边至少15px,最长边最大4096px,支持jpg/png/bmp格式
  176. * @param array $options - 可选参数对象,key: value都为string类型
  177. * @description options列表:
  178. * show 是否返回结果图(含统计值和跟踪框)。选true时返回渲染后的图片(base64),其它无效值或为空则默认false。
  179. * area 只统计该区域内的车辆数,缺省时为全图统计。<br>逗号分隔,如‘x1,y1,x2,y2,x3,y3...xn,yn',按顺序依次给出每个顶点的x、y坐标(默认尾点和首点相连),形成闭合多边形区域。<br>服务会做范围(顶点左边需在图像范围内)及个数校验(数组长度必须为偶数,且大于3个顶点)。只支持单个多边形区域,建议设置矩形框,即4个顶点。**坐标取值不能超过图像宽度和高度,比如1280的宽度,坐标值最大到1279**。
  180. * @return array
  181. */
  182. public function vehicleDetect($image, $options=array()){
  183. $data = array();
  184. $data['image'] = base64_encode($image);
  185. $data = array_merge($data, $options);
  186. return $this->request($this->vehicleDetectUrl, $data);
  187. }
  188. /**
  189. * 车辆外观损伤识别接口
  190. *
  191. * @param string $image - 图像数据,base64编码,要求base64编码后大小不超过4M,最短边至少15px,最长边最大4096px,支持jpg/png/bmp格式
  192. * @param array $options - 可选参数对象,key: value都为string类型
  193. * @description options列表:
  194. * @return array
  195. */
  196. public function vehicleDamage($image, $options=array()){
  197. $data = array();
  198. $data['image'] = base64_encode($image);
  199. $data = array_merge($data, $options);
  200. return $this->request($this->vehicleDamageUrl, $data);
  201. }
  202. /**
  203. * logo商标识别接口
  204. *
  205. * @param string $image - 图像数据,base64编码,要求base64编码后大小不超过4M,最短边至少15px,最长边最大4096px,支持jpg/png/bmp格式
  206. * @param array $options - 可选参数对象,key: value都为string类型
  207. * @description options列表:
  208. * custom_lib 是否只使用自定义logo库的结果,默认false:返回自定义库+默认库的识别结果
  209. * @return array
  210. */
  211. public function logoSearch($image, $options=array()){
  212. $data = array();
  213. $data['image'] = base64_encode($image);
  214. $data = array_merge($data, $options);
  215. return $this->request($this->logoSearchUrl, $data);
  216. }
  217. /**
  218. * logo商标识别—添加接口
  219. *
  220. * @param string $image - 图像数据,base64编码,要求base64编码后大小不超过4M,最短边至少15px,最长边最大4096px,支持jpg/png/bmp格式
  221. * @param string $brief - brief,检索时带回。此处要传对应的name与code字段,name长度小于100B,code长度小于150B
  222. * @param array $options - 可选参数对象,key: value都为string类型
  223. * @description options列表:
  224. * @return array
  225. */
  226. public function logoAdd($image, $brief, $options=array()){
  227. $data = array();
  228. $data['image'] = base64_encode($image);
  229. $data['brief'] = $brief;
  230. $data = array_merge($data, $options);
  231. return $this->request($this->logoAddUrl, $data);
  232. }
  233. /**
  234. * logo商标识别—删除接口
  235. *
  236. * @param string $image - 图像数据,base64编码,要求base64编码后大小不超过4M,最短边至少15px,最长边最大4096px,支持jpg/png/bmp格式
  237. * @param array $options - 可选参数对象,key: value都为string类型
  238. * @description options列表:
  239. * @return array
  240. */
  241. public function logoDeleteByImage($image, $options=array()){
  242. $data = array();
  243. $data['image'] = base64_encode($image);
  244. $data = array_merge($data, $options);
  245. return $this->request($this->logoDeleteUrl, $data);
  246. }
  247. /**
  248. * logo商标识别—删除接口
  249. *
  250. * @param string $contSign - 图片签名(和image二选一,image优先级更高)
  251. * @param array $options - 可选参数对象,key: value都为string类型
  252. * @description options列表:
  253. * @return array
  254. */
  255. public function logoDeleteBySign($contSign, $options=array()){
  256. $data = array();
  257. $data['cont_sign'] = $contSign;
  258. $data = array_merge($data, $options);
  259. return $this->request($this->logoDeleteUrl, $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. * top_num 返回预测得分top结果数,默认为6
  268. * baike_num 返回百科信息的结果数,默认不返回
  269. * @return array
  270. */
  271. public function animalDetect($image, $options=array()){
  272. $data = array();
  273. $data['image'] = base64_encode($image);
  274. $data = array_merge($data, $options);
  275. return $this->request($this->animalDetectUrl, $data);
  276. }
  277. /**
  278. * 植物识别接口
  279. *
  280. * @param string $image - 图像数据,base64编码,要求base64编码后大小不超过4M,最短边至少15px,最长边最大4096px,支持jpg/png/bmp格式
  281. * @param array $options - 可选参数对象,key: value都为string类型
  282. * @description options列表:
  283. * baike_num 返回百科信息的结果数,默认不返回
  284. * @return array
  285. */
  286. public function plantDetect($image, $options=array()){
  287. $data = array();
  288. $data['image'] = base64_encode($image);
  289. $data = array_merge($data, $options);
  290. return $this->request($this->plantDetectUrl, $data);
  291. }
  292. /**
  293. * 图像主体检测接口
  294. *
  295. * @param string $image - 图像数据,base64编码,要求base64编码后大小不超过4M,最短边至少15px,最长边最大4096px,支持jpg/png/bmp格式
  296. * @param array $options - 可选参数对象,key: value都为string类型
  297. * @description options列表:
  298. * with_face 如果检测主体是人,主体区域是否带上人脸部分,0-不带人脸区域,其他-带人脸区域,裁剪类需求推荐带人脸,检索/识别类需求推荐不带人脸。默认取1,带人脸。
  299. * @return array
  300. */
  301. public function objectDetect($image, $options=array()){
  302. $data = array();
  303. $data['image'] = base64_encode($image);
  304. $data = array_merge($data, $options);
  305. return $this->request($this->objectDetectUrl, $data);
  306. }
  307. /**
  308. * 地标识别接口
  309. *
  310. * @param string $image - 图像数据,base64编码,要求base64编码后大小不超过4M,最短边至少15px,最长边最大4096px,支持jpg/png/bmp格式
  311. * @param array $options - 可选参数对象,key: value都为string类型
  312. * @description options列表:
  313. * @return array
  314. */
  315. public function landmark($image, $options=array()){
  316. $data = array();
  317. $data['image'] = base64_encode($image);
  318. $data = array_merge($data, $options);
  319. return $this->request($this->landmarkUrl, $data);
  320. }
  321. /**
  322. * 花卉识别接口
  323. *
  324. * @param string $image - 图像数据,base64编码,要求base64编码后大小不超过4M,最短边至少15px,最长边最大4096px,支持jpg/png/bmp格式
  325. * @param array $options - 可选参数对象,key: value都为string类型
  326. * @description options列表:
  327. * top_num 返回预测得分top结果数,默认为5
  328. * baike_num 返回百科信息的结果数,默认不返回
  329. * @return array
  330. */
  331. public function flower($image, $options=array()){
  332. $data = array();
  333. $data['image'] = base64_encode($image);
  334. $data = array_merge($data, $options);
  335. return $this->request($this->flowerUrl, $data);
  336. }
  337. /**
  338. * 食材识别接口
  339. *
  340. * @param string $image - 图像数据,base64编码,要求base64编码后大小不超过4M,最短边至少15px,最长边最大4096px,支持jpg/png/bmp格式
  341. * @param array $options - 可选参数对象,key: value都为string类型
  342. * @description options列表:
  343. * top_num 返回预测得分top结果数,如果为空或小于等于0默认为5;如果大于20默认20
  344. * @return array
  345. */
  346. public function ingredient($image, $options=array()){
  347. $data = array();
  348. $data['image'] = base64_encode($image);
  349. $data = array_merge($data, $options);
  350. return $this->request($this->ingredientUrl, $data);
  351. }
  352. /**
  353. * 红酒识别接口
  354. *
  355. * @param string $image - 图像数据,base64编码,要求base64编码后大小不超过4M,最短边至少15px,最长边最大4096px,支持jpg/png/bmp格式
  356. * @param array $options - 可选参数对象,key: value都为string类型
  357. * @description options列表:
  358. * @return array
  359. */
  360. public function redwine($image, $options=array()){
  361. $data = array();
  362. $data['image'] = base64_encode($image);
  363. $data = array_merge($data, $options);
  364. return $this->request($this->redwineUrl, $data);
  365. }
  366. /**
  367. * 货币识别接口
  368. *
  369. * @param string $image - 图像数据,base64编码,要求base64编码后大小不超过4M,最短边至少15px,最长边最大4096px,支持jpg/png/bmp格式
  370. * @param array $options - 可选参数对象,key: value都为string类型
  371. * @description options列表:
  372. * @return array
  373. */
  374. public function currency($image, $options=array()){
  375. $data = array();
  376. $data['image'] = base64_encode($image);
  377. $data = array_merge($data, $options);
  378. return $this->request($this->currencyUrl, $data);
  379. }
  380. /**
  381. * 自定义菜品识别—入库
  382. *
  383. * @param string $image - 图像数据,base64编码,要求base64编码后大小不超过4M,最短边至少15px,最长边最大4096px,支持jpg/png/bmp格式
  384. * @param array $options - 可选参数对象,key: value都为string类型
  385. * @description options列表:
  386. * @return array
  387. */
  388. public function customDishesAddImage($image, $brief, $options=array()){
  389. $data = array();
  390. $data['image'] = base64_encode($image);
  391. $data['brief'] = $brief;
  392. $data = array_merge($data, $options);
  393. return $this->request($this->customDishAddUrl, $data);
  394. }
  395. /**
  396. * 自定义菜品识别—检索
  397. *
  398. * @param string $image - 图像数据,base64编码,要求base64编码后大小不超过4M,最短边至少15px,最长边最大4096px,支持jpg/png/bmp格式
  399. * @param array $options - 可选参数对象,key: value都为string类型
  400. * @description options列表:
  401. * @return array
  402. */
  403. public function customDishesSearch($image, $options=array()){
  404. $data = array();
  405. $data['image'] = base64_encode($image);
  406. $data = array_merge($data, $options);
  407. return $this->request($this->customDishSearchUrl, $data);
  408. }
  409. /**
  410. * 自定义菜品识别—删除
  411. *
  412. * @param string $image - 图像数据,base64编码,要求base64编码后大小不超过4M,最短边至少15px,最长边最大4096px,支持jpg/png/bmp格式
  413. * @param array $options - 可选参数对象,key: value都为string类型
  414. * @description options列表:
  415. * @return array
  416. */
  417. public function customDishesDeleteImage($image, $options=array()){
  418. $data = array();
  419. $data['image'] = base64_encode($image);
  420. $data = array_merge($data, $options);
  421. return $this->request($this->customDishDeleteUrl, $data);
  422. }
  423. /**
  424. * 自定义菜品识别—删除
  425. *
  426. * @param string $image - 图像数据签名
  427. * @param array $options - 可选参数对象,key: value都为string类型
  428. * @description options列表:
  429. * @return array
  430. */
  431. public function customDishesDeleteContSign($contSign, $options=array()){
  432. $data = array();
  433. $data['cont_sign'] = $contSign;
  434. $data = array_merge($data, $options);
  435. return $this->request($this->customDishDeleteUrl, $data);
  436. }
  437. /**
  438. * 图像多主体检测
  439. *
  440. * @param string $image - 图像数据,base64编码,要求base64编码后大小不超过4M,最短边至少15px,最长边最大4096px,支持jpg/png/bmp格式
  441. * @param array $options - 可选参数对象,key: value都为string类型
  442. * @description options列表:
  443. * @return array
  444. */
  445. public function multiObjectDetect($image, $options=array()){
  446. $data = array();
  447. $data['image'] = base64_encode($image);
  448. $data = array_merge($data, $options);
  449. return $this->request($this->multiObjectDetectUrl, $data);
  450. }
  451. /**
  452. * 组合接口-image
  453. *
  454. * @param string $image - 图像数据,base64编码,要求base64编码后大小不超过4M,最短边至少15px,最长边最大4096px,支持jpg/png/bmp格式
  455. * @param array $options - 可选参数对象,key: value都为string类型
  456. * @description options列表:
  457. * @return array
  458. */
  459. public function combinationByImage($image, $scenes, $options=array()){
  460. $data = array();
  461. $data['image'] = base64_encode($image);
  462. $data['scenes'] = $scenes;
  463. $data = array_merge($data, $options);
  464. return $this->request($this->combinationUrl, json_encode($data), array('Content-Type' => 'application/json;charset=utf-8'));
  465. }
  466. /**
  467. * 组合接口-imageUrl
  468. *
  469. * @param string $imageURl - 图像数据url
  470. * @param array $options - 可选参数对象,key: value都为string类型
  471. * @description options列表:
  472. * @return array
  473. */
  474. public function combinationByImageUrl($imageUrl, $scenes, $options=array()){
  475. $data = array();
  476. $data['imgUrl'] = $imageUrl;
  477. $data['scenes'] = $scenes;
  478. $data = array_merge($data, $options);
  479. return $this->request($this->combinationUrl, json_encode($data), array('Content-Type' => 'application/json;charset=utf-8'));
  480. }
  481. }