DetectionService.php 7.0 KB

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