public async Task <(int person, int vehicle)> DetectObject(VideoFrame frame) { await binding.SetInputImageAsync(frame); // frame is a Windows.Media.VideoFrame await skill.EvaluateAsync(binding); // Results are saved to binding object IReadOnlyList <ObjectDetectorResult> detections = binding.DetectedObjects; //foreach (ObjectDetectorResult detection in detections) //{ // Windows.Foundation.Rect boundingRect = detection.Rect; // ObjectKind objectKind = detection.Kind; // This enum is defined in the ObjectDetector namespace // // Use results as desired //} HashSet <ObjectKind> objectKindsOfInterest = new HashSet <ObjectKind> { ObjectKind.Motorbike, ObjectKind.Car }; var vehicleCount = binding.DetectedObjects.Where( detection => objectKindsOfInterest.Contains(detection.Kind) ).Count(); objectKindsOfInterest = new HashSet <ObjectKind> { ObjectKind.Person }; var personCount = binding.DetectedObjects.Where( detection => objectKindsOfInterest.Contains(detection.Kind) ).Count(); return(personCount, vehicleCount); }
/// <summary> /// Bind and evaluate the frame with the ObjectDetector skill /// </summary> /// <param name="frame"></param> /// <returns></returns> private async Task DetectObjectsAsync(VideoFrame frame) { m_evalStopwatch.Restart(); // Bind await m_binding.SetInputImageAsync(frame); m_bindTime = (float)m_evalStopwatch.ElapsedTicks / Stopwatch.Frequency * 1000f; m_evalStopwatch.Restart(); // Evaluate await m_skill.EvaluateAsync(m_binding); m_evalTime = (float)m_evalStopwatch.ElapsedTicks / Stopwatch.Frequency * 1000f; m_evalStopwatch.Stop(); }
/// <summary> /// Bind and evaluate the frame with the ObjectDetector skill /// </summary> /// <param name="frame"></param> /// <returns></returns> private async Task <IReadOnlyList <ObjectDetectorResult> > DetectObjectsAsync(VideoFrame frame) { // Bind await m_binding.SetInputImageAsync(frame); // Evaluate await m_skill.EvaluateAsync(m_binding); var results = m_binding.DetectedObjects; // Filter results if requested if (m_objectKinds != null && m_objectKinds.Count > 0) { results = results.Where(det => m_objectKinds.Contains(det.Kind)).ToList(); } return(results); }
/// <summary> /// Bind and evaluate the frame with the ObjectDetector skill /// </summary> /// <param name="frame"></param> /// <returns></returns> private async Task RunSkillsAsync(VideoFrame frame) { m_bindTime = 0F; m_evalTime = 0F; m_frameCounter++; // Update all trackers m_evalStopwatch.Restart(); var bindTasks = new List <Task>(); foreach (var binding in m_trackerBindings) { bindTasks.Add(binding.SetInputImageAsync(frame).AsTask()); } await Task.WhenAll(bindTasks); m_evalStopwatch.Stop(); m_bindTime += (float)m_evalStopwatch.ElapsedTicks / Stopwatch.Frequency * 1000f; m_evalStopwatch.Restart(); var evalTasks = new List <Task>(); foreach (var binding in m_trackerBindings) { evalTasks.Add(m_trackerSkill.EvaluateAsync(binding).AsTask()); } await Task.WhenAll(evalTasks); m_evalStopwatch.Stop(); m_evalTime += (float)m_evalStopwatch.ElapsedTicks / Stopwatch.Frequency * 1000f; // Add results to histories for (int i = 0; i < m_trackerBindings.Count; i++) { m_trackerHistories[i].Enqueue( new TrackerResult() { label = m_trackerHistories[i].Peek().label, boundingRect = m_trackerBindings[i].BoundingRect, succeeded = m_trackerBindings[i].Succeeded } ); if (m_trackerHistories[i].Count > m_maxTrackerHistoryLength) { m_trackerHistories[i].Dequeue(); } } // Run detector if no successful trackers or we've reached our desired detector period if (m_trackerBindings.Where(binding => binding.Succeeded).Count() == 0 || (m_frameCounter % m_detectorEvalInterval) == 0) { // Bind m_evalStopwatch.Restart(); await m_detectorBinding.SetInputImageAsync(frame); m_evalStopwatch.Stop(); m_bindTime += (float)m_evalStopwatch.ElapsedTicks / Stopwatch.Frequency * 1000f; // Evaluate m_evalStopwatch.Restart(); await m_detectorSkill.EvaluateAsync(m_detectorBinding); m_evalStopwatch.Stop(); m_evalTime += (float)m_evalStopwatch.ElapsedTicks / Stopwatch.Frequency * 1000f; // Clear and re-initialize trackers m_trackerBindings.Clear(); m_trackerHistories.Clear(); IEnumerable <ObjectDetectorResult> filteredDetections = m_detectorBinding.DetectedObjects.Where(det => ((m_objectKinds?.Count ?? 0) == 0) || m_objectKinds.Contains(det.Kind)); var initializeTasks = new List <Task>(); m_evalStopwatch.Restart(); // Since we're initializing trackers in parallel, it's easiest to count it all as evaluation // (including tracker binding) Dictionary <string, int> objectKindCounter = new Dictionary <string, int>(); foreach (ObjectDetectorResult detection in filteredDetections) { // Cap at max trackers if (m_trackerBindings.Count >= m_maxNumberTrackers) { break; } // Do some sanity checks to make sure the detection is valid if (detection.Rect.Width <= 0 || detection.Rect.Height <= 0) { break; } // Create and initialize new tracker ObjectTrackerBinding binding = await m_trackerSkill.CreateSkillBindingAsync() as ObjectTrackerBinding; Rect clampedRect = new Rect( Math.Max(detection.Rect.X, 0.0), Math.Max(detection.Rect.Y, 0.0), detection.Rect.Width, detection.Rect.Height); initializeTasks.Add(m_trackerSkill.InitializeTrackerAsync(binding, frame, clampedRect).AsTask()); m_trackerBindings.Add(binding); // Add corresponding tracker history string objectKindStr = detection.Kind.ToString(); if (!objectKindCounter.ContainsKey(objectKindStr)) { objectKindCounter.Add(objectKindStr, 0); } string label = $"{objectKindStr} {++objectKindCounter[objectKindStr]}"; m_trackerHistories.Add(new Queue <TrackerResult>()); m_trackerHistories.Last().Enqueue( new TrackerResult() { label = label, boundingRect = clampedRect, succeeded = true } ); } await Task.WhenAll(initializeTasks); m_evalStopwatch.Stop(); m_evalTime += (float)m_evalStopwatch.ElapsedTicks / Stopwatch.Frequency * 1000f; m_frameCounter = 0; // Reset frame counter } }