/// <summary> /// Evaluate the video frame /// </summary> public async Task EvaluateAsync(VideoFrame videoFrame) { // Evaluate input MLModelResult result = await this.CurrentModel.EvaluateAsync(videoFrame); // UI trick to smooth out transition from previous results if (this.PrevVideoWinMLModelResult != null && PrevVideoWinMLModelResult.OutputFeatures?.Count() > 0) { foreach (MLModelOutputFeature feature in result.OutputFeatures) { MLModelOutputFeature prevFeature = this.PrevVideoWinMLModelResult.OutputFeatures.FirstOrDefault(f => f.Label == feature.Label); if (prevFeature != null) { feature.Probability = prevFeature.Probability > 0 ? (feature.Probability + prevFeature.Probability) / 2 : feature.Probability; } } } // Set previous result to current result this.PrevVideoWinMLModelResult = result; // Set results this.Duration = string.Format("{0:f1} fps", (1000 / result.DurationInMilliSeconds)); this.SetResults(result); }
/// <summary> /// Evaluate video frame /// </summary> public async Task <MLModelResult> EvaluateAsync(VideoFrame inputFrame) { MLModelResult result = new MLModelResult() { CorrelationId = Guid.NewGuid().ToString() }; try { // Get current datetime DateTime startTime = DateTime.UtcNow; // Evaluate await EvaluateAsync(result, inputFrame); // Set duration TimeSpan duration = DateTime.UtcNow - startTime; // Prepare result result.DurationInMilliSeconds = duration.TotalMilliseconds; } catch (Exception ex) { Debug.WriteLine("Exception with EvaluateAsync: " + ex); } return(result); }
protected override async Task EvaluateAsync(MLModelResult result, VideoFrame inputFrame) { // Initialize the input PcbModelInput input = new PcbModelInput() { data = inputFrame }; // Evaluate the input PcbModelOutput output = await EvaluateAsync(input, result.CorrelationId); // Get first label from output string label = output.classLabel?.FirstOrDefault(); // Find probability for label if (string.IsNullOrEmpty(label) == false) { float probability = output.loss?.ContainsKey(label) == true ? output.loss[label] : 0f; result.OutputFeatures = new MLModelOutputFeature[] { new MLModelOutputFeature() { Label = label, Probability = probability } }; } }
/// <summary> /// Evaluate a static picture file /// </summary> public async Task EvaluateAsync(StorageFile inputFile) { // Clear previous results this.Results.Clear(); this.PrevVideoWinMLModelResult = null; // Evaluate input MLModelResult result = await this.CurrentModel.EvaluateAsync(inputFile); // Set results this.Duration = string.Format("{0:f1} ms", result.DurationInMilliSeconds); this.SetResults(result); }
protected override async Task EvaluateAsync(MLModelResult result, VideoFrame inputFrame) { // Initialize the input Squeezenet_oldModelInput input = new Squeezenet_oldModelInput() { data = inputFrame }; // Evaluate the input Squeezenet_oldModelOutput output = await EvaluateAsync(input, result.CorrelationId); // Get first label from output List <float> resultProbabilities = output.softmaxout_1 as List <float>; // Find the result of the evaluation in the bound output (the top classes detected with the max confidence) List <float> topProbabilities = new List <float>() { 0.0f, 0.0f, 0.0f }; List <int> topProbabilityLabelIndexes = new List <int>() { 0, 0, 0 }; for (int i = 0; i < resultProbabilities.Count; i++) { for (int j = 0; j < 3; j++) { if (resultProbabilities[i] > topProbabilities[j]) { topProbabilityLabelIndexes[j] = i; topProbabilities[j] = resultProbabilities[i]; break; } } } float probability = topProbabilities[0]; string label = ListOfObjectLabels[topProbabilityLabelIndexes[0]]; result.OutputFeatures = new MLModelOutputFeature[] { new MLModelOutputFeature() { Label = label, Probability = probability } }; }
/// <summary> /// Set the results to be displayed after evaluation /// </summary> private void SetResults(MLModelResult result) { this.SynchronizationContext.Post(_ => { if (this.Results.Count() == 0) { for (int index = 0; index < this.MaxNumResults; index++) { this.Results.Add(new ResultViewModel(this.currentModel.DisplayResultSettings)); } } // Sort output and filter features in descending order List <MLModelOutputFeature> features = result.OutputFeatures .Where(f => f.Probability >= this.currentModel.DisplayMinProbability) .OrderByDescending(f => f.Probability) .Take(this.MaxNumResults) .ToList(); // If no result found if (features.Count() == 0) { features.Add(new MLModelOutputFeature() { Label = "Nothing is found", Probability = 0 }); } int featureIndex = 0; foreach (ResultViewModel resultViewModel in this.Results) { if (featureIndex >= features.Count()) { resultViewModel.IsVisible = false; } else { resultViewModel.Label = features[featureIndex].Label; resultViewModel.Probability = features[featureIndex].Probability; resultViewModel.IsVisible = true; } featureIndex++; } }, null); }
/// <summary> /// Evaluate input video frame work ml model result /// </summary> protected virtual async Task EvaluateAsync(MLModelResult result, VideoFrame inputFrame) { throw new NotImplementedException(); }