Exemplo n.º 1
0
        private async Task <ImageInsights> AnalyzeImage(string imageLocalPath)
        {
            var fileName = Path.GetFileName(imageLocalPath);
            List <VisualFeature> features = new List <VisualFeature>()
            {
                VisualFeature.Categories, VisualFeature.Description, VisualFeature.Tags
            };

            try
            {
                VisionServiceHelper.ApiKey   = ConfigurationManager.AppSettings["ComputerVisionKey"];
                VisionServiceHelper.EndPoint = ConfigurationManager.AppSettings["ComputerVisionEndPoint"];

                var resized = ImageResizer.ResizeIfRequired(imageLocalPath, 750);
                Func <Task <Stream> > imageCB = async() => System.IO.File.OpenRead(resized.Item2);
                var imageAnalysisResult       = await VisionServiceHelper.AnalyzeImageAsync(imageCB, features);

                ImageInsights insights = new ImageInsights
                {
                    ImageId    = fileName,
                    Caption    = imageAnalysisResult.Description.Captions[0].Text,
                    Tags       = imageAnalysisResult.Tags.Select(t => t.Name).ToArray(),
                    Categories = imageAnalysisResult.Categories.Where(t => t.Score > 0.60).Select(t => t.Name).ToArray()
                };
                return(insights);
            }
            catch (Exception ex)
            {
                return(null);
            }
        }
Exemplo n.º 2
0
        private async Task <ImageMetadata> SaveData(AnalysisResultViewModel data)
        {
            var fileName = Path.GetFileName(data.ImageLocalPath);
            Func <Task <Stream> > imageCB;

            try
            {
                if (blobStorage == null || cosmosDb == null)
                {
                    BlobStorageHelper.ContainerName    = ConfigurationManager.AppSettings["ContainerName"];
                    BlobStorageHelper.ConnectionString = ConfigurationManager.AppSettings["StorageConnectionString"];
                    blobStorage = await BlobStorageHelper.BuildAsync();

                    CosmosDBHelper.AccessKey      = ConfigurationManager.AppSettings["CosmosDBKey"];
                    CosmosDBHelper.EndpointUri    = ConfigurationManager.AppSettings["CosmosDBEndpointURI"];
                    CosmosDBHelper.DatabaseName   = ConfigurationManager.AppSettings["DatabaseName"];
                    CosmosDBHelper.CollectionName = ConfigurationManager.AppSettings["CollectionName"];
                    cosmosDb = await CosmosDBHelper.BuildAsync();
                }
                var existing = await cosmosDb.FindDocumentByIdAsync <ImageMetadata>(fileName);

                var resized = ImageResizer.ResizeIfRequired(Server.MapPath(data.ImageLocalPath), 750);
                imageCB = async() => System.IO.File.OpenRead(resized.Item2);
                var imageBlob = await blobStorage.UploadImageAsync(imageCB, fileName);

                var metadata = new ImageMetadata(data.ImageLocalPath);
                metadata.AddInsights(data.Insights);
                metadata.BlobUri  = imageBlob.Uri;
                metadata.TaggedBy = User.Identity.Name;
                if (existing == null)
                {
                    metadata = (await cosmosDb.CreateDocumentIfNotExistsAsync(metadata, metadata.Id)).Item2;
                }
                else
                {
                    metadata = await cosmosDb.UpdateDocumentAsync(metadata, metadata.Id);
                }

                return(metadata);
            }
            catch (Exception ex)
            {
                return(null);
            }
        }