本文介绍了如何使用阿里云视觉智能开放平台的.NET SDK,具体包括SDK的安装方法以及SDK代码示例。
准备工作
- 在安装和使用阿里云SDK前,确保您已经注册阿里云账号并生成访问密钥(AccessKey)。详情请参见创建AccessKey。
- 安装.NET SDK核心库。
- (可选)如果您的代码中引用了某个算法的类,请先安装对应算法类目的SDK。此处以人脸人体服务为例。
- 通过NuGet方式安装。
- 在Visual Studio中打开已有的项目,选择 。
- 搜索AlibabaCloud.SDK.Facebody20191230,在结果中找到AlibabaCloud.SDK.Facebody20191230,选择最新版本,单击安装。
- 通过.NET CLI方式安装。
执行
dotnet add package AlibabaCloud.SDK.Facebody20191230 --version 0.0.12
安装人脸人体服务的.NET SDK。
具体服务的SDK名称如下: - 通过NuGet方式安装。
代码示例
人脸检测定位代码示例如下。
说明 本示例中只引用到了阿里云核心库的类。
using System;
using System.Collections.Generic;
using Aliyun.Acs.Core;
using Aliyun.Acs.Core.Profile;
using Aliyun.Acs.Core.Exceptions;
using Aliyun.Acs.Core.Http;
namespace CommonRequestDemo
{
class Program
{
static void Main(string[] args)
{
IClientProfile profile = DefaultProfile.GetProfile("cn-shanghai", "<accessKeyId>", "<accessSecret>");
DefaultAcsClient client = new DefaultAcsClient(profile);
CommonRequest request = new CommonRequest();
request.Method = MethodType.POST;
request.Domain = "facebody.cn-shanghai.aliyuncs.com"; //外网访问域名,Endpoint。
request.Version = "2019-12-30"; //当前API能力的版本,您可以在SDK地址中查看最新的版本。
request.Action = "DetectFace"; //API接口名称,此处为DetectFace。
// request.Protocol = ProtocolType.HTTP;
request.AddQueryParameters("ImageURL", "https://viapi-test.oss-cn-shanghai.aliyuncs.com/test-team/tiankong/6%E4%BA%BA%E8%84%B8%E5%AE%9A%E4%BD%8D%E6%A3%80%E6%B5%8B.jpg");
try {
CommonResponse response = client.GetCommonResponse(request);
Console.WriteLine(System.Text.Encoding.Default.GetString(response.HttpResponse.Content));
}
catch (ServerException e)
{
Console.WriteLine(e);
}
catch (ClientException e)
{
Console.WriteLine(e);
}
}
}
}