Esempio n. 1
0
        /// <summary>
        /// Used in "Manual" mode to record a single <c>BodyFrame</c>.
        /// </summary>
        public override void RecordFrame(BodyFrame frame)
        {
            if (!_isStarted)
            {
                throw new InvalidOperationException("Cannot record frames unless the KinectRecorder is started.");
            }

            var time = _dataAccessFacade.GetSceneInUseAccess().GetLocation();

            if (!ReferenceEquals(null, frame) && time.HasValue)
            {
                var replayFrame = new ReplayBodyFrameCustomTime(frame, time.Value);
                if (MapDepthPositions)
                {
                    replayFrame.MapDepthPositions();
                }
                if (MapColorPositions)
                {
                    replayFrame.MapColorPositions();
                }
                _recordQueue.Enqueue(replayFrame);
                System.Diagnostics.Debug.WriteLine("+++ Enqueued Body Frame ({0})", _recordQueue.Count);
            }
            else
            {
                System.Diagnostics.Debug.WriteLine("!!! FRAME SKIPPED (Body in KinectRecorder)");
            }
        }
Esempio n. 2
0
        public void _audioBeamReader_FrameArrived(object sender, AudioBeamFrameArrivedEventArgs e)
        {
            var frames = e.FrameReference.AcquireBeamFrames();

            if (ReferenceEquals(null, frames))
            {
                return;
            }

            foreach (var frame in frames)
            {
                if (ReferenceEquals(null, frame) || ReferenceEquals(null, frame.SubFrames))
                {
                    return;
                }
                foreach (var subFrame in frame.SubFrames)
                {
                    if (ReferenceEquals(null, subFrame.AudioBodyCorrelations))
                    {
                        return;
                    }
                    foreach (var audioBodyCorrelation in subFrame.AudioBodyCorrelations)
                    {
                        CheckPerson.Instance.CheckIfExistsPerson(audioBodyCorrelation.BodyTrackingId);
                        var time = _dataAccessFacade.GetSceneInUseAccess()?.GetLocation();
                        if (time.HasValue)
                        {
                            _dataAccessFacade.GetEventAccess().Add(CheckPerson.Instance.PersonsId[audioBodyCorrelation.BodyTrackingId], "Voice", "Talked", time.Value, 1);
                        }

                        //Console.WriteLine("Tiempo: {0}, Llegó Voz de {1}", DateTime.Now, audioBodyCorrelation.BodyTrackingId);
                    }
                }
            }
        }
Esempio n. 3
0
        public void _bodyReader_FrameArrived(object sender, BodyFrameArrivedEventArgs e)
        {
            IEnumerable <IBody> bodies = null; // to make the GetBitmap call a little cleaner

            using (var frame = e.FrameReference.AcquireFrame())
            {
                if (!ReferenceEquals(null, frame))
                {
                    frame.GetAndRefreshBodyData(_bodies);
                    bodies = _bodies;
                }
            }

            if (!ReferenceEquals(null, bodies))
            {
                CameraSpacePoint lastCentralPoint = new CameraSpacePoint();
                foreach (IBody body in bodies)
                {
                    //using only the central point
                    var centralPoint = body.Joints[JointType.SpineMid].Position;
                    if (centralPoint.X != 0 || centralPoint.Y != 0 || centralPoint.Z != 0)
                    {
                        if (lastCentralPoint.X != 0 || lastCentralPoint.Y != 0 || lastCentralPoint.Z != 0)
                        {
                            var distance = Math.Sqrt(
                                Math.Pow(lastCentralPoint.X - centralPoint.X, 2) +
                                Math.Pow(lastCentralPoint.Y - centralPoint.Y, 2) +
                                Math.Pow(lastCentralPoint.Z - centralPoint.Z, 2));


                            CheckPerson.Instance.CheckIfExistsPerson(body.TrackingId);
                            var time = _dataAccessFacade.GetSceneInUseAccess()?.GetLocation();
                            if (time.HasValue)
                            {
                                //_dataAccessFacade.GetEventAccess().Add(CheckPerson.Instance.PersonsId[body.TrackingId], "Proxemic", "Events", time.Value, distance, false);
                                if (distance < 0.45)
                                {
                                    _dataAccessFacade.GetEventAccess().Add(CheckPerson.Instance.PersonsId[body.TrackingId], "Proxemic", "Events", time.Value, distance, 0);
                                }
                                else if (distance < 0.65)
                                {
                                    _dataAccessFacade.GetEventAccess().Add(CheckPerson.Instance.PersonsId[body.TrackingId], "Proxemic", "Events", time.Value, distance, 1);
                                }
                                else if (distance < 0.85)
                                {
                                    _dataAccessFacade.GetEventAccess().Add(CheckPerson.Instance.PersonsId[body.TrackingId], "Proxemic", "Events", time.Value, distance, 2);
                                }
                                else
                                {
                                    _dataAccessFacade.GetEventAccess().Add(CheckPerson.Instance.PersonsId[body.TrackingId], "Proxemic", "Events", time.Value, distance, 3);
                                }
                                //Console.WriteLine($"Proxemic is: {distance}");
                            }
                        }
                        lastCentralPoint = centralPoint;
                    }
                }
            }
        }
Esempio n. 4
0
        /// <summary>
        /// Check if the person is in the dictionary, if not, check if the person
        /// is in the scene (checking by the name), if yes, add it to the dictionary,
        /// if not, create a new person and adds it to the scene and dictionary
        /// </summary>
        /// <param name="bodyTrackingId">The kinect body tracking identifier.</param>
        public void CheckIfExistsPerson(ulong bodyTrackingId)
        {
            if (!PersonsId.ContainsKey(bodyTrackingId))
            {
                bool   isPersonInScene = false;
                string personName      = "Kinect" + bodyTrackingId;
                var    personsInScene  = _dataAccessFacade.GetSceneInUseAccess()?.GetScene()?.PersonsInScene;
                foreach (var personInScene in personsInScene)
                {
                    string name = personInScene?.Person?.Name;
                    if (!ReferenceEquals(null, name) && name.Equals(personName))
                    {
                        PersonsId[bodyTrackingId] = personInScene.Person;
                        isPersonInScene           = true;
                        break;
                    }
                }
                if (!isPersonInScene)
                {
                    var newPerson = new Data.Model.Person()
                    {
                        Name       = personName,
                        Photo      = null,
                        Birthday   = null,
                        Sex        = null,
                        TrackingId = (long)bodyTrackingId
                    };
                    var pis = new Data.Model.PersonInScene()
                    {
                        Person = newPerson,
                        Scene  = _dataAccessFacade.GetSceneInUseAccess()?.GetScene()
                    };
                    _dataAccessFacade.GetSceneInUseAccess()?.GetScene().PersonsInScene.Add(pis);
                    PersonsId[bodyTrackingId] = newPerson;

                    /*
                     * //this writes to the database directly
                     * var newPerson = _dataAccessFacade.GetPersonAccess().Add(personName, null, null, null, (long)bodyTrackingId);
                     * PersonsId[bodyTrackingId] = newPerson.PersonId;
                     * _dataAccessFacade.GetPersonAccess().AddToScene(newPerson, _dataAccessFacade.GetSceneInUseAccess()?.GetScene());
                     */
                }
            }
        }
Esempio n. 5
0
        public void Play()
        {
            if (ReferenceEquals(null, _replay) &&
                !ReferenceEquals(null, _dataAccessFacade.GetSceneInUseAccess().GetScene()))
            {
                try
                {
                    string fileName = _dataAccessFacade.GetGeneralSettings().GetDataDirectory() +
                                      "scene/" + _dataAccessFacade.GetSceneInUseAccess().GetScene().SceneId + "/kinect.dvr";
                    if (File.Exists(fileName))
                    {
                        Console.WriteLine($"archivo {fileName} existe, a abrirlo");
                        _replay = new KinectReplay(File.Open(fileName, FileMode.Open, FileAccess.Read));
                        _replay.PropertyChanged += _replay_PropertyChanged;
                        if (_replay.HasBodyFrames)
                        {
                            _replay.BodyFrameArrived += KinectInput.SkeletonColorVideoViewer._replay_BodyFrameArrived;
                        }
                        if (_replay.HasColorFrames)
                        {
                            _replay.ColorFrameArrived += KinectInput.SkeletonColorVideoViewer._replay_ColorFrameArrived;
                        }

                        _replay.ScrubTo(new TimeSpan(0));
                        _replay.Start();
                    }
                }
                catch (Exception)
                {
                    Close();
                }
            }
            else
            {
                _replay.Start();
            }
        }
Esempio n. 6
0
        private void VgbFrameReader_FrameArrived(object sender, VisualGestureBuilderFrameArrivedEventArgs e)
        {
            var kFrame = e.FrameReference.AcquireFrame();

            if (!ReferenceEquals(null, kFrame))
            {
                var frame = new KinectGestureFrameArrivedArgs()
                {
                    Time       = _dataAccessFacade.GetSceneInUseAccess().GetLocation(),
                    TrackingId = TrackingId,
                    Frame      = kFrame
                };
                OnKinectGestureFrameArrived(frame);
            }
        }
Esempio n. 7
0
        private void SerialPort_DataReceived(object sender, SerialDataReceivedEventArgs e)
        {
            if (sender is SerialPort port)
            {
                byte[] buffer = new byte[port.BytesToRead];
                port.Read(buffer, 0, buffer.Length);
                var time = _dataAccessFacade.GetSceneInUseAccess().GetLocation();
                if (!time.HasValue)
                {
                    return;
                }
                foreach (var bufferData in buffer)
                {
                    double[] data = _interpretStream.interpretBinaryStream(bufferData);

                    if (!ReferenceEquals(null, data))
                    {
                        if (data.Length >= 9)
                        {
                            double[] dataToGraph = new double[9];
                            dataToGraph[0] = data[0];
                            var eegArgs = new EegFrameArrivedEventArgs()
                            {
                                Time     = time.Value,
                                Person   = _person,
                                Channels = new List <EegChannel>()
                            };
                            for (int j = 1; j < 9; j++)
                            {
                                double value = _filter.FiltersSelect((FilterType)(OpenBCISettings.Instance.Filter.Value),
                                                                     (NotchType)(OpenBCISettings.Instance.Notch.Value), data[j], j - 1);
                                dataToGraph[j] = value;
                                eegArgs.Channels.Add(new EegChannel()
                                {
                                    Filter   = FilterType.None,
                                    Notch    = (NotchType)OpenBCISettings.Instance.Notch.Value,
                                    Position = _positions[j - 1] ?? "",
                                    Value    = value
                                });
                            }
                            GraphTab.Enqueue(dataToGraph);
                            OnEegFrameArrived(eegArgs);
                        }
                        if (data.Length == 12 && data[9] != 0 && data[10] != 0 && data[11] != 0)
                        {
                            var accArgs = new AccelerometerFrameArrivedEventArgs()
                            {
                                Place = "Head",
                                // Time = ,
                                XAxis = data[9],
                                YAxis = data[10],
                                ZAxis = data[11]
                            };
                            OnAccelerometerArrived(accArgs);
                        }
                        var actualTime = _dataAccessFacade.GetSceneInUseAccess().GetLocation();
                        if (actualTime.HasValue)
                        {
                            _filemanage?.WriteFile(actualTime.Value, data);
                        }
                    }
                }
            }
        }
Esempio n. 8
0
        public void _bodyReader_FrameArrived(object sender, BodyFrameArrivedEventArgs e)
        {
            IEnumerable <IBody> bodies = null; // to make the GetBitmap call a little cleaner

            using (var frame = e.FrameReference.AcquireFrame())
            {
                if (!ReferenceEquals(null, frame))
                {
                    frame.GetAndRefreshBodyData(_bodies);
                    bodies = _bodies;
                }
            }

            if (!ReferenceEquals(null, bodies))
            {
                foreach (IBody body in bodies)
                {
                    try
                    {
                        var left  = body.Joints[JointType.ShoulderLeft].Position;
                        var right = body.Joints[JointType.ShoulderRight].Position;
                        var spine = body.Joints[JointType.SpineShoulder].Position;

                        var v1 = new double[]
                        {
                            spine.X - left.X + _movementToPositive,
                            spine.Y - left.Y + _movementToPositive,
                            spine.Z - left.Z + _movementToPositive
                        };

                        var v2 = new double[]
                        {
                            right.X - spine.X + _movementToPositive,
                            right.Y - spine.Y + _movementToPositive,
                            right.Z - spine.Z + _movementToPositive
                        };

                        //The dot product of v1 and v2 is a function of
                        //the cosine of the angle between them
                        //(it's scaled by the product of their magnitudes).
                        //So first normalize v1 and v2
                        var v1mag  = Math.Sqrt(v1[0] * v1[0] + v1[1] * v1[1] + v1[2] * v1[2]);
                        var v1norm = new double[] { v1[0] / v1mag, v1[1] / v1mag, v1[2] / v1mag };

                        var v2mag  = Math.Sqrt(v2[0] * v2[0] + v2[1] * v2[1] + v2[2] * v2[2]);
                        var v2norm = new double[] { v2[0] / v2mag, v2[1] / v2mag, v2[2] / v2mag };

                        //Then calculate the dot product:
                        var res = v1norm[0] * v2norm[0] + v1norm[1] * v2norm[1] + v1norm[2] * v2norm[2];

                        //And finally, recover the angle:
                        var angle = Math.Acos(res) * 180.0 / Math.PI;
                        if (!double.IsNaN(angle) && !double.IsInfinity(angle))
                        {
                            CheckPerson.Instance.CheckIfExistsPerson(body.TrackingId);
                            var time = _dataAccessFacade.GetSceneInUseAccess()?.GetLocation();
                            if (time.HasValue)
                            {
                                _dataAccessFacade.GetEventAccess().Add(CheckPerson.Instance.PersonsId[body.TrackingId], "Shoulders", "Angle", time.Value, angle, -1);
                                //Console.WriteLine($"El ángulo de los hombros es: {angle}");
                            }
                        }
                    }
                    catch (Exception ex)
                    {
                        System.Diagnostics.Debug.WriteLine($"Exception obtained at try get shoulders angle: {ex.Message}");
                    }
                }
            }
        }
Esempio n. 9
0
        public async Task StartRecording()
        {
            if (!GetSensor())
            {
                return;
            }

            int detectorCount = _sensor.BodyFrameSource.BodyCount > 0 ? _sensor.BodyFrameSource.BodyCount : 6;

            for (int i = 0; i < _sensor.BodyFrameSource.BodyCount; ++i)
            {
                try
                {
                    GestureDetector detector = new GestureDetector(i, _sensor);
                    GestureDetector.GestureDetectorList.Add(detector);
                }
                catch (Exception ex)
                {
                    Console.WriteLine("Error at creating the gesture detector: " + ex.Message);
                }
            }
            _bodyReader.FrameArrived += GestureDetector._bodyReader_FrameArrived;

            foreach (var module in ProcessingLoader.Instance.ProcessingModules)
            {
                if (module.IsEnabled)
                {
                    if (module is IKinectProcessingModule kinectModule)
                    {
                        if (!ReferenceEquals(null, kinectModule.BodyListener()))
                        {
                            _bodyReader.FrameArrived += kinectModule.BodyListener();
                        }

                        if (!ReferenceEquals(null, kinectModule.ColorListener()))
                        {
                            _colorReader.FrameArrived += kinectModule.ColorListener();
                        }

                        if (!ReferenceEquals(null, kinectModule.AudioListener()))
                        {
                            _audioBeamReader.FrameArrived += kinectModule.AudioListener();
                        }

                        if (!ReferenceEquals(null, kinectModule.GestureListener()))
                        {
                            foreach (var detector in GestureDetector.GestureDetectorList)
                            {
                                detector.KinectGestureFrameArrived += kinectModule.GestureListener();
                            }
                        }
                    }
                }
            }

            if (ReferenceEquals(null, _recorder) && !ReferenceEquals(null, _dataAccessFacade.GetSceneInUseAccess().GetScene()))
            {
                string fileName = Path.Combine(_dataAccessFacade.GetGeneralSettings().GetSceneInUseDirectory(), "kinect.dvr");
                _recorder = new DVR.Recorder(File.Open(fileName, FileMode.Create), _sensor)
                {
                    EnableBodyRecorder     = true,
                    EnableColorRecorder    = true,
                    EnableDepthRecorder    = false,
                    EnableInfraredRecorder = false,

                    ColorRecorderCodec = new JpegColorCodec()
                };
                //TODO: revisar estos pixeles
                //_recorder.ColorRecorderCodec.OutputWidth = 1280;
                //_recorder.ColorRecorderCodec.OutputHeight = 720;
                _recorder.Start();
            }
            _isRecording = true;
        }
Esempio n. 10
0
        public void _bodyReader_FrameArrived(object sender, BodyFrameArrivedEventArgs e)
        {
            IEnumerable <IBody> bodies = null; // to make the GetBitmap call a little cleaner

            using (var frame = e.FrameReference.AcquireFrame())
            {
                if (!ReferenceEquals(null, frame))
                {
                    frame.GetAndRefreshBodyData(_bodies);
                    bodies = _bodies;
                }
            }

            if (!ReferenceEquals(null, bodies))
            {
                foreach (IBody body in bodies)
                {
                    var orientation = body.JointOrientations[JointType.Neck].Orientation;

                    var rotationX = orientation.Pitch();
                    var rotationY = orientation.Yaw();
                    var rotationZ = orientation.Roll();
                    if (rotationX != 0 || rotationY != 0 || rotationZ != 0)
                    {
                        CheckPerson.Instance.CheckIfExistsPerson(body.TrackingId);
                        var time = _dataAccessFacade.GetSceneInUseAccess()?.GetLocation();
                        if (time.HasValue)
                        {
                            _dataAccessFacade.GetEventAccess().Add(CheckPerson.Instance.PersonsId[body.TrackingId], "Neck Orientation", "Pitch", time.Value, rotationX, -1);
                            //_dataAccessFacade.GetEventAccess().Add(CheckPerson.Instance.PersonsId[body.TrackingId], "Neck Orientation", "Yaw", time.Value, rotationY, false);
                            _dataAccessFacade.GetEventAccess().Add(CheckPerson.Instance.PersonsId[body.TrackingId], "Neck Orientation", "Roll", time.Value, rotationZ, -1);

                            _dataAccessFacade.GetEventAccess().Add(CheckPerson.Instance.PersonsId[body.TrackingId], "Lean", "X", time.Value, body.Lean.X, -1);
                            //_dataAccessFacade.GetEventAccess().Add(CheckPerson.Instance.PersonsId[body.TrackingId], "Lean", "Y", time.Value, body.Lean.Y, false);

                            if (body.Lean.Y < (2 / 3) - 1) //between -1 and -0.333
                            {
                                _dataAccessFacade.GetEventAccess().Add(CheckPerson.Instance.PersonsId[body.TrackingId], "Lean", "Y", time.Value, body.Lean.Y, 0);
                            }
                            else if (body.Lean.Y < 1 - (2 / 3)) //between -0.333 and 0.333
                            {
                                _dataAccessFacade.GetEventAccess().Add(CheckPerson.Instance.PersonsId[body.TrackingId], "Lean", "Y", time.Value, body.Lean.Y, 1);
                            }
                            else //between 0.333 and 1
                            {
                                _dataAccessFacade.GetEventAccess().Add(CheckPerson.Instance.PersonsId[body.TrackingId], "Lean", "Y", time.Value, body.Lean.Y, 2);
                            }

                            //Console.WriteLine($"XPitch: {rotationX}\tYYaw: {rotationY}\tZRoll: {rotationZ}");
                            //Console.WriteLine($"XLean: {body.Lean.X}\tYLean: {body.Lean.Y}");

                            //2 is because the horizontal max y 4 mts
                            double distanceMaxOfBorder = 2 + Math.Abs(body.Joints[JointType.Neck].Position.X);
                            //4.3 is the distance between the public and the person
                            Console.WriteLine($"The distance between the sensor and person is: {body.Joints[JointType.Neck].Position.Z}");
                            double hipoten = Math.Sqrt(distanceMaxOfBorder * distanceMaxOfBorder + 4.3 * 4.3);
                            double angle   = Math.Asin(distanceMaxOfBorder / hipoten);
                            if (rotationY <= angle)
                            {
                                _dataAccessFacade.GetEventAccess().Add(CheckPerson.Instance.PersonsId[body.TrackingId], "Neck Orientation", "Yaw", time.Value, body.Lean.Y, 0);
                            }
                            else
                            {
                                _dataAccessFacade.GetEventAccess().Add(CheckPerson.Instance.PersonsId[body.TrackingId], "Neck Orientation", "Yaw", time.Value, body.Lean.Y, -1);
                            }
                        }
                    }
                }
            }
        }
        public void _bodyReader_FrameArrived(object sender, BodyFrameArrivedEventArgs e)
        {
            IEnumerable <IBody> bodies = null; // to make the GetBitmap call a little cleaner

            using (var frame = e.FrameReference.AcquireFrame())
            {
                if (!ReferenceEquals(null, frame))
                {
                    frame.GetAndRefreshBodyData(_bodies);
                    bodies = _bodies;
                }
            }

            if (!ReferenceEquals(null, bodies))
            {
                List <CameraSpacePoint> lastPersonsJoints = new List <CameraSpacePoint>();
                foreach (IBody body in bodies)
                {
                    //using all points
                    List <CameraSpacePoint> actualPersonJoints = new List <CameraSpacePoint>();
                    double minDistance = double.PositiveInfinity;
                    foreach (var joint in body.Joints)
                    {
                        if (joint.Value.Position.X != 0 || joint.Value.Position.Y != 0 || joint.Value.Position.Z != 0)
                        {
                            actualPersonJoints.Add(joint.Value.Position);
                            foreach (var lastJoint in lastPersonsJoints)
                            {
                                var distance = Math.Sqrt(
                                    Math.Pow(lastJoint.X - joint.Value.Position.X, 2) +
                                    Math.Pow(lastJoint.Y - joint.Value.Position.Y, 2) +
                                    Math.Pow(lastJoint.Z - joint.Value.Position.Z, 2));
                                if (distance < minDistance)
                                {
                                    minDistance = distance;
                                }
                            }
                        }
                    }
                    if (actualPersonJoints.Count > 0)
                    {
                        lastPersonsJoints = actualPersonJoints;
                    }
                    if (!double.IsInfinity(minDistance))
                    {
                        CheckPerson.Instance.CheckIfExistsPerson(body.TrackingId);
                        var time = _dataAccessFacade.GetSceneInUseAccess()?.GetLocation();
                        if (time.HasValue)
                        {
                            //_dataAccessFacade.GetEventAccess().Add(CheckPerson.Instance.PersonsId[body.TrackingId], "AccProxemic", "Events", time.Value, minDistance, false);
                            if (minDistance < 0.45)
                            {
                                _dataAccessFacade.GetEventAccess().Add(CheckPerson.Instance.PersonsId[body.TrackingId], "AccProxemic", "Events", time.Value, minDistance, 0);
                            }
                            else if (minDistance < 0.65)
                            {
                                _dataAccessFacade.GetEventAccess().Add(CheckPerson.Instance.PersonsId[body.TrackingId], "AccProxemic", "Events", time.Value, minDistance, 1);
                            }
                            else if (minDistance < 0.85)
                            {
                                _dataAccessFacade.GetEventAccess().Add(CheckPerson.Instance.PersonsId[body.TrackingId], "AccProxemic", "Events", time.Value, minDistance, 2);
                            }
                            else
                            {
                                _dataAccessFacade.GetEventAccess().Add(CheckPerson.Instance.PersonsId[body.TrackingId], "AccProxemic", "Events", time.Value, minDistance, 3);
                            }
                            //Console.WriteLine($"Proxemic real min is: {minDistance}");
                        }
                    }
                }
            }
        }