Exemplo n.º 1
0
        public void Record(SkeletonFrame frame)
        {
            // Header
              writer.Write((int)KinectRecordOptions.Skeletons);

              // Data
              var time = DateTime.Now;
              TimeSpan timeSpan = time.Subtract(referenceTime);
              referenceTime = time;
              writer.Write((long)timeSpan.TotalMilliseconds);
              writer.Write((int)frame.TrackingMode);
              writer.Write(frame.FloorClipPlane.Item1);
              writer.Write(frame.FloorClipPlane.Item2);
              writer.Write(frame.FloorClipPlane.Item3);
              writer.Write(frame.FloorClipPlane.Item4);

              writer.Write(frame.FrameNumber);

              // Skeletons
              Skeleton[] skeletons = frame.GetSkeletons();
              frame.CopySkeletonDataTo(skeletons);

              BinaryFormatter formatter = new BinaryFormatter();
              formatter.Serialize(writer.BaseStream, skeletons);
        }
Exemplo n.º 2
0
        private void ExecutaRegraMaoDireitaAcimaDaCabeca(SkeletonFrame quadroAtual)
        {
            Skeleton[] esqueletos = new Skeleton[6];
            quadroAtual.CopySkeletonDataTo(esqueletos);

            Skeleton usuario = esqueletos.FirstOrDefault(esqueleto => esqueleto.TrackingState == SkeletonTrackingState.Tracked);

            if(usuario != null)
            {
                Joint maoDireita = usuario.Joints[JointType.HandRight];
                Joint maoEsquerda = usuario.Joints[JointType.HandLeft];
                Joint cabeca = usuario.Joints[JointType.Head];

                LmaoDireita.Content = maoDireita.Position.X.ToString();
                LmaoEsquerda.Content = maoEsquerda.Position.X.ToString();
                Lcabeca.Content = cabeca.Position.X.ToString();

                LmaoEsquerdaY.Content = maoEsquerda.Position.Y.ToString();
                LmaoDireitaY .Content = maoDireita.Position.Y.ToString();
                LcabecaY.Content = cabeca.Position.Y.ToString();

                bool novoTesteMaoDireitaAcimaCabeca = maoDireita.Position.Y > cabeca.Position.Y;

                if(MaoDireitaAcimaCabeca != novoTesteMaoDireitaAcimaCabeca)
                {
                    MaoDireitaAcimaCabeca = novoTesteMaoDireitaAcimaCabeca;

                    if(MaoDireitaAcimaCabeca)
                    {
                        MessageBox.Show("Mao direita acima da cabeca.");
                    }
                }
            }
        }
 /// <summary>
 /// Public constructor from SkeletonDataFrame
 /// </summary>
 /// <param name="skeletonFrame">Skeleton Frame</param>
 public SkeletonDataFrame(kinect.SkeletonFrame skeletonFrame)
 {
     this.FrameNumber    = skeletonFrame.FrameNumber;
     this.FloorClipPlane = skeletonFrame.FloorClipPlane;
     this.Timestamp      = skeletonFrame.Timestamp;
     this.SkeletonData   = new kinect.Skeleton[skeletonFrame.SkeletonArrayLength];
     skeletonFrame.CopySkeletonDataTo(this.SkeletonData);
 }
Exemplo n.º 4
0
Arquivo: Tools.cs Projeto: dtx/KMPC
        public static Skeleton[] GetSkeletons(SkeletonFrame frame)
        {
            if (frame == null)
                return null;

            var skeletons = new Skeleton[frame.SkeletonArrayLength];
            frame.CopySkeletonDataTo(skeletons);

            return skeletons;
        }
Exemplo n.º 5
0
Arquivo: Tools.cs Projeto: dtx/KMPC
        public static void GetSkeletons(SkeletonFrame frame, ref Skeleton[] skeletons)
        {
            if (frame == null)
                return;

            if (skeletons == null || skeletons.Length != frame.SkeletonArrayLength)
            {
                skeletons = new Skeleton[frame.SkeletonArrayLength];
            }
            frame.CopySkeletonDataTo(skeletons);
        }
Exemplo n.º 6
0
        public Skeleton GetFirstTrackedSkeleton(SkeletonFrame frame)
        {
            var skeletonData = new Skeleton[frame.SkeletonArrayLength];
            frame.CopySkeletonDataTo(skeletonData);

            var trackedSkel = TrackNearerSkeleton(skeletonData);

            var skeleton = (from s in skeletonData
                                 where s.TrackingState == SkeletonTrackingState.Tracked
                                    && s.TrackingId == trackedSkel
                                 select s).FirstOrDefault();
            return skeleton;
        }
Exemplo n.º 7
0
        /// <summary>
        /// Event handler for Kinect sensor's SkeletonFrameReady event
        /// </summary>
        /// <param name="sender">object sending the event</param>
        /// <param name="e">event arguments</param>
        private void SensorSkeletonFrameReady(object sender, kinect.SkeletonFrameReadyEventArgs e)
        {
            kinect.Skeleton[] skeletons = new kinect.Skeleton[0];

            using (kinect.SkeletonFrame skeletonFrame = e.OpenSkeletonFrame())
            {
                if (skeletonFrame != null)
                {
                    skeletons = new kinect.Skeleton[skeletonFrame.SkeletonArrayLength];
                    skeletonFrame.CopySkeletonDataTo(skeletons);
                }
            }

            processSkeletons(skeletons);
        }
Exemplo n.º 8
0
        void Context_AllFramesUpdated(KinectSensor sensor, ColorImageFrame cf, DepthImageFrame df, SkeletonFrame sf)
        {
            this.sensor = sensor;
            if (colorImage == null)
            {
                colorImage = new byte[cf.PixelDataLength];
                depthImage = new short[df.PixelDataLength];
                skeletonData = new Skeleton[sf.SkeletonArrayLength];
            }

            cf.CopyPixelDataTo(colorImage);
            df.CopyPixelDataTo(depthImage);
            sf.CopySkeletonDataTo(skeletonData);

            TrackFace();
        }
        private void DesenharEsqueletoUsuario(SkeletonFrame quadro)
        {
            if (quadro == null) return;

            using (quadro)
            {
                Skeleton[] esqueletos = new Skeleton[quadro.SkeletonArrayLength];
                quadro.CopySkeletonDataTo(esqueletos);
                IEnumerable<Skeleton> esqueletosRastreados = esqueletos.Where(esqueleto => esqueleto.TrackingState == SkeletonTrackingState.Tracked);
                if (esqueletosRastreados.Count() > 0)
                {
                    Skeleton esqueleto = esqueletosRastreados.First();
                    EsqueletoUsuarioAuxiliar funcoesEsqueletos = new EsqueletoUsuarioAuxiliar(kinect);
                    funcoesEsqueletos.DesenharArticulacao(esqueleto.Joints[JointType.HandRight], canvasKinect);
                    funcoesEsqueletos.DesenharArticulacao(esqueleto.Joints[JointType.HandLeft], canvasKinect);
                }
            }
        }
Exemplo n.º 10
0
        public void Record(SkeletonFrame frame)
        {
            if (writer == null)
                throw new Exception("You must call Start before calling Record");

            TimeSpan timeSpan = DateTime.Now.Subtract(referenceTime);
            referenceTime = DateTime.Now;
            writer.Write((long)timeSpan.TotalMilliseconds);
            writer.Write(frame.FloorClipPlane.Item1);
            writer.Write(frame.FloorClipPlane.Item2);
            writer.Write(frame.FloorClipPlane.Item3);
            writer.Write(frame.FloorClipPlane.Item4);

            Skeleton[] skeletons = Tools.GetSkeletons(frame);
            frame.CopySkeletonDataTo(skeletons);

            BinaryFormatter formatter = new BinaryFormatter();
            formatter.Serialize(writer.BaseStream, skeletons);
        }
 private void ExecutarRegraMaoDireitaAcimaDaCabeca(SkeletonFrame quadroAtual)
 {
     Skeleton[] esqueletos = new Skeleton[6];
     quadroAtual.CopySkeletonDataTo(esqueletos);
     Skeleton usuario = esqueletos.FirstOrDefault(esqueleto => esqueleto.TrackingState == SkeletonTrackingState.Tracked);
     if (usuario != null)
     {
         Joint maoDireita = usuario.Joints[JointType.HandRight];
         Joint cabeca = usuario.Joints[JointType.Head];
         bool novoTesteMaoDireitaAcimaCabeca = maoDireita.Position.Y > cabeca.Position.Y;
         if (MaoDireitaAcimaCabeca != novoTesteMaoDireitaAcimaCabeca)
         {
             MaoDireitaAcimaCabeca = novoTesteMaoDireitaAcimaCabeca;
             if (MaoDireitaAcimaCabeca)
             {
                 MessageBox.Show("A mão direita está acima da cabeça!");
             }
         }
     }
 }
Exemplo n.º 12
0
        public static ReplaySkeletonFrame FromSkeletonFrame(SkeletonFrame frame)
        {
            ReplaySkeleton[] skeletonData = new ReplaySkeleton[frame.SkeletonArrayLength];
            Skeleton[] frameSkeletonData = new Skeleton[frame.SkeletonArrayLength];
            frame.CopySkeletonDataTo(frameSkeletonData);

            for (int i = 0; i < frame.SkeletonArrayLength; i++)
            {
                Skeleton skeleton = frameSkeletonData[i];
                skeletonData[i] = ReplaySkeleton.FromSkeleton(skeleton);
            }

            return new ReplaySkeletonFrame()
            {
                FrameNumber = frame.FrameNumber,
                FloorClipPane = ReplaySkeletonFrameFloorClipPane.FromTuple(frame.FloorClipPlane),
                SkeletonArrayLength = frame.SkeletonArrayLength,
                SkeletonData = skeletonData,
                TrackingMode = frame.TrackingMode
            };
        }
Exemplo n.º 13
0
        public Option<HandInputEvent> Update(SkeletonFrame sf, byte[] cf)
        {
            if (sf != null) {
            if (skeletonData == null || skeletonData.Length != sf.SkeletonArrayLength) {
              skeletonData = new Skeleton[sf.SkeletonArrayLength];
            }

            sf.CopySkeletonDataTo(skeletonData);
            var trackedIndex = SkeletonUtil.FirstTrackedSkeletonIndex(skeletonData);
            if (trackedIndex >= 0) {
              var skeleton = skeletonData[trackedIndex];
              var handRight = SkeletonUtil.GetJoint(skeleton, JointType.HandRight);
              var handLeft = SkeletonUtil.GetJoint(skeleton, JointType.HandLeft);

              var handRightPos = coordMapper.MapSkeletonPointToDepthPoint(handRight.Position,
              DepthImageFormat.Resolution640x480Fps30);
              var handLeftPos = coordMapper.MapSkeletonPointToDepthPoint(handLeft.Position,
              DepthImageFormat.Resolution640x480Fps30);
              return new Some<HandInputEvent>(new HandInputEvent(handLeftPos, handRightPos));
            }
              }
              return new None<HandInputEvent>();
        }
Exemplo n.º 14
0
        private WriteableBitmap bmpWithSkelFromColor(WriteableBitmap bmpSource, SkeletonFrame SkelFrame)
        {
            bmpSource = BitmapFactory.ConvertToPbgra32Format(bmpSource);
            Skeletons = new Skeleton[SkelFrame.SkeletonArrayLength];
            SkelFrame.CopySkeletonDataTo(Skeletons);
            //Go through and draw active skeleton
            foreach (Skeleton Skel in Skeletons)
            {
                if (Skel.TrackingState == SkeletonTrackingState.Tracked)
                {
                    //iterate through each joint in the skeleton
                    foreach (Joint skeljoint in Skel.Joints)
                    {
                        SkeletonPoint JointLocation = Skel.Joints[skeljoint.JointType].Position;

                        ColorImagePoint Cloc = SkeletonPointToColorImage(JointLocation, ColorImageFormat.RgbResolution640x480Fps30);
                        bmpSource.FillRectangle((Cloc.X - 5), (Cloc.Y - 5), (Cloc.X + 5), (Cloc.Y + 5), Colors.Purple);
                    }
                    foreach (BoneOrientation orientation in Skel.BoneOrientations)
                    {
                    }
                }
            }
            return bmpSource;
        }
Exemplo n.º 15
0
        //Takes a skeleton frame and an activity step, and checks if it is correct
        public Boolean IsSkeletonMatch(SkeletonFrame skelFrame, Kinect.ActivityStep currentStep)
        {
            if (currentStep == null)
            {
                return false;
            }

            Skeletons = new Skeleton[skelFrame.SkeletonArrayLength];
            skelFrame.CopySkeletonDataTo(Skeletons);
            //flag, so that we can return false if no skeleton compared
            bool isGoodJoint = false;
            //flag to set as false if a joint comparison isn't run

            foreach (Skeleton Skel in Skeletons)
            {
                if (Skel.TrackingState == SkeletonTrackingState.NotTracked || Skel.TrackingState == SkeletonTrackingState.PositionOnly)
                {
                    continue;
                }

                if (currentStep.usesNewRotationFormat)
                {
                    foreach (Kinect.BoneOrientationCharacteristics boneIdealBone in currentStep.BoneComparison) //new format
                    {
                        if (boneIdealBone == null)
                        {
                            continue;
                        }
                        Joint jnt1 = new Joint();
                        Joint jnt2 = new Joint();
                        foreach (Joint skelJoint in Skel.Joints)
                        {
                            if (skelJoint.JointType == boneIdealBone.FirstJoint)
                            {
                                jnt1 = skelJoint;
                            }
                            if (skelJoint.JointType == boneIdealBone.SecondJoint)
                            {
                                jnt2 = skelJoint;
                            }
                        }
                        boneIdealBone.tempMatched = boneIdealBone.boolBoneOrientationIsMatch(jnt1, jnt2);
                    }
                    foreach (Kinect.BoneOrientationCharacteristics boneIdealBone in currentStep.BoneComparison)
                    {
                        if (boneIdealBone == null)
                        {
                            continue;
                        }
                        if (boneIdealBone.tempMatched)
                        {
                            isGoodJoint = true;
                        }
                        else
                        {
                            return false;
                        }
                    }
                }
                else //Old format comparison
                {
                    bool jointflag = false;
                    foreach (Kinect.JointOrientationCharacteristics jntIdealJoint in currentStep.JointComparison)
                    {
                        if (jntIdealJoint != null)
                        {
                            foreach (Joint skelJoint in Skel.Joints)
                            {
                                isGoodJoint = true;
                                if (skelJoint.JointType == jntIdealJoint.JointTypeID)
                                {
                                    jointflag = true;
                                    //run comparison and stop comparing if
                                    if (compareIsSame(skelJoint, jntIdealJoint) == false)
                                    {
                                        //stop checking through bones, comparison doesn't work
                                        return false;
                                    }
                                    break;
                                }
                            }
                            if (jointflag == false)
                            {
                                //returns false if not every joint has been available for comparison
                                return false;
                            }
                        }
                    }
                }
            }

            //return true if comparison run and no false joints found
            if (isGoodJoint == true)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
Exemplo n.º 16
0
        /// <summary>
        /// 頭の位置を返す
        /// </summary>
        /// <param name="skeletonFrame"></param>
        /// <returns></returns>
        private List<Tuple<SkeletonPoint, Matrix4>> GetHeadPoints( SkeletonFrame skeletonFrame )
        {
            // 頭の位置のリストを作成
            List<Tuple<SkeletonPoint, Matrix4>> headPoints = new List<Tuple<SkeletonPoint, Matrix4>>();

            // 骨格情報をバッファにコピー
            skeletonFrame.CopySkeletonDataTo( skeletonBuffer );

            // 取得できた骨格ごとにループ
            foreach ( Skeleton skeleton in skeletonBuffer ) {
                // トラッキングできていない骨格は処理しない
                if ( skeleton.TrackingState != SkeletonTrackingState.Tracked ) {
                    continue;
                }

                // 頭の位置を取得する
                Joint head = skeleton.Joints[JointType.Head];

                // 頭の位置をトラッキングしていない場合は処理しない
                if ( head.TrackingState == JointTrackingState.NotTracked ) {
                    continue;
                }

                // 頭の位置と向きを保存する
                headPoints.Add( Tuple.Create( head.Position,
                    skeleton.BoneOrientations[JointType.Head].AbsoluteRotation.Matrix ) );
            }

            return headPoints;
        }
Exemplo n.º 17
0
        //Skeleton[] fixSkleton = new Skeleton[1];
		public void Record(SkeletonFrame frame,KinectSensor psensor)
		{
			writer.Write((int) FrameType.Skeletons);

			var timeSpan = DateTime.Now.Subtract(referenceTime);
			referenceTime = DateTime.Now;
			writer.Write((long) timeSpan.TotalMilliseconds);
			writer.Write((int) frame.TrackingMode);
			writer.Write(frame.FloorClipPlane.Item1);
			writer.Write(frame.FloorClipPlane.Item2);
			writer.Write(frame.FloorClipPlane.Item3);
			writer.Write(frame.FloorClipPlane.Item4);

			writer.Write(frame.FrameNumber);

            //var skeletons = frame.GetSkeletons();

            frame.CopySkeletonDataTo(totalSkeleton);
            firstSkeleton = (from trackskeleton in totalSkeleton 
                             where trackskeleton.TrackingState 
                             == SkeletonTrackingState.Tracked 
                             select trackskeleton).FirstOrDefault();


            if ( firstSkeleton !=null )
            {
                if (firstSkeleton.Joints[JointType.Spine].TrackingState == JointTrackingState.Tracked)
                {
                    tmpHandRight = psensor.CoordinateMapper.
                        MapSkeletonPointToColorPoint(
                        firstSkeleton.Joints[JointType.HandRight].Position,
                        ColorImageFormat.RgbResolution640x480Fps30);

                    tmpHandLeft = psensor.CoordinateMapper.
                        MapSkeletonPointToColorPoint(
                        firstSkeleton.Joints[JointType.HandLeft].Position,
                        ColorImageFormat.RgbResolution640x480Fps30);

                    tmpSpine = psensor.CoordinateMapper.
                        MapSkeletonPointToColorPoint(
                        firstSkeleton.Joints[JointType.Spine].Position,
                        ColorImageFormat.RgbResolution640x480Fps30);

                    writer.Write(tmpHandRight.X);
                    writer.Write(tmpHandRight.Y);
                    writer.Write(tmpHandLeft.X);
                    writer.Write(tmpHandLeft.Y);
                    writer.Write(tmpSpine.X);
                    writer.Write(tmpSpine.Y);
                    writer.Write(true); // is skleton detected
                    //Console.WriteLine("spine x"+tmpSpine.X);
                    //Console.WriteLine("spine y" + tmpSpine.Y);
                    //Console.WriteLine("skleton detected");
                }
                else
                {
                    writer.Write(0);
                    writer.Write(0);
                    writer.Write(0);
                    writer.Write(0);
                    writer.Write(0);
                    writer.Write(0);
                    writer.Write(false); // is skleton detected
                    //Console.WriteLine("skleton NOT DETECTE222");
                }
            }
            else
            {
                writer.Write(0);
                writer.Write(0);
                writer.Write(0);
                writer.Write(0);
                writer.Write(0);
                writer.Write(0);
                writer.Write(false); // is skleton detected
                //Console.WriteLine("skleton NOT DETECTE");
            }
            

            //frame.CopySkeletonDataTo(skeletons);

            //var formatter = new BinaryFormatter();
            //formatter.Serialize(writer.BaseStream, skeletons);
		}
Exemplo n.º 18
0
        /// <summary>
        /// スケルトンを描画する
        /// </summary>
        /// <param name="kinect"></param>
        /// <param name="skeletonFrame"></param>
        /// <param name="source"></param>
        /// <returns></returns>
        private RenderTargetBitmap DrawSkeleton( KinectSensor kinect,
      SkeletonFrame skeletonFrame, ImageSource source )
        {
            // スケルトンのデータを取得する
              Skeleton[] skeletons = new Skeleton[skeletonFrame.SkeletonArrayLength];
              skeletonFrame.CopySkeletonDataTo( skeletons );

              DrawingVisual drawingVisual = new DrawingVisual();
              using ( DrawingContext drawingContext = drawingVisual.RenderOpen() ) {
            // ImageSourceを描画する
            drawingContext.DrawImage( source, new Rect( 0, 0, source.Width, source.Height ) );

            // トラッキングされているスケルトンのジョイントを描画する
            const int R = 5;
            foreach ( var skeleton in skeletons ) {
              // スケルトンがトラッキングされていなければ次へ
              if ( skeleton.TrackingState != SkeletonTrackingState.Tracked ) {
            continue;
              }

              // ジョイントを描画する
              foreach ( Joint joint in skeleton.Joints ) {
            // ジョイントがトラッキングされていなければ次へ
            if ( joint.TrackingState != JointTrackingState.Tracked ) {
              continue;
            }

            // スケルトンの座標を、RGBカメラの座標に変換して円を書く
            ColorImagePoint point = kinect.MapSkeletonPointToColor( joint.Position,
              kinect.ColorStream.Format );
            drawingContext.DrawEllipse( new SolidColorBrush( Colors.Red ),
                new Pen( Brushes.Red, 1 ), new Point( point.X, point.Y ), R, R );
              }
            }
              }

              // 描画可能なビットマップを作る
              RenderTargetBitmap bitmap = new RenderTargetBitmap( (int)source.Width, (int)source.Height,
            96, 96, PixelFormats.Default );
              bitmap.Render( drawingVisual );
              return bitmap;
        }
Exemplo n.º 19
0
        public void updategame(float gtime)
        {
            // Variable to generate random numbers
            Random randomiser = new Random();

            countdown -= gtime;

            // Main game code
            // Game is being played
            switch (ingamestate)
            {
                case 1:
                    // Round is about to start

                    // Allow user to tilt camera using arrow keys
                    if (cameracounter < 0)
                    {
                        cameracounter = 1500;
                        cameraangle = kfunctions.cameramove(kinectSensor, cameraangle);
                    }
                    cameracounter -= gtime;

                    // If round 1 is just about to begin then show video feed rather than frozen image
                    if (angletoguess == 0)
                    {
                        videoframe = kinectSensor.ColorStream.OpenNextFrame(0);
                        if (videoframe != null)
                        {
                            kinectRGBVideo = kfunctions.video2texture(graphics, videoframe);
                          //  kinectRGBVideo = kfunctions.video2texture(graphics, videoframe, vframe, vcolour);
                        }
                    }

                    // Once counter has reached zero begin round
                    if (countdown < 0)
                    {
                        if (round>0) funnypics[round - 1] = kinectRGBVideo;
                        if (saveallpics) savepictures();
                        ingamestate = 2;
                        resetround(gtime);
                    }

                    break;

                case 2:
                    // Round is being played

                    // Stream video from Kinect into imageframes
                    videoframe = kinectSensor.ColorStream.OpenNextFrame(0);
                    if (videoframe != null)
                    {
                        kinectRGBVideo = kfunctions.video2texture(graphics, videoframe);
                       // kinectRGBVideo = kfunctions.video2texture(graphics, videoframe, vframe, vcolour);
                    }

                    // Read Kinect Body sensor
                    body = kinectSensor.SkeletonStream.OpenNextFrame(0);
                    if (body != null)
                    {
                        // Get skeleton body data
                        Skeleton[] bodyskel = new Skeleton[body.SkeletonArrayLength];
                        body.CopySkeletonDataTo(bodyskel);

                        // Read Kinect and look for right hand movements on both players
                        int[] angles = new int[2];
                        kfunctions.readarmangle(bodyskel, JointType.HandRight, JointType.ElbowRight, out angles[0], out angles[1]);

                        // Handle lefties
                        int angletemp = 0;
                        if (gamer[0].lefthanded)
                            kfunctions.readarmangle(bodyskel, JointType.HandLeft, JointType.ElbowLeft, out angles[0], out angletemp);
                        if (gamer[1].lefthanded)
                            kfunctions.readarmangle(bodyskel, JointType.HandLeft, JointType.ElbowLeft, out angletemp, out angles[1]);

                        angles[0] = (int)(Math.Round((float)angles[0] / (float)round2nearest, 0) * round2nearest);
                        angles[1] = (int)(Math.Round((float)angles[1] / (float)round2nearest, 0) * round2nearest);

                        for (int i = 0; i < numberofplayers; i++)
                        {
                            if (gamer[i].countdown == handconfirmtime)
                            {
                                gamer[i].angle = angles[i];
                            }
                        }

                        // Check if either player has raised their hand
                        gestures.handup(bodyskel, false, out gamer[0].handisup, out gamer[1].handisup);

                        // Handle lefties
                        Boolean temphandup = false;
                        if (gamer[0].lefthanded)
                            gestures.handup(bodyskel, true, out gamer[0].handisup, out temphandup);
                        if (gamer[1].lefthanded)
                            gestures.handup(bodyskel, true, out temphandup, out gamer[1].handisup);
                    }

                    checkforhands(gtime);

                    if ((gamer[0].roundover && numberofplayers==1) || (gamer[0].roundover && gamer[1].roundover) || countdown < 0)
                    {
                        ingamestate = 1;
                        countdown = delaybetween;
                        for (int i = 0; i < numberofplayers; i++)
                        {
                            if (questiontype == 0)
                            {
                                gamer[i].score += angle2percentage(360 - Math.Abs((int)angletoguess - (int)gamer[i].angle));
                            }
                            else if (questiontype == 1 || questiontype == 3)
                            {
                                gamer[i].angle = angle2percentage((int)gamer[i].angle);
                                gamer[i].score += (100 - Math.Abs((int)angletoguess - (int)gamer[i].angle));
                            }
                            else if (questiontype == 2)
                            {
                                gamer[i].angle = angle2percentage((int)gamer[i].angle);
                                gamer[i].score += (100 - Math.Abs((int)(angletoguess * 100) - (int)gamer[i].angle));
                                gamer[i].angle /= 100f;
                            }

                            // Bonus points if they get the angle exactly right
                            if (gamer[i].angle == angletoguess)
                            {
                                gamer[i].score += bonuspoints;
                                applause[randomiser.Next(numberofsounds)].Play();
                            }
                        }
                    }

                    break;


                default:
                    // Game is over
                    piccounter += gtime;
                    if (piccounter >= questions2ask*2000)
                        piccounter = 0;

                    //if (keyboardreleased && gamer[0].score > highscores[9] && gamer[0].playername.Length < 12)
                    if (keyboardreleased && keyfocus==1)
                    {
                        if (keys.IsKeyDown(Keys.Enter))
                        {
                            if (gamer[1].score > highscores[9])
                                keyfocus = 2;
                            else
                                keyfocus = 0;
                        }
                        else if (keys.IsKeyDown(Keys.Back) && gamer[0].playername.Length > 0)
                        {
                            gamer[0].playername = gamer[0].playername.Substring(0, gamer[0].playername.Length - 1);
                        }
                        else
                        {
                            char nextchar = sfunctions.getnextkey();
                            if (nextchar != '!')
                            {
                                gamer[0].playername += nextchar;
                                if (gamer[0].playername.Length > 15)
                                    gamer[0].playername = gamer[0].playername.Substring(0, 15);
                            }
                        }
                    }
                    else if (keyboardreleased && keyfocus==2)
                    {
                        if (keys.IsKeyDown(Keys.Enter))
                        {
                            keyfocus = 0;
                        }
                        else if (keys.IsKeyDown(Keys.Back) && gamer[1].playername.Length > 0)
                        {
                            gamer[1].playername = gamer[1].playername.Substring(0, gamer[1].playername.Length - 1);
                        }
                        else
                        {
                            char nextchar = sfunctions.getnextkey();
                            if (nextchar != '!')
                            {
                                gamer[1].playername += nextchar;
                                if (gamer[1].playername.Length > 15)
                                    gamer[1].playername = gamer[1].playername.Substring(0, 15);
                            }
                        }
                    }

                    // Allow game to return to the main menu
                    if (keys.IsKeyDown(Keys.Escape))
                    {
                        if (gamer[0].score > highscores[9])
                        {
                            highscores[9] = gamer[0].score;
                            highscorenames[9] = gamer[0].playername.Trim();
                        }
                        // Sort the high score table
                        Array.Sort(highscores, highscorenames);
                        Array.Reverse(highscores);
                        Array.Reverse(highscorenames);

                        if (gamer[1].score > highscores[9])
                        {
                            highscores[9] = gamer[1].score;
                            highscorenames[9] = gamer[1].playername.Trim();
                        }
                        // Sort the high score table
                        Array.Sort(highscores, highscorenames);
                        Array.Reverse(highscores);
                        Array.Reverse(highscorenames);

                        gamestate = -1;
                    }

                    break;
            }

            if (keys.IsKeyDown(Keys.Escape) || pad[0].Buttons.Back == ButtonState.Pressed)
                ingamestate = 3;    // End Game
        }
        /// <summary>
        /// set frame data to data warehouse and return whether the data is valid
        /// </summary>
        /// <param name="sf"></param>
        /// <returns>whether the data is successfully stored</returns>
        public bool SetSkeletonFrameData(SkeletonFrame sf)
        {
            Skeleton[] skeletons = new Skeleton[sf.SkeletonArrayLength];
            sf.CopySkeletonDataTo(skeletons);
            foreach (Skeleton sk in skeletons)
            {
                if (sk.TrackingState == SkeletonTrackingState.Tracked)
                {
                    m_frameData.Add(new FrameData(++m_currentFrame));//first frame is frame 1
                    m_frameData[m_currentFrame].m_Player1.m_position = UtilityTools.SkeletonPointToVector3(sk.Position);
                    m_frameData[m_currentFrame].m_Player1.m_skeleton = sk;
                    //Console.WriteLine(sk.Position.X);
                    return true;
                }

            }
            return false;
        }
Exemplo n.º 21
0
        void updateAllFrames(ColorImageFrame inColor,DepthImageFrame inDepth,SkeletonFrame inSkelton)
        {
            if(inColor!=null)
            {
                inColor.CopyPixelDataTo(this.colorKinectData);
                for (int i = 0; i < this.colorData.Length/4;++i)
                {
                    this.colorData[i * 4 + 0] = this.colorKinectData[i * 4 + 2];
                    this.colorData[i * 4 + 1] = this.colorKinectData[i * 4 + 1];
                    this.colorData[i * 4 + 2] = this.colorKinectData[i * 4 + 0];
                    this.colorData[i * 4 + 3] = 255;
                }

                if (inDepth != null)
                {
                    inDepth.CopyPixelDataTo(this.depthData);
                    kinectSensor.MapDepthFrameToColorFrame(inDepth.Format, depthData, inColor.Format, mappedDepthData);
#if USE_COLOR_MAP
#if USE_PARALLEL
                    Parallel.For(0, mappedDepthData.Length, i =>
                    {
                        ColorImagePoint imagePoint = mappedDepthData[i];
                        imagePoint.X = (int)MathHelper.Clamp(imagePoint.X, 0, WIDTH - 1);
                        imagePoint.Y = (int)MathHelper.Clamp(imagePoint.Y, 0, HEIGHT - 1);
                        mappedColorData[imagePoint.Y * WIDTH + imagePoint.X].X = i % WIDTH;
                        mappedColorData[imagePoint.Y * WIDTH + imagePoint.X].Y = i / WIDTH;
                    });
#else
                    for(int i=0;i<mappedDepthData.Length;++i)
                    {
                        ColorImagePoint imagePoint = mappedDepthData[i];
                        imagePoint.X = (int)MathHelper.Clamp(imagePoint.X, 0, WIDTH - 1);
                        imagePoint.Y = (int)MathHelper.Clamp(imagePoint.Y, 0, HEIGHT - 1);
                        mappedColorData[imagePoint.Y * WIDTH + imagePoint.X].X=i%WIDTH;
                        mappedColorData[imagePoint.Y * WIDTH + imagePoint.X].Y=i/WIDTH;
                    }
#endif
#endif
                }
                if (inSkelton != null)
                {
                    if (this.skeletonData == null || this.skeletonData.Length != inSkelton.SkeletonArrayLength)
                    {
                        this.skeletonData = new Skeleton[inSkelton.SkeletonArrayLength];
                    }
                    inSkelton.CopySkeletonDataTo(this.skeletonData);
                }
            }
        }
Exemplo n.º 22
0
        /// <summary>
        /// スケルトンを描画する
        /// </summary>
        /// <param name="kinect"></param>
        /// <param name="skeletonFrame"></param>
        private void DrawSkeleton( KinectSensor kinect, SkeletonFrame skeletonFrame )
        {
            // スケルトンのデータを取得する
              Skeleton[] skeletons = new Skeleton[skeletonFrame.SkeletonArrayLength];
              skeletonFrame.CopySkeletonDataTo( skeletons );

              canvasSkeleton.Children.Clear();

              // トラッキングされているスケルトンのジョイントを描画する
              for ( int i = 0; i < skeletons.Length; i++ ) {
            // プレーヤーインデックスは、skeleton配列のインデックス + 1
            int playerIndex = i + 1;

            // プレイヤー位置を初期化する
            playerAngles[playerIndex] = -1;

            // スケルトンがトラッキング状態でなければ次へ
            if ( skeletons[i].TrackingState != SkeletonTrackingState.Tracked ) {
              continue;
            }

            Skeleton skeleton = skeletons[i];

            // 頭の位置を取得する
            Joint joint = skeleton.Joints[JointType.Head];
            if ( joint.TrackingState == JointTrackingState.NotTracked ) {
              continue;
            }

            // ジョイントの座標を描く
            DrawEllipse( kinect, joint.Position );

            // プレイヤーの角度を取得する
            playerAngles[playerIndex] = GetPlayerAngle( joint );

              }
        }
Exemplo n.º 23
0
        private List<SkeletonPoint> GetHipCenterPointList(SkeletonFrame skeletonFrame)
        {
            List<SkeletonPoint> hipCenterPointList = new List<SkeletonPoint>();

            skeletonFrame.CopySkeletonDataTo(this.skeletonBuffer);

            foreach (Skeleton skeleton in this.skeletonBuffer)
            {
                if (skeleton.TrackingState != SkeletonTrackingState.Tracked)
                    continue;

                Joint hipCenter = skeleton.Joints[JointType.HipCenter];

                if (hipCenter.TrackingState == JointTrackingState.NotTracked)
                    continue;

                hipCenterPointList.Add(hipCenter.Position);
            }

            return hipCenterPointList;
        }
Exemplo n.º 24
0
        Skeleton getSkeleton(SkeletonFrame skeletonFrame)
        {
            skeletonFrame.CopySkeletonDataTo(allSkeletons);
            

            Skeleton user_skeleton = null;
            float depth = float.MaxValue;
            foreach (Skeleton sk in allSkeletons)
            {
                if (sk.TrackingState == SkeletonTrackingState.Tracked)
                {
                    if (sk.Joints[JointType.HipCenter].Position.Z < depth)
                    {
                        // Console.WriteLine(depth);
                        user_skeleton = sk;
                        depth = sk.Joints[JointType.HipCenter].Position.Z;
                    }
                }
            }

            return user_skeleton;
        }
Exemplo n.º 25
0
        /// <summary>
        /// 頭の位置を返す
        /// </summary>
        /// <param name="skeletonFrame"></param>
        /// <returns></returns>
        private List<Player> GetHeadPoints( SkeletonFrame skeletonFrame )
        {
            // 頭の位置のリストを作成
            List<Player> headPoints = new List<Player>();

            // 骨格情報をバッファにコピー
            skeletonFrame.CopySkeletonDataTo( skeletonBuffer );

            // 取得できた骨格ごとにループ
            for ( int i = 0; i < skeletonBuffer.Length; i++ ) {
                Skeleton skeleton = skeletonBuffer[i];
                // トラッキングできていない骨格は処理しない
                if ( skeleton.TrackingState != SkeletonTrackingState.Tracked ) {
                    continue;
                }

                // 頭の位置を取得する
                Joint head = skeleton.Joints[JointType.Head];

                // 頭の位置をトラッキングしていない場合は処理しない
                if ( head.TrackingState == JointTrackingState.NotTracked ) {
                    continue;
                }

                // 頭の位置と向きを保存する
                headPoints.Add( new Player() {
                    HeadPosition = head.Position,
                    HeadRotation = skeleton.BoneOrientations[JointType.Head].AbsoluteRotation.Matrix,
                    Posture = playerGesture[i].Update( skeleton ),
                } );
            }

            return headPoints;
        }
Exemplo n.º 26
0
        private void processSkeleton(SkeletonFrame frame)
        {
            Skeleton[] skeletons = new Skeleton[frame.SkeletonArrayLength];
            frame.CopySkeletonDataTo(skeletons);

            //      Console.WriteLine("Skeletons:  " + skeletons.Where(S => S.TrackingState == SkeletonTrackingState.Tracked).Count() );

            Skeleton person = null;
            if ( (person = skeletons.Where(s => s.TrackingState == SkeletonTrackingState.Tracked).FirstOrDefault()) != null)
            {
            Console.WriteLine("Doing it, doing it!!");
            double person_core_x = Math.Round(person.Position.X, 1);
            double person_core_y = Math.Round(person.Position.Y, 1);
            double person_hand_l_x = Math.Round(person.Joints.Where(Joint => Joint.JointType == JointType.HandLeft).First().Position.X, 2);
            double person_hand_l_y = Math.Round(person.Joints.Where(Joint => Joint.JointType == JointType.HandLeft).First().Position.Y, 2);
            double person_hand_r_x = Math.Round(person.Joints.Where(Joint => Joint.JointType == JointType.HandRight).First().Position.X, 2);
            double person_hand_r_y = Math.Round(person.Joints.Where(Joint => Joint.JointType == JointType.HandRight).First().Position.Y, 2);

            label3.Content = (
                "Sensor: " + Math.Round(this.x_actual,2) + ", " + Math.Round(this.y_actual,2) +
                " Core: " + person_core_x + ", " + person_core_y +
                " LH: " + person_hand_l_x + ", " + person_hand_l_y +
                " RH: " + person_hand_r_x + ", " + person_hand_r_y
                );
            }
            else
            {
            Console.WriteLine("Totally gone wrong.");
            this.label3.Content = (
                "Sensor: " + Math.Round(this.x_actual,1) + ", " + Math.Round(this.y_actual,1) + " - no skeletons found"
                );
            }
        }
Exemplo n.º 27
0
        public void copyDataTo(SkeletonFrame frame)
        {
            // process the frames here , get all the tracked Joints and add them to Database.

            // dont copy first 60 frames used to stabilize skeletons and filter out garbage data
            if (++this.framesSkipped <= INITIAL_NUM_OF_FRAMES_TO_SKIP)
            {
                return;
            }

            frame.CopySkeletonDataTo(tempSkeletonFrames);
            for (int i = 0; i < 6; i++)
            {
                tempSkeleton = tempSkeletonFrames[i];
                if (tempSkeleton.TrackingState == SkeletonTrackingState.Tracked)
                {

                    aptJoint = trackAptJoints(tempSkeleton);
                    if (aptJoint == null) continue;
                    int count = gestureDatabase.addToDatabase(tempSkeleton, aptJoint);
                    onProcessGestures();

                }
            }
        }
        private void FuncoesEsqueletoUsuario(SkeletonFrame quadro)
        {
            if (quadro == null) return;

            using (quadro)
            {
                Skeleton esqueletoUsuario = quadro.ObterEsqueletoUsuario();

                if (btnDesenhar.IsChecked)
                {
                    Skeleton[] esqueletos = new Skeleton[6];
                    quadro.CopySkeletonDataTo(esqueletos);
                    fluxoInteracao.ProcessSkeleton(esqueletos, kinect.AccelerometerGetCurrentReading(), quadro.Timestamp);
                    EsqueletoUsuarioAuxiliar esqueletoAuxiliar = new EsqueletoUsuarioAuxiliar(kinect);

                    if (configuracaoMaoDireita.DesenhoAtivo)
                        esqueletoAuxiliar.InteracaoDesenhar(esqueletoUsuario.Joints[JointType.HandRight], canvasDesenho, configuracaoMaoDireita);

                    if (configuracaoMaoEsquerda.DesenhoAtivo)
                        esqueletoAuxiliar.InteracaoDesenhar(esqueletoUsuario.Joints[JointType.HandLeft], canvasDesenho, configuracaoMaoEsquerda);
                }
                else
                {
                    foreach (IRastreador rastreador in rastreadores)
                        rastreador.Rastrear(esqueletoUsuario);

                    if (btnEsqueletoUsuario.IsChecked)
                        quadro.DesenharEsqueletoUsuario(kinect, canvasKinect);
                }
            }
        }
Exemplo n.º 29
0
        /// <summary>
        /// 距離データをカラー画像に変換する
        /// </summary>
        /// <param name="kinect"></param>
        /// <param name="depthFrame"></param>
        /// <returns></returns>
        private void HeightMeasure( KinectSensor kinect, DepthImageFrame depthFrame, SkeletonFrame skeletonFrame )
        {
            ColorImageStream colorStream = kinect.ColorStream;
              DepthImageStream depthStream = kinect.DepthStream;

              // トラッキングされている最初のスケルトンを取得する
              // インデックスはプレイヤーIDに対応しているのでとっておく
              Skeleton[] skeletons = new Skeleton[skeletonFrame.SkeletonArrayLength];
              skeletonFrame.CopySkeletonDataTo( skeletons );

              int playerIndex = 0;
              for ( playerIndex = 0; playerIndex < skeletons.Length; playerIndex++ ) {
            if ( skeletons[playerIndex].TrackingState == SkeletonTrackingState.Tracked ) {
              break;
            }
              }
              if ( playerIndex == skeletons.Length ) {
            return;
              }

              // トラッキングされている最初のスケルトン
              Skeleton skeleton = skeletons[playerIndex];

              // 実際のプレイヤーIDは、スケルトンのインデックス+1
              playerIndex++;

              // 頭、足先がトラッキングされていない場合は、そのままRGBカメラのデータを返す
              Joint head = skeleton.Joints[JointType.Head];
              Joint leftFoot = skeleton.Joints[JointType.FootLeft];
              Joint rightFoot = skeleton.Joints[JointType.FootRight];
              if ( (head.TrackingState != JointTrackingState.Tracked) ||
               (leftFoot.TrackingState != JointTrackingState.Tracked) ||
               (rightFoot.TrackingState != JointTrackingState.Tracked) ) {
             return;
              }

              // 距離カメラのピクセルごとのデータを取得する
              short[] depthPixel = new short[depthFrame.PixelDataLength];
              depthFrame.CopyPixelDataTo( depthPixel );

              // 距離カメラの座標に対応するRGBカメラの座標を取得する(座標合わせ)
              ColorImagePoint[] colorPoint = new ColorImagePoint[depthFrame.PixelDataLength];
              kinect.MapDepthFrameToColorFrame( depthStream.Format, depthPixel,
            colorStream.Format, colorPoint );

              // 頭のてっぺんを探す
              DepthImagePoint headDepth = depthFrame.MapFromSkeletonPoint( head.Position );
              int top = 0;
              for ( int i = 0; (headDepth.Y - i) > 0; i++ ) {
            // 一つ上のY座標を取得し、プレイヤーがいなければ、現在の座標が最上位
            int index = ((headDepth.Y - i) * depthFrame.Width) + headDepth.X;
            int player = depthPixel[index] & DepthImageFrame.PlayerIndexBitmask;
            if ( player == playerIndex ) {
              top = i;
            }
              }

              // 頭のてっぺんを3次元座標に戻し、足の座標(下にあるほう)を取得する
              // この差分で身長を測る
              head.Position = depthFrame.MapToSkeletonPoint( headDepth.X, headDepth.Y - top );
              Joint foot = (leftFoot.Position.Y < rightFoot.Position.Y) ? leftFoot : rightFoot;

              // 背丈を表示する
              DrawMeasure( kinect, colorStream, head, foot );
        }
Exemplo n.º 30
0
        int[] frameToHuman(SkeletonFrame frame)
        {
            int[] pos = new int[3];
            Skeleton[] skeletons = null;
            if (frame != null)
            {
                if (skeletons == null)
                {
                    // Allocate array of skeletons
                    skeletons = new Skeleton[frame.SkeletonArrayLength];
                }

                // Copy skeletons from this frame
                frame.CopySkeletonDataTo(skeletons);

                // Find first tracked skeleton, if any
                Skeleton skeleton = skeletons.Where(s => s.TrackingState == SkeletonTrackingState.Tracked).FirstOrDefault();

                if (skeleton != null)
                {
                    // Obtain the left knee joint; if tracked, print its position
                    Joint j = skeleton.Joints[JointType.KneeLeft];

                    if (j.TrackingState == JointTrackingState.Tracked)
                    {
                        pos[2] = (int)(j.Position.Z * 1000);
                        pos[0] = (int)(640 * (j.Position.X + 2.0) / 4.0) + (int)(640 / 2);
                        pos[1] = (int)(480 * (j.Position.Y + 2.0) / 4.0) + (int)(480.0 / 2.0);
                        return pos;
                    }
                    else
                    {
                        return null;
                    }
                }
                else
                {
                    return null;
                }
            }
            else
            {
                return null;
            }
        }
Exemplo n.º 31
0
        /// <summary>
        /// スケルトンを描画する
        /// </summary>
        /// <param name="kinect"></param>
        /// <param name="skeletonFrame"></param>
        private void DrawSkeleton( KinectSensor kinect, SkeletonFrame skeletonFrame )
        {
            // スケルトンのデータを取得する
              Skeleton[] skeletons = new Skeleton[skeletonFrame.SkeletonArrayLength];
              skeletonFrame.CopySkeletonDataTo( skeletons );

              canvasSkeleton.Children.Clear();

              // トラッキングされているスケルトンのジョイントを描画する
              foreach ( Skeleton skeleton in skeletons ) {
            // スケルトンがトラッキング状態(デフォルトモード)の場合は、ジョイントを描画する
            if ( skeleton.TrackingState == SkeletonTrackingState.Tracked ) {
              // ジョイントを描画する
              foreach ( Joint joint in skeleton.Joints ) {
            // ジョイントがトラッキングされていなければ次へ
            if ( joint.TrackingState == JointTrackingState.NotTracked ) {
              continue;
            }

            // ジョイントの座標を描く
            DrawEllipse( kinect, joint.Position );
              }
            }
            // スケルトンが位置追跡(ニアモードの)の場合は、スケルトン位置(Center hip)を描画する
            else if ( skeleton.TrackingState == SkeletonTrackingState.PositionOnly ) {
              // スケルトンの座標を描く
              DrawEllipse( kinect, skeleton.Position );
            }
              }
        }
        private void ProcessFrame(SkeletonFrame skeletonFrame)
        {
            if (skeletonFrame == null)
            {
                return;
            }

            if (_skeletons == null || _skeletons.Length != skeletonFrame.SkeletonArrayLength)
            {
                _skeletons = new Skeleton[skeletonFrame.SkeletonArrayLength];
            }
            skeletonFrame.CopySkeletonDataTo(_skeletons);
            _skeletonFrameTimestamp = skeletonFrame.Timestamp;
        }