Microsoft-Cognitive Face API Enpoint Issue



我创建了一个认知服务faceapi,但我的端点似乎无法工作。它没有给出地址内的位置。这被列为我的端点: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;
}
}
}
}

您正在尝试将请求POST到uri,即uriBase + requestParameters;

所以它的价值是"https://kbob.cognitiveservices.azure.com/?returnFaceAttributes=emotion">

这里你缺少了服务url的一部分,它是名为"/face/v.0/{method}"的face根。在你的情况下,它看起来像是你想使用"detect"方法,所以你应该有https://kbob.cognitiveservices.azure.com/face/v1.0/detect?returnFaceAttributes=emotion">

添加"/face/v.1.0/detect",它应该可以工作

如果您不确定URL,请查看控制台:https://westeurope.dev.cognitive.microsoft.com/docs/services/563879b61984550e40cbbe8d/operations/563879b61984550f30395236/console

相关内容

  • 没有找到相关文章

最新更新