Пример #1
0
        /// <summary>
        /// Analyzes an image of a dog. Stores the image in blob storage, processes against the AI,
        /// and stores the result data before returning
        /// </summary>
        /// <param name="fileName">Name of the uploaded file</param>
        /// <param name="imageDate">The actual image</param>
        /// <returns>The result of the request with a submitted dog object</returns>
        public async Task <Result <SubmittedDog> > AnalyzeDogImageAsync(string fileName, byte[] imageDate)
        {
            try
            {
                // validate input
                if (string.IsNullOrEmpty(fileName) || imageDate == null)
                {
                    return(new InvalidResult <SubmittedDog>("Invalid image uploaded."));
                }

                // format filename
                var rgx = new Regex("[^a-zA-Z0-9-. -]");
                fileName = rgx.Replace(fileName, "");
                fileName = Guid.NewGuid().ToString() + fileName; // add guid to make unique

                // submit the image data to blob storage
                var imageUrl = await _blobStorageProvider.UploadImageAsync(fileName, imageDate);

                if (string.IsNullOrEmpty(imageUrl))
                {
                    return(new InvalidResult <SubmittedDog>("Error uploading image file"));
                }

                // submit image to breed detection
                var detectedBreedResult = await _breedDetectionProvider.SubmitDogAsync(new ProcessDogRequest
                {
                    IsTest   = false,
                    FaceName = imageUrl,
                    FaceUrl  = imageUrl,
                    Version  = "001"
                });

                // add submitted info to repository
                var entity = new SubmittedDog
                {
                    BreedName    = detectedBreedResult.BreedName,
                    BreedDetails = detectedBreedResult.Keywords,
                    ImageUrl     = imageUrl,
                    CreatedDate  = DateTime.UtcNow,
                    ModifiedDate = DateTime.UtcNow
                };
                var finalEntity = await _submittedDogRepository.AddAsync(entity);

                // return result
                return(new SuccessResult <SubmittedDog>(finalEntity));
            }
            catch (Exception ex)
            {
                Console.WriteLine("Error analyzing dog image:");
                Console.WriteLine(ex);
                return(new UnexpectedResult <SubmittedDog>());
            }
        }