Example #1
0
        // This method is executed by the ThreadPoolTimer, it performs the evaluation on a copy of the VideoFrame
        private async void EvaluateVideoFrame(ThreadPoolTimer timer)
        {
            // If a lock is being held, or WinML isn't fully initialized, return
            if (!semaphore.Wait(0) || !modelBindingComplete)
            {
                return;
            }

            try
            {
                using (evaluatableVideoFrame)
                {
                    // ************ WinML Evaluate Frame ************ //

                    Debug.WriteLine($"RelativeTime in Seconds: {evaluatableVideoFrame.RelativeTime?.Seconds}");

                    await model.EvaluateAsync(binding, "TinyYOLO");

                    // Remove overlapping and low confidence bounding boxes
                    filteredBoxes = parser.NonMaxSuppress(parser.ParseOutputs(outputArray.ToArray()), 5, .5F);

                    Debug.WriteLine(filteredBoxes.Count <= 0 ? $"No Valid Bounding Boxes" : $"Valid Bounding Boxes: {filteredBoxes.Count}");
                }
            }
            catch (Exception ex)
            {
                Debug.WriteLine($"EvaluateFrameException: {ex}");
            }
            finally
            {
                semaphore.Release();
            }
        }
        private async Task DrawOverlays(VideoFrame inputImage)
        {
            OverlayCanvas.Children.Clear();

            // Render output
            if (boxes.Count > 0)
            {
                // Remove overalapping and low confidence bounding boxes
                var filteredBoxes = parser.NonMaxSuppress(boxes, 5, .5F);

                foreach (var box in filteredBoxes)
                {
                    await DrawYoloBoundingBoxAsync(inputImage.SoftwareBitmap, box);
                }
            }
        }
        // This method is executed by the ThreadPoolTimer, it performs the evaluation on a copy of the VideoFrame
        private async void EvaluateVideoFrame(ThreadPoolTimer timer)
        {
            if (semaphore == null)
            {
                return;
            }

            if (!modelCreated)
            {
                Debug.WriteLine($"EvaluateVideoFrame Skipped - LearningModel Not Ready.");
                return;
            }

            // If a lock is being held, or WinML isn't fully initialized, return
            if (!semaphore.Wait(0))
            {
                Debug.WriteLine($"EvaluateVideoFrame Skipped - Waiting Semaphore Access");
                return;
            }

            if (session == null)
            {
                try
                {
                    Debug.WriteLine($"Attempting to create LearningModelSession using DeviceKind: {detectedDeviceKind}");

                    session = new LearningModelSession(model, new LearningModelDevice(detectedDeviceKind));

                    Debug.WriteLine($"LearningModelSession successfully created.");
                }
                catch (Exception ex)
                {
                    Debug.WriteLine($"Error Creating Session: {ex.Message}");
                    model = null;
                    return;
                }
            }

            Debug.WriteLine($"*************** Evaluating Video Frame [START] ***************");

            try
            {
                using (videoFrameToProcess)
                {
                    // ************ WinML Evaluate Frame ************ //

                    Debug.WriteLine($"VideoFrame RelativeTime: {videoFrameToProcess.RelativeTime?.Seconds}s");

                    // Create a binding object from the session
                    binding = new LearningModelBinding(session);

                    // Create an image tensor from a video frame
                    var image = ImageFeatureValue.CreateFromVideoFrame(videoFrameToProcess);

                    // The YOLO model's input name is "image" and output name is "grid"
                    var inputName  = "image";
                    var outputName = "grid";

                    // Bind the image to the input
                    binding.Bind(inputName, image);

                    // Process the frame with the model
                    var results = await session.EvaluateAsync(binding, $"YoloRun {++runCount}");

                    Debug.WriteLine($"Evaluation Result Success? {results.Succeeded} ");

                    if (!results.Succeeded)
                    {
                        return;
                    }

                    Debug.WriteLine($" **** {results.Outputs.Count} Outputs Available ***** ");

                    foreach (var output in results.Outputs)
                    {
                        Debug.WriteLine($" - {output.Key}");
                    }

                    // Retrieve the results of evaluation
                    var resultTensor = results.Outputs[outputName] as TensorFloat;

                    Debug.WriteLine($"Result Feature Kind: {resultTensor?.Kind.ToString()} ");

                    var resultVector = resultTensor?.GetAsVectorView();

                    // Remove overlapping and low confidence bounding boxes
                    filteredBoxes = parser.NonMaxSuppress(parser.ParseOutputs(resultVector?.ToArray()), 5, .5F);

                    Debug.WriteLine(filteredBoxes.Count <= 0 ? $"No Valid Bounding Boxes" : $"Valid Bounding Boxes: {filteredBoxes.Count}");
                }
            }
            catch (Exception ex)
            {
                Debug.WriteLine($"Error: EvaluateFrameException: {ex}");
            }
            finally
            {
                semaphore?.Release();

                Debug.WriteLine($"*************** Evaluating Video Frame [END] ***************");
            }
        }