public void saveDynamicUserProfiles(ElicitationSession session, List<UserProfile> userProfiles)
 {
     foreach (UserProfile user in userProfiles)
     {
         _csvExport.AddRow();
         _csvExport["SessionID"] = session.sessionID;
         _csvExport["Start Time"] = session.startTime;
         _csvExport["End Time"] = session.endTime;
         _csvExport["User ID"] = user.userID;
         _csvExport["Is User Sleeping"] = user.userSleeping;
         _csvExport["Is User Distracted"] = user.userDistracted;
     }
 }
 public void saveStaticUserProfiles(ElicitationSession session, List<UserProfile> userProfiles)
 {
     foreach (UserProfile user in userProfiles)
     {
         _csvExport.AddRow();
         _csvExport["SessionID"] = session.sessionID;
         _csvExport["Start Time"] = session.startTime;
         _csvExport["End Time"] = session.endTime;
         _csvExport["# of Users"] = userProfiles.Count;
         _csvExport["User ID"] = user.userID;
         _csvExport["Gender"] = user.gender;
         _csvExport["Is Child"] = user.userIsChild;
     }
 }
        private void OnAllFramesReady(object sender, AllFramesReadyEventArgs allFramesReadyEventArgs)
        {
            sessionEndTime = DateTime.Now;
            if (sessionEndTime.Subtract(sessionStartTime).TotalSeconds > sessionLength)
            {
                //publish data for the session
                session.startTime = sessionStartTime;
                session.endTime = sessionEndTime;
                this.dynamicRC.saveDynamicUserProfiles(session, new List<UserProfile>(userProfiles.Values));
                this.staticRC.saveStaticUserProfiles(session, new List<UserProfile>(userProfiles.Values));
                if (contentCounter == 3)
                {
                    this.dynamicRC.ExportToFile("DynamicData_" + DateTime.Now.ToString("dd_MM_yyyy_hh_mm_ss") + ".csv");
                    this.staticRC.ExportToFile("StaticData_" + DateTime.Now.ToString("dd_MM_yyyy_hh_mm_ss") + ".csv");
                    dynamicRC = new ResultCreator();
                    staticRC = new ResultCreator();
                    contentCounter = 0;
                }
                //clear old variables
                userProfiles = null;
                userTrackStatus = null;
                totalDistractiony = new Dictionary<int, float>();
                userTrackStatus = new Dictionary<int, Boolean>();
                userProfiles = new Dictionary<int, UserProfile>();
                rotationOldy = new Dictionary<int, float>();
                headSD = new Dictionary<int, double>();
                headSDDataset = new Dictionary<int, List<float>>();
                //start new session
                session = null;
                sessionStartTime = sessionEndTime;
                genderdetectionCount = 0;

            }
            else
            {
                //assign current data to the session
                List<UserProfile> sessionUsers;
                sessionUsers = new List<UserProfile>();

                if (session == null)
                {
                    contentCounter++;

                    //foreach (var item in userProfiles.Values)
                    //{
                    //    sessionUsers.Add(item);
                    //}
                    //session = new ElicitationSession(sessionUsers, contentCounter);
                    session = new ElicitationSession();
                }

                foreach (var item in userProfiles.Values)
                {
                    sessionUsers.Add(item);
                }
                session.setUsers(sessionUsers);
            }

            ColorImageFrame colorImageFrame = null;
            DepthImageFrame depthImageFrame = null;
            SkeletonFrame skeletonFrame = null;

            try
            {
                colorImageFrame = allFramesReadyEventArgs.OpenColorImageFrame();
                depthImageFrame = allFramesReadyEventArgs.OpenDepthImageFrame();
                skeletonFrame = allFramesReadyEventArgs.OpenSkeletonFrame();

                if (colorImageFrame == null || depthImageFrame == null || skeletonFrame == null)
                {
                    return;
                }

                // Check for image format changes.  The FaceTracker doesn't
                // deal with that so we need to reset.
                if (this.depthImageFormat != depthImageFrame.Format)
                {
                    this.ResetFaceTracking();
                    this.depthImage = null;
                    this.depthImageFormat = depthImageFrame.Format;
                }

                if (this.colorImageFormat != colorImageFrame.Format)
                {
                    this.ResetFaceTracking();
                    this.colorImage = null;
                    this.colorImageFormat = colorImageFrame.Format;
                }

                // Create any buffers to store copies of the data we work with
                if (this.depthImage == null)
                {
                    this.depthImage = new short[depthImageFrame.PixelDataLength];
                }

                if (this.colorImage == null)
                {
                    this.colorImage = new byte[colorImageFrame.PixelDataLength];
                }

                // Get the skeleton information
                if (this.skeletonData == null || this.skeletonData.Length != skeletonFrame.SkeletonArrayLength)
                {
                    this.skeletonData = new Skeleton[skeletonFrame.SkeletonArrayLength];
                }

                colorImageFrame.CopyPixelDataTo(this.colorImage);
                depthImageFrame.CopyPixelDataTo(this.depthImage);
                skeletonFrame.CopySkeletonDataTo(this.skeletonData);
                if (!this.Kinect.SkeletonStream.AppChoosesSkeletons)
                {
                    //this.Kinect.SkeletonStream.AppChoosesSkeletons = true; // Ensure AppChoosesSkeletons is set
                }

                //int count = 0;
                //foreach (Skeleton skeleton in this.skeletonData)
                //{
                //    if (skeleton.TrackingState == SkeletonTrackingState.Tracked
                //       || skeleton.TrackingState == SkeletonTrackingState.PositionOnly)
                //    {
                //        count++;
                //    }
                //}
                //if (count > 2)
                //{
                //    count++;
                //}

                foreach (Skeleton skeleton in this.skeletonData)
                {
                    if (skeleton.TrackingState == SkeletonTrackingState.Tracked
                       || skeleton.TrackingState == SkeletonTrackingState.PositionOnly)
                    {
                        if (!userProfiles.ContainsKey(skeleton.TrackingId))
                        {
                            UserProfile user = new UserProfile();
                            userProfiles.Add(skeleton.TrackingId, user);
                            user.userID = skeleton.TrackingId;
                            userTrackStatus.Add(skeleton.TrackingId, false);
                            totalDistractiony.Add(skeleton.TrackingId, 0);
                            rotationOldy.Add(skeleton.TrackingId, 0);
                            headSD.Add(skeleton.TrackingId, 0);
                            headSDDataset.Add(skeleton.TrackingId, new List<float>());

                        }
                        if (!childDetected.ContainsKey(skeleton.TrackingId))
                        {
                            childDetected.Add(skeleton.TrackingId, true);
                        }
                    }
                }

                foreach (int userID in userProfiles.Keys)
                {
                    Skeleton skeleton = getSkeletonForID(userID, skeletonData);

                    Boolean tempBool = userTrackStatus[userID];
                    // Boolean tempBool = userTrackStatus.TryGetValue(userID, out tempBool);

                    /*if (skeleton.TrackingState != SkeletonTrackingState.Tracked && !tempBool)
                    {
                        this.Kinect.SkeletonStream.ChooseSkeletons(userID);
                        userTrackStatus.Remove(userID);
                        userTrackStatus.Add(skeleton.TrackingId, true);
                        break;

                    }

                    userTrackStatus.Remove(userID);
                    userTrackStatus.Add(skeleton.TrackingId, false);*/
                    if (skeleton != null)
                    {

                        if (skeleton.TrackingState == SkeletonTrackingState.Tracked)
                        {

                            userTrackStatus[skeleton.TrackingId] = true;
                            //Debug.WriteLine("tracked Skeleton Id:" + skeleton.TrackingId);
                            // We want keep a record of any skeleton, tracked or untracked.
                            if (!this.trackedSkeletons.ContainsKey(skeleton.TrackingId))
                            {
                                this.trackedSkeletons.Add(skeleton.TrackingId, new SkeletonFaceTracker());

                            }

                            ////Sleeper Detection/*

                            if (!skeletonPositions.ContainsKey(skeleton.TrackingId))
                            {
                                if (skeleton.Joints[JointType.ShoulderLeft].TrackingState == JointTrackingState.Tracked & skeleton.Joints[JointType.ShoulderRight].TrackingState == JointTrackingState.Tracked
                                    & skeleton.Joints[JointType.Head].TrackingState == JointTrackingState.Tracked & skeleton.Joints[JointType.HandLeft].TrackingState == JointTrackingState.Tracked
                                    & skeleton.Joints[JointType.HandRight].TrackingState == JointTrackingState.Tracked)
                                {
                                    skeletonPositions.Add(skeleton.TrackingId, new SkeletonPosition(skeleton, Kinect));
                                }
                            }
                            else
                            {
                                if (skeletonPositions[skeleton.TrackingId].timeElapsed().TotalSeconds > sessionLength / 2)
                                {
                                    if (skeleton.Joints[JointType.ShoulderLeft].TrackingState != JointTrackingState.NotTracked
                                        & skeleton.Joints[JointType.ShoulderRight].TrackingState != JointTrackingState.NotTracked
                                        & skeleton.Joints[JointType.Head].TrackingState != JointTrackingState.NotTracked
                                        & skeleton.Joints[JointType.HandLeft].TrackingState != JointTrackingState.NotTracked
                                        & skeleton.Joints[JointType.HandRight].TrackingState != JointTrackingState.NotTracked)
                                    {
                                        userProfiles[skeleton.TrackingId].userSleeping = skeletonPositions[skeleton.TrackingId].userSleeping(new SkeletonPosition(skeleton, Kinect));
                                        skeletonPositions[skeleton.TrackingId] = new SkeletonPosition(skeleton, Kinect);
                                    }
                                }
                            }

                            //gender detection
                            if (DateTime.Now.Subtract(sessionStartTime).TotalSeconds >= sessionLength / 5 * genderdetectionCount && genderdetectionCount < 5)
                            {
                                genderdetectionCount++;

                                int gender = genderDetector.detectThroughKinect(Kinect, colorImageFrame, skeleton);
                                UserProfile user = userProfiles[skeleton.TrackingId];
                                if (gender == 0)
                                    user.gender = "Male";
                                else if (gender == 1)
                                    user.gender = "Female";
                                else
                                    user.gender = "Unknown";
                            }
                            //child detection

                            /*UserProfile tempUserProfile = new UserProfile();
                            userProfiles.TryGetValue(skeleton.TrackingId, out tempUserProfile);
                            tempUserProfile.userIsChild = childDetector.ChildOrNot(skeleton);*/

                            if (childDetected[skeleton.TrackingId])
                            {
                                userProfiles[skeleton.TrackingId].userIsChild = childDetector.ChildOrNot(skeleton);
                                if (!userProfiles[skeleton.TrackingId].userIsChild)
                                {
                                    childDetected[skeleton.TrackingId] = false;
                                }
                            }
                            else
                            {
                                userProfiles[skeleton.TrackingId].userIsChild = false;
                            }

                            // Give each tracker the upated frame.
                            SkeletonFaceTracker skeletonFaceTracker;
                            if (this.trackedSkeletons.TryGetValue(skeleton.TrackingId, out skeletonFaceTracker))
                            {
                                skeletonFaceTracker.OnFrameReady(this.Kinect, colorImageFormat, colorImage, depthImageFormat, depthImage, skeleton);
                                skeletonFaceTracker.LastTrackedFrame = skeletonFrame.FrameNumber;
                                if (skeletonFaceTracker.faceTracked)
                                {
                                    if (Math.Abs(skeletonFaceTracker.rotationNew - rotationOldy[skeleton.TrackingId]) > 1.5)
                                    {
                                        /* totalDistractiony[skeleton.TrackingId] = totalDistractiony[skeleton.TrackingId] + Math.Abs(skeletonFaceTracker.rotationNew - rotationOldy[skeleton.TrackingId]);
                                         rotationOldy[skeleton.TrackingId] = skeletonFaceTracker.rotationNew;
                                         skeletonFaceTracker.faceTracked = false;*/
                                        //totalDistractiony = skeletonFaceTracker.rotationNew;

                                        headSDDataset[skeleton.TrackingId].Add(Math.Abs(skeletonFaceTracker.rotationNew
                                            - rotationOldy[skeleton.TrackingId]));
                                        headSD[skeleton.TrackingId] = CalculateSD(headSDDataset[skeleton.TrackingId]);
                                        rotationOldy[skeleton.TrackingId] = skeletonFaceTracker.rotationNew;
                                        skeletonFaceTracker.faceTracked = false;

                                    }
                                    currentTime = DateTime.Now;
                                    if (currentTime.Subtract(startTime).TotalSeconds > sessionLength / 2)
                                    {
                                        //Debug.WriteLine("Head SD :" + headSD[skeleton.TrackingId]);
                                        if (userProfiles.ContainsKey(skeleton.TrackingId) && headSD[skeleton.TrackingId] > 5)
                                        {

                                            /*userDistractionStatus.Remove(skeleton.TrackingId);
                                            userDistractionStatus.Add(skeleton.TrackingId, true);
                                            UserProfile tempUser = new UserProfile();
                                            userProfiles.TryGetValue(skeleton.TrackingId, out tempUser);
                                            tempUser.userDistracted = true;*/

                                            userProfiles[skeleton.TrackingId].userDistracted = true;
                                        }
                                        startTime = currentTime;
                                    }

                                }

                            }
                        }
                    }
                }

                this.RemoveOldTrackers(skeletonFrame.FrameNumber);

                this.InvalidateVisual();
            }
            finally
            {
                if (colorImageFrame != null)
                {
                    colorImageFrame.Dispose();
                }

                if (depthImageFrame != null)
                {
                    depthImageFrame.Dispose();
                }

                if (skeletonFrame != null)
                {
                    skeletonFrame.Dispose();
                }

                bool allDone = true;
                foreach (var userID in userTrackStatus.Keys)
                {
                    bool temp = userTrackStatus[userID];
                    //  userTrackStatus.TryGetValue(userID, out temp);
                    if (!temp)
                    {
                        allDone = false;
                        break;
                    }
                }
                if (allDone)
                {
                    List<int> userIDList = new List<int>(userTrackStatus.Keys);

                    foreach (var userID in userIDList)
                    {
                        userTrackStatus[userID] = false;
                    }
                    int tempUserID = 0;
                    foreach (var userID in userTrackStatus.Keys)
                    {
                        bool temp = userTrackStatus[userID];
                        //userTrackStatus.TryGetValue(userID, out temp);
                        if (!temp)
                        {
                            this.Kinect.SkeletonStream.ChooseSkeletons(userID);
                            tempUserID = userID;
                            break;
                        }
                    }
                    userTrackStatus[tempUserID] = true;
                }
                else
                {
                    int tempUserID = 0;
                    foreach (var userID in userTrackStatus.Keys)
                    {
                        bool temp = userTrackStatus[userID];
                        //userTrackStatus.TryGetValue(userID, out temp);
                        if (!temp)
                        {
                            this.Kinect.SkeletonStream.ChooseSkeletons(userID);
                            tempUserID = userID;
                            break;
                        }
                    }
                    userTrackStatus[tempUserID] = true;

                }
               /* if (trackingModeSwitch)
                {
                    this.Kinect.SkeletonStream.TrackingMode = SkeletonTrackingMode.Seated;
                    trackingModeSwitch = false;
                }
                else
                {
                    this.Kinect.SkeletonStream.TrackingMode = SkeletonTrackingMode.Default;
                    trackingModeSwitch = true;
                }*/

            }
        }