视觉智能平台有没有C#调用SDK进行人脸表情识别的DEMO的?公司要迁移表情识别
是的,阿里云视觉智能平台提供了针对不同编程语言和平台的SDK,包括C#语言的SDK,可以用于快速访问平台提供的API接口。您可以前往阿里云官网,下载对应的C# SDK,并参考文档进行集成。
针对人脸表情识别,您可以调用平台提供的表情识别API来实现。下面提供一个C#示例代码供参考:
using System;
using System.IO;
using System.Net;
using System.Net.Http;
using System.Threading.Tasks;
using Newtonsoft.Json.Linq;
namespace FaceDemo
{
class Program
{
static async Task Main(string[] args)
{
string url = "https://dtplus-cn-shanghai.data.aliyuncs.com/face/attribute";
// 替换为您的AccessKey ID和AccessKey Secret
string accessKeyId = "<Your AccessKey ID>";
string accessKeySecret = "<Your AccessKey Secret>";
string imageFilePath = "image.jpg";
string imageBase64 = Convert.ToBase64String(File.ReadAllBytes(imageFilePath));
string body = "{\"type\":\"1\", \"content\":\"" + imageBase64 + "\"}";
string timestamp = DateTime.UtcNow.ToString("yyyy-MM-ddTHH:mm:ssZ");
string signature = ComputeSignature(accessKeySecret, "POST\napplication/json\nUTF-8\n" + timestamp + "\n"+ "/face/attribute\n" + body);
var request = new HttpRequestMessage(HttpMethod.Post, url);
request.Headers.Add("Authorization", "Dataplus " + accessKeyId + ":" + signature);
request.Headers.Add("accept", "application/json");
request.Headers.Add("x-acs-region-id", "cn-shanghai");
request.Headers.Add("date", timestamp);
var content = new StringContent(body, System.Text.Encoding.UTF8, "application/json");
request.Content = content;
var client = new HttpClient();
var response = await client.SendAsync(request);
if (response.StatusCode == HttpStatusCode.OK)
{
var responseContent = await response.Content.ReadAsStringAsync();
JObject jo = JObject.Parse(responseContent);
var facesExpression = jo["faceList"][0]["attribute"]["emotion"];
var anger = facesExpression["anger"].Value<float>();
var disgust = facesExpression["disgust"].Value<float>();
var fear = facesExpression["fear"].Value<float>();
var happiness = facesExpression["happiness"].Value<float>();
var sadness = facesExpression["sadness"].Value<float>();
var surprise = facesExpression["surprise"].Value<float>();
}
}
static string ComputeSignature(string accessKeySecret, string stringToSign)
{
var encoding = new System.Text.UTF8Encoding();
var key = encoding.GetBytes(accessKeySecret);
var hmacsha1 = new System.Security.Cryptography.HMACSHA1(key);
var hashBytes = hmacsha1.ComputeHash(encoding.GetBytes(stringToSign));
return Convert.ToBase64String(hashBytes);
}
}
}
该示例代码使用HttpClient发起了一个POST请求,将图片数据进行Base64编码后传给人脸属性分析接口(属性type为1代表分析表情),然后解析了json返回值中的表情信息。您需要自行替换您的AccessKey Id和AccessKey Secret,并修改图片路径和请求URL等信息。
这里:https://next.api.aliyun.com/api/facebody/2019-12-30/RecognizeExpression?spm=a2c4g.155004.0.i0&tab=DEMO&lang=CSHARP,此回答整理自钉群“阿里云视觉智能开放平台咨询1群”
版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。