Try Image Search's similar search using Java SDK [Registration]


Last time, I registered an image using the import function of Image Search. With the import function, only the parameters for V1 can be registered, and the parameters newly added for V2 cannot be registered ~~. ** It's done. I'm sorry. ** **

So, I think it's better to register using API to input data to Image Search. There are some points to be aware of when registering data, so I would like to describe those points.

How to register data

This time, we will register using the Java SDK. Please refer to [Search] for environment construction.

package imagesearch.sample;

import java.util.Base64;

import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.imagesearch.model.v20190325.AddImageRequest;
import com.aliyuncs.imagesearch.model.v20190325.AddImageResponse;
import com.aliyuncs.imagesearch.model.v20190325.AddImageResponse.PicInfo;
import com.aliyuncs.profile.DefaultProfile;
import com.aliyuncs.profile.IClientProfile;

public class SimpleAdd {
    /** AccessKey */
    private static final String ACCESS_KEY = "XXXXXXXXXXXXXxxx";
    /** SeacretKey */
    private static final String KEY_SEACRET = "YYYYYYYYYYYYYYY";

    public static void main(String[] args) throws Exception {
        //Initialization(Tokyo region case
        DefaultProfile.addEndpoint("ap-northeast-1", "ImageSearch", "");
        IClientProfile profile = DefaultProfile.getProfile("ap-northeast-1", ACCESS_KEY, KEY_SEACRET);
        IAcsClient client = new DefaultAcsClient(profile);

        AddImageRequest request = new AddImageRequest();
        request.setStrAttr("Furniture: chair");
        request.setCustomContent("{'sample': 'sample'}");
        ClassLoader loader = Thread.currentThread().getContextClassLoader();
        InputStream is = loader.getResourceAsStream("item01.jpg ");
        String image = Base64.getEncoder().encodeToString(is.readAllBytes());

        AddImageResponse response = client.getAcsResponse(request);

        // Debug
        boolean checkShowJsonItemName = response.checkShowJsonItemName();
        Integer code = response.getCode();
        String message = response.getMessage();
        String requestId = response.getRequestId();
        Boolean success = response.getSuccess();
        PicInfo picInfo = response.getPicInfo();
        Integer categoryId = picInfo.getCategoryId();
        String region = picInfo.getRegion();

        System.out.printf("requestId:%s%nmessage:%s%ncheckShowJsonItemName:%s%ncode:%s%nsuccess:%s%n", requestId, message, checkShowJsonItemName, code, success);
        System.out.printf("\tcategoryId:%s%n\tregion:%s%n", categoryId, region);

Parameter description



Input Required. Supports product IDs up to 512 characters. You can include multiple images in one product.



Input Required. Supports image names up to 512 characters.

  1. Identify the image with ProductId + PicName.
  2. If the image added multiple times has the same ProductId + PicName, the last added image will be overwritten.

By registering multiple images for one product, you can improve the accuracy of the search. I think it is good to register some pattern images such as front image and diagonal image.



Input is optional. Image category.

  1. For product search: If a category is set, that setting takes precedence, otherwise the system will perform a category prediction and you can get the results of the predicted category in Response.
  2. For universal search: The system sets the category to 88888888, regardless of whether the category is set.

Configurable categories

Category ID Description
0 tops
1 dress
2 Bottoms
3 bag
4 shoes
5 Accessories
6 snack
7 Makeup
8 Bottle drink
9 furniture
20 toy
21 underwear
22 Digital equipment
88888888 Other



Input Required. Base64-encoded image content. Supports images up to 2 MB in size and a send latency of 5 seconds. Currently only jpg and png image formats are supported. Both vertical and horizontal pixels must be between 200 and 1024, and the image cannot contain rotation information.

In production operation, it may be better to include conversion logic before registration. Also, I don't know what it means to "support a transmission wait time of 5 seconds" here. .. .. what do you want. If anyone knows, please let me know. (I will check it in the source soon ...)



Input is optional. Whether subject recognition is necessary, the default is true.

  1. If true, the system will recognize the subject and perform a search by the recognized subject. You can get the subject recognition result with Response.
  2. If false, the subject will not be recognized and will be searched for as a whole image.

In the case of product search, I think true is fine. For example, if you want to search for something like the atmosphere of a room, you may register with false.



Input is optional. The subject range of the image in the format x1, x2, y1, y2, x1, y1 is the upper left point, x2, y2 is the lower right point. If the user sets a Region, the search will be performed in that Region regardless of the value of the Crop parameter.

When there are multiple objects in the image to be registered (for example, when trying to register a chair and a curtain or table is also shown), by setting the position information of the target object in order to improve the accuracy of the search, You can improve the accuracy of the search.



Input is optional. An integer attribute that can be used for filtering when searching, this field is returned when searched. For example, you can set IntAttr for each site image / user image, and filter them when they are searched.

Parameters that can be used for filters added from V2. It is now possible to filter in combination with StrAttr, which will be described later. By using a filter, you can get data that matches the filter conditions from Image Search. Previously, it was convenient because I had to filter after getting the data. If you ask, it would be helpful if you could set multiple values. .. .. The filter conditions are ">,> =, <, <=, =". If you set the price of the product, I think that you can narrow down the product to 10,000 yen to 30,000 yen, which is common on EC sites.



Input is optional. Supports string type attributes, up to 128 characters. It can be used for filtering when searched. This field is returned when searched.

Here you can set a string and filter it. The filter condition that can be used is "=,! =". Moreover, it is a filter with an exact match. It's pretty inconvenient. .. .. A realistic way to use it is to convert the parameters into bits and set them. .. .. It's still difficult to use. It would be very helpful if you could use multiple parameter settings and regular expressions.



Input is optional. Supports user-customized content, up to 4096 characters. This field is returned when searched. For example, you can add text such as an image description.

You can make it easier to use by setting JSON etc.

System limit

System Limits-User Guide | Alibaba Cloud Document Center

Here are some things to keep in mind when registering.

  1. Image size is 2 MB or less, length and width pixels are 200 or more and 1024 or less
  2. Supports two types of images, JPG and PNG, images cannot contain rotation information
  3. Restrictions on the use of concurrency
  4. You can check the number of searches and additional concurrencies (query / second (QPS) selected when creating an instance) in the console. You can currently select 5 queries / sec or 10 queries / sec. This means that you can process up to 5 or 10 search requests in parallel per second. (Maximum 1 request per second for free version)
  5. The default query / second (QPS) for delete operations is 20. This means that you can process up to 20 delete requests in parallel per second.


The points to be careful are as follows

  1. Image type
  2. Image size
  3. In case of continuous registration, API call restriction is applied
  4. It is difficult to design IntAttr and StrAttr that can be used for filter conditions.
  5. If you register with the same key, it will be overwritten (note that there are no primary key restrictions)

Recommended Posts

Try Image Search's similar search using Java SDK [Registration]
Try similar search of Image Search using Java SDK [Search]
Try using Hyperledger Iroha's Java SDK
Try image classification using TensorFlow Lite on Android (JAVA)
Try scraping using java [Notes]
Try Spark Submit to EMR using AWS SDK for Java
Try using Redis with Java (jar)
[Java] Try to implement using generics
Try using IBM Java method tracing
[Java] Where did you try using java?
Try using Java framework Nablarch [Web application]
Try using the Stream API in Java
Study Java Try using Scanner or Map
Try using JSON format API in Java
Try using JobScheduler's REST-API --Java RestClient implementation--
Try using the Wii remote with Java
Try adding text to an image in Scala using the Java standard library
Try using Firebase Cloud Functions on Android (Java)
Try using JobScheduler's REST-API --Java RestClient Test class-
Try local file search using Fess on CentOS7
Try local file search using Fess on CentOS8
Try using Sourcetrail (win version) in Java code
Try using GCP's Cloud Vision API in Java
Try using Sourcetrail (macOS version) in Java code
Try accessing the dataset from Java using JZOS
Try communication using gRPC on Android + Java server
Try using the COTOHA API parsing in Java