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