Ejemplo n.º 1
0
        static void Main(string[] args)
        {
            int     deviceId = Initialize.QL2Initialize(filename_Password);
            QLError error    = QuickLink2API.QLDevice_Start(deviceId);

            if (error != QLError.QL_ERROR_OK)
            {
                System.Console.WriteLine("Device not started successfully!");
                System.Console.ReadLine();
                return;
            }

            System.Console.WriteLine("Press any key to begin calibration.");
            Console.ReadKey(true);

            //Calibrate the device
            int calibrationId = 0;

            if (Calibrate.AutoCalibrate(deviceId, QLCalibrationType.QL_CALIBRATION_TYPE_16, ref calibrationId))
            {
                System.Console.WriteLine("\n\nPress \'q\' to quit. \n");

                // If the calibration was successful then apply the calibration to the device.
                QuickLink2API.QLDevice_ApplyCalibration(deviceId, calibrationId);

                // Display the gaze information until the user quits.
                QLFrameData frameData = new QLFrameData();
                while ((!Console.KeyAvailable) || (Console.ReadKey(true).Key != ConsoleKey.Q))
                {
                    QuickLink2API.QLDevice_GetFrame(deviceId, 10000, ref frameData);
                    if (frameData.WeightedGazePoint.Valid)
                    {
                        System.Console.Write("\rX:{0:F}    Y:{1:F}", frameData.WeightedGazePoint.x,
                                             frameData.WeightedGazePoint.y);
                    }
                }
            }
            else
            {
                System.Console.WriteLine("The calibration did not finish successfully!");
                System.Console.Read();
                QuickLink2API.QLDevice_Stop(deviceId);
                return;
            }

            // Stop the device.
            QuickLink2API.QLDevice_Stop(deviceId);

            return;
        }
        public void Test_0360_QLDevice_ApplyCalibration()
        {
            int     calibrationId;
            QLError error = QuickLink2API.QLCalibration_Create(0, out calibrationId);

            Assert.AreEqual(QLError.QL_ERROR_OK, error);

            error = QuickLink2API.QLCalibration_Load(Test_SetUp.Helper.CalibrationFilename, ref calibrationId);
            Assert.AreNotEqual(QLError.QL_ERROR_INVALID_PATH, error, "Calibration file not found.  You may need to run the SetupDevice example first.");
            Assert.AreEqual(QLError.QL_ERROR_OK, error);

            error = QuickLink2API.QLDevice_ApplyCalibration(Test_SetUp.Helper.DeviceId, calibrationId);
            Assert.AreEqual(QLError.QL_ERROR_OK, error);
        }
Ejemplo n.º 3
0
        /// <summary>
        /// <para>
        /// Loads eye tracker device calibration from the location specified by
        /// <see cref="CalibrationFilename"/>, or performs interactive calibration--if calibration cannot
        /// be loaded from the file--and saves it to the calibration file.  Optionally, (when the
        /// <paramref name="promptToRecalibrate"/> parameter is true) this method will prompt the user to
        /// re-perform interactive calibration even when the calibration has successfully been loaded
        /// from the calibration file.
        /// </para>
        /// <para>
        /// If interactive calibration is chosen (via the <paramref name="promptToRecalibrate"/>
        /// parameter being set to true) or required (when no stored calibration file is present), the
        /// user is prompted to choose 5, 9, or 16 point calibration, choose the duration for
        /// display of each target (in milliseconds), and specify the device's distance from the user.
        /// </para>
        /// <para>
        /// During interactive calibration, the user will be given the option to view the live video
        /// stream from the device in order to adjust camera focus and orientation for best results.
        /// </para>
        /// <para>
        /// Interactive calibration may attempt to improve its results by redisplaying some targets
        /// several times immediately after the initial, ordered target sequence has been displayed.
        /// During this improvement process, warning messages may appear on the console; this is normal
        /// and informative only.
        /// </para>
        /// <para>
        /// After a successful interactive calibration procedure, the user will be shown a score and
        /// prompted to apply or discard the results.  The score is the average of the left and right
        /// scores for each target.  Each left and right score for a target is the magnitude of the
        /// distance of the projected gaze location from the center of the target.  In other words, a
        /// lower score is better.
        /// </para>
        /// </summary>
        /// <seealso cref="SetupCalibration()"/>
        /// <param name="promptToRecalibrate">
        /// When true, the user will be asked if they would like to re-perform the eye tracker device's
        /// calibration even when the calibration was successfully loaded from the calibration file.
        /// </param>
        /// <returns>
        /// <para>
        /// If calibration was loaded from file, this method returns true when the calibration has been
        /// successfully applied to the device.
        /// </para>
        /// <para>
        /// If calibration was not loaded from the device, then this method returns true when the
        /// calibration procedure has been completed, the calibration has been applied to the device, the
        /// calibration has been saved to the calibration file, and the eye tracker has been successfully
        /// stopped.
        /// </para>
        /// <para>
        /// If calibration was successfully loaded and applied from the calibration file, but the user
        /// has chosen to re-perform the calibration procedure, then true is returned if the user cancels
        /// the re-calibration before the actual calibration sequence begins, or when the newly performed
        /// calibration has been completed, the newly performed calibration has been applied to the
        /// device, the newly performed calibration has been saved to the calibration file, and the eye
        /// tracker device has been stopped.
        /// </para>
        /// <para>
        /// Otherwise, this method returns false.
        /// </para>
        /// </returns>
        /// <exception cref="DllNotFoundException">
        /// The QuickLink2 DLLs ("QuickLink2.dll," "PGRFlyCapture.dll," and "SMX11MX.dll") must be placed
        /// in the same directory as your program's binary executable; otherwise, this exception will be
        /// thrown.
        /// </exception>
        public bool SetupCalibration(bool promptToRecalibrate)
        {
            int deviceDistance;

            if (!LoadDeviceDistance(this.SettingsFilename, out deviceDistance))
            {
                return(false);
            }

            if (deviceDistance == 0)
            {
                deviceDistance = DefaultDeviceDistance;
                if (!SaveDeviceDistance(this.SettingsFilename, deviceDistance))
                {
                    return(false);
                }
            }

            if (!ApplyDeviceDistance(this.DeviceId, deviceDistance))
            {
                return(false);
            }

            bool calibrationLoadedFromFile = false;

            // Load the calibration out of a file into a new calibration container.
            int     calibrationId = -1;
            QLError error         = QuickLink2API.QLCalibration_Load(this.CalibrationFilename, ref calibrationId);

            if (error == QLError.QL_ERROR_OK)
            {
                // Apply the loaded calibration to the device.
                error = QuickLink2API.QLDevice_ApplyCalibration(this.DeviceId, calibrationId);
                if (error == QLError.QL_ERROR_OK)
                {
                    Console.WriteLine("Calibration loaded from file.");
                    if (!promptToRecalibrate || !PromptToRecalibrate())
                    {
                        // Loaded from file and no prompt for recalibration
                        // requested, or recalibration cancelled.
                        return(true);
                    }
                    calibrationLoadedFromFile = true;
                }
            }

            Console.WriteLine();
            Console.WriteLine("Beginning calibration setup.");

            bool showVideoStream;

            if (!PromptForShowVideoStream(out showVideoStream))
            {
                // Cancelled.
                return(calibrationLoadedFromFile);
            }
            else if (showVideoStream)
            {
                if (!ShowVideoStream())
                {
                    // Error.
                    return(false);
                }
            }

            Console.WriteLine();
            Console.WriteLine("[Calibration Parameters]");
            Console.WriteLine("  Press ENTER to use (default), or enter 'q' to quit.");

            int newDeviceDistance;

            if (!PromptForDeviceDistance(out newDeviceDistance))
            {
                // Cancelled.
                return(calibrationLoadedFromFile);
            }
            else if (newDeviceDistance != deviceDistance)
            {
                deviceDistance = newDeviceDistance;
                if (!SaveDeviceDistance(this.SettingsFilename, deviceDistance))
                {
                    // Error.
                    return(false);
                }
                else if (!ApplyDeviceDistance(this.DeviceId, newDeviceDistance))
                {
                    // Error.
                    return(false);
                }
            }

            QLCalibrationType calibrationType;

            if (!PromptForCalibrationType(out calibrationType))
            {
                // Cancelled.
                return(calibrationLoadedFromFile);
            }

            int targetDuration;

            if (!PromptForTargetDuration(out targetDuration))
            {
                // Cancelled.
                return(calibrationLoadedFromFile);
            }

            Console.WriteLine();
            Console.WriteLine("Beginning calibration.");

            if (!Calibrate(this.DeviceId, this.CalibrationFilename, deviceDistance, calibrationType, targetDuration))
            {
                Console.WriteLine("Calibration failed.");
                return(false);
            }
            else
            {
                Console.WriteLine("Calibration completed, applied, and saved.\n");
                return(true);
            }
        }
Ejemplo n.º 4
0
        private static bool Calibrate(int deviceId, string calibrationFilename, int deviceDistance, QLCalibrationType calibrationType, int targetDuration)
        {
            // Start the device.
            QLError error = QuickLink2API.QLDevice_Start(deviceId);

            if (error != QLError.QL_ERROR_OK)
            {
                Console.WriteLine("QLDevice_Start() returned {0}.", error.ToString());
                return(false);
            }

            bool calibrationSuccessful = false;

            using (CalibrationForm calibrationForm = new CalibrationForm(deviceId, calibrationType, targetDuration))
            {
                if (calibrationForm.Calibrate())
                {
                    // Calculate the total average score.
                    float avg = 0;
                    for (int i = 0; i < calibrationForm.LeftScores.Length; i++)
                    {
                        avg += calibrationForm.LeftScores[i].score;
                        avg += calibrationForm.RightScores[i].score;
                    }
                    avg /= (float)calibrationForm.LeftScores.Length * 2f;

                    Console.WriteLine("Calibration Score: {0}.", avg);

                    while (true)
                    {
                        // Flush the input buffer.
                        while (Console.KeyAvailable)
                        {
                            Console.ReadKey(true);
                        }

                        if (!PromptToApplyCalibration())
                        {
                            Console.WriteLine("Not applying calibration.");
                            calibrationSuccessful = false;
                            break;
                        }
                        else
                        {
                            error = QuickLink2API.QLCalibration_Finalize(calibrationForm.CalibrationId);
                            if (error != QLError.QL_ERROR_OK)
                            {
                                Console.WriteLine("QLCalibration_Finalize() returned {0}.", error.ToString());
                                calibrationSuccessful = false;
                                break;
                            }

                            error = QuickLink2API.QLDevice_ApplyCalibration(deviceId, calibrationForm.CalibrationId);
                            if (error != QLError.QL_ERROR_OK)
                            {
                                Console.WriteLine("QLCalibration_ApplyCalibration() returned {0}", error.ToString());
                                calibrationSuccessful = false;
                                break;
                            }

                            error = QuickLink2API.QLCalibration_Save(calibrationFilename, calibrationForm.CalibrationId);
                            if (error != QLError.QL_ERROR_OK)
                            {
                                Console.WriteLine("QLCalibration_Save() returned {0}", error.ToString());
                                calibrationSuccessful = false;
                                break;
                            }

                            calibrationSuccessful = true;
                            break;
                        }
                    }
                }
            }

            error = QuickLink2API.QLDevice_Stop(deviceId);
            if (error != QLError.QL_ERROR_OK)
            {
                Console.WriteLine("QLDevice_Stop() returned {0}.", error.ToString());
                return(false);
            }

            return(calibrationSuccessful);
        }