DetectionService.php 7.4 KB

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