«Плохой запрос» к Face ++ API FaceDetection - PullRequest
0 голосов
/ 24 сентября 2018

Я создаю приложение с распознаванием лиц и не могу вызвать Face ++ API распознавания лиц.Что может быть не так в моем запросе?Требуется ключ API, секретный ключ API и файл изображения в виде байтового массива или в виде строки Base64.Ориентиры и атрибуты являются необязательными.Я отправляю растровое изображение в функцию AnalyzeFace , она работает хорошо, пока client.PostAsync (url, content); .В ответ возвращается

Status code: 400, ReasonPhrase: 'Bad Request'

Мой код

using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;

namespace WindowsForms
{
    class FaceRecognition
    {
        private const string apiKey = "MY_API_KEY";
        private const string apiSecret = "MY_PRIVATE_KEY";
        private const string attributes = "gender,age";
        private const string landmark = "1";

        private const int CONNECT_TIME_OUT = 30000;

        private static readonly HttpClient client = new HttpClient();
        public static string AnalyzeFace(Bitmap bitmap)
        {
            byte[] image = ImageToByte(bitmap);
            string url = "https://api-us.faceplusplus.com/facepp/v3/detect";
            Dictionary<String,String> dictionary = new Dictionary<string, string>();

            dictionary.Add("api_key", apiKey);
            dictionary.Add("api_secret", apiSecret);
            dictionary.Add("return_landmark", landmark);
            dictionary.Add("image_file",Convert.ToBase64String(image));
            dictionary.Add("return_attributes", attributes);

            try
            {
                CallApi(url,dictionary);
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
                throw;
            }

            return null;
        }

        private static async void CallApi(string url, Dictionary<string, string> dictionary)
        {
            var content = new FormUrlEncodedContent(dictionary);

            var response = await client.PostAsync(url, content);

            var responseString = await response.Content.ReadAsStringAsync();
        }

        private static byte[] ImageToByte(Bitmap img)
        {
            ImageConverter converter = new ImageConverter();
            return (byte[])converter.ConvertTo(img, typeof(byte[]));
        }
    }
}

1 Ответ

0 голосов
/ 24 сентября 2018
Заголовок

"image_file" используется для изображений с форматом файла.Должно быть изменено на "image_base64"

...