public void AcquireLatestFrame()
        {
            bodyReader = GetBodyFrameReader();
            using ( var bodyFrame = bodyReader.AcquireLatestFrame() ) {
            }

            using ( var bodyFrame = bodyReader.AcquireLatestFrame() ) {
            }
        }
 public void RelativeTime()
 {
     bodyReader = GetBodyFrameReader();
     using ( var bodyFrame = bodyReader.AcquireLatestFrame() ) {
         Assert.AreNotEqual( 0, bodyFrame.RelativeTime() );
     }
 }
 public void GetAndRefreshBodyData()
 {
     bodyReader = GetBodyFrameReader();
     using ( var bodyFrame = bodyReader.AcquireLatestFrame() ) {
         Body[] bodies = new Body[6];
         bodyFrame.GetAndRefreshBodyData( bodies );
         foreach ( var body in bodies ) {
         }
     }
 }
Beispiel #4
0
    void Update()
    {
        if (_Reader != null)
        {
            var frame = _Reader.AcquireLatestFrame();
            if (frame != null)
            {
                if (_Data == null)
                {
                    _Data = new Body[_Sensor.BodyFrameSource.BodyCount];
                }

                frame.GetAndRefreshBodyData(_Data);

                frame.Dispose();
                frame = null;
            }
        }
    }
Beispiel #5
0
    // Update is called once per frame
    void Update()
    {
        IsAvailable = _sensor.IsAvailable;
        CameraSpacePoint postion;
        bool             found = false;

        if (_bodyFrameReader != null)
        {
            var frame = _bodyFrameReader.AcquireLatestFrame();

            if (frame != null)
            {
                frame.GetAndRefreshBodyData(_bodies);

                foreach (var body in _bodies.Where(b => b.IsTracked))
                {
                    IsAvailable = true;

                    if (body.TrackingId == personID)
                    {
                        postion   = body.Joints[JointType.HandLeft].Position;
                        handLeft  = new Vector3(postion.X, postion.Y, postion.Z);
                        postion   = body.Joints[JointType.HandRight].Position;
                        handRight = new Vector3(postion.X, postion.Y, postion.Z);

                        leaningPosition = body.Lean.X;

                        leftHandStatus  = body.HandLeftState;
                        rightHandStatus = body.HandRightState;

                        found = true;
                    }
                }
                if (!found)
                {
                    personID = Calibrate();
                }

                frame.Dispose();
                frame = null;
            }
        }
    }
Beispiel #6
0
    void Update()
    {
        IsAvailable = _sensor.IsAvailable;
        if (_bodyFrameReader != null)
        {
            var frame = _bodyFrameReader.AcquireLatestFrame();
            if (frame != null)
            {
                frame.GetAndRefreshBodyData(_bodies);
                foreach (var body in _bodies.Where(b => b.IsTracked))
                {
                    IsAvailable = true;
                    if (trCharacter == null)
                    {
                        Debug.LogError("trCharacter is NULL");
                    }
                    else
                    {
                        switch (controlType)
                        {
                        case ControlType.Lean:
                            LeanControl(body);
                            break;

                        case ControlType.OpenHand:
                            OpenHandControl(body);
                            break;

                        case ControlType.RiseHand:
                            RiseHandControl(body);
                            break;

                        case ControlType.RiseLeg:
                            RiseLegControl(body);
                            break;
                        }
                    }
                }
                frame.Dispose();
                frame = null;
            }
        }
    }
 void Update()
 {
     if (_reader != null)
     {
         //bool newData = false;
         var frame = _reader.AcquireLatestFrame();
         if (frame != null)
         {
             if (_data == null)
             {
                 _data = new Body[_sensor.BodyFrameSource.BodyCount];
             }
             frame.GetAndRefreshBodyData(_data);
             //newData = true;
             Floor = frame.FloorClipPlane;
             frame.Dispose();
             frame = null;
         }
     }
 }
    void Update()
    {
        if (i_reader != null)
        {
            var frame = i_reader.AcquireLatestFrame();
            if (frame != null)
            {
                if (i_bodies == null)
                {
                    i_bodies = new Body[i_sensor.BodyFrameSource.BodyCount];
                }

                CalculateTilt(frame);

                frame.GetAndRefreshBodyData(i_bodies);
                frame.Dispose();
                frame = null;
            }
        }
    }
Beispiel #9
0
 void Update()
 {
     IsAvailable = _sensor.IsAvailable;
     if (_bodyFrameReader != null)
     {
         var frame = _bodyFrameReader.AcquireLatestFrame();
         if (frame != null)
         {
             frame.GetAndRefreshBodyData(_bodies);
             foreach (var body in _bodies.Where(b => b.IsTracked))
             {
                 IsAvailable = true;
                 LeanX       = body.Lean.X;
                 LeanY       = body.Lean.Y;
             }
             frame.Dispose();
             frame = null;
         }
     }
 }
Beispiel #10
0
    void Update()
    {
        // If a kinect sensor was found, read the last acquired frame.
        if (_Reader != null)
        {
            var frame = _Reader.AcquireLatestFrame();
            if (frame != null)
            {
                if (_Data == null)
                {
                    _Data = new Body[_Sensor.BodyFrameSource.BodyCount];
                }

                frame.GetAndRefreshBodyData(_Data);

                frame.Dispose();
                frame = null;
            }
        }
    }
    // Update is called once per frame
    void Update()
    {
        IsAvailable = _sensor.IsAvailable;

        if (_bodyFrameReader != null)
        {
            var frame = _bodyFrameReader.AcquireLatestFrame();

            if (frame != null)
            {
                frame.GetAndRefreshBodyData(_bodies);

                foreach (var body in _bodies.Where(b => b.IsTracked))
                {
                    IsAvailable = true;

                    if (body.HandRightConfidence == TrackingConfidence.High && body.HandRightState == HandState.Lasso)
                    {
                        IsFire = true;
                    }
                    else
                    {
                        IsFire = false;
                    }

                    ShipPosition   = RescalingToRangesB(-1, 1, -3, 3, body.Lean.X);
                    handXText.text = body.Lean.X.ToString();
                    IsPlaying      = true;
                }
                if (_bodies == null)
                {
                    Debug.Log("oh no");
                    IsPlaying = false;
                }


                frame.Dispose();
                frame = null;
            }
        }
    }
Beispiel #12
0
    // Store data in Body array once per frame
    void Update()
    {
        _newBodyData = false;

        // check if reader has data
        if (_Reader != null)
        {
            // most recent body frame
            var frame = _Reader.AcquireLatestFrame();
            if (frame != null)
            {
                // get list of refreshed body data
                frame.GetAndRefreshBodyData(_bodies);
                _newBodyData = true;

                // clean up running stream
                frame.Dispose();
                frame = null;
            }
        }
    }
Beispiel #13
0
    void Update()
    {
        if (_Reader != null)
        {
            var frame = _Reader.AcquireLatestFrame();
            if (frame != null)
            {
                if (_Data == null)
                {
                    _Data = new Body[_Sensor.BodyFrameSource.BodyCount];
                }

                frame.GetAndRefreshBodyData(_Data);

                //Debug.Log("Got Body Data :" + ((Time.time - timed)*1000));
                //timed = Time.time;
                frame.Dispose();
                frame = null;
            }
        }
    }
Beispiel #14
0
        // ==================================== <summary>
        // アップデート処理
        // </summary> ===================================
        void Update()
        {
            // ------------------
            // ボーンデータ更新
            if (_BodyReader != null)
            {
                var frame = _BodyReader.AcquireLatestFrame();
                if (frame != null)
                {
                    if (_BodyData == null)
                    {
                        _BodyData = new Body[_Sensor.BodyFrameSource.BodyCount];
                    }

                    frame.GetAndRefreshBodyData(_BodyData);

                    // FloorClipPlaneを取得する
                    FloorClipPlane = frame.FloorClipPlane;

                    frame.Dispose();
                    frame = null;
                }
            }

            // ------------------
            // Depth
            if (_DepthReader != null)
            {
                var frame = _DepthReader.AcquireLatestFrame();
                if (frame != null)
                {
                    frame.CopyFrameDataToArray(_DepthDataRaw);

                    _Mapper.MapDepthFrameToCameraSpace(_DepthDataRaw, _DepthData);

                    frame.Dispose();
                    frame = null;
                }
            }
        }
Beispiel #15
0
    // Update is called once per frame
    void Update()
    {
        if (_bodyFrameReader != null)
        {
            var frame = _bodyFrameReader.AcquireLatestFrame();

            if (frame != null)
            {
                frame.GetAndRefreshBodyData(_bodies);

                if (_bodies == null)
                {
                    _bodies = new Body[_sensor.BodyFrameSource.BodyCount];
                }

                frame.GetAndRefreshBodyData(_bodies);

                frame.Dispose();
                frame = null;
            }
        }
    }
        void Update()
        {
            if (kinectReader == null)
            {
                return;
            }

            var frame = kinectReader.AcquireLatestFrame();

            if (frame != null)
            {
                if (body == null)
                {
                    body = new Body[kinectSensor.BodyFrameSource.BodyCount];
                }

                frame.GetAndRefreshBodyData(body);

                frame.Dispose();
                frame = null;
            }
        }
Beispiel #17
0
    void Update()
    {
        // 키넥트 센서가 있을 경우 계속해서 데이터를 받아온다.
        if (_Reader != null)
        {
            var frame = _Reader.AcquireLatestFrame();
            if (frame != null)
            {
                // 데이터가 없을 경우 인식되는 바디 데이터를 가져온다.
                if (_Data == null)
                {
                    _Data = new Body[_Sensor.BodyFrameSource.BodyCount];
                }

                // 뼈데이터 업데이트
                frame.GetAndRefreshBodyData(_Data);

                frame.Dispose();
                frame = null;
            }
        }
    }
Beispiel #18
0
    void Update()
    {
        if (_Reader != null)//フレーム取得
        {
            var frame = _Reader.AcquireLatestFrame();

            if (frame != null)
            {
                if (_Data == null)//ボディを取得
                {
                    _Data = new Body[_Sensor.BodyFrameSource.BodyCount];
                    //Debug.Log(_Data);
                }

                frame.GetAndRefreshBodyData(_Data);
                //Debug.Log(_Data.Length);

                frame.Dispose();
                frame = null;
                //Debug.Log(_Data.Length);
            }
        }
    }
Beispiel #19
0
    void Update()
    {
        abcd++;


        if (_Reader != null)
        {
            var frame = _Reader.AcquireLatestFrame();
            if (frame != null)//frame이 읽고 있다면
            {
                if (_Data == null)
                {
                    _Data = new Body[_Sensor.BodyFrameSource.BodyCount];//
                }

                frame.GetAndRefreshBodyData(_Data);//_data의 값을 가져옴


                frame.Dispose();
                frame = null;
            }
        }
    }
Beispiel #20
0
    void Update()
    {
        if (_Reader != null)
        {
            _Frame = _Reader.AcquireLatestFrame();
            if (_Frame != null)
            {
                //_vec4 = _Frame.FloorClipPlane;
                //Vector3 normalVector = new Vector3 (_vec4.X, _vec4.Y, _vec4.Z);

                if (_Data == null)
                {
                    _Data = new Body[_Sensor.BodyFrameSource.BodyCount];
                }
                _Frame.GetAndRefreshBodyData(_Data);

                NewFrame(this, _Frame);

                _Frame.Dispose();
                _Frame = null;
            }
        }
    }
    void FixedUpdate()
    {
        if (_bfReader != null)
        {
            //Text_connect.text = "Connect Success";

            //한 프레임 얻는다
            BodyFrame frame = _bfReader.AcquireLatestFrame();
            if (frame != null)
            {
                //몸 데이터 갱신
                frame.GetAndRefreshBodyData(KinectAction.BodyArray);
                KinectAction.KinectActionUpdate();

                //프레임 해제
                frame.Dispose();
                frame = null;
            }
        }
        else
        {
            //Text_connect.text = "Connect Fail";
        }
    }
    // Update is called once per frame
    void Update()
    {
        bool newBodyData = false;

        using (BodyFrame bodyFrame = _bodyFrameReader.AcquireLatestFrame())
        {
            if (bodyFrame != null)
            {
                bodyFrame.GetAndRefreshBodyData(_bodies);
                newBodyData = true;
            }
        }

        if (newBodyData)
        {
            for (int bodyIndex = 0; bodyIndex < _bodyCount; bodyIndex++)
            {
                var body = _bodies[bodyIndex];
                if (body != null)
                {
                    var trackingId = body.TrackingId;

                    if (trackingId != _gestureDetectorList[bodyIndex].TrackingId)
                    {
                        GestureTextGameObject.text = "none";

                        _gestureDetectorList[bodyIndex].TrackingId = trackingId;

                        _gestureDetectorList[bodyIndex].IsPaused           = (trackingId == 0);
                        _gestureDetectorList[bodyIndex].OnGestureDetected += CreateOnGestureHandler(bodyIndex);
                        Debug.Log(_gestureDetectorList[bodyIndex]);
                    }
                }
            }
        }
    }
Beispiel #23
0
    void FixedUpdate()
    {
        using (var frame = reader?.AcquireLatestFrame()) {
            frame?.GetAndRefreshBodyData(data);
            if (frame != null)
            {
                floorClipPlane = frame.FloorClipPlane;
            }
        }

        if (damp)
        {
            var _body = GetCurrentTrackedBody();
            if (_body.IsNone())
            {
                return;
            }
            var body = _body.Value();
            foreach (var joint in jointTypes)
            {
                dampers[(int)joint].Update(ToQuaternion(body.JointOrientations[joint]));
            }
        }
    }
Beispiel #24
0
    // Update is called once per frame
    void Update()
    {
        /*
         *  To extract this tracking data from the BodyFrame, allocate a vector of 6 body pointers and pass it to BodyFrame.GetAndRefreshBodyData Method.
         *  Each body in the array represents tracking information for each of the 6 possible bodies that can be tracked simultaneously.
         *  Each body that represents an actual live user in view of the sensor will be marked as tracked.
         */
        if (_Reader != null)
        {
            var frame = _Reader.AcquireLatestFrame(); //Gets the most recent body index frame
            if (frame != null)
            {
                if (_Data == null)
                {
                    _Data = new Body[_Sensor.BodyFrameSource.BodyCount];
                }

                frame.GetAndRefreshBodyData(_Data);
                //refresh the stream of data from the Reader
                frame.Dispose();
                frame = null;
            }
        }
    }
    // Update is called once per frame
    void Update()
    {
        if (_sensor == null) //null check!!!!
        {
            return;
        }
        IsAvailable = _sensor.IsAvailable;

        if (_bodyFrameReader != null)
        {
            var frame = _bodyFrameReader.AcquireLatestFrame();

            if (frame != null)
            {
                frame.GetAndRefreshBodyData(_bodies);

                foreach (var body in _bodies.Where(b => b.IsTracked))
                {
                    IsAvailable = true;

                    if (body.HandRightConfidence == TrackingConfidence.High && body.HandRightState == HandState.Lasso)
                    {
                        IsFire = true;
                    }
                    else
                    {
                        PaddlePosition = RescalingToRangesB(-1, 1, -8, 8, body.Lean.X);
                        handXText.text = PaddlePosition.ToString();
                    }
                }

                frame.Dispose();
                frame = null;
            }
        }
    }
Beispiel #26
0
    public bool PollBodyFrame(KinectInterop.SensorData sensorData, ref KinectInterop.BodyFrameData bodyFrame, ref Matrix4x4 kinectToWorld)
    {
        bool bNewFrame = false;

        if ((multiSourceFrameReader != null && multiSourceFrame != null) ||
            bodyFrameReader != null)
        {
            var frame = multiSourceFrame != null?multiSourceFrame.BodyFrameReference.AcquireFrame() :
                            bodyFrameReader.AcquireLatestFrame();

            if (frame != null)
            {
                frame.GetAndRefreshBodyData(bodyData);
                bodyFrame.liRelativeTime = frame.RelativeTime.Ticks;

                if (sensorData.hintHeightAngle)
                {
                    // get the floor plane
                    Windows.Kinect.Vector4 vFloorPlane = frame.FloorClipPlane;
                    Vector3 floorPlane = new Vector3(vFloorPlane.X, vFloorPlane.Y, vFloorPlane.Z);

                    sensorData.sensorRotDetected = Quaternion.FromToRotation(floorPlane, Vector3.up);
                    sensorData.sensorHgtDetected = vFloorPlane.W;
                }

                frame.Dispose();
                frame = null;

                for (int i = 0; i < sensorData.bodyCount; i++)
                {
                    Body body = bodyData[i];

                    if (body == null)
                    {
                        bodyFrame.bodyData[i].bIsTracked = 0;
                        continue;
                    }

                    bodyFrame.bodyData[i].bIsTracked = (short)(body.IsTracked ? 1 : 0);

                    if (body.IsTracked)
                    {
                        // transfer body and joints data
                        bodyFrame.bodyData[i].liTrackingID = (long)body.TrackingId;

                        // cache the body joints (following the advice of Brian Chasalow)
                        Dictionary <Windows.Kinect.JointType, Windows.Kinect.Joint> bodyJoints = body.Joints;

                        for (int j = 0; j < sensorData.jointCount; j++)
                        {
                            Windows.Kinect.Joint    joint     = bodyJoints[(Windows.Kinect.JointType)j];
                            KinectInterop.JointData jointData = bodyFrame.bodyData[i].joint[j];

                            jointData.jointType     = (KinectInterop.JointType)j;
                            jointData.trackingState = (KinectInterop.TrackingState)joint.TrackingState;

                            if ((int)joint.TrackingState != (int)TrackingState.NotTracked)
                            {
                                jointData.kinectPos = new Vector3(joint.Position.X, joint.Position.Y, joint.Position.Z);
                                jointData.position  = kinectToWorld.MultiplyPoint3x4(jointData.kinectPos);
                            }

                            jointData.orientation = Quaternion.identity;
//							Windows.Kinect.Vector4 vQ = body.JointOrientations[jointData.jointType].Orientation;
//							jointData.orientation = new Quaternion(vQ.X, vQ.Y, vQ.Z, vQ.W);

                            if (j == 0)
                            {
                                bodyFrame.bodyData[i].position    = jointData.position;
                                bodyFrame.bodyData[i].orientation = jointData.orientation;
                            }

                            bodyFrame.bodyData[i].joint[j] = jointData;
                        }

                        // tranfer hand states
                        bodyFrame.bodyData[i].leftHandState      = (KinectInterop.HandState)body.HandLeftState;
                        bodyFrame.bodyData[i].leftHandConfidence = (KinectInterop.TrackingConfidence)body.HandLeftConfidence;

                        bodyFrame.bodyData[i].rightHandState      = (KinectInterop.HandState)body.HandRightState;
                        bodyFrame.bodyData[i].rightHandConfidence = (KinectInterop.TrackingConfidence)body.HandRightConfidence;
                    }
                }

                bNewFrame = true;
            }
        }

        return(bNewFrame);
    }
Beispiel #27
0
        void LocalBodyLoop()
        {
            while (true)
            {
                // find closest tracked head
                var bodyFrame = bodyFrameReader.AcquireLatestFrame();

                if (bodyFrame != null)
                {
                    if (bodies == null)
                    {
                        bodies = new Body[bodyFrame.BodyCount];
                    }
                    bodyFrame.GetAndRefreshBodyData(bodies);

                    bool  foundTrackedBody                 = false;
                    float distanceToNearest                = float.MaxValue;
                    var   nearestHeadCameraSpacePoint      = new CameraSpacePoint();
                    var   nearestHandRightCameraSpacePoint = new CameraSpacePoint();
                    var   nearestHandLeftCameraSpacePoint  = new CameraSpacePoint();


                    foreach (var body in bodies)
                    {
                        if (body.IsTracked)
                        {
                            var cameraSpacePoint = body.Joints[JointType.Head].Position;
                            if (cameraSpacePoint.Z < distanceToNearest)
                            {
                                distanceToNearest                = cameraSpacePoint.Z;
                                nearestHeadCameraSpacePoint      = cameraSpacePoint;
                                nearestHandLeftCameraSpacePoint  = body.Joints[JointType.HandLeft].Position;
                                nearestHandRightCameraSpacePoint = body.Joints[JointType.HandRight].Position;
                                foundTrackedBody = true;
                            }
                        }
                    }

                    lock (headCameraSpacePointLock)
                    {
                        if (foundTrackedBody)
                        {
                            headCameraSpacePoint      = nearestHeadCameraSpacePoint;
                            handLeftCameraSpacePoint  = nearestHandLeftCameraSpacePoint;
                            handRightCameraSpacePoint = nearestHandRightCameraSpacePoint;

                            trackingValid = true;
                            //Console.WriteLine("{0} {1} {2}", headCameraSpacePoint.X, headCameraSpacePoint.Y, headCameraSpacePoint.Z);
                        }
                        else
                        {
                            headCameraSpacePoint.X = 0f;
                            headCameraSpacePoint.Y = 0.3f;
                            headCameraSpacePoint.Z = 1.5f;

                            trackingValid = false;
                        }
                    }

                    bodyFrame.Dispose();
                }
                else
                {
                    System.Threading.Thread.Sleep(5);
                }
            }
        }
Beispiel #28
0
        private static void UpdateBody()
        {
            if (reader == null)
            {
                return;
            }

            BodyFrame frame = reader.AcquireLatestFrame();

            if (frame == null)
            {
                return;
            }

            if (data == null)
            {
                data = new Body[sensor.BodyFrameSource.BodyCount];
            }

            frame.GetAndRefreshBodyData(data);
            for (int bodyNr = 0; bodyNr < data.Length; bodyNr++)
            {
                if (data[bodyNr] == null)
                {
                    continue;
                }

                if (data[bodyNr].IsTracked)
                {
                    status = Status.Tracking;
                    if (bodyID == -1)
                    {
                        bodyID = bodyNr;
#if hFACE
                        if (FaceFrameSource != null)
                        {
                            FaceFrameSource.TrackingId = data[bodyID].TrackingId;
                        }
#endif
                        if (onPlayerAppears != null)
                        {
                            onPlayerAppears(bodyID);
                        }
                    }
                }
                else
                {
                    if (bodyNr == bodyID)
                    {
                        if (onPlayerDisappears != null)
                        {
                            onPlayerDisappears(bodyID);
                        }
                        bodyID = -1;
                        status = Status.Present;

#if hFACE
                        if (FaceFrameSource != null)
                        {
                            FaceFrameSource.TrackingId = 0;
                        }
#endif
                    }
                }
            }

            if (bodyID > -1)
            {
                bodyData = data[bodyID];
            }
            else
            {
                bodyData = null;
            }

            frame.Dispose();
        }
Beispiel #29
0
    public bool PollBodyFrame(KinectInterop.SensorData sensorData, ref KinectInterop.BodyFrameData bodyFrame, ref Matrix4x4 kinectToWorld)
    {
        bool bNewFrame = false;

        if ((multiSourceFrameReader != null && multiSourceFrame != null) ||
            bodyFrameReader != null)
        {
            var frame = multiSourceFrame != null?multiSourceFrame.BodyFrameReference.AcquireFrame() :
                            bodyFrameReader.AcquireLatestFrame();

            if (frame != null)
            {
                frame.GetAndRefreshBodyData(bodyData);
                bodyFrame.liRelativeTime = frame.RelativeTime.Ticks;

                frame.Dispose();
                frame = null;

                for (int i = 0; i < sensorData.bodyCount; i++)
                {
                    Body body = bodyData[i];

                    if (body == null)
                    {
                        bodyFrame.bodyData[i].bIsTracked = 0;
                        continue;
                    }

                    bodyFrame.bodyData[i].bIsTracked = (short)(body.IsTracked ? 1 : 0);

                    if (body.IsTracked)
                    {
                        // transfer body and joints data
                        //传送身体和关节数据
                        bodyFrame.bodyData[i].liTrackingID = (long)body.TrackingId;

                        for (int j = 0; j < sensorData.jointCount; j++)
                        {
                            Windows.Kinect.Joint    joint     = body.Joints[(Windows.Kinect.JointType)j];
                            KinectInterop.JointData jointData = bodyFrame.bodyData[i].joint[j];

                            jointData.jointType     = (KinectInterop.JointType)j;
                            jointData.trackingState = (KinectInterop.TrackingState)joint.TrackingState;

                            if ((int)joint.TrackingState != (int)TrackingState.NotTracked)
                            {
                                jointData.kinectPos = new Vector3(joint.Position.X, joint.Position.Y, joint.Position.Z);
                                jointData.position  = kinectToWorld.MultiplyPoint3x4(jointData.kinectPos);
                            }

                            jointData.orientation = Quaternion.identity;
//							Windows.Kinect.Vector4 vQ = body.JointOrientations[jointData.jointType].Orientation;
//							jointData.orientation = new Quaternion(vQ.X, vQ.Y, vQ.Z, vQ.W);

                            if (j == 0)
                            {
                                bodyFrame.bodyData[i].position    = jointData.position;
                                bodyFrame.bodyData[i].orientation = jointData.orientation;
                            }

                            bodyFrame.bodyData[i].joint[j] = jointData;
                        }

                        // tranfer hand states
                        //转变手的状态
                        bodyFrame.bodyData[i].leftHandState      = (KinectInterop.HandState)body.HandLeftState;
                        bodyFrame.bodyData[i].leftHandConfidence = (KinectInterop.TrackingConfidence)body.HandLeftConfidence;

                        bodyFrame.bodyData[i].rightHandState      = (KinectInterop.HandState)body.HandRightState;
                        bodyFrame.bodyData[i].rightHandConfidence = (KinectInterop.TrackingConfidence)body.HandRightConfidence;
                    }
                }

                bNewFrame = true;
            }
        }

        return(bNewFrame);
    }
    protected override void RunBackgroundThreadAsync(int id)
    {
        // Attempt to open Kinect 2 sensor.
        try
        {
            BackgroundData currentFrameData = new BackgroundData();
            sensor = KinectSensor.GetDefault();

            if (sensor != null)
            {
                // Open bodyFrameReader (part of Kinect 2 Sensor)
                bodyFrameReader = sensor.BodyFrameSource.OpenReader();

                if (!sensor.IsOpen)
                {
                    // Open Kinect V2 Sensor.
                    sensor.Open();
                    UnityEngine.Debug.Log("Open Kinect2 device successful.");

                    // Get Frames

                    BodyFrame frame = null;

                    while (m_runBackgroundThread)
                    {
                        if (bodyFrameReader != null)
                        {
                            // Get latest frame from Kinect V2
                            frame = bodyFrameReader.AcquireLatestFrame();

                            if (frame != null)
                            {
                                // Ensure that the thread is set to be running
                                IsRunning = true;

                                if (bodies == null)
                                {
                                    bodies = new Windows.Kinect.Body[sensor.BodyFrameSource.BodyCount];
                                }

                                // Extract frame data
                                currentFrameData.NumOfBodies = (ulong)frame.BodyCount;
                                frame.GetAndRefreshBodyData(bodies);


                                foreach (var body in bodies)
                                {
                                    if (body == null)
                                    {
                                        continue;
                                    }

                                    if (body.IsTracked)
                                    {
                                        //Copy this body to the 'currentFrameData'
                                        currentFrameData.Bodies[0].CopyFromBodyTrackingSdk(body, sensor.CoordinateMapper);
                                        continue;
                                    }
                                }

                                // Time stamp infomation copied from K4A file
                                if (!readFirstFrame)
                                {
                                    readFirstFrame   = true;
                                    initialTimestamp = frame.RelativeTime;
                                }

                                currentFrameData.TimestampInMs    = (float)(frame.RelativeTime - initialTimestamp).TotalMilliseconds;
                                currentFrameData.DepthImageHeight = 100; //filler
                                currentFrameData.DepthImageWidth  = 100; //filler

                                // Discard frame now that we are finished with it.
                                frame.Dispose();
                                frame = null;

                                // Update data variable that is being read in the UI thread.
                                SetCurrentFrameData(ref currentFrameData);
                            }
                        }
                    }
                    // Close Resources once loop is exited.
                    sensor.Close();
                    if (RawDataLoggingFile != null)
                    {
                        RawDataLoggingFile.Close();
                    }
                }
            }
        }
        catch (Exception e)
        {
            UnityEngine.Debug.Log(e);
        }
    }
Beispiel #31
0
        void FixedUpdate()
        {
            if (_reader != null)
            {
                var frame = _reader.AcquireLatestFrame();

                if (frame != null)
                {
                    IList <Body> _bodies = new Body[frame.BodyFrameSource.BodyCount];

                    frame.GetAndRefreshBodyData(_bodies);

                    // Display only first active body
                    for (int i = 0; i < frame.BodyFrameSource.BodyCount; i++)
                    {
                        if (_bodies[i] != null)
                        {
                            if (_bodies[i].IsTracked)
                            {
                                if (_bodies[i].HandLeftState == HandState.Closed)
                                {
                                    leftHandClosed         = true;
                                    leftHandStateText.text = "Closed";
                                }
                                else //if (_bodies[i].HandLeftState == HandState.Open)
                                {
                                    leftHandClosed         = false;
                                    leftHandStateText.text = "Open";
                                }
                                if (_bodies[i].HandRightState == HandState.Closed)
                                {
                                    rightHandClosed         = true;
                                    rightHandStateText.text = "Closed";
                                }
                                else //if (_bodies[i].HandRightState == HandState.Open)
                                {
                                    rightHandClosed         = false;
                                    rightHandStateText.text = "Open";
                                }

                                bodyDrawer.DrawSkeleton(_bodies[i].Joints);

                                if (state == PractiseState.KinectChecking)
                                {
                                    ConvertedBody convertedBody = exerciseService.Convert(_bodies[i],
                                                                                          jsonExercise
                                                                                          .ExerciseRecording
                                                                                          .JointMappings);

                                    ProgressText.text = exerciseService.Progression();

                                    ExerciseScore score = exerciseService.Check(convertedBody);

                                    if (exerciseService.State() == ExerciseValidator.ValidatorState.Checking)
                                    {
                                        // Check body and add score to list
                                        exerciseResultScores.Add(score);

                                        // Add body to list for recording
                                        exerciseResultRecording.Add(convertedBody);
                                    }

                                    if (exerciseService.State() == ExerciseValidator.ValidatorState.Done)
                                    {
                                        CompletedOverlay.SetActive(true);
                                        TimerText.text = "";

                                        int endScore = 0;
                                        int total    = 0;


                                        for (int j = 0; j < exerciseResultScores.Count; j++)
                                        {
                                            total += exerciseResultScores[j].Score;
                                        }

                                        endScore = (int)Math.Round(((float)total / (float)exerciseResultScores.Count) * 25);

                                        string exerciseResultRecordingCompressed = Convert.ToBase64String(Gzip.Compress(JsonConvert.SerializeObject(exerciseResultRecording)));

                                        JObject resultJson = new JObject(
                                            new JProperty("duration", 0),
                                            new JProperty("score", endScore),
                                            new JProperty("exercisePlanning_ID", hrs.currentPlanningId),
                                            new JProperty("result", exerciseResultRecordingCompressed));

                                        ScoreText.text = baseScoreText + endScore + "%";

                                        StartCoroutine(
                                            requestService.Post("/exerciseresult", resultJson.ToString(), success =>
                                        {
                                            Debug.Log(success);
                                        },
                                                                error =>
                                        {
                                            Debug.Log(error);
                                        }
                                                                ));

                                        state = PractiseState.KinectDone;
                                    }
                                }

                                // Exit after first tracked body is found
                                break;
                            }
                        }
                    }

                    // Disable untracked body
                    for (int i = 0; i < frame.BodyFrameSource.BodyCount; i++)
                    {
                        if (!_bodies[i].IsTracked && bodyDrawer.Tracked)
                        {
                            //bodyDrawer.Untracked();
                        }
                    }

                    // Clear frame to get a new one
                    frame.Dispose();
                }
            }
        }
Beispiel #32
0
    void Update()
    {
        if (Input.GetKeyDown(KeyCode.Return))
        {
            Debug.Log("reset");
            this.gameObject.transform.position = new Vector3(0.0f, 0.0f, 0.0f);
        }
        if (Input.GetKeyDown(KeyCode.A))
        {
            this.gameObject.transform.Rotate(Vector3.down, 1.0f * 30);
        }
        if (Input.GetKeyDown(KeyCode.D))
        {
            this.gameObject.transform.Rotate(Vector3.up, 1.0f * 30);
        }



        if (_Reader != null)
        {
            var frame = _Reader.AcquireLatestFrame();

            if (frame != null)
            {
                if (_Data == null)
                {
                    _Data = new Body[_Sensor.BodyFrameSource.BodyCount];
                }

                frame.GetAndRefreshBodyData(_Data);

                frame.Dispose();
                frame = null;

                int idx = -1;
                for (int i = 0; i < _Sensor.BodyFrameSource.BodyCount; i++)
                {
                    if (_Data[i].IsTracked)
                    {
                        idx = i;
                    }
                }
                if (idx > -1)
                {
                    if (Input.GetKeyDown(KeyCode.Space))
                    {
                        Debug.Log("initialized");
                        init(idx);
                    }


                    if (initialized)
                    {
                        spinebase = new Vector3(_Data[idx].Joints[JointType.SpineBase].Position.X,
                                                _Data[idx].Joints[JointType.SpineBase].Position.Y,
                                                _Data[idx].Joints[JointType.SpineBase].Position.Z);

                        spineshoulder = new Vector3(_Data[idx].Joints[JointType.SpineShoulder].Position.X,
                                                    _Data[idx].Joints[JointType.SpineShoulder].Position.Y,
                                                    _Data[idx].Joints[JointType.SpineShoulder].Position.Z);

                        elbowleft = new Vector3(_Data[idx].Joints[JointType.ElbowLeft].Position.X,
                                                _Data[idx].Joints[JointType.ElbowLeft].Position.Y,
                                                _Data[idx].Joints[JointType.ElbowLeft].Position.Z);

                        elbowright = new Vector3(_Data[idx].Joints[JointType.ElbowRight].Position.X,
                                                 _Data[idx].Joints[JointType.ElbowRight].Position.Y,
                                                 _Data[idx].Joints[JointType.ElbowRight].Position.Z);

                        shoulderright = new Vector3(_Data[idx].Joints[JointType.ShoulderRight].Position.X,
                                                    _Data[idx].Joints[JointType.ShoulderRight].Position.Y,
                                                    _Data[idx].Joints[JointType.ShoulderRight].Position.Z);

                        shoulderleft = new Vector3(_Data[idx].Joints[JointType.ShoulderLeft].Position.X,
                                                   _Data[idx].Joints[JointType.ShoulderLeft].Position.Y,
                                                   _Data[idx].Joints[JointType.ShoulderLeft].Position.Z);

                        //if (leanBackward())
                        //{
                        //    moveDown();
                        //}

                        //if (leanForward())
                        //{
                        //    moveUp();
                        //}


                        //if (leanLeft())
                        //{
                        //    moveLeft();
                        //}

                        //if (leanRight())
                        //{
                        //    moveRight();
                        //}

                        //if (pull())
                        //{
                        //    moveBackward();
                        //}

                        //if (shakeHands())
                        //{
                        //    moveForward();
                        //}
                    }
                }
            }
        }
    }
Beispiel #33
0
    void FixedUpdate()
    {
        counter_cooldown--; // delay the amount of times you can shoot to once per movement
        IsAvailable = _sensor.IsAvailable;

        if (_bodyFrameReader != null)
        {
            var frame = _bodyFrameReader.AcquireLatestFrame();

            if (frame != null)
            {
                frame.GetAndRefreshBodyData(_bodies);
                int bodyCount = 0;

                foreach (var body in _bodies.Where(b => b.IsTracked))
                {
                    bodyCount++;
                    if (bodyCount == 1)
                    {
                        player1TrackingID = body.TrackingId;
                    }
                    else
                    {
                        player2TrackingID = body.TrackingId;
                        bodyCount         = 0;
                    }
                }

                foreach (var body in _bodies.Where(b => b.IsTracked))
                {
                    IsAvailable = true;



                    /* get instances of the elbow and shoulders and head*/
                    /*  insert left elbow and shoulder here for 2 hand throw*/
                    Windows.Kinect.Joint elbow    = body.Joints[JointType.ElbowLeft];
                    Windows.Kinect.Joint shoulder = body.Joints[JointType.ShoulderLeft];

                    Windows.Kinect.Joint hand = body.Joints[JointType.HandLeft];
                    // if the right elbow goes above the right shoulder the power meter becomes
                    // available
                    if (body.TrackingId == player1TrackingID)
                    {
                        if (elbow.Position.Y > shoulder.Position.Y)
                        {
                            /* Move Meter Arrow */
                            /* Move Meter Arrow player 1*/
                            if (player1Arrow.transform.position.x < 24f && player1Right)
                            {
                                player1Arrow.transform.position += new Vector3(player1ArrowSpeed, 0, 0);
                            }
                            if (player1Arrow.transform.position.x >= 24f)
                            {
                                player1Right = false;
                            }
                            if (player1Right == false)
                            {
                                player1Arrow.transform.position -= new Vector3(player1ArrowSpeed, 0, 0);
                            }
                            if (player1Arrow.transform.position.x <= 14.5f)
                            {
                                player1Right = true;
                            }

                            /* Shoot ball on Tap */

                            //if (Input.GetButton("Fire1") && !player1Thrown && player1AvailableShots > 0)
                            if (body.HandLeftState == HandState.Open && !player1Thrown && player1AvailableShots > 0)
                            {
                                playerShootsText.GetComponent <GUIText>().text = "Player1 Shoots";
                                player1Thrown = true;
                                player1AvailableShots--;
                                //player1AvailableShotsGO.GetComponent<GUIText>().text = player1AvailableShots.ToString();

                                player1BallClone = Instantiate(player1Ball, player1BallPos, transform.rotation) as GameObject;
                                //player1ThrowSpeed.y = player1ThrowSpeed.y + player1Arrow.transform.position.x;// * 0.7F;

                                player1ThrowSpeed.y = player1ThrowSpeed.y + (hand.Position.Y - shoulder.Position.Y) * 2.5F;
                                player1ThrowSpeed.z = player1ThrowSpeed.z + (hand.Position.Y - shoulder.Position.Y) * 2.0F;

                                //player1ThrowSpeed.z = player1ThrowSpeed.z + player1Arrow.transform.position.x * 0.8F;
                                player1ThrowSpeed.x = -10; // players are out to the side

                                player1BallClone.GetComponent <Rigidbody>().AddForce(player1ThrowSpeed, ForceMode.Impulse);
                                //GetComponent<AudioSource>().Play();
                            }
                        }
                    }


                    if (body.TrackingId == player2TrackingID)
                    {
                        if (elbow.Position.Y > shoulder.Position.Y)
                        {
                            /* move meter arrow player 2*/
                            if (player2Arrow.transform.position.x < -14.3f && player2Right)
                            {
                                player2Arrow.transform.position += new Vector3(player2ArrowSpeed, 0, 0);
                            }
                            if (player2Arrow.transform.position.x >= -14.3f)
                            {
                                player2Right = false;
                            }
                            if (player2Right == false)
                            {
                                player2Arrow.transform.position -= new Vector3(player2ArrowSpeed, 0, 0);
                            }
                            if (player2Arrow.transform.position.x <= -23.7f)
                            {
                                player2Right = true;
                            }

                            /* Shoot ball on Tap */

                            //if (Input.GetButton("Fire1") && !player1Thrown && player1AvailableShots > 0)
                            if (body.HandLeftState == HandState.Open && !player2Thrown && player2AvailableShots > 0)
                            {
                                playerShootsText.GetComponent <GUIText>().text = "Player2 Shoots";
                                player2Thrown = true;
                                player2AvailableShots--;
                                //player1AvailableShotsGO.GetComponent<GUIText>().text = player1AvailableShots.ToString();

                                player2BallClone = Instantiate(player2Ball, player2BallPos, transform.rotation) as GameObject;
                                //player1ThrowSpeed.y = player1ThrowSpeed.y + player1Arrow.transform.position.x;// * 0.7F;

                                player2ThrowSpeed.y = player2ThrowSpeed.y + (hand.Position.Y - shoulder.Position.Y) * 2.5F;
                                player2ThrowSpeed.z = player2ThrowSpeed.z + (hand.Position.Y - shoulder.Position.Y) * 1.3F;

                                //player1ThrowSpeed.z = player1ThrowSpeed.z + player1Arrow.transform.position.x * 0.8F;
                                player2ThrowSpeed.x = 15; // players are out to the side

                                player2BallClone.GetComponent <Rigidbody>().AddForce(player2ThrowSpeed, ForceMode.Impulse);
                                //GetComponent<AudioSource>().Play();
                            }
                        }
                    }


                    /* Remove Ball when it hits the floor */

                    if (player1BallClone != null && player1BallClone.transform.position.y < -16)
                    {
                        Destroy(player1BallClone);
                        player1Thrown     = false;
                        player1ThrowSpeed = new Vector3(0, 26, 40);//Reset perfect shot variable

                        /* Check if out of shots */

                        if (player1AvailableShots == 0)
                        {
                            player1Arrow.GetComponent <Renderer>().enabled = false;
                            Instantiate(gameOver, new Vector3(0.31f, 0.2f, 0), transform.rotation);
                            Invoke("restart", 2);
                        }
                    }
                    /* Remove Ball when it hits the floor */

                    if (player2BallClone != null && player2BallClone.transform.position.y < -16)
                    {
                        Destroy(player1BallClone);
                        player2Thrown     = false;
                        player2ThrowSpeed = new Vector3(0, 26, 40);//Reset perfect shot variable

                        /* Check if out of shots */

                        if (player2AvailableShots == 0)
                        {
                            player2Arrow.GetComponent <Renderer>().enabled = false;
                            Instantiate(gameOver, new Vector3(0.31f, 0.2f, 0), transform.rotation);
                            Invoke("restart", 2);
                        }
                    }
                }

                frame.Dispose();
                frame = null;
            }
        }
    }