Microsoft-Cognitive Face API Enpoint Проблема - PullRequest
0 голосов
/ 24 февраля 2020

Я создал 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;
            }
        }
    }
}

1 Ответ

0 голосов
/ 26 февраля 2020

Здесь вы пытаетесь отправить свой запрос на uri, что составляет uriBase + requestParameters;

Таким образом, его значение равно "https://kbob.cognitiveservices.azure.com/?returnFaceAttributes=emotion"

Здесь вы находитесь отсутствует часть URL службы, которая является лицом root, называемым "/face/v1.0/ enjmethod}". В вашем случае похоже, что вы хотите использовать метод «обнаружения», поэтому вы должны иметь «https://kbob.cognitiveservices.azure.com/face/v1.0/detect?returnFaceAttributes=emotion»

Добавить «/face/v1.0/detect» и он должен работа

Если вы не уверены в URL, посмотрите на консоль: https://westeurope.dev.cognitive.microsoft.com/docs/services/563879b61984550e40cbbe8d/operations/563879b61984550f30395236/console

...