示例#1
0
        private void imageFrameCaptured(object sender, EventArgs e)
        {
            frameCounter = frameCounter + 1;

            // Handle Every Frame if Video is Playing

            if (radioButtonVideo.Checked)
            {
                try
                {
                    if (_capture != null)
                    {
                        if (frameCounter == 1)
                        {
                            prevFrame = _capture.QueryFrame().ToImage <Gray, byte>().Resize(200, 200, Emgu.CV.CvEnum.Inter.Area);
                        }
                        else
                        {
                            Image <Bgr, byte> bgrImage = _capture.QueryFrame().ToImage <Bgr, byte>().Resize(400, 400, Emgu.CV.CvEnum.Inter.Area);
                            for (int i = 0; i < frameSkip; i++)
                            {
                                _capture.Grab(); // skip the number of frames
                            }
                            _opticalflow = new OpticalFlow(filename, (int)ActivityClass.walking);
                            nextFrame    = _capture.QueryFrame().ToImage <Gray, byte>().Resize(200, 200, Emgu.CV.CvEnum.Inter.Area);
                            Image <Hsv, byte> outputImg = _opticalflow.CalculateOpticalFlow(prevFrame, nextFrame, frameCounter);
                            var sample     = _opticalflow.GetFeatureMatrix();
                            int prediction = mlp.Inference(sample);
                            SetText(Enum.GetName(typeof(ActivityClass), prediction));
                            opticalViewBox.Image = outputImg.Resize(300, 300, Emgu.CV.CvEnum.Inter.Cubic);
                            outputImg.Dispose();
                            //_opticalflow.PyrLkOpticalFlow(prevFrame, nextFrame);
                            prevFrame.Dispose();
                            pictureViewBox.Image = bgrImage;
                            prevFrame            = nextFrame.Clone();
                            nextFrame.Dispose();
                        }
                    }
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);

                    _capture.Pause();
                    // detach the callback function
                    _capture.ImageGrabbed -= imageFrameCaptured;

                    _capture.Dispose();
                    _capture = null;

                    isPlaying = false;
                    // change the button icon to play
                    btnPlayPause.BackgroundImage = Properties.Resources.play36;
                }
            }

            // Handle Every Frame if Camera is On
            if (radioButtonCamera.Checked)
            {
                try
                {
                    if (_capture != null)
                    {
                        if (frameCounter == 1)
                        {
                            _capture.Retrieve(_frame, 0);
                            prevFrame = _frame.ToImage <Gray, byte>().Resize(200, 200, Emgu.CV.CvEnum.Inter.Cubic);
                        }
                        else
                        {
                            _capture.Retrieve(_frame, 0);
                            Image <Bgr, byte> bgrImage = _frame.ToImage <Bgr, byte>().Resize(400, 400, Emgu.CV.CvEnum.Inter.Cubic);
                            nextFrame            = _frame.ToImage <Gray, byte>().Resize(200, 200, Emgu.CV.CvEnum.Inter.Cubic);
                            _opticalflow         = new OpticalFlow("", (int)ActivityClass.walking);
                            opticalViewBox.Image = _opticalflow.CalculateOpticalFlow(prevFrame, nextFrame, frameCounter).Resize(400, 400, Emgu.CV.CvEnum.Inter.Cubic);
                            // Predict the Optical Flow Features in Real Time
                            var sample     = _opticalflow.GetFeatureMatrix();
                            int prediction = mlp.Inference(sample);
                            SetText(Enum.GetName(typeof(ActivityClass), prediction));
                            pictureViewBox.Image = bgrImage;
                            prevFrame.Dispose();
                            prevFrame = nextFrame.Clone();
                            nextFrame.Dispose();
                        }
                    }
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                    _capture.Pause();
                    // detach the callback function
                    _capture.ImageGrabbed -= imageFrameCaptured;

                    _capture.Dispose();
                    _capture  = null;
                    isPlaying = false;
                    // change the button icon to play
                    btnPlayPause.BackgroundImage = Properties.Resources.play36;
                }
            }
        }
示例#2
0
        public void OpticalFlowThread(String filePath, int TotalFrames)
        {
            //try
            //{
            Console.WriteLine("Child thread of Optical Flow starts");
            String       fileName = Path.GetFileName(filePath).Split('.')[0];
            VideoCapture capture  = new VideoCapture(filePath);

            for (int i = 0; i < TotalFrames - 1; i++)
            {
                try
                {
                    if (capture != null)
                    {
                        if (i == 0)
                        {
                            prevFrame = capture.QueryFrame().ToImage <Gray, byte>().Resize(200, 200, Emgu.CV.CvEnum.Inter.Area);
                        }
                        else
                        {
                            for (int j = 0; j < frameSkip; j++)
                            {
                                capture.Grab();     // skip the number of frames
                                i++;
                            }
                            _opticalflow = new OpticalFlow(filename, activityLabel);
                            nextFrame    = capture.QueryFrame().ToImage <Gray, byte>().Resize(200, 200, Emgu.CV.CvEnum.Inter.Area);
                            _opticalflow.CalculateOpticalFlow(prevFrame, nextFrame, frameCounter);
                            //opticalViewBox.Image = outputImg;
                            //outputImg.Dispose();
                            //_opticalflow.PyrLkOpticalFlow(prevFrame, nextFrame);
                            prevFrame.Dispose();
                            //pictureViewBox.Image = nextFrame;
                            prevFrame = nextFrame.Clone();
                            nextFrame.Dispose();
                        }
                    }
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);

                    capture.Pause();
                    capture.Stop();
                    capture.Dispose();
                    capture = null;

                    isPlaying = false;
                    // change the button icon to play
                    btnPlayPause.BackgroundImage = Properties.Resources.play36;
                }
            }
            Console.WriteLine("Child thread of Optical Flow Ends");
            //}
            //catch (ThreadAbortException e)
            //{
            //    Console.WriteLine("Thread Abort Exception");
            //}
            //finally
            //{
            //    Console.WriteLine("Couldn't catch the Thread Exception");
            //}
        }