DetectionService.php 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  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. }
  86. }
  87. /**
  88. * 文本垃圾检测
  89. * scenes字符串数组:
  90. * 关键词识别scene场景取值keyword
  91. * 分类label:正常normal 含垃圾信息spam 含广告ad 涉政politics 暴恐terrorism 色情porn 辱骂abuse
  92. * 灌水flood 命中自定义customized(阿里后台自定义)
  93. * 垃圾检测识别场景scene取值antispam
  94. * 分类label:正常normal 含违规信息spam 含广告ad 涉政politics 暴恐terrorism 色情porn 违禁contraband
  95. * 命中自定义customized(阿里后台自定义)
  96. *
  97. * tasks json数组 ,最多支持100个task即100段文本
  98. * content 待检测文本,最长4000个字符
  99. *
  100. * @param $text 支持字符串和数组
  101. * @return null
  102. */
  103. public function checkText($text){
  104. if(empty($text)){
  105. return null;
  106. }
  107. $request = new Green\TextScanRequest();
  108. $request->setMethod("POST");
  109. $request->setAcceptFormat("JSON");
  110. if(is_array($text)){
  111. $taskArr = [];
  112. foreach($text as $k => $v){
  113. $task = 'task'.$k;
  114. $$task = array('dataId' => md5(uniqid($task)),
  115. 'content' => $v,
  116. 'category' => 'post',
  117. 'time' => round(microtime(true)*1000)
  118. );
  119. array_push($taskArr, $$task);
  120. }
  121. $request->setContent(json_encode(array("tasks" => $taskArr,
  122. "scenes" => array("antispam"))));
  123. }else if(is_string($text)){
  124. $task1 = array('dataId' => md5(uniqid()),
  125. 'content' => $text
  126. );
  127. $request->setContent(json_encode(array("tasks" => array($task1),
  128. "scenes" => array("antispam"))));
  129. }
  130. return $this->processResponse($request);
  131. }
  132. /**
  133. * 图片检测
  134. * scenes字符串数组:
  135. * 图片广告识别scene场景取值ad
  136. * 分类label: 正常normal 含广告ad
  137. * 图片鉴黄识别场景scene取值porn
  138. * 分类label:正常normal 性感sexy 色情porn
  139. * 图片暴恐涉政识别场景scene取值terrorism
  140. * 分类label:正常normal terrorism含暴恐图片 outfit特殊装束 logo特殊标识 weapon武器 politics渉政 others 其它暴恐渉政
  141. *
  142. * tasks json数组 ,最多支持100个task即100张图片
  143. *
  144. * @param $img 支持字符串和数组
  145. * @return null
  146. */
  147. public function checkImg($img){
  148. if(empty($img)){
  149. return null;
  150. }
  151. $request = new Green\ImageSyncScanRequest();
  152. $request->setMethod("POST");
  153. $request->setAcceptFormat("JSON");
  154. if(is_array($img)){
  155. $taskArr = array();
  156. foreach($img as $k => $v){
  157. $task = 'task'.$k;
  158. $$task = array('dataId' => md5(uniqid($task)),
  159. 'url' => $v,
  160. 'time' => round(microtime(true)*1000)
  161. );
  162. array_push($taskArr, $$task);
  163. }
  164. $request->setContent(json_encode(array("tasks" => $taskArr,
  165. "scenes" => array("ad", "porn", "terrorism"))));
  166. }else if(is_string($img)){
  167. $task1 = array('dataId' => md5(uniqid()),
  168. 'url' => $img,
  169. 'time' => round(microtime(true)*1000)
  170. );
  171. $request->setContent(json_encode(array("tasks" => array($task1),
  172. "scenes" => array("ad", "porn", "terrorism"))));
  173. }
  174. return $this->processResponse($request);
  175. }
  176. }