百度AI内容审核结合laravel使用心得

首先登录百度云后台:https://cloud.baidu.com/ 找到内容审核栏目

 

创建应用获取AppID,APIKey,SecretKey

在每个创建的应用中可以进行高级设置

 

官方网站下载php SDK压缩包。

详细文档地址 https://cloud.baidu.com/doc/ANTIPORN/Antiporn-PHP-SDK.html

将下载的aip-php-sdk-version.zip解压后,复制AipImageCensor.php以及lib/*到app/Libraries/baiduyun2.2.2文件夹中。

在composer.json中添加app/Libraries/baiduyun2.2.2

执行composer dump-autoload

 

    /*
     * 内容审核-图像审核
     *
     * */
    $img=request('img');
    $key=$this->get_check_key();
    $client = new \AipImageCensor($key['AppID'], $key['APIKey'], $key['SecretKey']);
    //使用图片网络地址请求
    $result = $client->imageCensorUserDefined($img);
    //使用图片资源请求  两种方法二选一
    //$result = $client->imageCensorUserDefined(file_get_contents('example.jpg'));
    if(isset($result['error_code'])){
       echo '请求错误:错误编码'.$result['error_code'].';错误原因'.$result['error_msg'];
    }
    if(isset($result['conclusionType'])&&$result['conclusionType']==1){
       echo '图片合规';
    }
    if(isset($result['conclusionType'])&&$result['conclusionType']==2){
       $msg='';
       foreach ($result['data'] as $g){
          $msg=$msg.$g['msg'].';';
       }
       echo '图片不合规,不合规原因:'.$msg;
    }
    return dd($result);

 

 

 

    /*
     * 内容审核-文本审核
     *
     * */
    $content=request('content');
    $key=$this->get_check_key();
    $client = new \AipImageCensor($key['AppID'], $key['APIKey'], $key['SecretKey']);
    $result = $client->antiSpam($content);
    if(isset($result['result']['spam'])&&$result['result']['spam']==0){
       echo '内容合规';
    }
    if(isset($result['result']['spam'])&&$result['result']['spam']==1){
       $msg='';
       foreach ($result['result']['reject'] as $k=>$g){
          if(count($g['hit'])>0){
             foreach ($g['hit'] as $gg){
                $msg=$msg.'、'.$gg;
             }
          }
       }
       echo '内容违禁,违禁字段:'.trim($msg,'、');
    }
    if(isset($result['result']['spam'])&&$result['result']['spam']==2){
       $msg='';
       foreach ($result['result']['review'] as $k=>$g){
          if(count($g['hit'])>0){
             foreach ($g['hit'] as $gg){
                $msg=$msg.'、'.$gg;
             }
          }
       }
       echo '建议人工复审,疑似违禁字段:'.trim($msg,'、');
    }
    return dd($result);

 

  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值