Updated on 2022-02-22 GMT+08:00

Searching for Images

This section describes how to use an image to search for similar images using AK/SK authentication.

Replace the AK/SK in the sample code with the actual AK/SK. Set the Path parameter in the initialized SearchPictureReq command to specify the URL of the image you want to use to search for similar images. After the configuration is complete, run the code.

  1. The sample code for image searching is as follows:
    package main 
     
    import (
        "fmt"
        "github.com/huaweicloud/huaweicloud-sdk-go-v3/core/auth/basic"
         imagesearch "github.com/huaweicloud/huaweicloud-sdk-go-v3/services/imagesearch/v1"
        "github.com/huaweicloud/huaweicloud-sdk-go-v3/services/imagesearch/v1/model"
         region "github.com/huaweicloud/huaweicloud-sdk-go-v3/services/imagesearch/v1/region"
    )
     
    func main() {
        // Set the obtained AK and SK.
        ak := "<YOUR AK>"
        sk := "<YOUR SK>"
     
        auth := basic.NewCredentialsBuilder().
            WithAk(ak).
            WithSk(sk).
            Build()
        //Set the request client. In this example, CN North-Beijing4 is used.
        client := imagesearch.NewImageSearchClient(
            imagesearch.ImagesearchClientBuilder().
                WithRegion(region.ValueOf("cn-north-4")).
                WithCredential(auth).
                Build())
     
        request := &model.RunSearchPictureRequest{
            InstanceName: "instance-name",
        }
        //Use the image URL to search for similar images.
        path := "https://bucketname.obs.cn-north-4.myhuaweicloud.com/imagesearch/test.jpg"
        request.Body = &model.SearchPictureReq{
            Path: &path,
        }
        response, err := client.RunSearchPicture(request)
        if err == nil {
            fmt.Printf("%+v\n", response)
        } else {
            fmt.Println(err)
        }
    }
  2. Execute the sample code. If the image search result is displayed on the console, the code is successfully executed. For details, see Searching for Images.
    RunSearchPictureResponse {"count":1,"result":[{"path":"https://bucketname.obs.cn-north-4.myhuaweicloud.com/imagesearch/test.jpg","sim":1,"tags":{}}]}