首先创建应用:
https://console.bce.baidu.com/ai/?_=&fromai=1#/ai/imagesearch/overview/index
会有2参数要用到:
然后申请建库
随便传几张图片到里面。
接口:ImageIdentification.php
<?php
/**
* Created by PhpStorm.
* User: Administrator
* Date: 2021/4/12
* Time: 15:07
*/
namespace app\common;
/**
* AI图片搜索
* Class ImageIdentification
* @package app\common
*/
class ImageIdentification
{
/**
* @var string
*/
protected static $client_id;
/**
* @var string
*/
protected static $client_secret;
/**
* 构造函数
* ImageIdentification constructor.
*/
protected static function construct()
{
self::$client_id = 'PM4vfxjGTZt8SQBg0YhAtNDb';
self::$client_secret = 'secret';
}
/**
* @param $path
* @param string $name
* @param int $id
* @return string
* @throws \ImagickException
*/
public static function addImageDent($path, $name = '杜小明', $id = 1)
{
$path = self::reduceSize($path);
$token = static::getToken();
$url = 'https://aip.baidubce.com/rest/2.0/image-classify/v1/realtime_search/similar/add?access_token=' . $token;
$img = file_get_contents($path);
$img = base64_encode($img);
$bodys = array(
'brief' => '{"name":"' . $name . '", "id":"' . $id . '"}',
'image' => $img,
'tags' => "1,1"
);
$res = self::request_posts_http($url, $bodys