Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Kinect - Detecting when a Human exits the frame

Tags:

c#

kinect

So what I'm trying to do is take the Kinect Skeletal Sample and save x amount of photos, only when a human goes by. I have gotten it to work, except once it detects a human it just records x amount of photos even once the person leaves the vision of Kinect. Does anyone know how to make it so that once a person enters it starts recording, and once they leave it stops?

Variables

   Runtime nui;
    int totalFrames = 0;
    int totalFrames2 = 0;
    int lastFrames = 0;
    int lastFrameWithMotion = 0;
    int stopFrameNumber = 100;
    DateTime lastTime = DateTime.MaxValue;

Entering/Exiting the Frame

 void nui_SkeletonFrameReady(object sender, SkeletonFrameReadyEventArgs e)
    {
        SkeletonFrame skeletonFrame = e.SkeletonFrame;

        int iSkeleton = 0;
        ++totalFrames;
        string bb1 = Convert.ToString(totalFrames);
        //Uri uri1 = new Uri("C:\\Research\\Kinect\\Proposal_Skeleton\\Skeleton_Img" + bb1 + ".png");
        Uri uri1 = new Uri("C:\\temp\\Skeleton_Img" + bb1 + ".png");
        // string file_name_3 = "C:\\Research\\Kinect\\Proposal_Skeleton\\Skeleton_Img" + bb1 + ".png";    // xxx 
        Brush[] brushes = new Brush[6];
        brushes[0] = new SolidColorBrush(Color.FromRgb(255, 0, 0));
        brushes[1] = new SolidColorBrush(Color.FromRgb(0, 255, 0));
        brushes[2] = new SolidColorBrush(Color.FromRgb(64, 255, 255));
        brushes[3] = new SolidColorBrush(Color.FromRgb(255, 255, 64));
        brushes[4] = new SolidColorBrush(Color.FromRgb(255, 64, 255));
        brushes[5] = new SolidColorBrush(Color.FromRgb(128, 128, 255));

        skeleton.Children.Clear();
        //byte[] skeletonFrame32 = new byte[(int)(skeleton.Width) * (int)(skeleton.Height) * 4];
        foreach (SkeletonData data in skeletonFrame.Skeletons)
        {
            if (SkeletonTrackingState.Tracked == data.TrackingState)
            {
                // Draw bones
                Brush brush = brushes[iSkeleton % brushes.Length];
                skeleton.Children.Add(getBodySegment(data.Joints, brush, JointID.HipCenter, JointID.Spine, JointID.ShoulderCenter, JointID.Head));
                skeleton.Children.Add(getBodySegment(data.Joints, brush, JointID.ShoulderCenter, JointID.ShoulderLeft, JointID.ElbowLeft, JointID.WristLeft, JointID.HandLeft));
                skeleton.Children.Add(getBodySegment(data.Joints, brush, JointID.ShoulderCenter, JointID.ShoulderRight, JointID.ElbowRight, JointID.WristRight, JointID.HandRight));
                skeleton.Children.Add(getBodySegment(data.Joints, brush, JointID.HipCenter, JointID.HipLeft, JointID.KneeLeft, JointID.AnkleLeft, JointID.FootLeft));
                skeleton.Children.Add(getBodySegment(data.Joints, brush, JointID.HipCenter, JointID.HipRight, JointID.KneeRight, JointID.AnkleRight, JointID.FootRight));

                // Draw joints
                // try to add a comment, xxx
                foreach (Joint joint in data.Joints)
                {
                    Point jointPos = getDisplayPosition(joint);
                    Line jointLine = new Line();
                    jointLine.X1 = jointPos.X - 3;
                    jointLine.X2 = jointLine.X1 + 6;
                    jointLine.Y1 = jointLine.Y2 = jointPos.Y;
                    jointLine.Stroke = jointColors[joint.ID];
                    jointLine.StrokeThickness = 6;
                    skeleton.Children.Add(jointLine);
                }
                //       ExportToPng(uri1, skeleton);
               // SoundPlayerAction Source = "C:/LiamScienceFair/muhaha.wav";
                //SoundPlayer player1 = new SoundPlayer("muhaha.wav")
               //  player1.Play(); 
              // MediaPlayer.
               // axWindowsMediaPlayer1.currentPlaylist = axWindowsMediaPlayer1.mediaCollection.getByName("mediafile");


                nui.VideoFrameReady += new EventHandler<ImageFrameReadyEventArgs>(nui_ColorFrameReady2);



            }


            iSkeleton++;
        } // for each skeleton
    }

Actual Code

    void nui_ColorFrameReady2(object sender, ImageFrameReadyEventArgs e)
    {
        // 32-bit per pixel, RGBA image  xxx
        PlanarImage Image = e.ImageFrame.Image;
        int deltaFrames = totalFrames - lastFrameWithMotion;
        if (totalFrames2 <= stopFrameNumber & deltaFrames > 300)
        {
            ++totalFrames2;
            string bb1 = Convert.ToString(totalFrames2);
            // string file_name_3 = "C:\\Research\\Kinect\\Proposal\\Depth_Img" + bb1 + ".jpg"; xxx
            string file_name_4 = "C:\\temp\\Video2_Img" + bb1 + ".jpg";
            video.Source = BitmapSource.Create(
                Image.Width, Image.Height, 96, 96, PixelFormats.Bgr32, null, Image.Bits, Image.Width * Image.BytesPerPixel);

            BitmapSource image4 = BitmapSource.Create(
                Image.Width, Image.Height, 96, 96, PixelFormats.Bgr32, null, Image.Bits, Image.Width * Image.BytesPerPixel);
            image4.Save(file_name_4, Coding4Fun.Kinect.Wpf.ImageFormat.Jpeg);
            if (totalFrames2 == stopFrameNumber)
            {
                lastFrameWithMotion = totalFrames;
                stopFrameNumber += 100;
            }
        }

    }
like image 604
Liam McInroy Avatar asked Feb 03 '26 08:02

Liam McInroy


2 Answers

In most setups I have used in the skeletal tracking event area there is a check for if (skeleton != null) all you need to do is reset your trigger once a null skeleton is received.

The SDK will send a skeleton frame every time the event is fired so...

if(skeleton != null)
{
  \\do image taking here
}
else
{
  \\reset image counter
}
like image 198
davidbates Avatar answered Feb 05 '26 23:02

davidbates


I would try something like this. Create a bool class variable named SkeletonInFrame and initialize it to false. Every time SkeletonFrameReady fires, set this bool to true. When you process a color frame, only process if this variable is true. Then after you process a color frame, set the variable to false. This should help you stop processing frame when you are no longer receiving skeleton events.

like image 28
Jason Avatar answered Feb 05 '26 22:02

Jason