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

本文涉及的产品
对象存储 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 使用简明示例
阿里云常见参数获取位置

相关文章
|
2月前
|
SQL 存储 数据管理
阿里云视觉智能开放平台的逻辑数仓基于统一的SQL语法
【2月更文挑战第9天】阿里云视觉智能开放平台的逻辑数仓基于统一的SQL语法
52 2
|
2月前
|
存储 机器学习/深度学习 人工智能
阿里云视觉智能开放平台确实拥有视频目标检测的能力
【2月更文挑战第9天】阿里云视觉智能开放平台确实拥有视频目标检测的能力
94 7
|
2月前
|
开发者 Python
阿里云视觉智能开放平台中,请问这个如何排查?
【2月更文挑战第9天】阿里云视觉智能开放平台中,请问这个如何排查?
38 6
|
16天前
|
弹性计算 关系型数据库 MySQL
阿里云数据库服务器价格表,数据库创建、连接和使用教程
阿里云数据库使用流程包括购买和管理。选择所需数据库类型如MySQL,完成实名认证后购买,配置CPU、内存和存储。确保数据库地域与ECS相同以允许内网连接。创建数据库和账号,设置权限。通过DMS登录数据库,使用账号密码连接。同一VPC内的ECS需添加至白名单以进行内网通信。参考官方文档进行详细操作。
76 3
|
27天前
|
弹性计算 关系型数据库 MySQL
阿里云MySQL云数据库优惠价格、购买和使用教程分享!
阿里云数据库使用流程包括购买和管理。首先,选购支持MySQL、SQL Server、PostgreSQL等的RDS实例,如选择2核2GB的MySQL,设定地域和可用区。购买后,等待实例创建。接着,创建数据库和账号,设置DB名称、字符集及账号权限。最后,通过DMS登录数据库,填写账号和密码。若ECS在同一地域和VPC内,可内网连接,记得将ECS IP加入白名单。
420 2
|
27天前
|
SQL 关系型数据库 MySQL
阿里云mysql数据库价格购买和使用教程
阿里云数据库使用指南:购买MySQL、SQL Server等RDS实例,通过选择配置、地域和可用区完成购买。创建数据库和账号,分配权限。使用DMS登录数据库,进行管理操作。确保ECS与RDS在同一地域的VPC内,配置白名单实现内网连接。详细步骤见官方文档。
627 1
|
30天前
|
存储 弹性计算 数据库
阿里云优惠券是什么?2024年阿里云优惠券领取地址及使用教程汇总
阿里云作为国内领先的云计算服务提供商,为广大用户提供了丰富的云产品和解决方案。为了吸引用户上云,阿里云经常推出各种优惠活动,其中最受用户欢迎的就是阿里云优惠券。那么,阿里云优惠券究竟是什么呢?我们又该如何领取它呢?本文将为大家详细解答。
169 2
|
1月前
|
存储 弹性计算 数据库
2024年阿里云优惠券领取、使用教程及常见问题整理总结
随着云计算技术的不断发展,越来越多的企业和个人选择将业务迁移到云端。阿里云作为国内领先的云服务提供商,为用户提供了丰富的产品和服务。为了帮助用户降低成本,阿里云推出了优惠券活动,本文将为大家介绍阿里云优惠券的领取、使用方法以及常见问题解答。
175 0
|
1月前
|
存储 弹性计算 数据库
2024年阿里云优惠券领取和使用教程分享(图文教程)
2024年阿里云优惠券怎么领取?为了助力更多用户优惠上云,也为了让更多用户选择阿里云的云产品完成上云,阿里云公司针对新用户推出了满减优惠券,这款抵扣优惠券也就是大家俗称的满减抵扣券,自领取日30天起内有效。领取和使用优惠券是很多新手用户上云的必领福利,本文为各位新手用户介绍下2024年阿里云优惠券的领取和使用教程。
112 0
|
11天前
|
机器学习/深度学习 监控 算法
深度学习赋能智能监控:图像识别技术的革新与应用
【4月更文挑战第8天】 随着人工智能技术的飞速发展,深度学习在图像处理领域取得了突破性进展。特别是在智能监控系统中,基于深度学习的图像识别技术已成为提升安全和效率的关键工具。本文将探讨深度学习技术如何革新传统监控体系,增强其对复杂场景的理解能力,以及在实际部署中面临的挑战和解决方案。通过分析最新的研究成果和应用案例,我们揭示了深度学习在智能监控领域的潜力及其对未来社会发展的影响。
17 2

热门文章

最新文章