Ejemplo n.º 1
0
    void OnPacket(Pupil.GazeMSG data)
    {
        //add new frame
        _gazeFPS++;
        var ct = DateTime.Now;

        if ((ct - _lastT).TotalSeconds > 1)
        {
            _lastT      = ct;
            _currentFps = _gazeFPS;
            _gazeFPS    = 0;
        }

        if (m_status == EStatus.ProcessingGaze)
        { //gaze processing stage
            float x, y;
            x = (float)data.norm_pos[0];
            y = (float)data.norm_pos[1];
            gaze_average.x = (leftEye.gaze.x + rightEye.gaze.x) * 0.5f;
            gaze_average.y = (leftEye.gaze.y + rightEye.gaze.y) * 0.5f;



            if (data.id == 0)
            {
                leftEye.pupil_center = new Vector2((float)data.base_data[0].ellipse.center[0], (float)data.base_data[0].ellipse.center[1]);
                leftEye.gaze         = new Vector2(x, y);
                if (OnEyeGaze != null)
                {
                    OnEyeGaze(this);
                }
            }
            else if (data.id == 1)
            {
                rightEye.pupil_center = new Vector2((float)data.base_data[0].ellipse.center[0], (float)data.base_data[0].ellipse.center[1]);
                rightEye.gaze         = new Vector2(x, y);
                if (OnEyeGaze != null)
                {
                    OnEyeGaze(this);
                }
            }
        }
        else if (m_status == EStatus.Calibration)
        {//gaze calibration stage
            float t    = GetPupilTimestamp();
            var   ref0 = new Dictionary <string, object>()
            {
                { "norm_pos", new float[] { _calibPoints[_currCalibPoint].x, _calibPoints[_currCalibPoint].y } }, { "timestamp", t }, { "id", 0 }
            };
            var ref1 = new Dictionary <string, object>()
            {
                { "norm_pos", new float[] { _calibPoints[_currCalibPoint].x, _calibPoints[_currCalibPoint].y } }, { "timestamp", t }, { "id", 1 }
            };

            _CalibData(_calibPoints[_currCalibPoint].x, _calibPoints[_currCalibPoint].y);

            _calibrationData.Add(ref0);
            _calibrationData.Add(ref1);
            _currCalibSamples++;
            Thread.Sleep(1000 / 60);

            if (_currCalibSamples >= _calibSamples)
            {
                _currCalibSamples = 0;
                _currCalibPoint++;

                string pointsData = "[";
                int    index      = 0;
                foreach (var v in _calibrationData)
                {
                    pointsData += JsonUtility.ToJson(v);//String.Format("{'norm_pos':({0},{1}),'timestamp':{2},'id':{3}}",v.norm_pos[0],v.norm_pos[1],v.timestamp,v.id);
                    ++index;
                    if (index != _calibrationData.Count)
                    {
                        pointsData += ",";
                    }
                }
                pointsData += "]";
                //	pointsData = JsonUtility.ToJson (_CalibrationPoints);
                //Debug.Log (pointsData);

                _sendRequestMessage(new Dictionary <string, object> {
                    { "subject", "calibration.add_ref_data" }, { "ref_data", _CalibrationPoints }
                });
                _calibrationData.Clear();
                if (_currCalibPoint >= _calibPoints.Length)
                {
                    StopCalibration();
                }
            }
        }
    }
Ejemplo n.º 2
0
    void NetMQClient()
    {
        //thanks for Yuta Itoh sample code to connect via NetMQ with Pupil Service
        string IPHeader = ">tcp://" + ServerIP + ":";
        var    timeout  = new System.TimeSpan(0, 0, 1); //1sec

        // Necessary to handle this NetMQ issue on Unity editor
        // https://github.com/zeromq/netmq/issues/526
        AsyncIO.ForceDotNet.Force();
        NetMQConfig.ManualTerminationTakeOver();
        NetMQConfig.ContextCreate(true);

        string subport = "";

        Debug.Log("Connect to the server: " + IPHeader + ServicePort + ".");
        _requestSocket = new RequestSocket(IPHeader + ServicePort);

        _requestSocket.SendFrame("SUB_PORT");
        _isconnected = _requestSocket.TryReceiveFrameString(timeout, out subport);

        _lastT = DateTime.Now;

        if (_isconnected)
        {
            StartProcess();
            var subscriberSocket = new SubscriberSocket(IPHeader + subport);
            subscriberSocket.Subscribe("gaze");    //subscribe for gaze data
            subscriberSocket.Subscribe("notify."); //subscribe for all notifications
            _setStatus(EStatus.ProcessingGaze);
            var msg = new NetMQMessage();
            while (_isDone == false)
            {
                _isconnected = subscriberSocket.TryReceiveMultipartMessage(timeout, ref (msg));
                if (_isconnected)
                {
                    try
                    {
                        string msgType = msg[0].ConvertToString();
                        // Debug.Log(msgType);
                        if (msgType == "gaze")
                        {
                            var message = MsgPack.Unpacking.UnpackObject(msg[1].ToByteArray());
                            MsgPack.MessagePackObject mmap = message.Value;

                            lock (_dataLock)
                            {
                                _gazeMSG = JsonUtility.FromJson <Pupil.GazeMSG>(mmap.ToString());

                                // Debug.Log("----------------------");
                                // Debug.Log(mmap.ToString());
                                // Debug.Log(_gazeMSG.base_data[0].ellipse.center[0] + "," + _gazeMSG.base_data[0].ellipse.center[1]);


                                if (_gazeMSG.confidence > 0.5f)
                                {
                                    OnPacket(_gazeMSG);
                                }
                            }
                        }
                        else
                        {
                            // Debug.Log(msgType);
                            if (msgType == "notify.eye_process.started")
                            {
                                _sendRequestMessage(new Dictionary <string, object> {
                                    { "subject", "set_detection_mapping_mode" }, { "mode", "3d" }
                                });
                            }
                        }
                    }
                    catch
                    {
                        //	Debug.Log("Failed to unpack.");
                    }
                }
                else
                {
                    //	Debug.Log("Failed to receive a message.");
                    Thread.Sleep(500);
                }
            }
            StopProcess();
            subscriberSocket.Close();
        }
        else
        {
            Debug.Log("Failed to connect the server.");
        }
        _requestSocket.Close();
        // Necessary to handle this NetMQ issue on Unity editor
        // https://github.com/zeromq/netmq/issues/526
        Debug.Log("ContextTerminate: no eye gaze connection.");
        NetMQConfig.ContextTerminate();
    }