Example #1
0
        // -----------------------------------------------------------------------
        // 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
                        });
                    }
                }
            }
        }
Example #2
0
        private void SetupMotionSensor()
        {
            motionDetectionResults   = new MotionDetectionResult();
            MotionStatus.DataContext = motionDetectionResults;

            motionSensor = MotionSensorFactory.Create();
            motionSensor.InitGPIO();
            motionSensor.MotionDetected   += MotionSensor_MotionDetected;
            motionSensor.MotionUndetected += MotionSensor_MotionUndetected;
        }
 private void ProcessOldFrame(MotionDetectionResult result)
 {
     if (IsStaticFrame(result.MotionRect))
     {
         this._manager.DisposeFrame(result.FrameGuid);
     }
     else
     {
         this._manager.MoveToMotionFrames(result);
     }
 }
Example #4
0
        public void MoveToMotionFrames( MotionDetectionResult frameResult )
        {
            var f = RetrieveFrame(frameResult.FrameGuid);
            if (f != null)
            {
                if (this.motionFrames == null)
                {
                    this.motionFrames = new List<Frame>();
                }

                f.MotionRectangles.Add(frameResult.MotionRect);
                motionFrames.Add(f);
            }
        }
Example #5
0
        public void Test()
        {
            IMotionDetector motiondetector = new FaceProcessingWrapper.MotionDetector();

            foreach (var file in System.IO.Directory.GetFiles(@"M:\测试图片\Lb", "*.jpg"))
            {
                var ipl   = OpenCvSharp.IplImage.FromFile(file);
                var frame = new Frame(ipl);

                MotionDetectionResult result = new MotionDetectionResult();
                var groupCaptured            = motiondetector.Detect(frame, ref result);

                System.Diagnostics.Debug.WriteLine(groupCaptured.ToString() + " " + result.MotionRect.Width);
            }
        }
Example #6
0
        public void MoveToMotionFrames(MotionDetectionResult frameResult)
        {
            var f = RetrieveFrame(frameResult.FrameGuid);

            if (f != null)
            {
                if (this.motionFrames == null)
                {
                    this.motionFrames = new List <Frame>();
                }

                f.MotionRectangles.Add(frameResult.MotionRect);
                motionFrames.Add(f);
            }
        }
        public bool ProcessFrame(Frame frame)
        {
            try
            {
                frame.GetImage();
            }
            catch (System.ArgumentException ex)
            {
                return(false);
            }

            this._manager.AddNewFrame(frame);

            var  oldFrameMotionResult = new MotionDetectionResult();
            bool groupCaptured        =
                ProcessNewFrame(frame, ref oldFrameMotionResult);

            ProcessOldFrame(oldFrameMotionResult);

            return(groupCaptured);
        }
        private bool ProcessNewFrame(Frame frame, ref MotionDetectionResult detectionResult)
        {
            var result = CurrentMotionDetector.Detect(frame, ref detectionResult);

            return(result);
        }