/// <summary>
        /// Calibrates the Screen Based Tobii Eye Tracker using a Windows Form
        /// Based on http://devtobiipro.azurewebsites.net/tobii.research/dotnet/reference/1.7.2.7-alpha-g369155c3/class_tobii_1_1_research_1_1_screen_based_calibration.html
        /// </summary>
        /// <returns></returns>
        public override async Task <ApiResponseData> CalibrateEyeTrackerAsync()
        {
            //EyeTracker == null ? new ApiResponseData { Message = "No connection to an eye tracker has been established. Connect to an eye tracker first." } : null;

            throw new NotImplementedException();
            var calibration = new ScreenBasedCalibration(EyeTracker);
            await calibration.EnterCalibrationModeAsync();

            // Define the points on screen we should calibrate at.
            // The coordinates are normalized, i.e. (0.0f, 0.0f) is the upper left corner and (1.0f, 1.0f) is the lower right corner.
            var pointsToCalibrate = new NormalizedPoint2D[] {
                new NormalizedPoint2D(0.5f, 0.5f),
                new NormalizedPoint2D(0.1f, 0.1f),
                new NormalizedPoint2D(0.1f, 0.9f),
                new NormalizedPoint2D(0.9f, 0.1f),
                new NormalizedPoint2D(0.9f, 0.9f),
            };

            // Display Windows Form for Calibration Drawing
            CalibrationForm calibrationForm = new CalibrationForm();

            calibrationForm.Show();

            System.Threading.Thread.Sleep(3000);

            // Get screen resolution
            Screen myScreen   = Screen.FromControl(calibrationForm);
            float  screenResX = myScreen.WorkingArea.Width;
            float  screenResY = myScreen.WorkingArea.Height;

            // Collect data.
            foreach (var point in pointsToCalibrate)
            {
                // Get screen coodrinates from normalized coordinates
                float x = point.X * screenResX;
                float y = point.Y * screenResY;

                // Show an image on screen where you want to calibrate.
                Console.WriteLine("Show point on screen from UI thread at ({0}, {1})", x, y);

                calibrationForm.Paint += (sender, e) =>
                {
                    e.Graphics.DrawEllipse(Pens.Red, x, y, 100, 100);
                };

                // Wait a little for user to focus.
                System.Threading.Thread.Sleep(700);
                // Collect data.
                CalibrationStatus status = await calibration.CollectDataAsync(point);

                if (status != CalibrationStatus.Success)
                {
                    // Try again if it didn't go well the first time.
                    // Not all eye tracker models will fail at this point, but instead fail on ComputeAndApplyAsync.
                    await calibration.CollectDataAsync(point);
                }
            }
            // Compute and apply the calibration.
            CalibrationResult calibrationResult = await calibration.ComputeAndApplyAsync();

            Console.WriteLine("Compute and apply returned {0} and collected at {1} points.",
                              calibrationResult.Status, calibrationResult.CalibrationPoints.Count);
            // Analyze the data and maybe remove points that weren't good.
            calibration.DiscardData(new NormalizedPoint2D(0.1f, 0.1f));
            // Redo collection at the discarded point.
            Console.WriteLine("Show point on screen from UI thread at ({0}, {1})", 0.1f, 0.1f);
            await calibration.CollectDataAsync(new NormalizedPoint2D(0.1f, 0.1f));

            // Compute and apply again.
            calibrationResult = await calibration.ComputeAndApplyAsync();

            Console.WriteLine("Second compute and apply returned {0} and collected at {1} points.",
                              calibrationResult.Status, calibrationResult.CalibrationPoints.Count);
            // See that you're happy with the result.
            // The calibration is done. Leave calibration mode.
            await calibration.LeaveCalibrationModeAsync();
        }
Exemple #2
0
        private async void Calibrate()
        {
            ClearSurface();
            // Create a calibration object.
            var calibration = new ScreenBasedCalibration(_tracker);
            // Enter calibration mode.
            await calibration.EnterCalibrationModeAsync();

            // Define the points on screen we should calibrate at.
            // The coordinates are normalized, i.e. (0.0f, 0.0f) is the upper left corner and (1.0f, 1.0f) is the lower right corner.
            var pointsToCalibrate = new[]
            {
                new NormalizedPoint2D(0.5f, 0.5f),
                new NormalizedPoint2D(0.2f, 0.5f),
                new NormalizedPoint2D(0.8f, 0.5f),
                new NormalizedPoint2D(0.5f, 0.2f),
                new NormalizedPoint2D(0.5f, 0.8f),
            };

            // Collect data.
            foreach (var point in pointsToCalibrate)
            {
                // Show an image on screen where you want to calibrate.

                DrawCircle(ToPoint(point));
                // Wait a little for user to focus.
                System.Threading.Thread.Sleep(1000);
                // Collect data.
                var status = await calibration.CollectDataAsync(point);

                if (status != CalibrationStatus.Success)
                {
                    // Try again if it didn't go well the first time.
                    // Not all eye tracker models will fail at this point, but instead fail on ComputeAndApply.
                    await calibration.CollectDataAsync(point);
                }

                ClearSurface();
            }

            // Compute and apply the calibration.
            var calibrationResult = await calibration.ComputeAndApplyAsync();

            var drawErrorCount = 0;

            foreach (var point in calibrationResult.CalibrationPoints)
            {
                var pointPosition = ToPoint(point.PositionOnDisplayArea);
                DrawCircle(pointPosition, _grayBrush);
                foreach (var sample in point.CalibrationSamples)
                {
                    var leftPosition  = sample.LeftEye.PositionOnDisplayArea;
                    var rightPosition = sample.RightEye.PositionOnDisplayArea;
                    var leftValidity  = sample.LeftEye.Validity == CalibrationEyeValidity.ValidAndUsed;
                    var rightValidity = sample.RightEye.Validity == CalibrationEyeValidity.ValidAndUsed;
                    var leftPoint     = ToPoint(leftPosition);
                    var rightPoint    = ToPoint(rightPosition);

                    if (double.IsNaN(leftPoint.X) || double.IsNaN(leftPoint.Y))
                    {
                        drawErrorCount++;
                    }
                    else
                    {
                        DrawLine(pointPosition, leftPoint, leftValidity ? _blueBrush : _redBrush);
                    }

                    if (double.IsNaN(rightPoint.X) || double.IsNaN(rightPoint.Y))
                    {
                        drawErrorCount++;
                    }
                    else
                    {
                        DrawLine(pointPosition, rightPoint, rightValidity ? _greenBrush : _redBrush);
                    }
                }
            }

            LegendPanel.Visibility = Visibility.Visible;
            _status = Status.CalibrationCompleted;
            MessageLabel.Content    = "Калибровка завершена. Нажмите \"Пробел\" для визуализации";
            MessageLabel.Visibility = Visibility.Visible;

            if (drawErrorCount > 0)
            {
                MessageBox.Show($"{drawErrorCount} points were not been drawn");
            }

            await calibration.LeaveCalibrationModeAsync();
        }