Help Center/ ImageSearch/ API Reference/ APIs/ Searching for Images
Updated on 2023-12-13 GMT+08:00

Searching for Images

Function

You can use the API to search for similar images from the image library.

URI

POST /v1/{project_id}/{instance_name}/image/search

Table 1 Path parameters

Name

Mandatory

Type

Description

instance_name

Yes

String

Instance name

project_id

Yes

String

Project ID. See Obtaining a Project ID and Name.

Request Message

Table 2 Request header parameters

Name

Mandatory

Type

Description

X-Auth-Token

Yes

String

User token

Used to obtain the permission to call APIs. For details about how to obtain the token, see Authentication. The value of X-Subject-Token in the response header is the token.

Table 3 Request body parameters

Name

Mandatory

Type

Description

file

No

String

Base64 character string converted from the image. Only the JPEG, JPG, PNG, and BMP formats are supported. The image resolution of the narrow sides of an image must be greater than or equal to 100 pixels, and that of the wide sides cannot exceed 2048 pixels. Configure either this parameter or path. If both parameters are set, file is used.

path

No

String

Image URL, which is the index ID of an image in the image library. Configure either this parameter or file. If both parameters are set, file is used.

limit

No

Integer

Number of retrieved images. The value is an integer ranging from 1 to 100.

Minimum value: 1

Maximum value: 100

Default value: 10

offset

No

Integer

Offset used to specify the start position of the search result. The value is an integer greater than or equal to 0. The default value is 0.

Minimum value: 0

tags

No

Object

Custom image tag. A maximum of 10 tags can be customized. The tags are key-value pairs. To add a tag name (key), do as follows:

  • Log in to the ImageSearch management console, click Create Instance, and configure Customize Tag.
  • Log in to the Image Search management console. Go to the Instance Management page, and click an instance name. On the Instance Details page that is displayed, configure Customize Tag. This parameter is mandatory when the image tag is used for search.

is_crop

No

Boolean

Indicates whether to specify an area (parameter box) in an image for search. This parameter is valid only for certain models. The value can be true or false.

  • true: Specify an area (parameter box) in an image for search.
  • false: Use the entire image to search.

The default value is false.

box

No

SearchBoxDetail object

Area specified in the image.

Table 4 SearchBoxDetail

Parameter

Mandatory

Type

Description

x

No

Integer

Minimum value of the horizontal (x) coordinate in an area, in pixels

y

No

Integer

Minimum value of the vertical (y) coordinate in an area, in pixels

width

No

Integer

Width of the specified area, in pixels

height

No

Integer

Height of the specified area, in pixels

Response Parameters

Status code: 200

Table 5 Response body parameter

Name

Type

Description

count

Integer

Total number of search records

result

Array of SearchPictureItem objects

Search result details

Table 6 SearchPictureItem

Name

Type

Description

path

String

Path of the searched image

sim

Float

Similarity between the original image and the searched image. The closer the value reaches 1, the more similar the images are.

tags

Object

Custom tag name and content

Status code: 400

Table 7 Response body parameter

Name

Type

Description

error_code

String

Error code of a failed API call. For details, see Error Codes.

This parameter is not included when the API is successfully called.

error_msg

String

Error message when the API call fails.

This parameter is not included when the API is successfully called.

Example Requests

  • Method 1: Use the Base64 code of an image to search for similar images in the image index library. The number of returned images is 10.
    POST https://{endpoint}/v1/{project_id}/{instance_name}/image/search
    
    {
      "limit" : 10,
      "offset" : 0,
      "file" : "/9j/4AAQSkZJRgABAgEASABIAAD/4RFZRXhpZgAATU0AKgAAAAgABwESAAMAAAABAAEAAAEaAAUAAAABAAAAYgEbAAUAAAABAAAA..."
    }
  • Method 2: Use the image URL to search for similar images in the image index library. The number of returned images is 10.
    POST https://{endpoint}/v1/{project_id}/{instance_name}/image/search
    
    {
      "limit" : 10,
      "offset" : 0,
      "path" : "https://BucketName.obs.myhuaweicloud.com/image/test1.jpg"
    }
  • Method 3: Use tags to search for similar images in the image index library. The number of returned images is 10.
    POST https://{endpoint}/v1/{project_id}/{instance_name}/image/search
    
    {
      "limit" : 10,
      "offset" : 0,
      "tags" : {
        "test-tags" : "test-image"
      }
    }
  • Method 4: Use the Base64 code and tags of an image to search for similar images in the image index library. The number of returned images is 10.
    POST https://{endpoint}/v1/{project_id}/{instance_name}/image/search
    
    {
      "limit" : 10,
      "offset" : 0,
      "file" : "/9j/4AAQSkZJRgABAgEASABIAAD/4RFZRXhpZgAATU0AKgAAAAgABwESAAMAAAABAAEAAAEaAAUAAAABAAAAYgEbAAUAAAABAAAA...",
      "tags" : {
        "test-tags" : "test-image"
      }
    }
  • Method 5: Use the image URL and tags to search for similar images in the image index library. The number of returned images is 10.
    POST https://{endpoint}/v1/{project_id}/{instance_name}/image/search
    
    {
      "limit" : 10,
      "offset" : 0,
      "path" : "https://BucketName.obs.myhuaweicloud.com/image/test1.jpg",
      "tags" : {
        "test-tags" : "test-image"
      }
    }
  • Method 6: Use a specified area to search for similar images in the image index library. The number of returned images is 10.
    POST https://{endpoint}/v1/{project_id}/{instance_name}/image/search
    
    {
      "limit" : 10,
      "offset" : 0,
      "path" : "https://BucketName.obs.myhuaweicloud.com/image/test1.jpg",
      "box" : {
        "x" : 100,
        "y" : 200,
        "height" : 50,
        "width" : 20
      }
    }

Example Response

Status code: 200

Example of a successful response

{
  "count" : 2,
  "result" : [ {
    "path" : "https://BucketName.obs.myhuaweicloud.com/CBL025106G/CBL025106G_2.JPG",
    "sim" : 0.51263,
    "tags" : {
      "test-tags" : "test-image"
    }
  } ]
}

Status code: 400

Example of a failed response

{
  "error_code" : "IS.0003",
  "error_msg" : "Arguments of path can not be null."
}

Status Code

Status Code

Description

200

Successful response

400

Failed response

Error Code

For details, see Error Codes.