利用PHP和百度ai实现文本以及图片的审核
Posted 头大的冯冯
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了利用PHP和百度ai实现文本以及图片的审核相关的知识,希望对你有一定的参考价值。
步骤:
首先打开百度ai 开发平台 注册一个账号:
注册账号,进入控制台
创建自己的应用,获取apikey 和秘钥
进入文档页 文本审核:
图像审核:
代码实例:
1 class Sentive 2 3 { 4 5 protected $accessTokenUrl = \'https://aip.baidubce.com/oauth/2.0/token\';//获取token url 6 7 protected $textUrl = \'https://aip.baidubce.com/rest/2.0/antispam/v2/spam\';//文本审核url 8 9 protected $imgUrl = \'https://aip.baidubce.com/api/v1/solution/direct/img_censor\';//图片审核url 10 11 protected $avatarUrl = \'https://aip.baidubce.com/rest/2.0/solution/v1/face_audit\';//头像审核url 12 13 protected $grant_type; 14 15 protected $client_id; 16 17 protected $client_secret; 18 19 function __construct() 20 21 { 22 23 $this->grant_type = \'client_credentials\'; 24 25 $this->client_id = \'xxx\';//API Key 26 27 $this->client_secret = \'xxx\';//Secret Key 28 29 } 30 31 static function request($url = \'\', $param = \'\') 32 33 { 34 35 if (empty($url) || empty($param)) { 36 37 return false; 38 39 } 40 41 $postUrl = $url; 42 43 $curlPost = $param; 44 45 $curl = curl_init();//初始化curl 46 47 curl_setopt($curl, CURLOPT_URL, $postUrl);//抓取指定网页 48 49 curl_setopt($curl, CURLOPT_HEADER, 0);//设置header 50 51 curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);//要求结果为字符串且输出到屏幕上 52 53 curl_setopt($curl, CURLOPT_POST, 1);//post提交方式 54 55 curl_setopt($curl, CURLOPT_POSTFIELDS, $curlPost); 56 57 $data = curl_exec($curl);//运行curl 58 59 curl_close($curl); 60 61 return $data; 62 63 } 64 65 static function request_post($url = \'\', $param = array(), $type) 66 67 { 68 69 if (empty($url) || empty($param)) { 70 71 return false; 72 73 } 74 75 $postUrl = $url; 76 77 $curlPost = $param; 78 79 $curl = curl_init(); 80 81 curl_setopt($curl, CURLOPT_URL, $postUrl); 82 83 curl_setopt($curl, CURLOPT_HEADER, 0); 84 85 // 要求结果为字符串 86 87 curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); 88 89 // post方式 90 91 curl_setopt($curl, CURLOPT_POST, 1); 92 93 curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false); 94 95 curl_setopt($curl, CURLOPT_POSTFIELDS, $curlPost); 96 97 if ($type == "text") { 98 99 curl_setopt($curl, CURLOPT_HTTPHEADER, array(\'Content-Type: application/x-www-form-urlencoded\')); 100 101 } else { 102 103 curl_setopt($curl, CURLOPT_HTTPHEADER, array(\'Content-Type: application/json;charset=utf-8\')); 104 105 } 106 107 curl_setopt($curl, CURLINFO_HEADER_OUT, true); 108 109 $data = curl_exec($curl); 110 111 $code = curl_getinfo($curl, CURLINFO_HTTP_CODE); 112 113 if ($code === 0) { 114 115 throw new \\Exception(curl_error($curl)); 116 117 } 118 119 curl_close($curl); 120 121 return $data; 122 123 } 124 125 //获取token 126 127 public function getToken() 128 129 { 130 131 new Redis(); 132 133 $post_data[\'grant_type\'] = $this->grant_type; 134 135 $post_data[\'client_id\'] = $this->client_id; 136 137 $post_data[\'client_secret\'] = $this->client_secret; 138 139 $o = ""; 140 141 foreach ($post_data as $k => $v) { 142 143 $o .= "$k=" . urlencode($v) . "&"; 144 145 } 146 147 $post_data = substr($o, 0, -1); 148 149 $res = self::request($this->accessTokenUrl, $post_data); 150 151 $redis->setkey("filterToken", json_decode($res, true)[\'access_token\']); 152 153 return json_decode($res, true)[\'access_token\']; 154 155 } 156 157 //文本审核 158 159 public function textVerify($data) 160 161 { 162 163 new Redis(); 164 165 $token = $redis->get("filterToken"); 166 167 if (empty($token)) { 168 169 $token = $this->getToken(); 170 171 } 172 173 $curl = $this->textUrl . "?access_token=" . $token; 174 175 $result = self::request_post($curl, $data, "text"); 176 177 return json_decode($result, true); 178 179 } 180 181 //图片审核 182 183 public function imgVerify($img) 184 185 { 186 187 $redis = new Redis(); 188 189 $token = $redis->get("filterToken"); 190 191 if (empty($token)) { 192 193 $token = $this->getToken(); 194 195 } 196 197 $curl = $this->imgUrl . "?access_token=" . $token; 198 199 $bodys = array( 200 201 \'image\' => $img, 202 203 \'scenes\' => array("ocr", 204 205 "face", "public", "politician", "antiporn", "terror", "webimage", "disgust", 206 207 \'watermark\') 208 209 ); 210 211 $bodys = json_encode($bodys); 212 213 $result = self::request_post($curl, $bodys, "img"); 214 215 return json_decode($result, true); 216 217 } 218 219 //头像审核 220 221 public function avatarVerify($img) 222 223 { 224 225 $redis = new Redis(); 226 227 $token = $redis->get("filterToken"); 228 229 if (empty($token)) { 230 231 $token = $this->getToken(); 232 233 } 234 235 $curl = $this->avatarUrl . "?access_token=" . $token; 236 237 $bodys = array( 238 239 "configId" => "1", 240 241 "images" => $img 242 243 ); 244 245 $result = self::request_post($curl, $bodys, "text"); 246 247 return json_decode($result, true); 248 249 } 250 251 }
以上是关于利用PHP和百度ai实现文本以及图片的审核的主要内容,如果未能解决你的问题,请参考以下文章
使用百度AI平台图像识别-人脸动漫化与黑白图片上色(PHP)
森说AI:使用paddlehub来实现基于文本审核的AI鉴黄
森说AI:使用paddlehub来实现基于文本审核的AI鉴黄