コード例 #1
0
        public void AnalyzeBrandsTest()
        {
            using (MockContext context = MockContext.Start(this.GetType()))
            {
                HttpMockServer.Initialize(this.GetType(), "AnalyzeBrandsTest");

                using (IComputerVisionClient client = GetComputerVisionClient(HttpMockServer.CreateInstance()))
                    using (FileStream stream = new FileStream(GetTestImagePath("microsoft.jpg"), FileMode.Open))
                    {
                        ImageAnalysis result = client.AnalyzeImageInStreamAsync(
                            stream,
                            new List <VisualFeatureTypes?>()
                        {
                            VisualFeatureTypes.Brands
                        })
                                               .Result;

                        Assert.Matches("^\\d{4}-\\d{2}-\\d{2}(-preview)?$", result.ModelVersion);
                        Assert.Equal("Microsoft", result.Brands[0].Name);
                        Assert.True(result.Brands[0].Confidence > 0.7);
                        Assert.True(result.Brands[0].Rectangle.X >= 0);
                        Assert.True(result.Brands[0].Rectangle.W >= 0);
                        Assert.True(result.Brands[0].Rectangle.X + result.Brands[0].Rectangle.W <= result.Metadata.Width);
                        Assert.True(result.Brands[0].Rectangle.Y >= 0);
                        Assert.True(result.Brands[0].Rectangle.H >= 0);
                        Assert.True(result.Brands[0].Rectangle.Y + result.Brands[0].Rectangle.H <= result.Metadata.Height);
                    }
            }
        }
コード例 #2
0
        private async Task <ImageAnalyzeResult> GetAnalyzeResult(IFormFile file)
        {
            var analyzeImageResult = await _computerVisionClient.AnalyzeImageInStreamAsync(file.OpenReadStream(), VisualFeatures);

            var caption = GetCaption(analyzeImageResult);

            var faces = analyzeImageResult.Faces.ToList();
            var face  = faces.FirstOrDefault();
            var year  = GetYearFromFace(face);

            var tracks = await GetTopRatedTracks(year);

            var randomTracks = tracks.Take(5).OrderBy(a => Guid.NewGuid()).ToList();

            var viewModel = new ImageAnalyzeResult
            {
                ImageDescription = MakeSentence(caption),
                Faces            = faces,

                MusicYear   = year,
                MusicTracks = randomTracks.Select(FullTrackToMusicTrack).Take(5).ToList()
            };

            return(viewModel);
        }
コード例 #3
0
        public async Task <SearchResult> SearchAsync(Stream image)
        {
            var imageAnalysisResult = await visionClient.AnalyzeImageInStreamAsync(image, visualFeatures, visionDetails);

            var description = ExtractDescription(imageAnalysisResult);

            if (!string.IsNullOrWhiteSpace(description))
            {
                var searchResult = await searchClient.Images.SearchAsync(description);

                var result = new SearchResult(description, searchResult.Value);
                return(result);
            }

            return(null);
        }
コード例 #4
0
        // This method is adapted from the AnalyzeLocalAsync() method from
        // the following tutorial:
        // https://docs.microsoft.com/en-us/azure/cognitive-services/Computer-vision/quickstarts-sdk/csharp-analyze-sdk
        private async Task AnalyzeImageAsync()
        {
            // If computerVision is not initialized, display an error message
            // prompting the user to enter the relevant credentials (i.e. API key
            // and/or Endpoint).
            if (computerVision == null)
            {
                lblError.Content    = "Computer Vision API Client not initialized. Please enter your API key and/or Endpoint if you haven't already done so.";
                lblError.Visibility = Visibility.Visible;
                return;
            }

            // If the user has not uploaded an image, display an error message
            // prompting the user to do so.
            if (String.IsNullOrEmpty(filePath))
            {
                lblError.Content    = "Please upload an image.";
                lblError.Visibility = Visibility.Visible;
                return;
            }

            lblError.Visibility            = Visibility.Hidden;
            imageDescriptionStatusBar.Text = "Analyzing...";

            using (Stream imageStream = File.OpenRead(filePath))
            {
                // Call the Computer Vision API to perform Image Analysis.
                //
                // The returned object (of ImageAnalysis type) contains the
                // information returned from the Image Analysis operation.
                ImageAnalysis analysis = await computerVision.AnalyzeImageInStreamAsync(
                    imageStream, features);

                imageDescriptionStatusBar.Text = "";

                // Each set of information contained in the ImageAnalysis object
                // is processed and displayed as text.
                DisplayImageTypeInfo(analysis);
                DisplayColorInfo(analysis);
                DisplayCategoryInfo(analysis);
                DisplayDescriptionInfo(analysis);
                DisplayTagInfo(analysis);
                DetectImagesInFace(analysis);
            }
        }
コード例 #5
0
        public void AnalyzeImageInStreamTest()
        {
            using (MockContext context = MockContext.Start(this.GetType()))
            {
                HttpMockServer.Initialize(this.GetType(), "AnalyzeImageInStreamTest");

                using (IComputerVisionClient client = GetComputerVisionClient(HttpMockServer.CreateInstance()))
                    using (FileStream stream = new FileStream(GetTestImagePath("house.jpg"), FileMode.Open))
                    {
                        ImageAnalysis result = client.AnalyzeImageInStreamAsync(
                            stream,
                            new List <VisualFeatureTypes?>()
                        {
                            VisualFeatureTypes.Adult,
                            VisualFeatureTypes.Categories,
                            VisualFeatureTypes.Color,
                            VisualFeatureTypes.Faces,
                            VisualFeatureTypes.ImageType,
                            VisualFeatureTypes.Tags
                        })
                                               .Result;

                        Assert.Matches("^\\d{4}-\\d{2}-\\d{2}(-preview)?$", result.ModelVersion);
                        Assert.Equal("grass", result.Tags[0].Name);
                        Assert.True(result.Tags[0].Confidence > 0.9);
                        Assert.Equal("Jpeg", result.Metadata.Format);
                        Assert.False(result.Adult.IsAdultContent);
                        Assert.False(result.Adult.IsRacyContent);
                        Assert.False(result.Adult.IsGoryContent);
                        Assert.True(result.Adult.AdultScore < 0.1);
                        Assert.True(result.Adult.RacyScore < 0.1);
                        Assert.True(result.Adult.GoreScore < 0.1);
                        Assert.Equal("building_", result.Categories[0].Name);
                        Assert.True(result.Categories[0].Score > 0.5);
                        Assert.Equal("Green", result.Color.DominantColorBackground);
                        Assert.Equal("Green", result.Color.DominantColorForeground);
                    }
            }
        }
コード例 #6
0
        private async Task <ImageAnalysis> DownloadAndAnalyzePhoto(Uri uri)
        {
            var localFileName = Path.GetTempFileName();

            using (var wc = new WebClient())
            {
                wc.DownloadFile(uri, localFileName);
            }

            var fileInfo = new FileInfo(localFileName);

            if (fileInfo.Length > 4000000) // we need to resize too big photos
            {
                using (var image = new Bitmap(localFileName))
                {
                    var resized = new Bitmap(image.Width / 2, image.Height / 2);
                    using (var graphics = Graphics.FromImage(resized))
                    {
                        graphics.CompositingQuality = CompositingQuality.HighSpeed;
                        graphics.InterpolationMode  = InterpolationMode.HighQualityBicubic;
                        graphics.CompositingMode    = CompositingMode.SourceCopy;
                        graphics.DrawImage(image, 0, 0, image.Width / 2, image.Height / 2);
                        image.Dispose();
                        resized.Save(localFileName, ImageFormat.Jpeg);
                    }
                }
            }

            using (var imageStream = File.OpenRead(localFileName))
            {
                var analysis = await _computerVisionClient.AnalyzeImageInStreamAsync(
                    imageStream, AnalysisFeatures);

                File.Delete(localFileName);
                return(analysis);
            }
        }
コード例 #7
0
        public void AnalyzeImageModelVersionTest()
        {
            using (MockContext context = MockContext.Start(this.GetType()))
            {
                HttpMockServer.Initialize(this.GetType(), "AnalyzeImageModelVersionTest");

                using (IComputerVisionClient client = GetComputerVisionClient(HttpMockServer.CreateInstance()))
                    using (FileStream stream = new FileStream(GetTestImagePath("house.jpg"), FileMode.Open))
                    {
                        const string targetModelVersion = "2021-04-01";

                        ImageAnalysis result = client.AnalyzeImageInStreamAsync(
                            stream,
                            new List <VisualFeatureTypes?>()
                        {
                            VisualFeatureTypes.Categories
                        },
                            modelVersion: targetModelVersion)
                                               .Result;

                        Assert.Equal(targetModelVersion, result.ModelVersion);
                    }
            }
        }
コード例 #8
0
ファイル: AnalyzeEnricher.cs プロジェクト: makhin/PhotoBank
 public async Task Enrich(Photo photo, SourceDataDto source)
 {
     source.ImageAnalysis = await _client.AnalyzeImageInStreamAsync(new MemoryStream(photo.PreviewImage), _features);
 }
コード例 #9
0
        private async Task GetImageMetadata(Byte[] byteArray, string fileName, int assetId)
        {
            var MetadataDetails = await _visionClient.AnalyzeImageInStreamAsync(new MemoryStream(byteArray), features);

            var result = _metadataRepository.SaveMetadata(MetadataDetails.Description.Captions[0].Text, string.Join(", ", MetadataDetails.Description.Tags), assetId);
        }