天天看點

阿裡雲視覺智能開放平台--視覺搜尋(圖像搜尋)使用教程

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 使用簡明示例 阿裡雲常見參數擷取位置