public override void Process(Bitmap[] frames)
        {
            extraTrackingInfo = null;

            Bitmap frame = frames[0];

            if (frame == null)
            {
                throw new Exception("Frame is null!");
            }

            if (frame.Width != imageSize.Width || frame.Height != imageSize.Height)
            {
                throw new Exception("Invalid frame sizes");
            }


            _curFrame.setImage(frame);

            CvImageWrapper.ConvertImageColor(_curFrame, _grey, ColorConversion.BGR2GRAY);

            if (!validTrackPoints)
            {
                eyeLocator.AddImage(frame);

                if (eyeLocator.TrackingPointsFound)
                {
                    _current_track_points[MousePointIndex]    = eyeLocator.MouseTrackingPoint;
                    _current_track_points[LeftEyePointIndex]  = eyeLocator.LeftEyeTrackingPoint;
                    _current_track_points[RightEyePointIndex] = eyeLocator.RightEyeTrackingPoint;

                    leftEyeOffset.x = eyeLocator.LeftEyePoint.x - eyeLocator.LeftEyeTrackingPoint.x;
                    leftEyeOffset.y = eyeLocator.LeftEyePoint.y - eyeLocator.LeftEyeTrackingPoint.y;

                    rightEyeOffset.x = eyeLocator.RightEyePoint.x - eyeLocator.RightEyeTrackingPoint.x;
                    rightEyeOffset.y = eyeLocator.RightEyePoint.y - eyeLocator.RightEyeTrackingPoint.y;

                    validTrackPoints = true;
                }
                else
                {
                    trackingSuiteAdapter.SendMessage(InitMessage);
                }
            }

            for (int i = 0; i < NumberOfTrackingPoints; ++i)
            {
                SwapPoints(ref _current_track_points[i], ref _last_track_points[i]);
            }

            cvCalcOpticalFlowPyrLK(_prev_grey._rawPtr, _grey._rawPtr, _prev_pyramid._rawPtr,
                                   _pyramid._rawPtr, _last_track_points, _current_track_points, NumberOfTrackingPoints, _pwinsz, 3,
                                   _status, null, _criteria, _flowflags);

            if (validTrackPoints)
            {
                for (int i = 0; i < NumberOfTrackingPoints; ++i)
                {
                    if (_status[i] == 0)
                    {
                        validTrackPoints = false;
                        trackingSuiteAdapter.ToggleSetup(true);
                        eyeLocator.Reset();
                        break;
                    }
                }
            }

            for (int i = 0; i < NumberOfTrackingPoints; ++i)
            {
                LimitTPDelta(ref _current_track_points[i], _last_track_points[i]);
            }


            SwapImages(ref _grey, ref _prev_grey);
            SwapImages(ref _pyramid, ref _prev_pyramid);

            if (validTrackPoints)
            {
                extraTrackingInfo = new BlinkLinkClickControlModule.BlinkLinkCMSExtraTrackingInfo(
                    new CvPoint2D32f(_current_track_points[LeftEyePointIndex].x + leftEyeOffset.x,
                                     _current_track_points[LeftEyePointIndex].y + leftEyeOffset.y),
                    new CvPoint2D32f(_current_track_points[RightEyePointIndex].x + rightEyeOffset.x,
                                     _current_track_points[RightEyePointIndex].y + rightEyeOffset.y));

                imagePoint.X = _current_track_points[MousePointIndex].x;
                imagePoint.Y = _current_track_points[MousePointIndex].y;

                eyeImagePoints[0].X = _current_track_points[LeftEyePointIndex].x;
                eyeImagePoints[0].Y = _current_track_points[LeftEyePointIndex].y;

                eyeImagePoints[1].X = _current_track_points[RightEyePointIndex].x;
                eyeImagePoints[1].Y = _current_track_points[RightEyePointIndex].y;
            }
        }
예제 #2
0
        public override void Process(System.Drawing.Bitmap[] frames)
        {
            try
            {
                lock (mutex)
                {
                    Bitmap frame = frames[0];

                    if (frame == null)
                    {
                        throw new Exception("Frame is null!");
                    }

                    if (frame.Width != imageSize.Width || frame.Height != imageSize.Height)
                    {
                        throw new Exception("Invalid frame sizes");
                    }

                    _curFrame.setImage(frame);


                    ProcessLucasKanade();

                    //bool drawOnFrame = true;
                    if (state.Equals(AHMTrackingState.NoFeature))
                    {
                        //drawOnFrame = false;
                        extraTrackingInfo = null;
                        long eyeLocatorNewTickCount = Environment.TickCount;

                        if (eyeLocatorNewTickCount - eyeLocatorTickCount > 10000)
                        {
                            if (!autoStartEnded)
                            {
                                trackingSuiteAdapter.SendMessage("Please Blink");
                                autoStartEnded = true;
                            }
                            eyeLocator.AddImage(frame);
                            if (eyeLocator.TrackingPointsFound)
                            {
                                _current_track_points[MousePointIndex]    = eyeLocator.MouseTrackingPoint;
                                _current_track_points[LeftEyePointIndex]  = eyeLocator.LeftEyeTrackingPoint;
                                _current_track_points[RightEyePointIndex] = eyeLocator.RightEyeTrackingPoint;

                                leftEyeOffset.x = eyeLocator.LeftEyePoint.x - eyeLocator.LeftEyeTrackingPoint.x;
                                leftEyeOffset.y = eyeLocator.LeftEyePoint.y - eyeLocator.LeftEyeTrackingPoint.y;

                                rightEyeOffset.x = eyeLocator.RightEyePoint.x - eyeLocator.RightEyeTrackingPoint.x;
                                rightEyeOffset.y = eyeLocator.RightEyePoint.y - eyeLocator.RightEyeTrackingPoint.y;



                                SetState(AHMTrackingState.AHMSetup);
                            }
                        }
                        else
                        {
                            int second = (int)Math.Round(((double)(10000 - (eyeLocatorNewTickCount - eyeLocatorTickCount))) / 1000.0);
                            trackingSuiteAdapter.SendMessage("Auto Start in " + second + " seconds");
                        }
                    }
                    else if (state.Equals(AHMTrackingState.Tracking))
                    {
                        //drawOnFrame = false;
                        ProcessAHM();
                        extraTrackingInfo = new BlinkLinkClickControlModule.BlinkLinkCMSExtraTrackingInfo(
                            new CvPoint2D32f(_current_track_points[LeftEyePointIndex].x + leftEyeOffset.x,
                                             _current_track_points[LeftEyePointIndex].y + leftEyeOffset.y),
                            new CvPoint2D32f(_current_track_points[RightEyePointIndex].x + rightEyeOffset.x,
                                             _current_track_points[RightEyePointIndex].y + rightEyeOffset.y));
                    }
                    else if (state.Equals(AHMTrackingState.AHMSetup))
                    {
                        extraTrackingInfo = null;
                        if (setupPoints == null)
                        {
                            setupPoints = new PointF[NumberOfTrackingPoints];
                        }
                        for (int i = 0; i < NumberOfTrackingPoints; i++)
                        {
                            setupPoints[i].X = _current_track_points[i].x;
                            setupPoints[i].Y = _current_track_points[i].y;
                        }

                        if (ahmSetup.Process(setupPoints, frames))
                        {
                            //ahmSetup.DrawOnFrame(frames);
                            //drawOnFrame = ahmSetup.DrawLucasKanade;
                        }
                        else
                        {
                            Thread t = new Thread(new ThreadStart(SetupFinished));
                            t.Start();

                            //SetupFinished();
                            //drawOnFrame = false;
                            SetState(AHMTrackingState.Tracking);
                        }
                    }

                    //if (drawOnFrame)
                    //  DrawPointOnFrame(frame);
                }
            }
            catch (Exception e)
            {
            }
        }