c# - Microsoft-Cognitive Face API 端点问题
问题描述
我创建了一个认知服务人脸 API,但我的端点似乎无法正常工作。它没有给出地址内的位置。这被列为我的端点:https ://kbob.cognitiveservices.azure.com/
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Runtime.Serialization.Json;
using System.Text;
using System.Threading.Tasks;
using System.Xml;
using System.Xml.Linq;
using System.Xml.XPath;
namespace emobe
{
public class CognoF
{
const string subscriptionKey = "xxxx_VALUE_HIDDEN_xxxx";
const string uriBase = "https://kbob.cognitiveservices.azure.com/";
async public Task<emotions> MakeAnalysisRequest(byte[] imageData)
{
HttpClient client = new HttpClient();
// Request headers.
client.DefaultRequestHeaders.Add("Ocp-Apim-Subscription-Key", subscriptionKey);
// Request parameters. A third optional parameter is "details".
string requestParameters = "?returnFaceAttributes=emotion";
// Assemble the URI for the REST API Call.
string uri = uriBase + requestParameters;
HttpResponseMessage response;
// Request body. Posts a locally stored JPEG image.
//byte[] byteData = GetImageAsByteArray(imageFilePath);
using (ByteArrayContent content = new ByteArrayContent(imageData))
{
// This example uses content type "application/octet-stream".
// The other content types you can use are "application/json" and "multipart/form-data".
content.Headers.ContentType = new MediaTypeHeaderValue("application/octet-stream");
// Execute the REST API call.
response = await client.PostAsync(uri, content);
// Get the JSON response.
string contentString = await response.Content.ReadAsStringAsync();
// Display the JSON response.
Console.WriteLine("\nResponse:\n");
//Console.WriteLine(JsonPrettyPrint(contentString));
// For that you will need to add reference to System.Runtime.Serialization
var jsonReader = JsonReaderWriterFactory.CreateJsonReader(Encoding.UTF8.GetBytes(contentString), new System.Xml.XmlDictionaryReaderQuotas());
// For that you will need to add reference to System.Xml and System.Xml.Linq
var root = XElement.Load(jsonReader);
Console.WriteLine(root.XPathSelectElement("//faceAttributes").Value);
Console.WriteLine(root.XPathSelectElement("//faceAttributes/emotion").Value);
Console.WriteLine(root.XPathSelectElement("//faceAttributes/emotion/anger").Value);
emotions emos = new emotions();
emos.anger = decimal.Parse(root.XPathSelectElement("//faceAttributes/emotion/anger").Value);
emos.contempt = decimal.Parse(root.XPathSelectElement("//faceAttributes/emotion/contempt").Value);
emos.disgust = decimal.Parse(root.XPathSelectElement("//faceAttributes/emotion/disgust").Value);
emos.fear = decimal.Parse(root.XPathSelectElement("//faceAttributes/emotion/fear").Value);
emos.happiness = decimal.Parse(root.XPathSelectElement("//faceAttributes/emotion/happiness").Value);
emos.neutral = decimal.Parse(root.XPathSelectElement("//faceAttributes/emotion/neutral").Value);
emos.sadness = decimal.Parse(root.XPathSelectElement("//faceAttributes/emotion/sadness").Value);
emos.surprise = decimal.Parse(root.XPathSelectElement("//faceAttributes/emotion/surprise").Value);
return emos;
}
}
}
}
解决方案
在这里,您尝试将您的请求发布uri
到uriBase + requestParameters;
所以它的值为“ https://kbob.cognitiveservices.azure.com/?returnFaceAttributes=emotion ”
在这里,您缺少服务 url 的一部分,即名为“/face/v1.0/{method}”的人脸根。在您的情况下,您似乎想使用“检测”方法,因此您应该具有“ https://kbob.cognitiveservices.azure.com/face/v1.0/detect?returnFaceAttributes=emotion ”
添加“/face/v1.0/detect”,它应该可以工作
如果您不确定网址,请查看控制台:https ://westeurope.dev.cognitive.microsoft.com/docs/services/563879b61984550e40cbbe8d/operations/563879b61984550f30395236/console
推荐阅读
- mongodb - 谁将在 MongoDB 中计算 new Date()
- android - 使用 CameraX 的代码“imageCapture.takePicture”时出现错误
- php - WC_get_products 元查询被忽略
- android - 尝试在空对象引用上调用虚拟方法“android.content.res.Resources android.app.Activity.getResources()”
- r - 如何使用 lag/lead 和 ifelse/case_when (或其他解决方案)处理 R 中的纵向症状数据?
- reactjs - React 组件中的内联 SVG 未显示
- ruby-on-rails - 如何用 Minimagick 打开 .jpg 文件?
- fortran - 我可以在 Fortran 中模仿多个传递对象的虚拟参数吗?
- sql - 排除出现在不同表中的值
- javascript - 使用 Emotion.sh 从样式化组件传递道具