private void GetTrackingPoints(FastBitmap img, EyeData leftEye, EyeData rightEye)
        {
            const int   YDownShift  = 30;
            const int   XExpand     = 5;
            const float eyebrowMult = 0.6f;

            int        eyebrowWidth = (int)Math.Abs((leftEye.loc.X - rightEye.loc.X) * eyebrowMult);
            Rectangle  searchArea   = new Rectangle();
            FastBitmap smoothed     = img.GaussianSmooth(5);
            FastBitmap sobel        = smoothed.SobelGradient();

            smoothed.Dispose();

            searchArea.Y      = Math.Max(leftEye.loc.Y, rightEye.loc.Y) + YDownShift;
            searchArea.X      = leftEye.loc.X - XExpand;
            searchArea.Width  = rightEye.loc.X - leftEye.loc.X + (2 * XExpand);
            searchArea.Height = (int)((rightEye.loc.X - leftEye.loc.X) * 0.6f);

            float[,] trackingPointScores = new float[searchArea.Width, searchArea.Height];

            for (int x = searchArea.Left; x < searchArea.Right; ++x)
            {
                for (int y = searchArea.Top; y < searchArea.Bottom; ++y)
                {
                    float score = 0;

                    for (int i = 0; i < TrackingPointWidth; ++i)
                    {
                        for (int j = 0; j < TrackingPointWidth; ++j)
                        {
                            score += sobel.GetIntensity(x + i, y + j);
                        }
                    }
                    trackingPointScores[x - searchArea.Left, y - searchArea.Top] = score;
                }
            }

            mouseTrackingPoint    = GetBestTrackingPointWithinColumn(trackingPointScores, searchArea, (leftEye.loc.X + rightEye.loc.X) / 2);
            leftEyeTrackingPoint  = GetBestTrackingForEye(sobel, leftEye, eyebrowWidth);
            rightEyeTrackingPoint = GetBestTrackingForEye(sobel, rightEye, eyebrowWidth);

            sobel.Dispose();
        }
        private CvPoint2D32f GetBestTrackingPointWithinColumn(float[,] scores, Rectangle searchArea, int x)
        {
            float        bestScore = float.NegativeInfinity;
            CvPoint2D32f bestPoint = new CvPoint2D32f();

            for (int i = x - 2; i <= x + 2; ++i)
            {
                for (int j = 0; j < searchArea.Height; ++j)
                {
                    if (scores[i - searchArea.Left, j] > bestScore)
                    {
                        bestScore   = scores[i - searchArea.Left, j];
                        bestPoint.x = i;
                        bestPoint.y = j + searchArea.Top;
                    }
                }
            }

            return(bestPoint);
        }
        private bool FindTrackingPoints()
        {
            EyeData leftEyeData, rightEyeData;

            if (!FindEyes(out leftEyeData, out rightEyeData))
            {
                return(false);
            }

            leftEyePoint  = new CvPoint2D32f(leftEyeData.loc.X, leftEyeData.loc.Y);
            rightEyePoint = new CvPoint2D32f(rightEyeData.loc.X, rightEyeData.loc.Y);
            try
            {
                GetTrackingPoints(workerImageArray[workerImageArray.Length - 1], leftEyeData, rightEyeData);
            }
            catch (Exception)
            {
                return(false);
            }

            return(true);
        }
Exemple #4
0
        public override void Init(Size [] imageSizes)
        {
            Clean();

            int imageWidth  = imageSizes[0].Width;
            int imageHeight = imageSizes[0].Height;

            eyeLocator = new EyeLocator(3);
            eyeLocator.Reset();
            eyeLocatorTickCount = Environment.TickCount;

            _flowflags      |= CMSConstants.CV_LKFLOW_PYR_A_READY;
            validTrackPoint  = false;
            _pwinsz          = new CvSize(10, 10);
            _status          = new byte[1];
            imageSize.Width  = imageWidth;
            imageSize.Height = imageHeight;

            _last_track_points    = new CvPoint2D32f[1];
            _current_track_points = new CvPoint2D32f[1];

            _criteria = new CvTermCriteria(CMSConstants.CV_TERMCRIT_ITER | CMSConstants.CV_TERMCRIT_EPS, 20, 0.03);

            _curFrame = CvImageWrapper.CreateImage(imageSize, PIXEL_DEPTH, PIXEL_COLOR_CHANNELS);

            _grey = CvImageWrapper.CreateImage(imageSize, PIXEL_DEPTH, PIXEL_CHANNELS);

            _prev_grey = CvImageWrapper.CreateImage(imageSize, PIXEL_DEPTH, PIXEL_CHANNELS);

            _pyramid = CvImageWrapper.CreateImage(imageSize, PIXEL_DEPTH, PIXEL_CHANNELS);

            _prev_pyramid = CvImageWrapper.CreateImage(imageSize, PIXEL_DEPTH, PIXEL_CHANNELS);

            _swap_temp = CvImageWrapper.CreateImage(imageSize, PIXEL_DEPTH, PIXEL_CHANNELS);

            CMSTrackingSuiteAdapter.SendMessage(CMSConstants.PLEASE_CLICK_TF);
        }
Exemple #5
0
        public override void Process(System.Drawing.Bitmap [] frames)
        {
            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);

            SwapPoints(ref _current_track_points[0], ref _last_track_points[0]);

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

            if (validTrackPoint && _status[0] == 0)
            {
                if (CMSLogger.CanCreateLogEvent(false, false, false, "CMSLogStandardStateEvent"))
                {
                    CMSLogStandardStateEvent logEvent = new CMSLogStandardStateEvent();
                    if (logEvent != null)
                    {
                        logEvent.ValidTrackingPoint = false;
                        CMSLogger.SendLogEvent(logEvent);
                    }
                }

                eyeLocatorTickCount = Environment.TickCount;
                validTrackPoint     = false;
                imagePoint          = PointF.Empty;
                trackingSuiteAdapter.ToggleSetup(true);
                trackingSuiteAdapter.SendMessage(CMSConstants.PLEASE_CLICK_TF);
            }


            LimitTPDelta(ref _current_track_points[0], _last_track_points[0]);

            //CvPoint2D32f p = _current_track_points[0];


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

            if (validTrackPoint)
            {
                imagePoint.X = _current_track_points[0].x;
                imagePoint.Y = _current_track_points[0].y;
                DrawOnFrame(frame);
            }
            else
            {
                if (!autoStartMode.Equals(AutoStartMode.None))
                {
                    long eyeLocatorNewTickCount = Environment.TickCount;

                    if (eyeLocatorNewTickCount - eyeLocatorTickCount > 10000)
                    {
                        if (!autoStartEnded)
                        {
                            trackingSuiteAdapter.SendMessage("Please Blink");
                            autoStartEnded = true;
                        }

                        eyeLocator.AddImage(frame);
                        if (eyeLocator.TrackingPointsFound)
                        {
                            CvPoint2D32f p = new CvPoint2D32f();

                            if (autoStartMode.Equals(AutoStartMode.LeftEye))
                            {
                                p = eyeLocator.LeftEyeTrackingPoint;
                            }
                            else if (autoStartMode.Equals(AutoStartMode.RightEye))
                            {
                                p = eyeLocator.RightEyeTrackingPoint;
                            }
                            else if (autoStartMode.Equals(AutoStartMode.NoseMouth))
                            {
                                p = eyeLocator.MouseTrackingPoint;
                            }

                            eyeLocator.Reset();

                            imagePoint.X = (int)p.x;
                            imagePoint.Y = (int)p.y;
                            _current_track_points[0].x = p.x;
                            _current_track_points[0].y = p.y;
                            _last_track_points[0].x    = p.x;
                            _last_track_points[0].y    = p.y;

                            validTrackPoint = true;
                            trackingSuiteAdapter.ToggleSetup(false);
                            trackingSuiteAdapter.ToggleControl(true);
                        }
                    }
                    else
                    {
                        autoStartEnded = false;
                        int second = (int)Math.Round(((double)(10000 - (eyeLocatorNewTickCount - eyeLocatorTickCount))) / 1000.0);
                        trackingSuiteAdapter.SendMessage("Auto Start in " + second + " seconds");
                    }
                }
            }
        }