借助百度智能云实现内容审核
发布于 2 年前 作者 lujun 1900 次浏览 来自 分享

互联网内容数量井喷式增长,大量不宜传播的有害内容,对国家安全、社会安定、和谐,特别是对青少年成长,都造成了不良影响;政府监管日趋严格,2017-2018年,政府主管部门座谈、约谈和处罚30多起内容安全问题,各部门监管法规陆续出台;2019年截止6月12日,国家网信办关闭、取消备案网站4644家;种种结果表明,内容审核的必要性和紧迫性逐渐显现。

这个内容审核,可以借助很多平台提供的审核产品来进行实现,我们这里借助百度智能云平台给出的进行实现;

https://console.bce.baidu.com/ai/#/ai/antiporn/overview/index

点进领取,然后选择审核服务,然后0元领取

领取后,返回内容__审核-概览__创建应用

点进去后进行信息填写

创建完成后,点击__管理应用__,就能看到你的Ak和Sk了

有了AK和Sk我们就能来做这个内容审核的功能了。

后端我们使用laravel实现,我们可以直接拿来用;

下面这是我封装在app下的,看命名空间就能知道

Akey和Skey要注意填写,不要忘记了

<?php
namespace App\Audit;

class Audit
{
    /**
     * 内容审核
     */
    public function contentAudit($content)
    {
        $token = $this->getAccessToken('Akey', 'Skey');

        $url = 'https://aip.baidubce.com/rest/2.0/solution/v1/text_censor/v2/user_defined?access_token=' . $token;
        $bodys = array(
            'text' => $content
        );
        $res = $this->curlPost($url, $bodys);
        //结果转成数组
        $res = json_decode($res, true);
        //根据自己的业务逻辑进行处理
        print_r($res);die;
    }
    /**
     * 图片审核
     */
    public function imageAudit($fileTmp)
    {
        $token = $this->getAccessToken('Akey','Skey');
        $url = 'https://aip.baidubce.com/rest/2.0/solution/v1/img_censor/v2/user_defined?access_token=' . $token;
        $img = file_get_contents($fileTmp);//本地路径
        $img = base64_encode($img);
        $bodys = array(
            'image' => $img
        );
        $res = $this->curlPost($url, $bodys);
        //结果转成数组
        $res = json_decode($res, true);
        //根据自己的业务逻辑进行处理
        print_r($res);
    }
    /**
     * CURL的Post请求方法
     * @param string $url
     * @param string $param
     * @return bool|string
     */
    function curlPost($url = '', $param = '')
    {
        if (empty($url) || empty($param)) {
            return false;
        }

        $postUrl = $url;
        $curlPost = $param;
        // 初始化curl
        $curl = curl_init();
        curl_setopt($curl, CURLOPT_URL, $postUrl);
        curl_setopt($curl, CURLOPT_HEADER, 0);
        // 要求结果为字符串且输出到屏幕上
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
        // post提交方式
        curl_setopt($curl, CURLOPT_POST, 1);
        curl_setopt($curl, CURLOPT_POSTFIELDS, $curlPost);
        // 运行curl
        $data = curl_exec($curl);
        curl_close($curl);

        return $data;
    }

    /**
     * 获取百度开放平台的票据
     * 参考链接:https://ai.baidu.com/ai-doc/REFERENCE/Ck3dwjhhu
     */
    public function getAccessToken($ApiKey = 'Akey', $SecretKey = 'Skey', $grantType = 'client_credentials')
    {
        $url = 'https://aip.baidubce.com/oauth/2.0/token';
        $post_data['grant_type'] = $grantType;
        $post_data['client_id'] = $ApiKey;
        $post_data['client_secret'] = $SecretKey;
        $o = "";
        foreach ($post_data as $k => $v) {
            $o .= "$k=" . urlencode($v) . "&";
        }
        $post_data = substr($o, 0, -1);

        $res = $this->curlPost($url, $post_data);
        //进行把返回结果转成数组
        $res = json_decode($res, true);
        if (isset($res['error'])) {
            exit('API Key或者Secret Key不正确');
        }
        $accessToken = $res['access_token'];
        return $accessToken;
    }
}

定义两个路由来测试文本和图片(我是在route下api.php中定义的路由

控制器中方法

到这里我们就需要测试了,我们使用postman进行测试

测试图片

前端可以用小程序来请求后端,进行文本内容或者图片的审核

至此,百度智能云内容审核到此结束,有问题的地方请指点--------

回到顶部