Web应用程序未返回Cognitive Face API返回结果

时间:2019-03-29 05:56:38

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

我正在使用Microsoft Cognitive Face API实施人脸检测以对人进行身份验证并登录到应用程序。

为此,我要创建一个PersonGroup并添加Person,然后添加Face。训练人员组等。

我想在MVC Web应用程序中实现它。我已经在MVC Web应用程序中编写了相同的代码。但是API不会返回任何内容,只是挂在Http请求调用上。

那么我们是否需要为Web应用程序获取一些不同的API密钥,还是需要对Web.Config文件进行任何更改才能成功调用?

我在控制台应用程序和WPF应用程序中尝试了相同的代码。那里一切正常。

public class HttpClientHelper
    {
        HttpClient httpClient = new HttpClient();

        private const string subscriptionKey = "XXXXXXXXXXXXXXXXXXXXXXXXXX";

        private const string faceEndpoint = "https://southeastasia.api.cognitive.microsoft.com/face/v1.0/";

        public HttpClientHelper()
        {
            httpClient.DefaultRequestHeaders.Add("Ocp-Apim-Subscription-Key", subscriptionKey);
            httpClient.BaseAddress = new Uri(faceEndpoint);
        }

        public async Task<T> GetAsync<T>(string url)
        {
            var response = await httpClient.GetAsync(url);
            string contentString = await response.Content.ReadAsStringAsync();
            return JsonConvert.DeserializeObject<T>(contentString);
        }
    }


public static async Task CreatePersonGroup(UserGroup userGroup)
        {
            bool isGroupExists = false;
            PersonGroup personGroup;

            try
            {
                HttpClientHelper httpClientHelper = new HttpClientHelper();
                var response = httpClientHelper.GetAsync<PersonGroup>(string.Format("persongroups/{0}", userGroup.UserGroupId)).Result;

               isGroupExists = true;
            }
            catch (APIErrorException ex)
            {
                if (ex.Body.Error.Code == "PersonGroupNotFound")
                    isGroupExists = false;
            }

            if (isGroupExists == false)
            {
                await faceClient.PersonGroup.CreateAsync(userGroup.UserGroupId, userGroup.Name);
            }
        }

我希望相同的代码在Web应用程序中也必须能正常工作。因为其中没有很大的逻辑。只是一个简单的API调用。

1 个答案:

答案 0 :(得分:0)

private const string faceEndpoint = 
"https://southeastasia.api.cognitive.microsoft.com/";

希望它应该能工作