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 Java.
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 about how to create a project by using the IMM console, see Create a project.
NoteYou can also create a project by calling the CreateProject operation. For more information, see CreateProject.
You can call the ListProjects operation to query 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 Java, 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
For more information about how to configure access credentials for temporary access to IMM by using IMM SDK for Java, see Configure environment variables to store temporary access credentials.
The following sample code provides an example on how to detect faces and face attributes in an image within a project in the China (Hangzhou) region by using temporary access credentials.
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.
package com.aliyun.sample;
import com.aliyun.imm20200930.Client;
import com.aliyun.imm20200930.models.DetectImageFacesRequest;
import com.aliyun.imm20200930.models.DetectImageFacesResponse;
import com.aliyun.sts20150401.models.AssumeRoleRequest;
import com.aliyun.sts20150401.models.AssumeRoleResponse;
import com.aliyun.sts20150401.models.AssumeRoleResponseBody;
import com.aliyun.teaopenapi.models.Config;
public class STS_Test {
// Specify the name of the project. Make sure that the specified project exists.
static String projectName = "imm-java-sdk-doc-demo";
// Specify the URI of the image in OSS.
static String sourceURI = "oss://your-bucket-name/your-path/your-image.jpg";
// 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 more information about how to configure environment variables, visit https://help.aliyun.com/document_detail/2361894.html.
static String immAccessKeyId = System.getenv("ALIBABA_CLOUD_ACCESS_KEY_ID");
static String immAccessKeySecret = System.getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET");
public static void main(String[] args) throws Exception {
Config stsConfig = new Config()
.setAccessKeyId(immAccessKeyId)
.setAccessKeySecret(immAccessKeySecret)
.setEndpoint("sts-share.cn-hangzhou.aliyuncs.com");
com.aliyun.sts20150401.Client stsClient = new com.aliyun.sts20150401.Client(stsConfig);
AssumeRoleRequest assumeRoleRequest = new AssumeRoleRequest()
.setRoleArn("acs:ram::125499367423****:role/STStokenTestRole/STSsessionName")
.setRoleSessionName("immtest")
.setDurationSeconds(3600L);
AssumeRoleResponse assumeRoleResponse = stsClient.assumeRole(assumeRoleRequest);
AssumeRoleResponseBody.AssumeRoleResponseBodyCredentials assumeRoleResponseBodyCredentials = assumeRoleResponse.body.credentials;
Config immConfig = new Config();
immConfig.setAccessKeyId(assumeRoleResponseBodyCredentials.getAccessKeyId());
immConfig.setAccessKeySecret(assumeRoleResponseBodyCredentials.getAccessKeySecret());
immConfig.setSecurityToken(assumeRoleResponseBodyCredentials.getSecurityToken());
immConfig.setEndpoint("imm.cn-hangzhou.aliyuncs.com");
com.aliyun.imm20200930.Client immClient = new com.aliyun.imm20200930.Client(immConfig);
// Create a request to detect faces and face attributes in the image.
DetectImageFacesRequest detectImageFacesRequest = new DetectImageFacesRequest();
detectImageFacesRequest.setProjectName(STS_Test.projectName);
detectImageFacesRequest.setSourceURI(STS_Test.sourceURI);
// Initiate the request.
DetectImageFacesResponse detectImageFacesResponse = immClient.detectImageFaces(detectImageFacesRequest);
// Print the response body.
System.out.println(detectImageFacesResponse.body.toMap());
}
}
The following content provides a sample response.
{
"faces": [
{
"attractive": 0.9810000061988831,
"beard": "none",
"beardConfidence": 0.9990000128746033,
"boundary": {
"height": 320,
"left": 217,
"top": 169,
"width": 226
},
"emotion": "happiness",
"emotionConfidence": 1,
"faceQuality": 0.9869999885559082,
"figureClusterId": "figure-cluster-id-unavailable",
"figureConfidence": 1,
"figureId": "92b7ed67-6344-4410-b5ed-****",
"figureType": "face",
"glasses": "none",
"glassesConfidence": 0.9990000128746033,
"hat": "none",
"hatConfidence": 1,
"headPose": {
"pitch": -17.742000579833984,
"roll": 3.2850000858306885,
"yaw": -0.7279999852180481
},
"mask": "none",
"maskConfidence": 0.7559999823570251,
"mouth": "open",
"mouthConfidence": 1,
"sharpness": 1,
...
}
],
"requestId": "5BE08720-554C-566F-A642-****"
}
Use long-term access credentials
The following sample code provides an example on how to detect faces and face attributes in an image within a project in the China (Hangzhou) region by using long-term access credentials.
package com.aliyun.sample;
import com.aliyun.imm20200930.Client;
import com.aliyun.imm20200930.models.DetectImageFacesRequest;
import com.aliyun.imm20200930.models.DetectImageFacesResponse;
import com.aliyun.teaopenapi.models.Config;
public class Sample {
// Specify the name of the project. Make sure that the specified project exists.
static String projectName = "imm-java-sdk-doc-demo";
// Specify the URI of the image in OSS.
static String sourceURI = "oss://your-bucket-name/your-path/your-image.jpg";
// 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 more information about how to configure environment variables, visit https://help.aliyun.com/document_detail/2361894.html.
static String immAccessKeyId = System.getenv("ALIBABA_CLOUD_ACCESS_KEY_ID");
static String immAccessKeySecret = System.getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET");
public static void main(String[] args) {
Config config = new Config()
.setRegionId("cn-hangzhou") // Specify the ID of the region in which the project resides.
.setAccessKeyId(immAccessKeyId)
.setAccessKeySecret(immAccessKeySecret);
Client client = null;
try {
client = new Client(config);
} catch (Exception e) {
throw new RuntimeException(e);
}
// Create a request to detect faces and face attributes in the image.
DetectImageFacesRequest detectImageFacesRequest = new DetectImageFacesRequest();
detectImageFacesRequest.setProjectName(Sample.projectName);
detectImageFacesRequest.setSourceURI(Sample.sourceURI);
// Initiate the request.
DetectImageFacesResponse detectImageFacesResponse = null;
try {
detectImageFacesResponse = client.detectImageFaces(detectImageFacesRequest);
} catch (Exception e) {
throw new RuntimeException(e);
}
// Print the response body.
System.out.println(detectImageFacesResponse.body.toMap());
}
}
The following content provides a sample response.
{
"faces": [
{
"attractive": 0.9810000061988831,
"beard": "none",
"beardConfidence": 0.9990000128746033,
"boundary": {
"height": 320,
"left": 217,
"top": 169,
"width": 226
},
"emotion": "happiness",
"emotionConfidence": 1,
"faceQuality": 0.9869999885559082,
"figureClusterId": "figure-cluster-id-unavailable",
"figureConfidence": 1,
"figureId": "92b7ed67-6344-4410-b5ed-****",
"figureType": "face",
"glasses": "none",
"glassesConfidence": 0.9990000128746033,
"hat": "none",
"hatConfidence": 1,
"headPose": {
"pitch": -17.742000579833984,
"roll": 3.2850000858306885,
"yaw": -0.7279999852180481
},
"mask": "none",
"maskConfidence": 0.7559999823570251,
"mouth": "open",
"mouthConfidence": 1,
"sharpness": 1,
...
}
],
"requestId": "5BE08720-554C-566F-A642-****"
}