Exemplo n.º 1
0
        public override async Task <GetVisualFeaturesStringResponse> GetVisualFeatures(CognitiveServicesRequest request)
        {
            var cognitiveServicesRestHttpClient = new CognitiveServicesRestHttpClient();
            var restHttpResponse = await cognitiveServicesRestHttpClient.GetRestHttpStringResponse(request);

            var getVisualFeaturesResponse = new GetVisualFeaturesStringResponse
            {
                Success             = restHttpResponse.Success,
                ImplementedInModule = restHttpResponse.ImplementedInModule,
                FailureInformation  = restHttpResponse.FailureInformation,
                VisualFeatures      =
                    CommonFramework.Library.Helpers.JsonHelpers.JsonPrettyPrint(restHttpResponse.RestResponse),
            };

            return(getVisualFeaturesResponse);
        }
        public override async Task <GetVisualFeaturesStringResponse> GetVisualFeatures(CognitiveServicesRequest request)
        {
            var response = new GetVisualFeaturesStringResponse();

            try
            {
                var apiKeyServiceClientCredentials = new ApiKeyServiceClientCredentials(request.EndPointKey1);
                var delegatingHandlers             = new System.Net.Http.DelegatingHandler[] { };

                IComputerVisionClient computerVision = new ComputerVisionClient(
                    credentials: apiKeyServiceClientCredentials,
                    handlers: delegatingHandlers)
                {
                    Endpoint = request.EndPoint
                };

                IList <VisualFeatureTypes> features = new List <VisualFeatureTypes>()
                {
                    VisualFeatureTypes.ImageType,
                    VisualFeatureTypes.Faces,
                    VisualFeatureTypes.Adult,
                    VisualFeatureTypes.Categories,
                    VisualFeatureTypes.Color,
                    VisualFeatureTypes.Tags,
                    VisualFeatureTypes.Description,
                    VisualFeatureTypes.Objects,
                };

                var imageAnalysis = await computerVision.AnalyzeImageInStreamAsync(
                    image : request.ImageStream,
                    visualFeatures : features);

                var visualFeaturesTemp = new StringBuilder();

                visualFeaturesTemp.AppendLine("Description - Captions");
                foreach (var item in imageAnalysis.Description.Captions)
                {
                    visualFeaturesTemp.AppendLine($"{item.Text} ({item.Confidence})");
                }

                visualFeaturesTemp.AppendLine("");
                visualFeaturesTemp.AppendLine("Description - Tags");
                visualFeaturesTemp.AppendLine(string.Join(", ", imageAnalysis.Description.Tags));

                visualFeaturesTemp.AppendLine("");
                visualFeaturesTemp.AppendLine("Adult / Racy Content");
                visualFeaturesTemp.AppendLine($"({imageAnalysis.Adult.AdultScore}) , ({imageAnalysis.Adult.RacyScore})");

                visualFeaturesTemp.AppendLine("");
                visualFeaturesTemp.AppendLine("Categories - name, score, detail");
                foreach (var item in imageAnalysis.Categories)
                {
                    visualFeaturesTemp.AppendLine($"{item.Name} ({item.Score})");
                    visualFeaturesTemp.AppendLine($"{item.Detail}");
                }

                visualFeaturesTemp.AppendLine("");
                visualFeaturesTemp.AppendLine("Color");
                visualFeaturesTemp.AppendLine($"{imageAnalysis.Color}");

                visualFeaturesTemp.AppendLine("");
                visualFeaturesTemp.AppendLine("Faces - Age, Gender");
                foreach (var item in imageAnalysis.Faces)
                {
                    visualFeaturesTemp.AppendLine($"{item.Age} {item.Gender}");
                }

                visualFeaturesTemp.AppendLine("");
                visualFeaturesTemp.AppendLine("Image Type - ClipArtType, LineDrawingType");
                visualFeaturesTemp.AppendLine($"{imageAnalysis.ImageType.ClipArtType} {imageAnalysis.ImageType.LineDrawingType}");

                visualFeaturesTemp.AppendLine("");
                visualFeaturesTemp.AppendLine("Objects");
                foreach (var item in imageAnalysis.Objects)
                {
                    visualFeaturesTemp.AppendLine($"{item.ObjectProperty} ({item.Confidence})");
                }

                visualFeaturesTemp.AppendLine("");
                visualFeaturesTemp.AppendLine("Tags - Name, Confidence, Hint");
                foreach (var item in imageAnalysis.Tags)
                {
                    visualFeaturesTemp.AppendLine($"{item.Name} ({item.Confidence})");
                    visualFeaturesTemp.AppendLine($"{item.Hint}");
                }


                response.VisualFeatures = visualFeaturesTemp.ToString();
                response.Success        = true;
            }
            catch (Exception e)
            {
                response.FailureInformation = e.Message;
            }

            return(response);
        }