All Products
Search
Document Center

Intelligent Media Management:IMM SDK for Go

Last Updated:Nov 19, 2024

Alibaba Cloud OpenAPI Explorer provides API documentation, API debugging, and SDK examples to help you get started with API development. This topic describes how to install and use Intelligent Media Management (IMM) SDK for Go.

Prerequisites

  • An AccessKey pair is created and obtained. For more information, see Create an AccessKey pair.

  • OSS is activated, a bucket is created, and objects are uploaded to the bucket. For more information, see Upload objects.

  • IMM is activated. For more information, see Activate IMM.

  • A project is created in the IMM console. For more information, see Create a project.

    Note
    • You can call the CreateProject operation to create a project. For more information, see CreateProject.

    • You can call the ListProjects operation to query the existing projects in a specific region. For more information, see ListProjects.

Install the SDK

Important

To use IMM API V2020-09-30, you must use IMM SDK V2020-09-30.

For more information about how to install IMM SDK for Go, see Quick Start.

Access credentials

The following types of access credentials are supported:

  • Temporary access credentials: In scenarios where high security is critical, such as temporary authorization of access to IMM, we recommend that you use temporary access credentials. Temporary access credentials are valid within the specified period of time, which helps prevent credential leaks. Temporary access credentials support fine-grained access control, which prevents security risks caused by excessive permissions.

  • Long-term access credentials: To ensure data security, we recommend that you do not use long-term access credentials. For scenarios that require convenience, long-term access credentials eliminate the need for multiple refreshes within a long period of time. We recommend that you change your long-term access credentials every three months to ensure account security. If long-term access credentials are leaked or no longer used, you can delete or disable the long-term access credentials to reduce security risks.

Use temporary access credentials

The following sample code provides an example on how to access IMM by using IMM SDK for Go based on temporary access credentials. For more information about how to configure temporary access credentials, see Configure environment variables to store temporary access credentials.

package main

import (
	"fmt"
	"github.com/alibabacloud-go/tea/tea"
	"os"

	openapi "github.com/alibabacloud-go/darabonba-openapi/v2/client"
	imm "github.com/alibabacloud-go/imm-20200930/v4/client"
	sts20150401 "github.com/alibabacloud-go/sts-20150401/v2/client"
)

func main() {
	// Create an STS client and call the AssumeRole operation to obtain an STS token.
	stsConfig := &openapi.Config{}
	stsConfig.SetAccessKeyId(os.Getenv("ALIBABA_CLOUD_ACCESS_KEY_ID"))
	stsConfig.SetAccessKeySecret(os.Getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET"))
	stsConfig.SetEndpoint("sts.cn-beijing.aliyuncs.com")
	stsClient, _err := sts20150401.NewClient(stsConfig)
	if _err != nil {
		panic(_err)
	}

	// Call the AssumeRole operation to obtain an STS token.
	assumeRoleRequest := &sts20150401.AssumeRoleRequest{
		RoleArn:         tea.String("acs:ram::125499367423****:role/STStokenTestRole"),
		RoleSessionName: tea.String("STSsessionName"),
		DurationSeconds: tea.Int64(3600),
	}
	assumeRoleResp, err := stsClient.AssumeRole(assumeRoleRequest)
	if err != nil {
		panic(err)
	}

	// Use the STS token to create an IMM client.
	immConfig := &openapi.Config{
		AccessKeyId:     assumeRoleResp.Body.Credentials.AccessKeyId,
		AccessKeySecret: assumeRoleResp.Body.Credentials.AccessKeySecret,
		SecurityToken:   assumeRoleResp.Body.Credentials.SecurityToken,
		Endpoint:        tea.String("imm.cn-beijing.aliyuncs.com"),
	}

	immClient, err := imm.NewClient(immConfig)
	if err != nil {
		panic(err)
	}

	// Construct a request to detect faces and face information from an image.
	projectName := "imm-go-sdk-doc-demo"
	sourceUri := "oss://your-bucket-name/your-path/your-image.jpg"
	detectImageFacesRequest := &imm.DetectImageFacesRequest{
		ProjectName: tea.String(projectName),
		SourceURI:   tea.String(sourceUri),
	}

	// Initiate the request.
	detectImageFacesResp, err := immClient.DetectImageFaces(detectImageFacesRequest)
	if err != nil {
		fmt.Println(err)
		return
	}

	// Display the response body.
	fmt.Println(detectImageFacesResp.Body.String())
}

Use long-term access credentials

To access IMM by using IMM SDK for Go based on long-term access credentials, you need to configure long-term access credentials first. For more information, see Configure environment variables to store the AccessKey pair.

The following example describes how to use IMM SDK for Go to detect faces and obtain the face information in an image in the China (Hangzhou) region.

  1. Create a main.go file and write the following code to the file:

    package main
    import (
       "fmt"
       "os"
       openapi  "github.com/alibabacloud-go/darabonba-openapi/v2/client"
       imm "github.com/alibabacloud-go/imm-20200930/v4/client"
       "github.com/alibabacloud-go/tea/tea"
    )
    func main() {
       // Initialize the IMM client. 
       // The AccessKey pair of an Alibaba Cloud account has permissions on all API operations. We recommend that you use a RAM user to call API operations or perform routine O&M. 
       // For security reasons, we recommend that you do not embed your AccessKey pair in code. 
       // In this example, the AccessKey pair is obtained from the environment variables. For more information about how to configure environment variables, visit https://www.alibabacloud.com/help/document_detail/2361894.html. 
       immClient, err := imm.NewClient(&openapi.Config{
          RegionId: tea.String("cn-beijing"),                   // Specify the region ID. 
          AccessKeyId: tea.String(os.Getenv("ALIBABA_CLOUD_ACCESS_KEY_ID")),         
          AccessKeySecret: tea.String(os.Getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET")), 
       })
       if err != nil {
          // Handle errors. 
          panic(err)
       }
       // Specify the name of the project. 
       projectName := "imm-go-sdk-doc-demo"
       // Specify the OSS URI of the image. 
       sourceUri := "oss://your-bucket-name/your-path/your-image.jpg"
       // Create a request to detect faces and obtain the face information in the image. 
       detectImageFacesRequest := &imm.DetectImageFacesRequest{
          ProjectName: tea.String(projectName),
          SourceURI:   tea.String(sourceUri),
       }
       // Initiate the request. 
       detectImageFacesResp, err := immClient.DetectImageFaces(detectImageFacesRequest)
       if err != nil {
          // Handle errors. 
          fmt.Println(err)
          return
       }
       // Print the response body. 
       fmt.Println(detectImageFacesResp.Body.String())
    }
  2. Run the go run main.go command. Sample response:

    {
       "Faces": [
          {
             "Age": 25,
             "AgeSD": 7,
             "Attractive": 0.998,
             "Beard": "none",
             "BeardConfidence": 1,
             "Boundary": {
                "Height": 127,
                "Left": 81,
                "Top": 62,
                "Width": 96
             },
             "Emotion": "happiness",
             "EmotionConfidence": 0.999,
             "FaceQuality": 0.998,
             "FigureClusterId": "figure-cluster-id-unavailable",
             "FigureConfidence": 0.999,
             "FigureId": "30f18b25-db81-4dc6-8461-fd63ab0ef16d",
             "FigureType": "face",
             "Gender": "female",
             "GenderConfidence": 1,
             "Glasses": "none",
             "GlassesConfidence": 1,
             "Hat": "none",
             "HatConfidence": 1,
             "HeadPose": {
                "Pitch": -16.989,
                "Roll": 6.006,
                "Yaw": 7.47
             },
             "Mask": "none",
             "MaskConfidence": 0.83,
             "Mouth": "open",
             "MouthConfidence": 0.998,
             "Sharpness": 1
          }
       ],
       "RequestId": "28084591-0A06-0981-9AD5-B0FBD0967FD7"
    }