阿里云视觉智能开放平台--视觉搜索(图像搜索)使用教程

本文涉及的产品
对象存储 OSS,20GB 3个月
对象存储 OSS,恶意文件检测 1000次 1年
对象存储 OSS,内容安全 1000次 1年
简介: 视觉搜索服务基于阿里云深度学习技术,进行视觉内容搜索,在指定图像图像库中搜索出相同或相似的视觉信息,适用于内容比对、内容精确查找、相似素材搜索等场景。

概述

视觉搜索服务基于阿里云深度学习技术,进行视觉内容搜索,在指定图像图像库中搜索出相同或相似的视觉信息,适用于内容比对、内容精确查找、相似素材搜索等场景。

Step By Step

1、服务开通,参考链接:阿里云视觉智能开放平台使用简明教程

2、目前提供图像搜索相关的7个API能力;

3、操作流程
_

4、Code Sample

  • 4.1 pom.xml
        <dependency>
            <groupId>com.aliyun</groupId>
            <artifactId>aliyun-java-sdk-core</artifactId>
            <version>4.5.1</version>
        </dependency>
  • 4.2 Java Code
import com.aliyuncs.CommonRequest;
import com.aliyuncs.CommonResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;

public class DemoSearch {

    static IAcsClient client = null;

    public static void main(String[] args) {

        // ccess Key ID、Access Key Secret 获取参考:https://yq.aliyun.com/articles/693979
        DefaultProfile profile = DefaultProfile.getProfile(
                "cn-shanghai",             //默认
                "LTAI9r2Z********",         //您的Access Key ID
                "W4SPz7p4McBhhc****************");    //您的Access Key Secret

        client = new DefaultAcsClient(profile);

        String dbName = "searchdemo";
//        createImageDb(client,dbName);
//        deleteImageDb(client, dbName);

//        deleteImage(client, dbName, "1");
//        deleteImage(client, dbName, "2");
//        deleteImage(client, dbName, "3");

//        listImageDbs(client);

        String imageUrl1 = "https://taroshanghai.oss-cn-shanghai.aliyuncs.com/searchpictest/changcheng.jpg";
        String imageUrl2 = "https://taroshanghai.oss-cn-shanghai.aliyuncs.com/searchpictest/erfeiertieta.jpg";
        String imageUrl3 = "https://taroshanghai.oss-cn-shanghai.aliyuncs.com/searchpictest/jingtian2.jpeg";

//        addImage(client,dbName,"1","图搜图片1",imageUrl1);
//        addImage(client,dbName,"2","图搜图片2",imageUrl2);
//        addImage(client,dbName,"3","图搜图片3",imageUrl3);
//        searchImage(client,imageUrl1,2,dbName);

        listImages(client,dbName);
    }


    /**
     * 创建图片数据库
     * @param client client
     * @param name 数据库名称
     */
    public static void createImageDb(IAcsClient client, String name)
    {
        CommonRequest commonRequest = new CommonRequest();
        commonRequest.setSysAction("CreateImageDb");
        commonRequest.putBodyParameter("Name",name);
        commonRequest.setSysMethod(MethodType.POST);
        commonRequest.setSysDomain("imgsearch.cn-shanghai.aliyuncs.com");
        commonRequest.setSysVersion("2020-03-20");

        CommonResponse response = null;
        try {
            response = client.getCommonResponse(commonRequest);
        } catch (ClientException e) {
            e.printStackTrace();
        }
        System.out.println("创建图片数据库:");
        System.out.println(response.getData());
    }


    /**
     * 查看数据库的列表
     * @param client
     */
    public static void listImageDbs(IAcsClient client)
    {
        CommonRequest commonRequest = new CommonRequest();
        commonRequest.setSysVersion("2020-03-20");
        commonRequest.setSysAction("ListImageDbs");
        commonRequest.setSysMethod(MethodType.POST);
        commonRequest.setSysDomain("imgsearch.cn-shanghai.aliyuncs.com");

        CommonResponse response = null;
        try {
            response = client.getCommonResponse(commonRequest);
        } catch (ClientException e) {
            e.printStackTrace();
        }
        System.out.println("查看数据库的列表:");
        System.out.println(response.getData());
    }

    /**
     * 为指定数据库添加图片数据
     * @param client
     * @param dbName 数据库名称
     * @param entityId 实体ID,可以作为数据分组的ID
     * @param extraData 自定义数据
     * @param imageUrl 图片地址,必须是同Region的OSS图片地址
     */
    public static void addImage(IAcsClient client, String dbName, String entityId, String extraData, String imageUrl)
    {
        CommonRequest commonRequest = new CommonRequest();
        commonRequest.setSysVersion("2020-03-20");
        commonRequest.setSysAction("AddImage");
        commonRequest.setSysMethod(MethodType.POST);
        commonRequest.setSysDomain("imgsearch.cn-shanghai.aliyuncs.com");
        commonRequest.putBodyParameter("DbName", dbName);
        commonRequest.putBodyParameter("EntityId", entityId);
        commonRequest.putBodyParameter("ExtraData", extraData);
        commonRequest.putBodyParameter("ImageUrl", imageUrl);

        CommonResponse response = null;
        try {
            response = client.getCommonResponse(commonRequest);
        } catch (ClientException e) {
            e.printStackTrace();
        }
        System.out.println("为指定数据库添加图片数据:");
        System.out.println(response.getData());
    }

    /**
     * 数据库中搜索相似的图片
     * @param client
     * @param imageUrl 图片地址,必须是同Region的OSS的图片地址
     * @param limit 获取结果数量上限,取值范围1~1000
     * @param dbName 数据库名称
     */
    public static void searchImage(IAcsClient client, String imageUrl, Integer limit, String dbName)
    {
        CommonRequest commonRequest = new CommonRequest();
        commonRequest.setSysVersion("2020-03-20");
        commonRequest.setSysAction("SearchImage");
        commonRequest.setSysMethod(MethodType.POST);
        commonRequest.setSysDomain("imgsearch.cn-shanghai.aliyuncs.com");
        commonRequest.putBodyParameter("Limit", limit);
        commonRequest.putBodyParameter("ImageUrl", imageUrl);
        commonRequest.putBodyParameter("DbName", dbName);

        CommonResponse response = null;
        try {
            response = client.getCommonResponse(commonRequest);
        } catch (ClientException e) {
            e.printStackTrace();
        }
        System.out.println("搜索图片:");
        System.out.println(response.getData());
    }

    /**
     * 删除指定数据库
     * @param client
     * @param dbName 数据库名称
     */
    public static void deleteImageDb(IAcsClient client, String dbName)
    {
        CommonRequest commonRequest = new CommonRequest();
        commonRequest.setSysVersion("2020-03-20");
        commonRequest.setSysAction("DeleteImageDb");
        commonRequest.setSysMethod(MethodType.POST);
        commonRequest.setSysDomain("imgsearch.cn-shanghai.aliyuncs.com");
        commonRequest.putBodyParameter("Name", dbName);

        CommonResponse response = null;
        try {
            response = client.getCommonResponse(commonRequest);
        } catch (ClientException e) {
            e.printStackTrace();
        }
        System.out.println("删除指定数据库:");
        System.out.println(response.getData());
    }


    /**
     * 删除指定数据库中的图片
     * @param client
     * @param dbName 数据库名称
     * @param entityId 待删除数据的实体ID
     */
    public static void deleteImage(IAcsClient client, String dbName, String entityId)
    {
        CommonRequest commonRequest = new CommonRequest();
        commonRequest.setSysVersion("2020-03-20");
        commonRequest.setSysAction("DeleteImage");
        commonRequest.setSysMethod(MethodType.POST);
        commonRequest.setSysDomain("imgsearch.cn-shanghai.aliyuncs.com");
        commonRequest.putBodyParameter("DbName", dbName);
        commonRequest.putBodyParameter("EntityId", entityId);

        CommonResponse response = null;
        try {
            response = client.getCommonResponse(commonRequest);
        } catch (ClientException e) {
            e.printStackTrace();
        }
        System.out.println("删除指定数据库中的图片:");
        System.out.println(response.getData());
    }


    /**
     * 查看指定数据库中的图片数据列表
     * @param client
     * @param dbName 数据库名称
     * 更多参数的支持可以参考API说明:https://help.aliyun.com/document_detail/159128.html?spm=a2c4g.11186623.6.715.2c697c1edNt7uW 通过方法重载实现
     */
    public static void listImages(IAcsClient client, String dbName)
    {
        CommonRequest commonRequest = new CommonRequest();
        commonRequest.setSysVersion("2020-03-20");
        commonRequest.setSysAction("ListImages");
        commonRequest.setSysMethod(MethodType.POST);
        commonRequest.setSysDomain("imgsearch.cn-shanghai.aliyuncs.com");
        commonRequest.putBodyParameter("DbName", dbName);

        CommonResponse response = null;
        try {
            response = client.getCommonResponse(commonRequest);
        } catch (ClientException e) {
            e.printStackTrace();
        }
        System.out.println("查看指定数据库中的图片数据列表:");
        System.out.println(response.getData());
    }
}

参考链接

视觉搜索介绍
阿里云人脸识别 1:N 使用简明示例
阿里云常见参数获取位置

相关文章
|
26天前
|
机器学习/深度学习 编解码 人工智能
阿里云Stable Diffusion操作教程
阿里云Stable Diffusion操作教程
188 0
|
27天前
|
SQL 弹性计算 安全
购买阿里云活动内云服务器之后设置密码、安全组、增加带宽、挂载云盘教程
当我们通过阿里云的活动购买完云服务器之后,并不是立马就能使用了,还需要我们设置云服务器密码,配置安全组等基本操作之后才能使用,有的用户还需要购买并挂载数据盘到云服务器上,很多新手用户由于是初次使用阿里云服务器,因此并不知道这些设置的操作流程,下面给大家介绍下这些设置的具体操作流程。
购买阿里云活动内云服务器之后设置密码、安全组、增加带宽、挂载云盘教程
|
28天前
|
SQL 关系型数据库 MySQL
2024年阿里云数据库创建_数据库账号密码和连接教程
阿里云数据库怎么使用?阿里云百科整理阿里云数据库从购买到使用全流程,阿里云支持MySQL、SQL Server、PostgreSQL和MariaDB等数据库引擎,阿里云数据库具有高可用、高容灾特性,阿里云提供数据库备份、恢复、迁移全套解决方案。详细阿里云数据库购买和使用流程方法如下
|
1月前
|
弹性计算 安全 数据安全/隐私保护
2024年阿里云幻兽帕鲁Palworld联机服务器搭建部署指南,图文教程
近年来,幻兽帕鲁以其独特的游戏魅力,迅速成为玩家们的热门选择。对于许多想要与好友一起体验这款游戏的玩家来说,如何搭建一个稳定的联机服务器成为了他们关注的焦点。今天,我将为大家带来一篇简单易懂的图文教程,让你轻松搭建幻兽帕鲁的联机服务器,与好友们畅快游戏。
|
1月前
|
弹性计算 网络协议 安全
宝塔面板放行阿里云端口命令操作教程
布署宝塔面板阿里云服务器需要开放哪些端口?阿里云服务器“安全组设置”详解
170 1
|
24天前
|
SQL 关系型数据库 MySQL
阿里云MySQL数据库价格、购买、创建账号密码和连接数据库教程
阿里云数据库使用指南:购买MySQL、SQL Server等RDS实例,选择配置和地区,完成支付。创建数据库和账号,设置权限。通过DMS登录数据库,使用账号密码访问。同地域VPC内的ECS需将IP加入白名单以实现内网连接。参考链接提供详细步骤。
366 3
|
30天前
|
弹性计算 运维 安全
2024年阿里云上自建Palworld/幻兽帕鲁服务器教程
对于深爱《幻兽帕鲁》的玩家们,能够与好友共同联机探索游戏的奇幻世界,无疑是提升游戏体验的一大乐趣。但如何迅速且有效地搭建一个稳定且高效的联机服务器呢?今天,我们就来为大家分享一个详细的教程,让你轻松掌握自建《幻兽帕鲁》服务器的方法,让联机游戏之旅更加顺畅。
1409 4
|
14天前
|
弹性计算 关系型数据库 MySQL
阿里云数据库服务器价格表,数据库创建、连接和使用教程
阿里云数据库使用流程包括购买和管理。选择所需数据库类型如MySQL,完成实名认证后购买,配置CPU、内存和存储。确保数据库地域与ECS相同以允许内网连接。创建数据库和账号,设置权限。通过DMS登录数据库,使用账号密码连接。同一VPC内的ECS需添加至白名单以进行内网通信。参考官方文档进行详细操作。
71 3
|
24天前
|
弹性计算 关系型数据库 MySQL
阿里云MySQL云数据库优惠价格、购买和使用教程分享!
阿里云数据库使用流程包括购买和管理。首先,选购支持MySQL、SQL Server、PostgreSQL等的RDS实例,如选择2核2GB的MySQL,设定地域和可用区。购买后,等待实例创建。接着,创建数据库和账号,设置DB名称、字符集及账号权限。最后,通过DMS登录数据库,填写账号和密码。若ECS在同一地域和VPC内,可内网连接,记得将ECS IP加入白名单。
414 2
|
25天前
|
SQL 关系型数据库 MySQL
阿里云mysql数据库价格购买和使用教程
阿里云数据库使用指南:购买MySQL、SQL Server等RDS实例,通过选择配置、地域和可用区完成购买。创建数据库和账号,分配权限。使用DMS登录数据库,进行管理操作。确保ECS与RDS在同一地域的VPC内,配置白名单实现内网连接。详细步骤见官方文档。
626 1

热门文章

最新文章