Skip to main content

Get data into a Json file using Unity

Comments

8 comments

  • Official comment
    Sergi Tortosa

    Hello Almudena,

    The best way will be to output directly to your desired format. For accumulating the data you have two options:

    • You can subscribe to the events and accumulate when you receive the message. You have the base code on how to do this here.  
    • Update: you will need the reference to the glia behavior (same as with the above option) and call the function GetLast{Sensor}();

    Note that on Update you are defining your own sampling rate and with delegates, you are getting the frequency of the sensor.

    To save this data you also have two options: 

    [System.Serializable]
    private struct MyAppHR{
           public List<uint> rateValues;
    }

    private MyAppHR myAppHR;

    A quick example of how this will work altogether: 

    using System.Collections.Generic;
    using UnityEngine;
    using HP.Omnicept.Unity;
    using HP.Omnicept.Messaging.Messages;
    using System.IO;

    public class WriteHRToJSON : MonoBehaviour {
    [System.Serializable]
    private struct MyAppHR
    {
    public List<uint> rateValues;
    }

    public string filename = "HR.json";
    private MyAppHR myAppHR;

    private GliaBehaviour _gliaBehaviour;
    private GliaBehaviour m_gliaBehaviour {
    get{
    if(_gliaBehaviour == null){
    _gliaBehaviour = FindObjectOfType<GliaBehaviour>();
    }

    return _gliaBehaviour;
    }
    }
    private string savePath {
    get{
    return $"{Application.persistentDataPath}/{filename}";
    }
    }


    #region Unity LifeCycle
    private void Start() {
    myAppHR = new MyAppHR{ rateValues = new List<uint>()};
    }

    private void OnEnable() {
    m_gliaBehaviour.OnHeartRate.AddListener(HandleHR);
    }

    private void OnDisable() {
    m_gliaBehaviour.OnHeartRate.RemoveListener(HandleHR);
    }

    private void Update()
    {
    if (Input.GetKeyDown(KeyCode.S))
    {
    SaveToJson();
    }
    }
    #endregion

    private void HandleHR(HeartRate hr)
    {
    if (hr != null)
    {
    myAppHR.rateValues.Add(hr.Rate);
    }
    }

    private void SaveToJson()
    {
    string json = JsonUtility.ToJson(myAppHR, true);
    File.WriteAllText(savePath, json);
    Debug.Log($"Saved values at: {savePath}");
    }
    }
  • Almudena Palacios-Ibáñez

    Hi Sergi,

    Thanks so much for your rapid response! It is a pleasure to have professionals like you to help.

    Thank you!

    1
  • Sergi Tortosa

    Thank you for asking great questions on the forum! Do not hesitate to reach us with any other issues or questions  :)

    1
  • Almudena Palacios-Ibáñez

    Great! Just say that your script works perfectly, and I was able to modify it in order to have another data into my json file (as pupil diameter).

    I had a question about the frequency rate of the sensor... is there a site to see which frequency of update does every sensor have for the physiological measures? I've seen that in a few seconds a get more pupil size data than heart rate.

    Thank you again!

    0
  • Sergi Tortosa

    Yes, you are right, we have the fundamentals page which contains information on the frequency of the different sensors. 

    See that eye-tracking is reported at a 120HZ rate. Which will give you approximately 2 samples per frame assuming you are at 60 fps. And for HR you have 1 sample every 5 seconds. 

    One option here for you will be saving a timestamp with the values so you later can put them in the right order/time despite them having different frequencies. 

    Hope this helps :)

    0
  • Almudena Palacios-Ibáñez

    Hello again Sergi,

    I was trying to export the eye gaze coordinates into a json file. I used the same script as the one used for seeing the heart rate, but I am not able to access the combined eye tracking data. I am seeing this website: https://developers.hp.com/omnicept/docs/unity/sensors#eyeTracking, but I don't understand how to export the combinedgaze data.

    I modified the script this way:

    But, as you see, gd.CombinedGaze is underlined red.

    Thank you,

    Almudena.

    0
  • Almudena Palacios-Ibáñez

    This is the whole script:

    using System.Collections.Generic;
    using UnityEngine;
    using HP.Omnicept.Unity;
    using HP.Omnicept.Messaging.Messages;
    using System.IO;

    public class WriteGazeData : MonoBehaviour
    {
        [System.Serializable]
        private struct MyAppGD
        {
            public List<uint> rateValues;
        }

        public string filename = "GazeData_FirstExperiment.json";
        private MyAppGD myAppGD;
        private GliaBehaviour _gliaBehaviour;
        private GliaBehaviour m_gliaBehaviour
        {
            get
            {
                if (_gliaBehaviour == null)
                {
                    _gliaBehaviour = FindObjectOfType<GliaBehaviour>();
                }

                return _gliaBehaviour;
            }
        }
        private string savePath
        {
            get
            {
                return $"{Application.streamingAssetsPath}/{filename}";
            }
        }

        #region Unity LifeCycle
        private void Start()
        {
            myAppGD = new MyAppGD { rateValues = new List<uint>() };
        }

        private void OnEnable()
        {
            m_gliaBehaviour.OnEyeTracking.AddListener(HandleGD);
        }

        private void OnDisable()
        {
            m_gliaBehaviour.OnEyeTracking.RemoveListener(HandleGD);
        }

        private void Update()
        {
            
        }
        #endregion

        private void HandleGD(EyeTracking gd)
        {
            if (gd != null)
            {
                myAppGD.rateValues.Add(gd.CombinedGaze));
            }
        }

        private void SaveToJson()
        {
            string json = JsonUtility.ToJson(myAppGD, true);
            File.WriteAllText(savePath, json);
            Debug.Log($"Saved values at: {savePath}");
        }
    }
    0
  • Almudena Palacios-Ibáñez

    Hello again,

    I modified the script and I got the data into the json file with x, y and z as separated columns, but it seems that the headset refresh rate turns slow, I mean, it's impossible to run the application... any ideas of what is going on?

    using System.Collections.Generic;
    using UnityEngine;
    using HP.Omnicept.Unity;
    using HP.Omnicept.Messaging.Messages;
    using System.IO;

    public class WriteGazeData : MonoBehaviour
    {
        [System.Serializable]
        private struct MyAppGD
        {
            public List<Vector3> rateValues;
        }

        public float timer;
        public bool dataExported;

        public string filename = "GazeData_FirstExperiment.json";
        private MyAppGD myAppGD;
        private GliaBehaviour _gliaBehaviour;
        private GliaBehaviour m_gliaBehaviour
        {
            get
            {
                if (_gliaBehaviour == null)
                {
                    _gliaBehaviour = FindObjectOfType<GliaBehaviour>();
                }

                return _gliaBehaviour;
            }
        }
        private string savePath
        {
            get
            {
                return $"{Application.streamingAssetsPath}/{filename}";
            }
        }

        #region Unity LifeCycle

        private void OnEnable()
        {
            m_gliaBehaviour.OnEyeTracking.AddListener(HandleGD);
        }

        private void OnDisable()
        {
            m_gliaBehaviour.OnEyeTracking.RemoveListener(HandleGD);
        }

        private void Start()
        {
            myAppGD = new MyAppGD { rateValues = new List<Vector3>() };
            dataExported = false;
        }

        private void Update()
        {
            timer -= Time.deltaTime;

            if (timer < 0 && dataExported == false)
            {
                Debug.Log("Data was exported");
                SaveToJson();
                dataExported = true;
            }
        }
        #endregion

        private void HandleGD(EyeTracking gd)
        {
            if (gd != null)
            {
                myAppGD.rateValues.Add(new Vector3 (gd.CombinedGaze.X, gd.CombinedGaze.Y, gd.CombinedGaze.Z));
            }
        }

        private void SaveToJson()
        {
            string json = JsonUtility.ToJson(myAppGD, true);
            File.WriteAllText(savePath, json);
            Debug.Log($"Saved values at: {savePath}");
        }
    }
    0

Please sign in to leave a comment.

Powered by Zendesk