Beispiel #1
0
        /// <summary>
        /// Process a local file
        /// </summary>
        /// <param name="localImagePath">Local file path</param>
        /// <returns></returns>
        public async Task <FaceServiceResult> ProcessImageAtLocal(string localImagePath)
        {
            FaceServiceResult result = new FaceServiceResult();

            if (string.IsNullOrWhiteSpace(localImagePath) || !File.Exists(localImagePath))
            {
                return(result);
            }

            byte[] imageBytes = GetImageAsByteArray(localImagePath);

            if (imageBytes == null || imageBytes.Length == 0)
            {
                return(result);
            }

            try
            {
                result = await ProcessImageAsByteArray(imageBytes);
            }
            catch (Exception ex)
            {
                // TODO log exception

                result.Succeeded = false;
            }

            return(result);
        }
Beispiel #2
0
        /// <summary>
        /// Process a file that's at a public URI
        /// </summary>
        /// <param name="imageUrl"></param>
        /// <returns></returns>
        public async Task <FaceServiceResult> ProcessImageAtUrl(string imageUrl)
        {
            FaceServiceResult result = new FaceServiceResult();

            if (string.IsNullOrWhiteSpace(imageUrl))
            {
                return(result);
            }

            try
            {
                HttpContent content = GetImageUriContent(imageUrl);

                if (content != null)
                {
                    HttpResponseMessage response = await this.HttpClient.PostAsync(string.Empty, content);

                    result = await GetFaceResult(response);
                }
            }
            catch (Exception ex)
            {
                // TODO log exception

                result.Succeeded = false;
            }


            return(result);
        }
Beispiel #3
0
        internal async Task <FaceServiceResult> GetFaceResult(HttpResponseMessage response)
        {
            FaceServiceResult result = new FaceServiceResult();

            if (response == null)
            {
                return(result);
            }

            result.Succeeded      = response.IsSuccessStatusCode;
            result.ServiceMessage = response.ReasonPhrase;

            try
            {
                if (response.IsSuccessStatusCode)
                {
                    string responseContent = await response.Content.ReadAsStringAsync();

                    result.Faces = JsonConvert.DeserializeObject <List <FaceResult> >(responseContent);
                }
            }
            catch (Exception ex)
            {
                // TODO log exception

                result.Succeeded = false;
            }

            return(result);
        }
Beispiel #4
0
        /// <summary>
        /// Process an image byte array
        /// </summary>
        /// <param name="imageBytes">Local file path</param>
        /// <returns></returns>
        internal async Task <FaceServiceResult> ProcessImageAsByteArray(byte[] imageBytes)
        {
            FaceServiceResult result = new FaceServiceResult();

            if (imageBytes == null || imageBytes.Length == 0)
            {
                return(result);
            }

            try
            {
                using (var content = GetImagePayload(imageBytes))
                {
                    HttpResponseMessage response = await this.HttpClient.PostAsync(string.Empty, content);

                    result = await GetFaceResult(response);
                }
            }
            catch (Exception ex)
            {
                // TODO log exception

                result.Succeeded = false;
            }

            return(result);
        }