// ----------------------------------------------------------------------- // KEY SAMPLE CODE ENDS HERE // ----------------------------------------------------------------------- /// <summary> /// This method parses the JSON ouput, and converts to a sequence of time frames with highlight region. A full video highlight is created if there is motion detected in the frame. /// </summary> /// <param name="json">JSON output of motion detection result.</param> /// <returns>Sequence of time frames with highlight regions.</returns> private static IEnumerable<FrameHighlight> GetHighlights(string json) { MotionDetectionResult motionDetectionResult = Helpers.FromJson<MotionDetectionResult>(json); double timescale = motionDetectionResult.Timescale; if (motionDetectionResult.Regions == null) yield break; List<int> regionIds = motionDetectionResult.Regions.Select(x => x.Id).ToList(); Rect hasMotionRect = new Rect(new Point(0, 0), new Size(1, 1)); // Uses this full-frame rectangle to represent motion is detected in one frame Rect noMotionRect = new Rect(new Point(0, 0), new Size(0, 0)); // Uses this empty rectangle to represent motion is not detected foreach (Fragment<MotionEvent> fragment in motionDetectionResult.Fragments) { if (fragment.Events == null || fragment.Events.Length == 0) { // If 'Events' is empty, there isn't any motion detected in this fragment Rect[] rects = new Rect[regionIds.Count]; for (int i = 0; i < rects.Length; i++) rects[i] = noMotionRect; yield return new FrameHighlight() { Time = fragment.Start / timescale, HighlightRects = rects.ToArray() }; } else { long interval = fragment.Interval.GetValueOrDefault(); for (int i = 0; i < fragment.Events.Length; i++) { double currentTime = (fragment.Start + interval*i)/timescale; MotionEvent[] evts = fragment.Events[i]; Rect[] rects = regionIds.Select(id => { MotionEvent evt = evts.FirstOrDefault(x => x.RegionId == id); if (evt == null) return noMotionRect; return hasMotionRect; }).ToArray(); yield return new FrameHighlight() {Time = currentTime, HighlightRects = rects}; } } } }