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

  1. 云栖社区>
  2. 博客>
  3. 正文

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

taro_秋刀鱼 2020-04-21 11:31:46 浏览1464
展开阅读全文

概述

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

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 使用简明示例
阿里云常见参数获取位置

网友评论

登录后评论
0/500
评论