PHP 및 Baidu AI를 사용하여 텍스트 및 이미지 검토

王林
풀어 주다: 2023-04-07 19:50:01
원래의
3992명이 탐색했습니다.

PHP 및 Baidu AI를 사용하여 텍스트 및 이미지 검토

단계:

계정 등록을 위해 먼저 Baidu ai 개발 플랫폼을 엽니다.

PHP 및 Baidu AI를 사용하여 텍스트 및 이미지 검토

계정 등록, 콘솔 입력

PHP 및 Baidu AI를 사용하여 텍스트 및 이미지 검토

나만의 애플리케이션 만들기, apikey 및 비밀 키 받기

PHP 및 Baidu AI를 사용하여 텍스트 및 이미지 검토

문서 페이지 텍스트 검토 입력:

PHP 및 Baidu AI를 사용하여 텍스트 및 이미지 검토

이미지 검토:

PHP 및 Baidu AI를 사용하여 텍스트 및 이미지 검토

코드 예:

class Sentive
{
  protected $accessTokenUrl = 'https://aip.baidubce.com/oauth/2.0/token';//获取token url
  protected $textUrl = 'https://aip.baidubce.com/rest/2.0/antispam/v2/spam';//文本审核url
  protected $imgUrl = 'https://aip.baidubce.com/api/v1/solution/direct/img_censor';//图片审核url
  protected $avatarUrl = 'https://aip.baidubce.com/rest/2.0/solution/v1/face_audit';//头像审核url
  protected $grant_type;
  protected $client_id;
  protected $client_secret;
  function __construct()
  {
    $this->grant_type = 'client_credentials';
    $this->client_id = 'xxx';//API Key
    $this->client_secret = 'xxx';//Secret Key
  }
  static function request($url = '', $param = '')
  {
    if (empty($url) || empty($param)) {
      return false;
    }
    $postUrl = $url;
    $curlPost = $param;
    $curl = curl_init();//初始化curl
    curl_setopt($curl, CURLOPT_URL, $postUrl);//抓取指定网页
    curl_setopt($curl, CURLOPT_HEADER, 0);//设置header
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);//要求结果为字符串且输出到屏幕上
    curl_setopt($curl, CURLOPT_POST, 1);//post提交方式
    curl_setopt($curl, CURLOPT_POSTFIELDS, $curlPost);
    $data = curl_exec($curl);//运行curl
    curl_close($curl);
    return $data;
  }
  static function request_post($url = '', $param = array(), $type)
  {
    if (empty($url) || empty($param)) {
      return false;
    }
    $postUrl = $url;
    $curlPost = $param;
    $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);
    if ($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);
    if ($code === 0) {
      throw new \Exception(curl_error($curl));
    }
    curl_close($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_secret'] = $this->client_secret;
    $o = "";
    foreach ($post_data as $k => $v) {
      $o .= "$k=" . urlencode($v) . "&";
    }
    $post_data = substr($o, 0, -1);
    $res = self::request($this->accessTokenUrl, $post_data);
    $redis->setkey("filterToken", 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 = self::request_post($curl, $data, "text");
    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 = self::request_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 = self::request_post($curl, $bodys, "text");
    return json_decode($result, true);
  }
}
로그인 후 복사

추천 튜토리얼: PHP 튜토리얼

위 내용은 PHP 및 Baidu AI를 사용하여 텍스트 및 이미지 검토의 상세 내용입니다. 자세한 내용은 PHP 중국어 웹사이트의 기타 관련 기사를 참조하세요!

관련 라벨:
원천:php.cn
본 웹사이트의 성명
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.
인기 튜토리얼
더>
최신 다운로드
더>
웹 효과
웹사이트 소스 코드
웹사이트 자료
프론트엔드 템플릿
회사 소개 부인 성명 Sitemap
PHP 중국어 웹사이트:공공복지 온라인 PHP 교육,PHP 학습자의 빠른 성장을 도와주세요!