首页 > 解决方案 > 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;
            }
        }
    }
}

标签: c#microsoft-cognitiveazure-cognitive-servicesface-api

解决方案


在这里,您尝试将您的请求发布uriuriBase + 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


推荐阅读