private bool ClassifyDuplicateSortDronoPhotos(string modelInputFileText, string inputDirText, string outputDirText, BackgroundWorker worker, DoWorkEventArgs e) { StatusString = "initializing prediction engine"; int imageClassificationCount = 0; ImageCountString = imageClassificationCount.ToString(); int highestPercentageReached = 0; string[] filesToProcess = Directory.GetFiles(inputDirText); MLContext mlContext = new MLContext(); ITransformer mlModel = mlContext.Model.Load(modelInputFileText, out var modelInputSchema); var predEngine = mlContext.Model.CreatePredictionEngine <ModelInput, ModelOutput>(mlModel); foreach (var item in filesToProcess) { StatusString = "In Progress"; if (worker.CancellationPending) { e.Cancel = true; } else { ModelInput mip = new ModelInput(); mip.Label = "none";//useful for evaluation section mip.ImageSource = item; ModelOutput result = predEngine.Predict(mip); //Degbugging string toprintDebugConsole = $"prediction class: {result.Prediction}|| score: {result.Score.FirstOrDefault()}"; System.Diagnostics.Debug.WriteLine(imageClassificationCount++); System.Diagnostics.Debug.WriteLine(item); System.Diagnostics.Debug.WriteLine(toprintDebugConsole); //Updating UI Properties on Predictions ItemNameString = item; ClassificationString = result.Prediction; ScoreString = result.Score.FirstOrDefault().ToString(); ImageCountString = imageClassificationCount.ToString(); //create destination and/or save new file to destination location and prediction var filename = System.IO.Path.GetFileName(item); string disclass = System.IO.Path.Combine(outputDirText, result.Prediction); //TODO: find another way to compare and upload UI on the classifications found from the model. if (!System.IO.Directory.Exists(disclass)) { // uiContext.Send(x => ClassificationListOfStrings.Add(result.Prediction.ToString()), null); uiContext.Post(x => ClassificationListOfStrings.Add(result.Prediction.ToString()), null); //Dispatcher.CurrentDispatcher.Invoke(new Action(delegate () //{ // ClassificationListOfStrings.Add(result.Prediction.ToString()); //})); //ClassificationListOfStrings.Add(result.Prediction.ToString()); System.IO.Directory.CreateDirectory(disclass); } var destfile = System.IO.Path.Combine(disclass, filename); System.IO.File.Copy(item, destfile, true); //precentage int percentComplete = (int)((float)imageClassificationCount / (float)filesToProcess.Length * 100); if (percentComplete > highestPercentageReached) { highestPercentageReached = percentComplete; worker.ReportProgress(percentComplete); } } } return(true);// all files have been classifed and sorted }
private void UpdateListBox() {//not using, but storing now to consider using delegate methods for updating UI thread from bgworker. Not tested, //but considering it as alternative. ClassificationListOfStrings.Add(_classificationString); }