ImageCensor.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. <?php
  2. namespace App\Services\Vendor\BaiduAi;
  3. use App\Services\Service;
  4. class ImageCensor extends Service
  5. {
  6. /** @var ApiBase $base */
  7. protected $base;
  8. /**
  9. * @var string
  10. */
  11. private $imageCensorCombUrl = 'https://aip.baidubce.com/api/v1/solution/direct/img_censor';
  12. public function __construct()
  13. {
  14. $this->base = new ApiBase(
  15. config('baidu.face.appid'),
  16. config('baidu.face.apikey'),
  17. config('baidu.face.secretKey')
  18. );
  19. }
  20. /**
  21. * @param $imageUrl
  22. * @return array
  23. */
  24. public function image($imageUrl)
  25. {
  26. $suggestion = 1;
  27. $labels = [];
  28. // antiporn 色情;terror 暴恐;watermark: 广告检测
  29. $scenes = ['antiporn', 'terror', 'watermark', 'politician', 'quality'];
  30. $response = $this->request($imageUrl, $scenes);
  31. if (isset($response['result'])) {
  32. $array = $response['result'];
  33. foreach ($array as $key => $value) {
  34. switch ($key) {
  35. case "antiporn":
  36. if (!isset($value['error_code'])) {
  37. foreach ($value['result'] as $v) {
  38. switch ($v['class_name']) {
  39. case "性感":
  40. if ($v['probability'] >= 0.7 && $v['probability'] <= 1) {
  41. $suggestion = 0;
  42. array_push($labels, "性感");
  43. }
  44. break;
  45. case "色情":
  46. if ($v['probability'] >= 0.7 && $v['probability'] <= 0.9) {
  47. $suggestion = 0;
  48. array_push($labels, "色情");
  49. } elseif ($v['probability'] >= 0.9) {
  50. $suggestion = -1;
  51. array_push($labels, "色情");
  52. }
  53. break;
  54. }
  55. }
  56. } else {
  57. $suggestion = 0;
  58. }
  59. break;
  60. case "terror":
  61. if (!isset($value['error_code'])) {
  62. foreach ($value['result_coarse'] as $v) {
  63. if ($v['name'] == '暴恐' && $v['score'] > 0.7 && $v['score'] < 0.9) {
  64. $suggestion = 0;
  65. array_push($labels, "暴恐");
  66. } elseif ($v['name'] == '暴恐' && $v['score'] >= 0.9 && $v['score'] <= 1) {
  67. $suggestion = -1;
  68. array_push($labels, "暴恐");
  69. }
  70. }
  71. foreach ($value['result_fine'] as $v) {
  72. switch ($v['name']) {
  73. case "警察部队":
  74. if ($v['score'] >= 0.7) {
  75. $suggestion = 0;
  76. array_push($labels, "暴恐");
  77. }
  78. break;
  79. case "血腥":
  80. if ($v['score'] >= 0.7 && $v['score'] < 0.9) {
  81. $suggestion = 0;
  82. array_push($labels, "血腥");
  83. } elseif ($v['score'] >= 0.9 && $v['score'] > 1) {
  84. $suggestion = -1;
  85. array_push($labels, "血腥");
  86. }
  87. break;
  88. case "尸体":
  89. if ($v['score'] >= 0.7 && $v['score'] < 0.9) {
  90. $suggestion = 0;
  91. array_push($labels, "尸体");
  92. } elseif ($v['score'] >= 0.9 && $v['score'] > 1) {
  93. $suggestion = -1;
  94. array_push($labels, "尸体");
  95. }
  96. break;
  97. case "杀人":
  98. if ($v['score'] >= 0.7 && $v['score'] < 0.9) {
  99. $suggestion = 0;
  100. array_push($labels, "杀人");
  101. } elseif ($v['score'] >= 0.9 && $v['score'] > 1) {
  102. $suggestion = -1;
  103. array_push($labels, "杀人");
  104. }
  105. break;
  106. case "暴乱":
  107. if ($v['score'] >= 0.7 && $v['score'] < 0.9) {
  108. $suggestion = 0;
  109. array_push($labels, "暴乱");
  110. } elseif ($v['score'] >= 0.9 && $v['score'] > 1) {
  111. $suggestion = -1;
  112. array_push($labels, "暴乱");
  113. }
  114. break;
  115. case "暴恐人物":
  116. if ($v['score'] >= 0.7 && $v['score'] < 0.9) {
  117. $suggestion = 0;
  118. array_push($labels, "暴恐人物");
  119. } elseif ($v['score'] >= 0.9 && $v['score'] > 1) {
  120. $suggestion = -1;
  121. array_push($labels, "暴恐人物");
  122. }
  123. break;
  124. case "血腥动物或动物尸体":
  125. case "暴恐旗帜":
  126. case "军事武器":
  127. case "车祸":
  128. case "爆炸火灾":
  129. break;
  130. }
  131. }
  132. } else {
  133. $suggestion = 0;
  134. }
  135. break;
  136. case "watermark":
  137. if (!isset($value['error_code'])) {
  138. foreach ($value['result'] as $v) {
  139. switch ($v['type']) {
  140. case "watermark":
  141. if ($v['probability'] >= 0.7) {
  142. $suggestion = 0;
  143. array_push($labels, "水印");
  144. }
  145. break;
  146. case "QR code":
  147. if ($v['probability'] >= 0.7) {
  148. $suggestion = 0;
  149. array_push($labels, "二维码");
  150. }
  151. break;
  152. case "bar code":
  153. if ($v['probability'] >= 0.7) {
  154. $suggestion = 0;
  155. array_push($labels, "条形码");
  156. }
  157. break;
  158. }
  159. }
  160. } else {
  161. $suggestion = 0;
  162. }
  163. break;
  164. case "politician":
  165. if (!isset($value['error_code'])) {
  166. if ($value['include_politician'] == '是' && $value['result_confidence'] == '确定') {
  167. $suggestion = -1;
  168. array_push($labels, "政治人物");
  169. } elseif ($value['include_politician'] == '是' && $value['result_confidence'] == '不确定') {
  170. $suggestion = 0;
  171. array_push($labels, "政治人物");
  172. } elseif ($value['include_politician'] == '否' && $value['result_confidence'] == '不确定') {
  173. $suggestion = 0;
  174. array_push($labels, "政治人物");
  175. }
  176. } else {
  177. $suggestion = 0;
  178. }
  179. break;
  180. case "quality":
  181. if (!isset($value['error_code'])) {
  182. } else {
  183. $suggestion = 0;
  184. }
  185. break;
  186. }
  187. }
  188. }
  189. return [
  190. 'machine_result' => $response,
  191. 'suggestion' => $suggestion,
  192. 'labels' => array_unique($labels),
  193. ];
  194. }
  195. /**
  196. * 请求
  197. * @param $imageUrl
  198. * @param array $scenes
  199. * @return mixed
  200. */
  201. public function request($imageUrl, array $scenes)
  202. {
  203. $response = $this->base->request(
  204. $this->imageCensorCombUrl,
  205. json_encode([
  206. 'imgUrl' => $imageUrl,
  207. 'scenes' => $scenes,
  208. ]),
  209. [
  210. 'Content-Type' => 'application/json',
  211. ]
  212. );
  213. return $response;
  214. }
  215. }