利用PHP和百度ai实现文本以及图片的审核
步骤:
首先打开百度ai 开发平台 注册一个账号:

注册账号,进入控制台

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

进入文档页 文本审核:

图像审核:

代码实例:
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);
}
}
链接:https://www.php.cn/php-weizijiaocheng-435576.html
利用PHP和百度ai实现文本以及图片的审核的更多相关文章
- PHP利用百度ai实现文本和图片审核
之前做平台内容发布审核都是自己构建一套违禁词库,在代码中利用词库判断用户发布的内容,现在可以使用百度ai api完成这个功能.接下来就简单说下怎么做吧: 首先打开百度ai 开发平台 注册一个账号: 注 ...
- c# 利用AForge和百度AI开发实时人脸识别
baiduAIFaceIdentify项目是C#语言,集成百度AI的SDK利用AForge开发的实时人脸识别的小demo,里边包含了人脸检测识别,人脸注册,人脸登录等功能 人脸实时检测识别功能 思路是 ...
- 利用开源框架Volley来下载文本和图片。
Android Volley是Android平台上很好用的第三方开源网络通信框架.使用简单,功能强大. 下载连接地址:http://download.csdn.net/detail/zhangphil ...
- 百度AI认为最漂亮的中国女星是----范冰冰
一.程序说明 1.1 程序说明 之前写调用百度AI接口的程序,然后刷到了两条明星的新闻,就想到了写个给明星颜值排下名的程序. 程序的关键点是两个,第一个是百度AI接口的调用这点其实直接使用早前实现的类 ...
- C# 如何添加Word文本和图片超链接
超链接简单来讲就是内容链接,通过设置超链接可以实现对象与网页.站点之间的连接.链接目标可以是网页.图片.邮件地址.文件夹或者是应用程序.设置链接的对象可以是文本或者图片. 在以下内容中,我将介绍如何用 ...
- 百度AI文本审核API使用说明
虽然,虽然,虽然,今天: 百度发布了2019年第一季度未经审计的财务报告.本季度百度营收241亿元人民币(约合35.9亿美元),同比增长15%,移除业务拆分收入影响,同比增长21%.低于市场预期242 ...
- 利用百度AI OCR图片识别,Java实现PDF中的图片转换成文字
序言:我们在读一些PDF版书籍的时候,如果PDF中不是图片,做起读书笔记的还好:如果PDF中的是图片的话,根本无法编辑,做起笔记来,还是很痛苦的.我是遇到过了.我们搞技术的,当然得自己学着解决现在的痛 ...
- 百度AI技术
利用百度提供接口,实现智能语音 语音合成 -- TTS(text to speech) 注册 在 ai.baidu.com 页面中点击 控制台 ,弹出登陆 / 注册页面 创建应用 登陆成功后,点击左侧 ...
- 百度AI开放平台- API实战调用
百度AI开放平台- API实战调用 一. 前言 首先说一下项目需求. 两个用户,分别上传了两段不同的文字,要计算两段文字相似度有多少,匹配数据库中的符合条件的数据,初步估计列出来会有60-1 ...
随机推荐
- 【leetcode】1023. Camelcase Matching
题目如下: A query word matches a given pattern if we can insert lowercaseletters to the pattern word so ...
- 【leetcode】564. Find the Closest Palindrome
题目如下: 解题思路:既然是要求回文字符串,那么最终的输出结果就是对称的.要变成对称字符串,只要把处于对称位置上对应的两个字符中较大的那个变成较小的那个即可,假设n=1234,1和4对称所以把4变成1 ...
- 获取mysql数据库表字段的备注信息
SELECT COLUMN_NAME as field_name , COLUMN_COMMENT as remark FROM information_schema.COLUMNS WHERE T ...
- objc_setAssociatedObject 关联对象
使用场景:在分类中,不允许创建实例变量,这里就解决了此问题 参考: https://www.cnblogs.com/someonelikeyou/p/7162613.html 属性的实质:就是实例变量 ...
- JS中的立即执行函数
JS 立即执行函数可以让函数在创建后立即执行,这种模式本质上就是函数表达式(命名的或者匿名的),在创建后立即执行. 1.立即执行函数的写法 立即执行函数通常有下面两种写法: //第一种写法 (func ...
- 【Linux】grub引导修复
将系统盘挂载上并设置开机从光盘启动(U盘也可以) 进入系统安装引导初始界面,然后选择最后一项Troubleshooting 然后选择第二项Rescue a CentOS system进入系统救援模式选 ...
- MIS(管理信息系统)
MIS 管理信息系统(Management Information System,简称MIS) 是一个以人为主导,利用计算机硬件.软件.网络通信设备以及其他办公设备,进行信息的收集.传输.加工.储存. ...
- CF 778D Parquet Re-laying——构造
题目:http://codeforces.com/problemset/problem/778/D 完全没思路……就看了题解. 很好地思路是考虑操作可逆,所以起始状态和最终状态都变到一个中转状态,即都 ...
- 初识 ❤ TensorFlow |【一见倾心】
说明
- python input() 与raw_input()
使用input和raw_input都可以读取控制台的输入,但是input和raw_input在处理数字时是有区别的1:纯数字输入 当输入为纯数字时 input返回的是数值类型,如int,float ...