当前位置: 首页 > 后端技术 > PHP

使用百度ai实现文字图片审核

时间:2023-03-29 15:32:00 PHP

在做平台内容发布审核之前,自己建了一套违禁词库,在代码中使用词库判断用户发布的内容。现在就可以使用百度aiapi来完成这个功能了。下面简单说一下具体操作方法:首先打开百度AI开发平台注册账号:注册账号,进入控制台创建自己的应用,获取apikey和秘钥,进入文档页面Textreview:Imagereview:文档很详细,实现了用户发帖内容审核和图片审核,非常方便简单。我没有使用官方的sdk,只是简单的集成了一下作为练习。下面是我简单的代码演示:ImagedescriptionclassSentive{protected$accessTokenUrl='https://aip.baidubce.com/oauth/2.0/token';//获取tokenurlprotected$textUrl='https://aip.baidubce.com/rest/2.0/antispam/v2/spam';//文本审核urlprotected$imgUrl='https://aip.baidubce.com/api/v1/solution/direct/img_censor';//图片审核urlprotected$avatarUrl='https://aip.baidubce.com/rest/2.0/solution/v1/face_audit';//头像审核urlprotected$受保护的$client_id;受保护的$client_secret;函数__construct(){$this->grant_type='client_credentials';$this->client_id='xxx';//APIKey$this->client_secret='xxx';//SecretKey}staticfunctionrequest($url='',$param=''){if(empty($url)||empty($param)){返回false;}$postUrl=$url;$curlPost=$参数;$curl=curl_init();//初始化curlcurl_setopt($curl,CURLOPT_URL,$postUrl);//捕获获取指定网页curl_setopt($curl,CURLOPT_HEADER,0);//设置headercurl_setopt($curl,CURLOPT_RETURNTRANSFER,1);//要求结果为字符串输出到屏幕curl_setopt($curl,CURLOPT_POST,1);//post提交方法curl_setopt($curl,CURLOPT_POSTFIELDS,$curlPost);$data=curl_exec($curl);//运行curlcurl_close($curl);返回$数据;}staticfunctionrequest_post($url='',$param=array(),$type){if(empty($url)||empty($param)){returnfalse;}$postUrl=$url;$curlPost=$参数;$curl=curl_init();curl_setopt($curl,CURLOPT_URL,$postUrl);curl_setopt($curl,CURLOPT_HEADER,0);//要求的结果是一个字符串curl_setopt($curl,CURLOPT_RETURNTRANSFER,1);//post方法curl_setopt($curl,CURLOPT_POST,1);curl_setopt($curl,CURLOPT_SSL_VERIFYPEER,false);curl_setopt($curl,CURLOPT_POSTFIELDS,$curlPost);如果($type=="text"){curl_setopt($curl,CURLOPT_HTTPHEADER,array('Content-Type:application/x-www-form-urlencoded'));}else{curl_setopt($curl,CURLOPT_HTTPHEADER,array('Content-Type:application/json;charset=utf-8'));}curl_setopt($curl,CURLINFO_HEADER_OUT,true);$data=curl_exec($curl);$code=curl_getinfo($curl,CURLINFO_HTTP_CODE);如果($code===0){thrownew\Exception(curl_error($curl));}curl_close($curl);返回$数据;}//获取tokenpublicfunctiongetToken(){newRedis();$post_data['grant_type']=$this->grant_type;$post_data['client_id']=$this->client_id;$post_data['client_secret']=$this->client_secret;$o="";foreach($post_dataas$k=>$v){$o.="$k=".网址编码($v)。"&";}$post_data=substr($o,0,-1);$res=self::request($this->accessTokenUrl,$post_data);$redis->setkey("filterToken",json_decode($res,true)['access_token']);返回json_decode($res,true)['access_token'];}//文档核publicfunctiontextVerify($data){newRedis();$token=$redis->get("filterToken");如果(空($token)){$token=$this->getToken();$curl=$this->textUrl。“?access_token=”。$令牌;$result=self::request_post($curl,$data,"text");返回json_decode($result,true);}//图片审核publicfunctionimgVerify($img){$redis=newRedis();$token=$redis->get("filterToken");如果(空($token)){$token=$this->getToken();$curl=$this->imgUrl。“?access_token=”。$令牌;$bodys=array('image'=>$img,'scenes'=>array("ocr","face","public","politician","antiporn","terror","webimage","disgust",'watermark'));$bodys=json_encode($bodys);$result=self::request_post($curl,$bodys,"img");returnjson_decode($result,true);}//头像核publicfunctionavatarVerify($img){$redis=newRedis();$token=$redis->get("filterToken");if(empty($token)){$token=$this->getToken();}$curl=$this->avatarUrl."?access_token=".$token;$bodys=array("configId"=>"1","images"=>$img);$result=self::request_post($curl,$bodys,"text");返回json_decode($result,true);}}