Example #1
0
        public Faces DetectFaces(byte[] imageData = null, string imageDataName = null, string imageDataMimeType = null, string[] urls = null)
        {
            Faces result = null;

            if (imageData == null && (urls == null || urls.Length < 1))
            {
                throw new ArgumentNullException(string.Format("{0} or {1} are required for 'DetectFaces()'", nameof(imageData), "'urls'"));
            }

            if (imageData != null)
            {
                if (string.IsNullOrEmpty(imageDataName) || string.IsNullOrEmpty(imageDataMimeType))
                {
                    throw new ArgumentNullException(string.Format("{0} and {1} are required for 'DetectFaces()'", nameof(imageDataName), nameof(imageDataMimeType)));
                }
            }

            try
            {
                string parameters = null;

                if (urls != null && urls.Length > 0)
                {
                    DetectFacesParameters parametersObject = new DetectFacesParameters();
                    parametersObject.URLs = urls != null ? urls : new string[0];

                    parameters = JsonConvert.SerializeObject(parametersObject);
                }


                var formData = new MultipartFormDataContent();

                if (imageData != null)
                {
                    var imageContent = new ByteArrayContent(imageData);
                    imageContent.Headers.ContentType = MediaTypeHeaderValue.Parse(imageDataMimeType);
                    formData.Add(imageContent, imageDataName, imageDataName);
                }

                if (!string.IsNullOrEmpty(parameters))
                {
                    var parametersContent = new StringContent(parameters, Encoding.UTF8, HttpMediaType.TEXT_PLAIN);
                    parametersContent.Headers.ContentType = MediaTypeHeaderValue.Parse("application/json");
                    formData.Add(parametersContent);
                }

                result = this.Client.PostAsync($"{ this.Endpoint}{PATH_DETECT_FACES}")
                         .WithArgument("version", VERSION_DATE_2016_05_20)
                         .WithArgument("api_key", ApiKey)
                         .WithBodyContent(formData)
                         .As <Faces>()
                         .Result;
            }
            catch (AggregateException ae)
            {
                throw ae.Flatten();
            }

            return(result);
        }
Example #2
0
        public virtual Faces DetectFaces(byte[] imageData = null, string imageDataName = null, string imageDataMimeType = null, string[] urls = null)
        {
            if (imageData == null && (urls == null || urls.Length < 1))
            {
                throw new ArgumentNullException($"{nameof(imageData)} or 'urls' are required for 'DetectFaces()'");
            }

            if (imageData != null && (string.IsNullOrEmpty(imageDataName) || string.IsNullOrEmpty(imageDataMimeType)))
            {
                throw new ArgumentNullException($"{nameof(imageDataName)} and {nameof(imageDataMimeType)} are required for 'DetectFaces()'");
            }

            var formData = new MultipartFormDataContent();

            if (urls != null && urls.Length > 0)
            {
                DetectFacesParameters parametersObject = new DetectFacesParameters
                {
                    urls = urls
                };

                var parametersContent = new StringContent(JsonConvert.SerializeObject(parametersObject), Encoding.UTF8, HttpMediaType.TEXT_PLAIN);
                parametersContent.Headers.ContentType = MediaTypeHeaderValue.Parse("application/json");
                formData.Add(parametersContent);
            }

            if (imageData != null)
            {
                var imageContent = new ByteArrayContent(imageData);
                imageContent.Headers.ContentType = MediaTypeHeaderValue.Parse(imageDataMimeType);
                formData.Add(imageContent, imageDataName, imageDataName);
            }

            try
            {
                var result = RepositoryClient.PostAsync($"{ApiKeys.VisualRecognitionEndpoint}{detectFacesUrl}")
                             .WithArgument("version", versionDate)
                             .WithArgument("api_key", ApiKeys.VisualRecognition)
                             .WithBodyContent(formData)
                             .As <Faces>()
                             .Result;

                return(result);
            }
            catch (AggregateException ae)
            {
                throw ae.Flatten();
            }
        }