Alibaba Cloud OpenAPI Explorer provides API documentation, API debugging, and SDK examples to help you get started by using the API. This topic describes how to install and use Intelligent Media Management (IMM) SDK for C#.
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 call the CreateProject operation to create a project. 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 IMM SDK for C#
To use IMM API V2020-09-30, you must use IMM SDK V2020-09-30.
For more information about how to use IMM SDK for C#, see Quick Start.
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 only within a specific 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 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 must delete or disable the long-term access credentials at the earliest opportunity 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 C#, see Configure environment variables to store temporary access credentials.
The following example describes how to use a temporary access credential in IMM SDK for C# to call API operations and detect faces and face information in an image in the China (Hangzhou) region.
When you use the following sample code, replace the IMM endpoint with the endpoint for the region where your IMM project resides. Make sure that the IMM project and OSS object reside in the same region.
Edit the
Program.csfile. The following example describes how to use IMM SDK for C# to detect faces and face information in an image in the China (Hangzhou) region.// This file is auto-generated, don't edit it. Thanks. using System; using System.Collections; using System.Collections.Generic; using System.IO; using System.Threading.Tasks; using Newtonsoft.Json; using Tea; using Tea.Utils; using Aliyun.Credentials.Models; namespace AlibabaCloud.SDK.Sample { public class Sample { /** * Use your AccessKey ID and AccessKey secret to initialize a client. * @return Client * @throws Exception */ public static AlibabaCloud.SDK.Imm20200930.Client CreateClient() { // Use the default credential to initialize a Credentials client. Aliyun.Credentials.Models.Config credentialsConfig = new Aliyun.Credentials.Models.Config() { // The type of the credential. Type = "sts", // Replace <ALIBABA_CLOUD_ACCESS_KEY_ID> with the temporary AccessKey ID that is obtained from the response. AccessKeyId = "<ALIBABA_CLOUD_ACCESS_KEY_ID>", // Replace <ALIBABA_CLOUD_ACCESS_KEY_SECRET> with the temporary AccessKey secret that is obtained from the response. AccessKeySecret = "<ALIBABA_CLOUD_ACCESS_KEY_SECRET>", // Replace <ALIBABA_CLOUD_SECURITY_TOKEN> with the STS token that is obtained from the response. SecurityToken = "<ALIBABA_CLOUD_SECURITY_TOKEN>" }; Aliyun.Credentials.Client credentialClient = new Aliyun.Credentials.Client(credentialsConfig); AlibabaCloud.OpenApiClient.Models.Config config = new AlibabaCloud.OpenApiClient.Models.Config() { Credential = credentialClient, // Specify the endpoint of IMM. Endpoint = "imm.cn-beijing.aliyuncs.com" }; return new AlibabaCloud.SDK.Imm20200930.Client(config); } public static void Main(string[] args) { // Initialize the ECS SDK client. AlibabaCloud.SDK.Imm20200930.Client client = CreateClient(); AlibabaCloud.SDK.Imm20200930.Models.DetectImageFacesRequest detectImageFacesRequest = new AlibabaCloud.SDK.Imm20200930.Models.DetectImageFacesRequest { ProjectName = "test", SourceURI = "oss://your-bucket-name/your-path/your-image.jpg", }; AlibabaCloud.TeaUtil.Models.RuntimeOptions runtime = new AlibabaCloud.TeaUtil.Models.RuntimeOptions(); try { // After you copy and run the sample code, obtain the return value of the operation. Console.WriteLine(JsonConvert.SerializeObject(client.DetectImageFacesWithOptions(detectImageFacesRequest, runtime).Body)); } catch (TeaException error) { // Handle exceptions with caution in actual business scenarios and never ignore exceptions in your project. In this example, error messages are displayed for your reference only. // Return error messages. Console.WriteLine(error.Message); // The URL of the corresponding error diagnostics page. Console.WriteLine(error.Data["Recommend"]); AlibabaCloud.TeaUtil.Common.AssertAsString(error.Message); } catch (Exception _error) { TeaException error = new TeaException(new Dictionary<string, object> { { "message", _error.Message } }); // Handle exceptions with caution in actual business scenarios and never ignore exceptions in your project. In this example, error messages are displayed for your reference only. // Return error messages. Console.WriteLine(error.Message); // The URL of the corresponding error diagnostics page. Console.WriteLine(error.Data["Recommend"]); AlibabaCloud.TeaUtil.Common.AssertAsString(error.Message); } } } }Run the
csc /out:D:\Program.cs D:\Program.execommand to compile the source file. After you run Program.exe, the following sample output is returned:{ "RequestId": "2CCA6D7E-C9FB-5C87-8AC5-D6E6AC493096", "Faces": [ { "Beard": "none", "MaskConfidence": 0.83, "Gender": "female", "Boundary": { "Left": 81, "Top": 62, "Height": 127, "Width": 96 }, "BeardConfidence": 1, "FigureId": "0fccc5f9-22df-4507-ac53-f6a37f11a15a", "Mouth": "open", "Emotion": "happiness", "Age": 25, "MouthConfidence": 0.998, "FigureType": "face", "GenderConfidence": 1, "HeadPose": { "Pitch": -27.196, "Roll": 6.075, "Yaw": 3.477 }, "Mask": "none", "EmotionConfidence": 0.999, "HatConfidence": 1, "GlassesConfidence": 1, "Sharpness": 1, "FigureClusterId": "figure-cluster-id-unavailable", "FaceQuality": 0.998, "Attractive": 0.998, "AgeSD": 7, "Glasses": "none", "FigureConfidence": 0.999, "Hat": "none" } ] }
Use long-term access credentials
The following example describes how to use a long-term access credential in IMM SDK for C# to call API operations and detect faces and face information in an image in the China (Hangzhou) region.
Edit the
Program.csfile. The following example describes how to use IMM SDK for C# to detect faces and face information in an image in the China (Hangzhou) region.// This file is auto-generated, don't edit it. Thanks. using System; using System.Collections; using System.Collections.Generic; using System.IO; using System.Threading.Tasks; using Newtonsoft.Json; using Tea; using Tea.Utils; namespace AlibabaCloud.SDK.Sample { public class Sample { /** * Use your AccessKey ID and AccessKey secret to initialize a client. * @return Client * @throws Exception */ public static AlibabaCloud.SDK.Imm20200930.Client CreateClient() { // If the project code is leaked, the AccessKey pair may be leaked and the security of all resources within your account may be compromised. The following sample code is provided only for reference. // We recommend that you use Security Token Service (STS) authentication, which provides higher security. For more information about authentication methods, visit https://www.alibabacloud.com/help/en/sdk/developer-reference/v2-manage-net-access-credentials?spm=a2c63.l28256.help-menu-262060.d_1_8_1_2.30be1b76RW6PpC. AlibabaCloud.OpenApiClient.Models.Config config = new AlibabaCloud.OpenApiClient.Models.Config { // Required. Make sure that the following environment variable is set in the code runtime environment: ALIBABA_CLOUD_ACCESS_KEY_ID. AccessKeyId = Environment.GetEnvironmentVariable("ALIBABA_CLOUD_ACCESS_KEY_ID"), // Required. Make sure that the following environment variable is set in the code runtime environment: ALIBABA_CLOUD_ACCESS_KEY_SECRET. AccessKeySecret = Environment.GetEnvironmentVariable("ALIBABA_CLOUD_ACCESS_KEY_SECRET"), }; // For a list of IMM endpoints for supported regions, visit https://api.aliyun.com/product/imm. config.Endpoint = "imm.cn-beijing.aliyuncs.com"; return new AlibabaCloud.SDK.Imm20200930.Client(config); } public static void Main(string[] args) { AlibabaCloud.SDK.Imm20200930.Client client = CreateClient(); AlibabaCloud.SDK.Imm20200930.Models.DetectImageFacesRequest detectImageFacesRequest = new AlibabaCloud.SDK.Imm20200930.Models.DetectImageFacesRequest { ProjectName = "test", SourceURI = "oss://your-bucket-name/your-path/your-image.jpg", }; AlibabaCloud.TeaUtil.Models.RuntimeOptions runtime = new AlibabaCloud.TeaUtil.Models.RuntimeOptions(); try { // After you copy and run the sample code, obtain the return value of the operation. Console.WriteLine(JsonConvert.SerializeObject(client.DetectImageFacesWithOptions(detectImageFacesRequest, runtime).Body)); } catch (TeaException error) { // Handle exceptions with caution in actual business scenarios and never ignore exceptions in your project. In this example, error messages are displayed for your reference only. // Return error messages. Console.WriteLine(error.Message); // The URL of the corresponding error diagnostics page. Console.WriteLine(error.Data["Recommend"]); AlibabaCloud.TeaUtil.Common.AssertAsString(error.Message); } catch (Exception _error) { TeaException error = new TeaException(new Dictionary<string, object> { { "message", _error.Message } }); // Handle exceptions with caution in actual business scenarios and never ignore exceptions in your project. In this example, error messages are displayed for your reference only. // Return error messages. Console.WriteLine(error.Message); // The URL of the corresponding error diagnostics page. Console.WriteLine(error.Data["Recommend"]); AlibabaCloud.TeaUtil.Common.AssertAsString(error.Message); } } } }Run the
csc /out:D:\Program.cs D:\Program.execommand to compile the source file. After you run Program.exe, the following sample output is returned:{ "RequestId": "2CCA6D7E-C9FB-5C87-8AC5-D6E6AC493096", "Faces": [ { "Beard": "none", "MaskConfidence": 0.83, "Gender": "female", "Boundary": { "Left": 81, "Top": 62, "Height": 127, "Width": 96 }, "BeardConfidence": 1, "FigureId": "0fccc5f9-22df-4507-ac53-f6a37f11a15a", "Mouth": "open", "Emotion": "happiness", "Age": 25, "MouthConfidence": 0.998, "FigureType": "face", "GenderConfidence": 1, "HeadPose": { "Pitch": -27.196, "Roll": 6.075, "Yaw": 3.477 }, "Mask": "none", "EmotionConfidence": 0.999, "HatConfidence": 1, "GlassesConfidence": 1, "Sharpness": 1, "FigureClusterId": "figure-cluster-id-unavailable", "FaceQuality": 0.998, "Attractive": 0.998, "AgeSD": 7, "Glasses": "none", "FigureConfidence": 0.999, "Hat": "none" } ] }