PHP code example of meilunzhi / baidu-ai

1. Go to this page and download the library: Download meilunzhi/baidu-ai library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

meilunzhi / baidu-ai example snippets



declare(strict_types=1);
= [
        'client_id'     => 'client_id',  // 应用ID
        'client_secret' => 'client_secret', // 应用密钥
        "debug"         => true, // http调用异常会记录文件
        "log"           => [     
            'tempDir' => __DIR__ // 日志存放路径
        ],
        "cache"         => [
            'tempDir' => __DIR__, // access_token存放路径
        ],
        "request"       => [
            "timeout"          => 5 // 请求超时时间
        ]
    ];
    try {
        $ocr = \AIP\BaiduAIP\Factory::ocr($config);
        // cache rebind ,默认:文件存储
        $ocr->rebind(\AIP\BaiduAIP\Kernel\ServiceProviders::Cache, new \AIP\BaiduAIP\AccessTokenCache());
        var_dump($ocr->ocr->businessCardUrl("https://xxxxx"));
    } catch (\Exception $e) {
        var_dump($e->getMessage(), $e->getCode());
    }
});


declare(strict_types=1);
   => 'client_id',  // 应用ID
    'client_secret' => 'client_secret', // 应用密钥
    "debug"         => true, // http调用异常会记录文件
    "log"           => [     
        'tempDir' => __DIR__ // 日志存放路径
    ],
    "cache"         => [
        'tempDir' => __DIR__, // access_token存放路径
    ],
    "request"       => [
        "httpClientDriver" => \AIP\BaiduAIP\Kernel\HttpClient\CurlClientDriver::class,
        "timeout"          => 5 // 请求超时时间
    ]
];
try {
    $ocr = \AIP\BaiduAIP\Factory::ocr($config);
    // cache rebind ,默认:文件存储
    $ocr->rebind(\AIP\BaiduAIP\Kernel\ServiceProviders::Cache, new \AIP\BaiduAIP\AccessTokenCache());
    var_dump($ocr->ocr->businessCardUrl("https://xxxxx"));
} catch (\Exception $e) {
    var_dump($e->getMessage(), $e->getCode());
}




declare(strict_types=1);

namespace AIP\BaiduAIP;

use Psr\SimpleCache\CacheInterface;

class AccessTokenCache implements CacheInterface
{
    public function get($key, $default = null)
    {
        // $redis client
        return $redis->get($key);
    }

    public function set($key, $value, $ttl = null)
    {
        // $redis client
        return $redis->set($key, $value, $ttl);
    }

    public function delete($key)
    {
        // $redis client
        return $redis->del($key);
    }

    public function clear()
    {
    }

    public function getMultiple($keys, $default = null)
    {
        // TODO: Implement getMultiple() method.
    }

    public function setMultiple($values, $ttl = null)
    {
        // TODO: Implement setMultiple() method.
    }

    public function deleteMultiple($keys)
    {
        // TODO: Implement deleteMultiple() method.
    }

    public function has($key)
    {
        // $redis client
        return $redis->exists($key);
    }
}


$config = [];
$face = \AIP\BaiduAIP\Factory::face($config);
//人脸检测
$face->face->detect();
//在线活体检测
$face->face->faceVerify();
//人脸特征抽取同步接口
$face->face->feature();
//人脸比对
$face->face->match();
//人脸搜索 M:N 识别接口
$face->face->multiSearch();
//人脸搜索接口
$face->face->search();

$config = [];
$face = \AIP\BaiduAIP\Factory::face($config);
//添加
$face->group->add();
//删除
$face->group->delete();
//列表
$face->group->list();
//组用户 
$face->group->users();

$config = [];
$face = \AIP\BaiduAIP\Factory::face($config);
//添加
$face->user->add();
//更新
$face->user->update();
//删除
$face->user->delete();
//拷贝
$face->user->copy();
//获取用户
$face->user->get();
//脸列表
$face->user->faceList();
//脸删除
$face->user->faceDelete();

$config = [];
$face = \AIP\BaiduAIP\Factory::face($config);
//身份验证接口
$face->other->personVerify();
//语音校验码接口
$face->other->videoSessionCode();

$config = [];
$image = \AIP\BaiduAIP\Factory::image($config);
//通用物体识别接口
$image->classify->advancedGeneral();
//菜品识别接口
$image->classify->dishDetect();
//车辆识别接口
$image->classify->carDetect();
//车辆检测接口
$image->classify->vehicleDetect();

// 具体查看现实类中的方法...

$config = [];
$image = \AIP\BaiduAIP\Factory::image($config);
//图像审核
$image->censor->imageCensorUserDefined();
//图像地址审核
$image->censor->imageUrlCensorUserDefined();
//文本审核
$image->censor->textCensorUserDefined();
//语音审核
$image->censor->voiceCensorUserDefined();
//语音地址审核
$image->censor->voiceUrlCensorUserDefined();
//视频审核
$image->censor->videoCensorUserDefined();

$config = [];
$ocr = \AIP\BaiduAIP\Factory::ocr($config);
//通用文字识别接口
$ocr->ocr->generalBasic();
//通用文字识别(含位置高精度版)接口
$ocr->ocr->accurate();
//通用文字识别(高精度版)接口
$ocr->ocr->accurateBasic();

// 具体查看现实类中的方法...

$config = [];
$speech = \AIP\BaiduAIP\Factory::speech($config);
//语音识别
$speech->speech->asr();

$config = [];
$body = \AIP\BaiduAIP\Factory::body($config);
//人体关键点识别接口
$body->body->bodyAnalysis();
//人体检测与属性识别接口
$body->body->bodyAttr();
//人流量统计接口
$body->body->bodyNum();
//手势识别接口
$body->body->gesture();
//人像分割接口
$body->body->bodySeg();
//驾驶行为分析接口
$body->body->driverBehavior();
//人流量统计-动态版接口
$body->body->bodyTracking();
//手部关键点识别接口
$body->body->handAnalysis();

$config = [];
$kg = \AIP\BaiduAIP\Factory::kg($config);
//创建任务接口
$kg->kg->createTask();
//更新任务接口
$kg->kg->updateTask();
//获取任务详情接口
$kg->kg->getTaskInfo();
//以分页的方式查询当前用户所有的任务信息接口
$kg->kg->getUserTasks();
//启动任务接口
$kg->kg->startTask();
//查询任务状态接口
$kg->kg->getTaskStatus();

$config = [];
$image = \AIP\BaiduAIP\Factory::image($config);
// 图像无损放大接口
$image->process->imageQualityEnhance();
// 图像去雾接口
$image->process->dehaze();
// 图像对比度增强接口
$image->process->contrastEnhance();
// 拉伸图像恢复接口
$image->process->stretchRestore();
// 人像动漫化
$image->process->selfieAnime();
// 图像清晰度增强
$image->process->imageDefinitionEnhance();
// 图像风格转换
$image->process->styleTrans();
// 天空分割
$image->process->skySeg();
// 图像修复
$image->process->inPaintingByMask();