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 Swift.
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.
NoteYou 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
To use IMM API V2020-09-30, you need to install IMM SDK V2020-09-30.
For more information about how to use IMM SDK for Swift, see Quick Start.
Usage
The following sample code detects face information in an image stored in the China (Hangzhou) region by using IMM SDK for Swift.
When you use the following sample code, replace the IMM endpoint with one for the region where your IMM project resides. Make sure that the IMM project and OSS object reside in the same region.
#!/usr/bin/env xcrun swift
import Cocoa
import Foundation
import Tea
import AlibabacloudImm20200930
import AlibabacloudOpenApi
import TeaUtils
open class Client {
public static func createClient(_ accessKeyId: String?, _ accessKeySecret: String?) throws -> AlibabacloudImm20200930.Client {
var config: AlibabacloudOpenApi.Config = AlibabacloudOpenApi.Config([
"accessKeyId": accessKeyId as! String,
"accessKeySecret": accessKeySecret as! String
])
// Specify the endpoint for the region which the IMM project resides.
config.endpoint = "imm.cn-hangzhou.aliyuncs.com"
return AlibabacloudImm20200930.Client(config)
}
@available(macOS 10.15, iOS 13, tvOS 13, watchOS 6, *)
public static func main(_ args: [String]?) async throws -> Void {
// The AccessKey pair of an Alibaba Cloud account has permissions to call all API operations. We recommend that you use the AccessKey pair of a RAM user to call API operations or perform routine O&M.
// We recommend that you do not include your AccessKey pair (AccessKey ID and AccessKey secret) in your project code. Otherwise, the AccessKey pair may be leaked and the security of all resources within your account may be compromised.
// In this example, the AccessKey pair is obtained from the environment variables. For information about how to configure environment variables, visit https://www.alibabacloud.com/help/en/imm/developer-reference/configure-environment-variables.
let accessKeyId = ProcessInfo.processInfo.environment["AccessKeyId"]
let accessKeySecret = ProcessInfo.processInfo.environment["AccessKeySecret"]
var client: AlibabacloudImm20200930.Client = try Client.createClient(accessKeyId, accessKeySecret)
var detectImageFacesRequest: AlibabacloudImm20200930.DetectImageFacesRequest = AlibabacloudImm20200930.DetectImageFacesRequest([
// Specify the name of the IMM project.
"projectName": "immtest",
// Specify the URI of the image in OSS.
"sourceURI": "oss://test-bucket/test-object.jpg"
])
var runtime: TeaUtils.RuntimeOptions = TeaUtils.RuntimeOptions([:])
do {
// Initiate the request.
try await client.detectImageFacesWithOptions(detectImageFacesRequest as! AlibabacloudImm20200930.DetectImageFacesRequest, runtime as! TeaUtils.RuntimeOptions)
}
catch {
if error is Tea.TeaError {
try TeaUtils.Client.assertAsString(error.message)
} else {
throw error
}
}
}
}
Client.main(CommandLine.arguments)