private async void OnTrain() { if (string.IsNullOrWhiteSpace(ShapeName)) { StatusMessage = "The shape needs to have a non-whitespace name."; return; } if (Drawings == null || Drawings.Count < CustomVisionEngine.RequiredImageCount) { StatusMessage = $"There needs to be at least {CustomVisionEngine.RequiredImageCount} drawings."; return; } StatusMessage = "Initiating training..."; try { await CustomVisionEngine.TrainAsync(ShapeName, Drawings); StatusMessage = "Training complete."; } catch (CustomVisionErrorException ex) { StatusMessage = "There was an error during the training: " + ex.Body.Message; } catch (Exception ex) { StatusMessage = "There was an error during the training: " + ex.Message; } }
private async Task DetectShapeAsync() { if (isTraining) { return; } if (!CustomVisionEngine.IsInitialized) { return; } if (CurrentDrawing == null) { return; } StatusMessage = "Trying to recognize the shape..."; HasStatusButtons = false; try { var shape = await CustomVisionEngine.DetectShapeAsync(CurrentDrawing); if (string.IsNullOrEmpty(shape)) { DetectedShape = "The shape was not recognized."; StatusMessage = "Do you want to add this shape to the model?"; HasStatusButtons = true; } else { DetectedShape = $"The shape was recognized as a {shape}."; StatusMessage = "Do you want to update the model?"; HasStatusButtons = true; } } catch (CustomVisionErrorException ex) { StatusMessage = "There was an error during the recognition: " + ex.Body.Message; } catch (Exception ex) { StatusMessage = "There was an error during the recognition: " + ex.Message; HasStatusButtons = false; } }
private async Task InitializeEngineAsync() { StatusMessage = "Initializing recognition engine..."; HasStatusButtons = false; DetectedShape = "Draw a shape below."; try { await CustomVisionEngine.InitializeAsync(); StatusMessage = null; } catch (Exception ex) { StatusMessage = "There was an error during the initialization: " + ex.Message; HasStatusButtons = false; } }