Exemple #1
0
            // MonoBehaviour methods

            /// <summary>
            /// Calls the <see cref="Calibrated"/> event when a calibration has just completed.
            /// </summary>
            protected virtual void LateUpdate()
            {
                Exception e = null;
                bool      calibrationDone = false;

                calibratingMutex.WaitOne();
                {
                    e = calibratingException;
                    calibratingException = null;

                    calibrationDone = CalibrationRunning && IsCalibrated;
                }
                calibratingMutex.ReleaseMutex();

                // Check for exception in calibrating thread
                if (e != null)
                {
                    calibratingThread.Abort();
                    CalibrationRunning = false;
                    throw e;
                }

                // Check for calibration done
                if (calibrationDone)
                {
                    CalibrationRunning = false;
                    Calibrated.Invoke();
                }
            }
Exemple #2
0
        public void Stop()
        {
            IsStarted     = false;
            IsCalibrating = false;
            if (calibTask != null)
            {
                tokenSource.Cancel(false);

                calibTask.Wait();

                tokenSource.Dispose();

                tokenSource = null;
                calibTask   = null;

                Calibrated?.Invoke(this, null);
            }
        }
 public void CalibrationComplete() => Calibrated?.Invoke(this, EventArgs.Empty);
Exemple #4
0
        private void CalibProc(ScreenProperties screen, bool train)
        {
            var token = tokenSource.Token;

            if (token.IsCancellationRequested)
            {
                return;
            }

            var labelResultDict = new Dictionary <Point3D, CalibratingPushData>();
            var calibed         = new bool[GridWidth * GridHeight];

            CalibrateBegin?.Invoke(this, null);

            for (int i = 0; i < calibed.Length; i++)
            {
                if (token.IsCancellationRequested)
                {
                    return;
                }

                int targetIndex = 1;
                while (true)
                {
                    var ind = Random.R.NextInt(0, calibed.Length);
                    ind = Math.Min(ind, calibed.Length - 1);
                    if (!calibed[ind])
                    {
                        targetIndex = ind;
                        break;
                    }
                }

                double x = (double)(targetIndex % GridWidth) / (GridWidth - 1);
                x = x * screen.PixelSize.Width;

                double y = Math.Floor((double)targetIndex / GridWidth) / (GridHeight - 1);
                y = y * screen.PixelSize.Height;

                var targetPoint  = new Point(x, y);
                var calibPercent = (double)i / calibed.Length;

                Calibarting.Invoke(this, new CalibratingArgs(CalibratingState.Point, targetPoint, calibPercent));
                Task.Delay((int)Interval).Wait();
                if (token.IsCancellationRequested)
                {
                    return;
                }

                Calibarting.Invoke(this, new CalibratingArgs(CalibratingState.Wait, targetPoint, calibPercent));
                Task.Delay((int)WaitInterval).Wait();
                if (token.IsCancellationRequested)
                {
                    return;
                }

                for (int sampling = 0; sampling < SampleCount; sampling++)
                {
                    var samplePercent = calibPercent + ((double)(sampling + 1) / SampleCount) * (1.0 / calibed.Length);

                    Calibarting.Invoke(this, new CalibratingArgs(CalibratingState.SampleWait, targetPoint, samplePercent));
                    Task.Delay((int)SampleWaitInterval).Wait();
                    if (token.IsCancellationRequested)
                    {
                        return;
                    }

                    if (lastData == null || lastData.Face.GazeInfo == null)
                    {
                        Logger.Error("Data is not sented... Maybe machine is too slow.");
                        while (lastData == null || lastData.Face.GazeInfo == null)
                        {
                            if (token.IsCancellationRequested)
                            {
                                return;
                            }
                            Task.Delay(500).Wait();
                            Logger.Error("Gaze is not captured");
                        }
                    }

                    Calibarting.Invoke(this, new CalibratingArgs(CalibratingState.Sample, targetPoint, samplePercent));
                    Task.Delay((int)SampleInterval).Wait();
                    if (token.IsCancellationRequested)
                    {
                        return;
                    }

                    var targetVec = lastData.Face.SolveLookScreenVector(targetPoint, screen);
                    labelResultDict.Add(targetVec, lastData);
                    Logger.Log(this, $"Calibrating {targetPoint} ({i + 1}/{calibed.Length}) [{sampling + 1}/{SampleCount}] - {targetVec} : {lastData.Face.GazeInfo.Vector}");
                }

                calibed[targetIndex] = true;
            }

            Engine.SetData(labelResultDict);
            if (train)
            {
                Engine.Train();
            }
            IsCalibrating = false;

            Logger.Log(this, "Calibrated");
            Calibrated?.Invoke(this, new CalibratedArgs(labelResultDict, token));
            IsStarted = false;
        }