DetectionService.php 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. <?php
  2. namespace App\Service;
  3. use Green\Request\V20170112 as Green;
  4. class DetectionService
  5. {
  6. /**
  7. * Get the acs client
  8. * @return \DefaultAcsClient
  9. */
  10. private function getClient()
  11. {
  12. date_default_timezone_set("PRC");
  13. $iClientProfile = \DefaultProfile::getProfile("cn-shanghai", config('aliyun.access_key'), config('aliyun.access_secret'));
  14. \DefaultProfile::addEndpoint("cn-shanghai", "cn-shanghai", "Green", "green.cn-shanghai.aliyuncs.com");
  15. $client = new \DefaultAcsClient($iClientProfile);
  16. return $client;
  17. }
  18. /**
  19. * scene 风险场景,和传递进来的场景对应
  20. * suggestion 建议用户处理,取值范围:[“pass”, “review”, “block”], pass:图片正常,review:需要人工审核,block:图片违规,
  21. * 可以直接删除或者做限制处理
  22. * label 文本的分类
  23. * rate 浮点数,结果为该分类的概率;值越高,越趋于该分类;取值为[0.00-100.00]
  24. * extras map,可选,附加信息. 该值将来可能会调整,建议不要在业务上进行依赖
  25. *
  26. * -10000 检测数据有问题
  27. * 10000 检测数据正常
  28. * 20000 检测出异常 重试三次
  29. * @param $request
  30. */
  31. private function processResponse($request)
  32. {
  33. $client = $this->getClient();
  34. try {
  35. $response = $client->getAcsResponse($request);
  36. if(200 == $response->code){
  37. $taskResults = $response->data;
  38. $detail = [];
  39. $flag = true;
  40. foreach ($taskResults as $taskResult) {
  41. if(200 == $taskResult->code){
  42. $this->processSceneResult($taskResult, $flag);
  43. }else{
  44. return $this->echoStr(-2000, 'task process fail:'.$response->code);
  45. }
  46. if (isset($taskResult->filteredContent)) {
  47. array_push($detail, $taskResult->filteredContent);
  48. }
  49. }
  50. if($flag == false){
  51. return $this->echoStr(-10000, 'the scene is not normal', $detail);
  52. }else{
  53. return $this->echoStr(10000, 'the scene is normal');
  54. }
  55. }else{
  56. return $this->echoStr(-2000, 'detect not success. code:'.$response->code);
  57. }
  58. } catch (Exception $e) {
  59. return $this->echoStr(-2000, $e);
  60. }
  61. }
  62. /**
  63. * @param $code
  64. * @param $msg
  65. */
  66. private function echoStr($code, $msg, $detail = []){
  67. return array(
  68. 'code' => $code,
  69. 'msg' => $msg,
  70. 'detail' => $detail
  71. );
  72. }
  73. /**
  74. * @param $taskResult
  75. */
  76. private function processSceneResult($taskResult, &$flag){
  77. $sceneResults = $taskResult->results;
  78. foreach ($sceneResults as $sceneResult) {
  79. //根据scene和suggetion做相关的处理
  80. $suggestion = $sceneResult->suggestion;
  81. $rate = $sceneResult->rate;
  82. // if($suggestion!='pass' && $rate>80){
  83. // $flag = false;
  84. // }
  85. if ($suggestion == 'block') {
  86. $flag = false;
  87. } elseif ($suggestion == 'review' && $rate > 90) {
  88. $flag = false;
  89. }
  90. }
  91. }
  92. /**
  93. * 文本垃圾检测
  94. * scenes字符串数组:
  95. * 关键词识别scene场景取值keyword
  96. * 分类label:正常normal 含垃圾信息spam 含广告ad 涉政politics 暴恐terrorism 色情porn 辱骂abuse
  97. * 灌水flood 命中自定义customized(阿里后台自定义)
  98. * 垃圾检测识别场景scene取值antispam
  99. * 分类label:正常normal 含违规信息spam 含广告ad 涉政politics 暴恐terrorism 色情porn 违禁contraband
  100. * 命中自定义customized(阿里后台自定义)
  101. *
  102. * tasks json数组 ,最多支持100个task即100段文本
  103. * content 待检测文本,最长4000个字符
  104. *
  105. * @param $text 支持字符串和数组
  106. * @return null
  107. */
  108. public function checkText($text){
  109. if(empty($text)){
  110. return null;
  111. }
  112. $request = new Green\TextScanRequest();
  113. $request->setMethod("POST");
  114. $request->setAcceptFormat("JSON");
  115. if(is_array($text)){
  116. $taskArr = [];
  117. foreach($text as $k => $v){
  118. $task = 'task'.$k;
  119. $$task = array('dataId' => md5(uniqid($task)),
  120. 'content' => $v,
  121. 'category' => 'post',
  122. 'time' => round(microtime(true)*1000)
  123. );
  124. array_push($taskArr, $$task);
  125. }
  126. $request->setContent(json_encode(array("tasks" => $taskArr,
  127. "scenes" => array("antispam"))));
  128. }else if(is_string($text)){
  129. $task1 = array('dataId' => md5(uniqid()),
  130. 'content' => $text
  131. );
  132. $request->setContent(json_encode(array("tasks" => array($task1),
  133. "scenes" => array("antispam"))));
  134. }
  135. return $this->processResponse($request);
  136. }
  137. /**
  138. * 图片检测
  139. * scenes字符串数组:
  140. * 图片广告识别scene场景取值ad
  141. * 分类label: 正常normal 含广告ad
  142. * 图片鉴黄识别场景scene取值porn
  143. * 分类label:正常normal 性感sexy 色情porn
  144. * 图片暴恐涉政识别场景scene取值terrorism
  145. * 分类label:正常normal terrorism含暴恐图片 outfit特殊装束 logo特殊标识 weapon武器 politics渉政 others 其它暴恐渉政
  146. *
  147. * tasks json数组 ,最多支持100个task即100张图片
  148. *
  149. * @param $img 支持字符串和数组
  150. * @return null
  151. */
  152. public function checkImg($img){
  153. if(empty($img)){
  154. return null;
  155. }
  156. $request = new Green\ImageSyncScanRequest();
  157. $request->setMethod("POST");
  158. $request->setAcceptFormat("JSON");
  159. if(is_array($img)){
  160. $taskArr = array();
  161. foreach($img as $k => $v){
  162. $task = 'task'.$k;
  163. $$task = array('dataId' => md5(uniqid($task)),
  164. 'url' => $v,
  165. 'time' => round(microtime(true)*1000)
  166. );
  167. array_push($taskArr, $$task);
  168. }
  169. $request->setContent(json_encode(array("tasks" => $taskArr,
  170. "scenes" => array("porn", "terrorism"))));
  171. }else if(is_string($img)){
  172. $task1 = array('dataId' => md5(uniqid()),
  173. 'url' => $img,
  174. 'time' => round(microtime(true)*1000)
  175. );
  176. $request->setContent(json_encode(array("tasks" => array($task1),
  177. "scenes" => array("porn", "terrorism"))));
  178. }
  179. return $this->processResponse($request);
  180. }
  181. }