首页 > 作文

PHP利用百度ai实现文本和图片审核

更新时间:2023-04-07 09:59:57 阅读: 评论:0

之前做平台内容发布审核都是自己构建一套违禁词库,在代码中利用词库判断用户发布的内容,现在可以使用百度ai api完成这个功能。接下来就简单说下怎么做吧:

首先打开百度ai 开发平台 注册一个账号:

注册账号,进入控制台

创建自己的应用,获取apikey 和秘钥

进入文档页 文本审核:

图像审核:

文档很详细,实现用户发布内容审核 图片审核还是很方便简单的。我没有使用官方的sdk,简单的整合了一下作为练手,以下是我简单的代码demo:

class ntive{  protected $accesstokenurl = '/d/file/titlepic/question.html url  protected $texturl = '/d/file/titlepic/文本审核url  protected $imgurl = 'https://aip.baidubce.com/api/v1/solution/direct/img_censor';//图片审核url  protected $avatarurl = '/d/file/titlepic/头像审核url  protected $grant_type;  protected $client_id;  protected $client_cret;  function __construct()  {    $this->grant_type = 'client_credentials';    $this->client_id = 'xxx';//api key    $this->client_cret = 'xxx';//cret key  }  static function request($url = '', $param = '')  {    if (empty($url) || empty($param)) {      return fal;    }    $posturl = $url;    $curlpost = $param;    $curl = curl_init();//初始化curl    curl_topt($curl, curlopt_url, $posturl);//抓取指定网页    curl_topt($curl, curlopt_header, 0);//设置header    curl_topt($curl, curlopt_returntransfer, 1);//要求结果为字符串且输出到屏幕上    curl_topt($curl, curlopt_post, 1);//post提交方式    curl_topt($curl, curlopt_postfields, $curlpost);    $data = curl_exec($curl);//运行curl    curl_clo($curl);    return $data;  }  static function request_post($url = '', $param = array(), $type)  {    if (empty($url) || empty($param)) {      return fal;    }    $posturl = $url;    $curlpost = $param;    $curl = curl_init();    curl_topt($curl, curlopt_url, $posturl);    curl_topt($curl, curlopt_header, 0);    // 要求结果为字符串    curl_topt($curl, curlopt_returntransfer, 1);    // post方式    curl_topt($curl, curlopt_post, 1);    curl_topt($curl, curlopt_ssl_verifypeer, fal);    curl_topt($curl, curlopt_postfields, $curlpost);    if ($type == "text") {      curl_topt($curl, curlopt_httpheader, array('content-type: application/x-www-form-urlencoded'));    } el {      curl_topt($curl, curlopt_httpheader, array('content-type: application/json;chart=utf-8'));    }    curl_topt($curl, curlinfo_header_out, true)山西寿阳县;    $data = curl_exec($curl);    $code = curl_getinfo($curl, curlinfo_http_code);    if ($code === 0) {      throw new \exception(curl_error($curl));    }    curl_clo($curl);    return $data;  }  //获取token  public function gettoken()  {    new redis();    $post_data['grant_type'] = $this->grant_type;    $post_data['client_id'] = $this->client_id;    $post_data['client_cret'] = $this->client_cret;    $o = "";    foreach ($post_data as $k => $v) {      $o .= "$k=" . urlencode($v) . "&";    }    $post_data = substr($o, 0, -1);    $res = lf::request($this->accesstokenurl, $post_data);    $redis->tkey("filtertoke佳能mp236n", json_decode($res, true)['access_token']);    return json_decode($res, true)['access_token'];  }  //文本审核  public function textverify($data)  {    new redis();    $token = $redis->get("filtertoken");    if (empty($token)) {      $token = $this->gettoken();   宇宙苍穹 }    $curl = $this->texturl . "?access_token=" . $token;    $result = lf::request_post($curl, $data, "tewindows7操作系统xt");    return json_decode($result, true);  }  //图片审核  public function imgverify($img)  {    $redis = new redis();    $token = $redis->get("filtertoken");    if (empty($token)) {      $token = $this->gettoken();    }    $curl = $this->imgurl . "?access_token=" . $token;    $bodys = array(      'image' => $img,      'scenes' => array("ocr",        "face", "public", "politician", "antiporn", "terror", "webimage", "disgust",        'watermark')    );    $bodys = json_encode($bodys);    $result = lf::re西瓜英文quest_post($curl, $bodys, "img");    return json_decode($result, true);  }  //头像审核  public function avatarverify($img)  {    $redis = new redis();    $token = $redis->get("filtertoken");    if (empty($token)) {      $token = $this->gettoken();    }    $curl = $this->avatarurl . "?access_token=" . $token;    $bodys = array(      "configid" => "1",      "images" => $img    );    $result = lf::request_post($curl, $bodys, "text");    return json_decode($result, true);  }}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持www.887551.com。

本文发布于:2023-04-07 09:59:54,感谢您对本站的认可!

本文链接:https://www.wtabcd.cn/fanwen/zuowen/2ffc862b659839d3adfcc41676adf2c3.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

本文word下载地址:PHP利用百度ai实现文本和图片审核.doc

本文 PDF 下载地址:PHP利用百度ai实现文本和图片审核.pdf

标签:简单   寿阳县   词库   文本
相关文章
留言与评论(共有 0 条评论)
   
验证码:
Copyright ©2019-2022 Comsenz Inc.Powered by © 专利检索| 网站地图