# 百度AI

## 获取ai实例

```
use Crisen\AI\Ai;

.....

$config = [
     'app_id' => 'your appid',
     'api_key' => 'your api key',
     'secret_key' => 'your secret key'
];

$ai = AI::baidu($config);
```

> 特别说明 在后面的文章出现的$ai变量 如果没有特别说明 均为当前实例

## 选择路由

```
// 获取人脸识别路由
$gateway = $ai->face();
// 获取人脸库用户路由
$gateway = $ai->faceset();
....
```

## 必选图片参数优化

> 百度ai的图像参数有三种 base64 ,url,face\_token 这里为了调用方便 因为进行了封装

```
// 以图片url的形式进行调用
$url = 'http://domain/someimg.jpg';
$gateway->url($url);
// 以base64编码图片进行调用
$code = 'Y3Jpc2VuY2hvdQ==';
$gateway->base64($code);
// 以本地路径
$path = 'path/to/some_img.jpg';
$gateway->path($path);
// 以百度ai的face token
$faceToken = 'face token';
$gateway->faceToken($path);
```

## 调用动作

```
// 如调用人脸检测
$res = $ai->face()->url($url)->detect();
```

## 兼容官方的可选参数格式

```
// 图片检索
$url = 'http://domain/someimg.jpg';
$ai->face()->url($url)->detect([
    'face_field'    => 'age',
    'max_face_num'  => '10',
    'face_type'     => 'LIVE'
]);
```

## 返回数据处理

```
$url = 'http://domain/someimg.jpg';
$res = $ai->face()->url($url)->detect([
    'face_field'    => 'age',
    'max_face_num'  => '10',
    'face_type'     => 'LIVE'
]);
if($res->success()){
    var_dump($res->toArray());
}else{
    // do something with exception
}
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://doc.crisen.org/ai/baidu.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
