AipImageCensor.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  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. /**
  19. * 黄反识别
  20. */
  21. class AipImageCensor extends AipBase{
  22. /**
  23. * antiporn api url
  24. * @var string
  25. */
  26. private $antiPornUrl = 'https://aip.baidubce.com/rest/2.0/antiporn/v1/detect';
  27. /**
  28. * antiporn gif api url
  29. * @var string
  30. */
  31. private $antiPornGifUrl = 'https://aip.baidubce.com/rest/2.0/antiporn/v1/detect_gif';
  32. /**
  33. * antiterror api url
  34. * @var string
  35. */
  36. private $antiTerrorUrl = 'https://aip.baidubce.com/rest/2.0/antiterror/v1/detect';
  37. /**
  38. * @var string
  39. */
  40. private $faceAuditUrl = 'https://aip.baidubce.com/rest/2.0/solution/v1/face_audit';
  41. /**
  42. * @var string
  43. */
  44. private $imageCensorCombUrl = 'https://aip.baidubce.com/api/v1/solution/direct/img_censor';
  45. /**
  46. * @var string
  47. */
  48. private $imageCensorUserDefinedUrl = 'https://aip.baidubce.com/rest/2.0/solution/v1/img_censor/user_defined';
  49. /**
  50. * @param string $image 图像读取
  51. * @return array
  52. */
  53. public function antiPorn($image){
  54. $data = array();
  55. $data['image'] = base64_encode($image);
  56. return $this->request($this->antiPornUrl, $data);
  57. }
  58. /**
  59. * @param string $image 图像读取
  60. * @return array
  61. */
  62. public function multi_antiporn($images){
  63. $data = array();
  64. foreach($images as $image){
  65. $data[] = array(
  66. 'image' => base64_encode($image),
  67. );
  68. }
  69. return $this->multi_request($this->antiPornUrl, $data);
  70. }
  71. /**
  72. * @param string $image 图像读取
  73. * @return array
  74. */
  75. public function antiPornGif($image){
  76. $data = array();
  77. $data['image'] = base64_encode($image);
  78. return $this->request($this->antiPornGifUrl, $data);
  79. }
  80. /**
  81. * @param string $image 图像读取
  82. * @return array
  83. */
  84. public function antiTerror($image){
  85. $data = array();
  86. $data['image'] = base64_encode($image);
  87. return $this->request($this->antiTerrorUrl, $data);
  88. }
  89. /**
  90. * @param string $images 图像读取
  91. * @return array
  92. */
  93. public function faceAudit($images, $configId=''){
  94. // 非数组则处理为数组
  95. if(!is_array($images)){
  96. $images = array(
  97. $images,
  98. );
  99. }
  100. $data = array(
  101. 'configId' => $configId,
  102. );
  103. $isUrl = substr(trim($images[0]), 0, 4) === 'http';
  104. if(!$isUrl){
  105. $arr = array();
  106. foreach($images as $image){
  107. $arr[] = base64_encode($image);
  108. }
  109. $data['images'] = implode(',', $arr);
  110. }else{
  111. $urls = array();
  112. foreach($images as $url){
  113. $urls[] = urlencode($url);
  114. }
  115. $data['imgUrls'] = implode(',', $urls);
  116. }
  117. return $this->request($this->faceAuditUrl, $data);
  118. }
  119. /**
  120. * @param string $image 图像读取
  121. * @return array
  122. */
  123. public function imageCensorComb($image, $scenes='antiporn', $options=array()){
  124. $scenes = !is_array($scenes) ? explode(',', $scenes) : $scenes;
  125. $data = array(
  126. 'scenes' => $scenes,
  127. );
  128. $isUrl = substr(trim($image), 0, 4) === 'http';
  129. if(!$isUrl){
  130. $data['image'] = base64_encode($image);
  131. }else{
  132. $data['imgUrl'] = $image;
  133. }
  134. $data = array_merge($data, $options);
  135. return $this->request($this->imageCensorCombUrl, json_encode($data), array(
  136. 'Content-Type' => 'application/json',
  137. ));
  138. }
  139. /**
  140. * @param string $image 图像
  141. * @return array
  142. */
  143. public function imageCensorUserDefined($image){
  144. $data = array();
  145. $isUrl = substr(trim($image), 0, 4) === 'http';
  146. if(!$isUrl){
  147. $data['image'] = base64_encode($image);
  148. }else{
  149. $data['imgUrl'] = $image;
  150. }
  151. return $this->request($this->imageCensorUserDefinedUrl, $data);
  152. }
  153. }