【问题标题】:Microsoft-Cognitive Face API Enpoint IssueMicrosoft 认知人脸 API 端点问题
【发布时间】:2020-02-26 06:24:46
【问题描述】:

我创建了一个认知服务人脸 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;
            }
        }
    }
}

【问题讨论】:

  • “它没有给出地址中的位置”是什么意思。 ?您能否添加更多实施细节?
  • 我被告知它应该是“northcentralus.cognitiveservices.azure.com/face。或类似的东西。
  • 当我在 Visual Studio 中使用它时,它会抛出一个似乎与端点有关的错误。
  • 添加您的实施细节,以便我们提供帮助。有两种类型的端点:一种使用区域名称,另一种使用您提到的子域。两者都有效,您只需要根据您的实施方式传递正确的信息
  • @NicolasR 我可以附在某处吗?\

标签: c# microsoft-cognitive azure-cognitive-services face-api


【解决方案1】:

您正在尝试将您的请求发布到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

【讨论】:

    猜你喜欢
    • 2017-09-26
    • 2018-11-27
    • 2018-12-09
    • 2017-12-29
    • 2018-06-19
    • 1970-01-01
    • 2016-10-06
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多