DetectionService.php 7.1 KB

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