Beispiel #1
0
        /// <summary>
        /// Analyzes the image to see if we have
        /// something we're interested in.
        /// </summary>
        /// <param name="endPoint">The endpoint of our CustomVision project</param>
        /// <param name="projectId">The <see cref="Guid"/>of the project</param>
        /// <param name="publishedModelName">The name of the published model</param>
        /// <param name="fileName">The raw image name we're analyzing</param>
        public void DetectImageCharacteristics(
            CustomVisionPredictionClient endPoint,
            Guid projectId,
            string publishedModelName,
            string fileName)
        {
            Console.WriteLine("Making a prediction:");

            // Generate the full path
            var imageFile = Path.Combine("/home/pi/images/", $"{fileName}.jpg");

            // Open the image stream for uploading
            using (FileStream stream = File.OpenRead(imageFile))
            {
                // Call the API with the image stream
                ImagePrediction result = endPoint.DetectImage(
                    projectId,
                    publishedModelName,
                    File.OpenRead(imageFile));

                // Loop over each prediction and write out the results
                foreach (PredictionModel c in result.Predictions)
                {
                    Console.WriteLine(
                        $"\t{c.TagName}: {c.Probability:P1} [ {c.BoundingBox.Left}, {c.BoundingBox.Top}, {c.BoundingBox.Width}, {c.BoundingBox.Height} ]");
                }
            }
        }
Beispiel #2
0
        private async Task <ImagePrediction> AnalyzeProductImageAsync(Guid modelId)
        {
            ImagePrediction result = null;

            try
            {
                var iteractions = await trainingApi.GetIterationsAsync(modelId);

                var latestTrainedIteraction = iteractions.Where(i => i.Status == "Completed").OrderByDescending(i => i.TrainedAt.Value).FirstOrDefault();

                if (latestTrainedIteraction == null)
                {
                    throw new Exception("This project doesn't have any trained models yet.");
                }

                if (CurrentInputProductImage?.ImageUrl != null)
                {
                    result = await CustomVisionServiceHelper.PredictImageUrlWithRetryAsync(predictionApi, modelId, new ImageUrl(CurrentInputProductImage.ImageUrl), latestTrainedIteraction.Id);
                }
                else if (CurrentInputProductImage?.GetImageStreamCallback != null)
                {
                    result = await CustomVisionServiceHelper.PredictImageWithRetryAsync(predictionApi, modelId, CurrentInputProductImage.GetImageStreamCallback, latestTrainedIteraction.Id);
                }
            }
            catch (Exception ex)
            {
                await Util.GenericApiCallExceptionHandler(ex, "Custom Vision error analyzing product image");
            }
            return(result);
        }
        public void PredictFolder(string folderPath)
        {
            LabelPrediction.Clear();
            try
            {
                var regexTest = new Func <string, bool>(i => Regex.IsMatch(i, @".jpg|.jpeg|.jpe|.jfif|.png|.bin$", RegexOptions.Compiled | RegexOptions.IgnoreCase));
                var files     = Directory.GetFiles(folderPath).Where(regexTest).ToList();
                MaxProgress = files.Count;
                foreach (string imagePath in files)
                {
                    PredictImage(imagePath);
                    Debug.WriteLine(ValueProgress + "/" + MaxProgress);// A ENLEVER
                }
            }
            catch (Exception e)
            {
                Console.WriteLine(e.StackTrace);
            }
            foreach (String str in ImagePrediction.Select(o => o.PredictedLabel).Distinct())
            {
                LabelPrediction.Add(str);
            }
            SavedList = new ObservableCollection <ImageNetDataProbability>(ImagePrediction);

            SaveResults();
        }
        public static async Task PredictionPipeline(Guid projectID, string modelName, string resourcesPath, string testImagesFolder, string mode = "url", List <string> imagesList = null)
        {
            Console.WriteLine($"Predicting results for image...");
            ImagePrediction result = null;

            if (mode == "url")
            {
                foreach (string url in imagesList)
                {
                    result = await PredictImageURL(projectID, modelName, url);
                }
            }
            else
            {
                try
                {
                    string path  = Path.GetFullPath(resourcesPath + testImagesFolder);
                    var    files = Directory.EnumerateFiles(path);
                    foreach (string file in files)
                    {
                        result = await PredictImageFile(projectID, modelName, file);
                    }
                }
                catch (Exception e)
                {
                    Console.WriteLine($"\n{e.GetType().Name}: {e.Message} \nCould not retrieve test images.");
                }
            }
        }
        public static async Task <ImagePrediction> PredictImageURL(Guid projectID, string modelName, string url)
        {
            ImageUrl imageUrl = new ImageUrl(url);

            ImagePrediction result = null;

            try
            {
                result = await endpoint.ClassifyImageUrlAsync(projectID, modelName, imageUrl);

                Console.WriteLine($"\nSuccessfully retrieved predictions for image '{url}'.");
            }
            catch (Exception e)
            {
                Console.WriteLine($"\n{e.GetType().Name}: {e.Message} \nCould not get prediction for image '{url}'.");
            }

            // Loop over each prediction and write out the results
            if (result != null)
            {
                foreach (var c in result.Predictions)
                {
                    Console.WriteLine($"\t{c.TagName}: {c.Probability:P1}");
                }
            }

            return(result);
        }
        public static async Task <ImagePrediction> PredictImageFile(Guid projectID, string modelName, string file)
        {
            var img = new MemoryStream(File.ReadAllBytes(file));

            ImagePrediction result = null;

            try
            {
                result = await endpoint.ClassifyImageAsync(projectID, modelName, img);

                Console.WriteLine($"\nSuccessfully retrieved predictions for image '{file}'.");
            }
            catch (Exception e)
            {
                Console.WriteLine($"\n{e.GetType().Name}: {e.Message} \nCould not get prediction for image '{file}'.");
            }

            // Loop over each prediction and write out the results
            if (result != null)
            {
                foreach (var c in result.Predictions)
                {
                    Console.WriteLine($"\t{c.TagName}: {c.Probability:P1}");
                }
            }

            return(result);
        }
        private async Task <ImagePrediction> CustomVisionAnalyzeImageByStreamAsync(Stream imageStream, Guid projectId, string publishedName, CustomVisionProjectType projectType)
        {
            ImagePrediction imagePrediction = null;

            switch (projectType)
            {
            case CustomVisionProjectType.Classification:
                imagePrediction = await _customVisionPredictionClient.ClassifyImageWithNoStoreAsync(
                    projectId : projectId,
                    publishedName : publishedName,
                    imageData : imageStream);

                break;

            case CustomVisionProjectType.Object_Detection:
                imagePrediction = await _customVisionPredictionClient.DetectImageWithNoStoreAsync(
                    projectId : projectId,
                    publishedName : publishedName,
                    imageData : imageStream);

                break;
            }

            return(imagePrediction);
        }
Beispiel #8
0
        public async Task OnPostAsync()
        {
            Message = "LOADING...";


            string assetsRelativePath = @"../../../assets";
            string assetsPath         = GetAbsolutePath(assetsRelativePath);


            var imagesFolder       = Path.Combine(assetsPath, "inputs", "images-for-predictions", "tmp-web-image");
            var imageClassifierZip = Path.Combine(assetsPath, "inputs", "MLNETModel", "imageClassifier.zip");

            imagesFolder += @"/" + ImageFile.FileName;

            try
            {
                using (var stream = System.IO.File.Create(imagesFolder))
                {
                    await ImageFile.CopyToAsync(stream);
                }


                var modelScorer = new ModelScorer(imagesFolder, imageClassifierZip);
                Predict = modelScorer.ClassifySingleImage();
            }
            catch (Exception ex)
            {
                Message = ex.Message;
            }

            Message = "...";
            //return RedirectToPage("./Index");
        }
Beispiel #9
0
 private void UpdateWithAnalysis(ImageAnalysis analysis, ImagePrediction analysisCV)
 {
     try
     {
         if (Dispatcher.HasThreadAccess)
         {
             captionsControl.UpdateEvent(new CognitiveEvent()
             {
                 ImageAnalysis = analysis, ImageAnalysisCV = analysisCV, ImageHeight = imageHeight, ImageWidth = imageWidth
             });
             tagsControl.UpdateEvent(new CognitiveEvent()
             {
                 ImageAnalysis = analysis, ImageAnalysisCV = analysisCV
             });
         }
         else
         {
             var task = Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, () => { captionsControl.UpdateEvent(new CognitiveEvent()
                 {
                     ImageAnalysis = analysis, ImageAnalysisCV = analysisCV, ImageHeight = imageHeight, ImageWidth = imageWidth
                 }); tagsControl.UpdateEvent(new CognitiveEvent()
                 {
                     ImageAnalysis = analysis, ImageAnalysisCV = analysisCV
                 }); });
         }
     }
     catch (Exception)
     {
         // Eat this exception
     }
 }
Beispiel #10
0
        static void EfetuarTesteImagem(string caminho, bool eURL)
        {
            string chaveAPI     = "0c91a890892d40a0ba9f9e22a5e358de";
            string chaveProjeto = "4a1a0670-7689-4d01-ac59-fc56227cc3ed";

            // Crio o client do Custom View Service.
            PredictionEndpoint endpoint = new PredictionEndpoint()
            {
                ApiKey = chaveAPI
            };
            ImagePrediction resultado = null;

            // Verifico se o projeto é uma URL ou um arquivo local para usar o método correto.
            if (eURL)
            {
                resultado = endpoint.PredictImageUrl(new Guid(chaveProjeto), new ImageUrl(caminho));
            }
            else
            {
                resultado = endpoint.PredictImage(new Guid(chaveProjeto), File.OpenRead(caminho));
            }

            // Percorro as analises.
            foreach (var possibilidade in resultado.Predictions)
            {
                Console.WriteLine($"Tag: {possibilidade.TagName}");
                Console.WriteLine($"Possibilidade: {possibilidade.Probability:P1}");
            }
        }
 public void SortResultsBy(String label)
 {
     ImagePrediction.Clear();
     foreach (ImageNetDataProbability image in SavedList)
     {
         if (image.PredictedLabel == label)
         {
             ImagePrediction.Add(image);
         }
     }
 }
        private async Task IdentifyImageAsync(MediaFile image)
        {
            if (Connectivity.NetworkAccess == NetworkAccess.None)
            {
                UserDialogs.Instance.Alert("Se necesita conexión a la red para realizar esta operación.", "Desconectado", "Aceptar");
                return;
            }
            ;



            //TODO: Apply permissions recommendations: https://github.com/jamesmontemagno/MediaPlugin#permission-recommendations
            //TODO: return null exception and pick it up for AppCenter and display Userdialog (https://github.com/aritchie/userdialogs/blob/master/docs/progress.md)

            if (image == null)
            {
                return;
            }

            using (UserDialogs.Instance.Loading("Analizando..."))
            {
                var fileStream = image.GetStream();

                App.SourceImage = ImageSource.FromStream(() =>
                {
                    return(image.GetStream());
                });



                ImagePrediction result = null;

                try
                {
                    result = await _predictionService.ClassifyImageAsync(App.CustomVisionProjectId, "default", fileStream);
                }
                catch (Exception ex)
                {
                    Crashes.TrackError(ex);
                    await UserDialogs.Instance.AlertAsync("Error analizando imagen. No se pudo conectar con el servicio.", "Error");

                    return;
                }

                try
                {
                    var vm = await CreateResultViewModelAsync(result);

                    await Navigation.PushAsync(new IdentificationResultsPage(vm));
                }
                catch { return; }
            }
        }
Beispiel #13
0
 private static void ValidateResults(ImagePrediction results)
 {
     Assert.Equal(ProjectId, results.Project);
     Assert.NotEqual(Guid.Empty, results.Iteration);
     Assert.Equal(2, results.Predictions.Count);
     Assert.Equal("Tag1", results.Predictions[0].TagName);
     Assert.InRange(results.Predictions[0].Probability, 0.9, 1);
     Assert.NotEqual(Guid.Empty, results.Predictions[0].TagId);
     Assert.Equal("Tag2", results.Predictions[1].TagName);
     Assert.InRange(results.Predictions[1].Probability, 0, 0.1);
     Assert.NotEqual(Guid.Empty, results.Predictions[1].TagId);
 }
 public void PredictImage(string imagePath)
 {
     try
     {
         var modelScorer = new TFModelScorer(tagsTsv, imagesFolder, inceptionPb, labelsTxt);
         Debug.WriteLine(imagePath);// A ENLEVER
         ImagePrediction.Add(modelScorer.ScoreImage(imagePath)[0]);
         ValueProgress += 1;
     }
     catch (Exception e)
     {
         Console.WriteLine(e.StackTrace);
     }
 }
        public void DetectImageNoStore()
        {
            using (MockContext context = MockContext.Start(this.GetType()))
            {
                HttpMockServer.Initialize(this.GetType(), "DetectImageNoStore", RecorderMode);

                ICustomVisionPredictionClient client = GetPredictionClientClient();
                using (FileStream stream = new FileStream(Path.Combine("TestImages", "test_image.jpg"), FileMode.Open))
                {
                    ImagePrediction results = client.DetectImageWithNoStore(ObjectDetectionProjectId, ObjDetectionPublishedName, stream);
                    ValidateObjDetectionResults(results);
                }
            }
        }
Beispiel #16
0
        public void PredictImage()
        {
            using (MockContext context = MockContext.Start(this.GetType().Name))
            {
                HttpMockServer.Initialize(this.GetType().Name, "PredictImage", RecorderMode);

                ICustomVisionPredictionClient client = GetPredictionClientClient();
                using (FileStream stream = new FileStream(Path.Combine("TestImages", "test_image.jpg"), FileMode.Open))
                {
                    ImagePrediction results = client.PredictImageAsync(ProjectId, stream).Result;
                    ValidateResults(results);
                }
            }
        }
 public void TestImages(string imagePath, string expectedLabel)
 {
     try
     {
         var TestCase = new ImageData();
         TestCase.ImagePath = imagePath;
         ImagePrediction prediction = model.predictor.Predict(TestCase);
         Console.WriteLine($"Test case {TestCase.ImagePath} predicted as {prediction.PredictedLabelValue.ToLower()} should be {expectedLabel.ToLower()}");
         Assert.That(prediction.PredictedLabelValue.ToLower(), Is.EqualTo(expectedLabel.ToLower()));
     }
     catch (Exception ex)
     {
         Console.WriteLine(ex.Message);
     }
 }
        public void ClassifyImageUrlNoStore()
        {
            string testImageUrl = "https://raw.githubusercontent.com/Microsoft/Cognitive-CustomVision-Windows/master/Samples/Images/Test/test_image.jpg";

            using (MockContext context = MockContext.Start(this.GetType()))
            {
                HttpMockServer.Initialize(this.GetType(), "ClassifyImageUrlNoStore", RecorderMode);

                ICustomVisionPredictionClient client = GetPredictionClientClient();
                ImageUrl url = new ImageUrl(testImageUrl);

                ImagePrediction results = client.ClassifyImageUrlWithNoStore(ClassificationProjectId, ClassificationPublishedName, url);
                ValidateResults(results);
            }
        }
Beispiel #19
0
        public void PredictImageUrl()
        {
            string testImageUrl = "https://raw.githubusercontent.com/Microsoft/Cognitive-CustomVision-Windows/master/Samples/Images/Test/test_image.jpg";

            using (MockContext context = MockContext.Start(this.GetType().Name))
            {
                HttpMockServer.Initialize(this.GetType().Name, "PredictImageUrl", RecorderMode);

                ICustomVisionPredictionClient client = GetPredictionClientClient();
                ImageUrl url = new ImageUrl(testImageUrl);

                ImagePrediction results = client.PredictImageUrlAsync(ProjectId, url).Result;
                ValidateResults(results);
            }
        }
 private static void ValidateResults(ImagePrediction results)
 {
     Assert.Equal(ClassificationProjectId, results.Project);
     Assert.NotEqual(Guid.Empty, results.Iteration);
     Assert.Equal(2, results.Predictions.Count);
     Assert.Equal("Tag1", results.Predictions[0].TagName);
     Assert.Equal(TagType.Regular, results.Predictions[0].TagType);
     Assert.InRange(results.Predictions[0].Probability, 0, 1);
     Assert.NotEqual(Guid.Empty, results.Predictions[0].TagId);
     Assert.Null(results.Predictions[0].BoundingBox);
     Assert.Equal("Tag2", results.Predictions[1].TagName);
     Assert.Equal(TagType.Regular, results.Predictions[1].TagType);
     Assert.InRange(results.Predictions[1].Probability, 0, 0.1);
     Assert.NotEqual(Guid.Empty, results.Predictions[1].TagId);
     Assert.Null(results.Predictions[1].BoundingBox);
 }
        public ImagePrediction MakePrediction(string localfile)
        {
            CustomVisionPredictionClient client = new CustomVisionPredictionClient()
            {
                ApiKey   = "key",
                Endpoint = "prediction url from admin console"
            };
            Guid projectId = new Guid("project GUID");

            ImagePrediction imagePrediction = null;

            MemoryStream testImage = new MemoryStream(File.ReadAllBytes(localfile));

            client.ClassifyImage(projectId, "DataEventProject", testImage);
            return(imagePrediction);
        }
        public string DrawLightRectangles(IFormFile file, ImagePrediction predictionResult, int lightness)
        {
            SKBitmap bitmap = SKBitmap.Decode(file.OpenReadStream());

            foreach (var prediction in predictionResult.Predictions)
            {
                if (prediction.Probability > 0.2)
                {
                    int x = (int)(prediction.BoundingBox.Left * bitmap.Width + 0.5);
                    int y = (int)(prediction.BoundingBox.Top * bitmap.Height + 0.5);

                    int width  = (int)(prediction.BoundingBox.Width * bitmap.Width + 0.5);
                    int height = (int)(prediction.BoundingBox.Height * bitmap.Height + 0.5);

                    for (int i = x; i < x + width; ++i)
                    {
                        for (int j = y; j < y + height; ++j)
                        {
                            var rlin = PixelInfo.ConvertToLinear(PixelInfo.ConvertToDecimal(bitmap.GetPixel(i, j).Red));
                            var glin = PixelInfo.ConvertToLinear(PixelInfo.ConvertToDecimal(bitmap.GetPixel(i, j).Green));
                            var blin = PixelInfo.ConvertToLinear(PixelInfo.ConvertToDecimal(bitmap.GetPixel(i, j).Blue));

                            var Y = PixelInfo.CalculateYLuminence(rlin, glin, blin);

                            var lum = PixelInfo.CalculatePerceivedLightness(Y);
                            if (lum > lightness)
                            {
                                bitmap.SetPixel(i, j, new SKColor(103, 235, 52));
                            }
                        }
                    }
                }
            }

            var image = SKImage.FromBitmap(bitmap);

            var imageStream = image.Encode().AsStream();

            using (MemoryStream ms = new MemoryStream())
            {
                imageStream.CopyTo(ms);

                var bytesArray = ms.ToArray();

                return(Convert.ToBase64String(bytesArray));
            }
        }
        private async Task <ResultsViewModel> CreateResultViewModelAsync(ImagePrediction result)
        {
            var vm = new ResultsViewModel();

            try
            {
                if (!result.Predictions.Any())
                {
                    return(null);
                }

                var resultsToDisplay = await MapToPredictionDisplayAsync(result.Predictions);

                vm.FirstResult = resultsToDisplay.FirstOrDefault(m => m.Probability * 100 >= App.ProbabilityThresholdFirstResult);


                if (vm.FirstResult != null)
                {
                    resultsToDisplay.Remove(vm.FirstResult);

                    await _dataService.SaveHistoryItemAsync(new Models.Data.HistoryItem()
                    {
                        TakenOn    = DateTime.Now,
                        MushroomId = vm.FirstResult.Mushroom.Id
                    });
                }


                vm.SecondaryResults = resultsToDisplay.Where(m => m.Probability * 100 >= App.ProbabilityThresholdSecondaryResults).Take(5).ToArray();
            }
            catch (WebException ex)
            {
                Crashes.TrackError(ex);
                await UserDialogs.Instance.AlertAsync("Error conectando a servicio de datos.", "Error");

                throw;
            }
            catch (Exception ex)
            {
                Crashes.TrackError(ex);
                await UserDialogs.Instance.AlertAsync("Error de datos.", "Error");

                throw;
            }

            return(vm);
        }
Beispiel #24
0
        public static void WarmUpPredictionEnginePool(this IServiceCollection services, string warmupImage, byte additionalTestCount = 5)
        {
            var predictionEnginePool =
                services.BuildServiceProvider()
                .GetRequiredService <PredictionEnginePool <InMemoryImageData, ImagePrediction> >();
            var predictionEngine = predictionEnginePool.GetPredictionEngine();

            predictionEnginePool.ReturnPredictionEngine(predictionEngine);

            // Get a sample image
            var img = Image.FromFile(warmupImage);

            byte[] imageData;
            using (MemoryStream ms = new MemoryStream())
            {
                img.Save(ms, System.Drawing.Imaging.ImageFormat.Png);
                imageData = ms.ToArray();
            }

            var imageInputData = new InMemoryImageData(image: imageData, label: null, imageFileName: null);

            // Measure execution time.
            var watch = System.Diagnostics.Stopwatch.StartNew();

            ImagePrediction prediction = predictionEnginePool.Predict(imageInputData);

            for (int i = 1; i < additionalTestCount; i++)
            {
                predictionEnginePool.Predict(imageInputData);
            }

            // Stop measuring time.
            watch.Stop();
            var elapsedMs = watch.ElapsedMilliseconds;

            Console.WriteLine();
            Console.WriteLine(new string('*', 25));
            Console.WriteLine();
            Console.WriteLine("Warmup prediction: {0}, {1}% - {2} sec.",
                              prediction.PredictedLabel,
                              prediction.Score.Max() * 100,
                              elapsedMs / 1000.0);
            Console.WriteLine();
            Console.WriteLine(new string('*', 25));
            Console.WriteLine();
        }
Beispiel #25
0
        static void PerformSubMenu_Prediction()
        {
            Console.ForegroundColor = ConsoleColor.Cyan;
            ConsoleHelper.Print_CenteredTitle("VUI LÒNG CHỌN CHỨC NĂNG DƯỚI ĐÂY:", 100);
            Console.WriteLine("\n" + new string('=', 100));
            Console.WriteLine("1. Tự động dự đoán tất cả các bức hình có trong thư mục ");
            Console.WriteLine("2. Dự đoán một bức hình tùy ý");
            ConsoleHelper.Print_WarningText("Lưu ý: Chỉ nhận hình ảnh là tập tin *.jpg hoặc *.png");
            Console.WriteLine(new string('=', 100));
            Console.ResetColor();
            int    function = SelectMenu(2);
            string trainedModelPath;

            switch (function)
            {
            case 1:
                Console.Clear();
                Print_FilePathPrompt(out trainedModelPath, "Nhập đường dẫn đến mô hình đã được huấn luyện trước: ");
                Print_FolderPathPrompt(out string predictionFolderPath, "Nhập đường dẫn đến thư mục hình ảnh cần dự đoán: ");
                MLTraining mlTraining_1 = new MLTraining(trainedModelPath, null, null);
                mlTraining_1.TryMultiplePredictions(predictionFolderPath);
                break;

            case 2:
                Console.Clear();
                Print_FilePathPrompt(out trainedModelPath, "Nhập đường dẫn đến mô hình đã được huấn luyện trước: ");
                ConsumeModel.MLNetModelPath = trainedModelPath;
                while (true)
                {
                    if (!Print_FilePathPrompt(out string imagePath, "Nhập đường dẫn hình ảnh cần dự đoán, hoặc gõ 'exit' để THOÁT: "))
                    {
                        break;
                    }
                    ImageDataInMemory imageData        = new ImageDataInMemory(imagePath, Path.GetFileName(imagePath));
                    ImagePrediction   predictionResult = ConsumeModel.Predict(imageData);
                    Console.WriteLine($"Tên dự đoán: {predictionResult.PredictedLabel}");
                    Console.WriteLine($"Độ chính xác: {predictionResult.Score.Max()}");
                }
                break;

            default:
                break;
            }
            Console.WriteLine("Nhấn phím bất kỳ để quay trở lại...");
        }
Beispiel #26
0
        public PredictionResultsPage(MediaDetails media, ImagePrediction prediction)
        {
            Title          = ApplicationResource.PagePredictionResultsTitle;
            BindingContext = viewModel = new PredictionResultsViewModel(Navigation, media, prediction);
            viewModel.PredictionsChanged += OnPredictionsChanged;

            var screenSize = new Size(App.ScreenWidth, App.ScreenHeight);

            // Image View
            photoView        = new Image();
            photoView.Source = ImageSource.FromFile(media.FullPath);
            AbsoluteLayout.SetLayoutBounds(photoView, new Rectangle(0, 0, 1, 1));
            AbsoluteLayout.SetLayoutFlags(photoView, AbsoluteLayoutFlags.All);

            // Canvas View
            canvasView = new SKCanvasView();
            canvasView.PaintSurface += OnCanvasViewPaintSurface;
            AbsoluteLayout.SetLayoutBounds(canvasView, new Rectangle(0, 0, 1, 1));
            AbsoluteLayout.SetLayoutFlags(canvasView, AbsoluteLayoutFlags.All);

            // Slider view
            Slider confidenceSlider = new Slider()
            {
                Minimum = 0, Maximum = 1
            };

            confidenceSlider.SetBinding(Slider.ValueProperty, new Binding("Confidence"));
            AbsoluteLayout.SetLayoutBounds(confidenceSlider, new Rectangle(.5, 1, .8, .1));
            AbsoluteLayout.SetLayoutFlags(confidenceSlider, AbsoluteLayoutFlags.All);

            var layout = new AbsoluteLayout();

            layout.Margin = new Thickness(10);
            layout.Children.Add(photoView);
            layout.Children.Add(canvasView);
            layout.Children.Add(confidenceSlider);
            layout.SizeChanged += Layout_SizeChanged;
            Content             = layout;

            var orientation = ImageUtils.GetImageOrientation(media.FullPath);

            imageBytes   = File.ReadAllBytes(media.FullPath);
            masterBitmap = ImageUtils.HandleOrientation(SKBitmap.Decode(imageBytes), orientation);
        }
        public string DrawRectangles(IFormFile file, ImagePrediction predictionResult)
        {
            SKBitmap bitmap = SKBitmap.Decode(file.OpenReadStream());

            SKCanvas canvas = new SKCanvas(bitmap);

            SKPaint paint = new SKPaint
            {
                IsAntialias = true,
                Color       = new SKColor(220, 38, 38),
                Style       = SKPaintStyle.Stroke,
                StrokeWidth = 3
            };


            foreach (var prediction in predictionResult.Predictions)
            {
                if (prediction.Probability > 0.2)
                {
                    var x = prediction.BoundingBox.Left * bitmap.Width;
                    var y = prediction.BoundingBox.Top * bitmap.Height;

                    var width  = prediction.BoundingBox.Width * bitmap.Width;
                    var height = prediction.BoundingBox.Height * bitmap.Height;

                    canvas.DrawRect((float)x, (float)y, (float)width, (float)height, paint);
                }
            }

            var image = SKImage.FromBitmap(bitmap);

            var imageStream = image.Encode().AsStream();

            using (MemoryStream ms = new MemoryStream())
            {
                imageStream.CopyTo(ms);

                var bytesArray = ms.ToArray();

                return(Convert.ToBase64String(bytesArray));
            }
        }
        /// <summary>
        /// Get the first image file and run prediction method
        /// </summary>
        public void TrySinglePrediction(string InputFolderPathForPrediction)
        {
            if (trainedModel == null)
            {
                if (File.Exists(OutputModelFilePath))
                {
                    LoadTrainedModel();
                }
                else
                {
                    throw new Exception("Please run the pipeline before predicting!");
                }
            }
            PredictionEngine <ImageDataInMemory, ImagePrediction> predictionEngine = mlContext.Model.CreatePredictionEngine <ImageDataInMemory, ImagePrediction>(trainedModel);
            IEnumerable <ImageDataInMemory> predictedImages = FileUtils.LoadImagesFromDirectoryInMemory(InputFolderPathForPrediction, false);
            ImageDataInMemory image      = predictedImages.First();
            ImagePrediction   prediction = predictionEngine.Predict(image);

            PrintImagePrediction(image.ImagePath, image.Label, prediction.PredictedLabel, prediction.Score.Max());
        }
        private static void ValidateObjDetectionResults(ImagePrediction results)
        {
            Assert.Equal(ObjectDetectionProjectId, results.Project);
            Assert.NotEqual(Guid.Empty, results.Iteration);
            Assert.Equal(7, results.Predictions.Count);

            var expectedTags = new string[] { "fork", "fork", "fork", "fork", "scissors", "scissors", "scissors" };

            for (var i = 0; i < expectedTags.Length; i++)
            {
                Assert.Equal(expectedTags[i], results.Predictions[i].TagName);
                Assert.InRange(results.Predictions[i].Probability, 0, 1);
                Assert.NotEqual(Guid.Empty, results.Predictions[i].TagId);
                Assert.NotNull(results.Predictions[i].BoundingBox);
                Assert.InRange(results.Predictions[i].BoundingBox.Left, 0, 1);
                Assert.InRange(results.Predictions[i].BoundingBox.Top, 0, 1);
                Assert.InRange(results.Predictions[i].BoundingBox.Width, 0, 1);
                Assert.InRange(results.Predictions[i].BoundingBox.Height, 0, 1);
            }
        }
Beispiel #30
0
        void CreateDebugPrediction()
        {
            var media = new Models.MediaDetails()
            {
                Path = "IMG_0111.jpg"
            };

            var predictions = new List <PredictionModel>
            {
                new PredictionModel(tagName: "test", boundingBox: new BoundingBox(.25, .25, .5, .5), probability: .5),
                new PredictionModel(tagName: "test", boundingBox: new BoundingBox(.1, .1, .2, .2), probability: .8)
            };

            var imagePrediction = new ImagePrediction(predictions: predictions);

            var testNavigationPage = new NavigationPage(new PredictionResultsPage(media, imagePrediction));

            testNavigationPage.Title              = ApplicationResource.NavigationTestTitle;
            testNavigationPage.Icon               = "capture.png";
            testNavigationPage.BarTextColor       = Color.White;
            testNavigationPage.BarBackgroundColor = AppColors.HeaderColor;
            Children.Add(testNavigationPage);
        }