2012-01-11 54 views
3

因此,我想要做的就是採取Kinect骨骼樣本並保存x張照片,只有當人類經過時。我已經完成了它的工作,除非一旦它檢測到人類,即使人離開Kinect的視覺,它也只記錄x張照片。有誰知道如何製作它,以便一旦有人進入它就開始錄製,一旦他們離開它就停下來了?Kinect - 檢測人類什麼時候退出框架

變量

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

進入/退出框架

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 
    } 

實際代碼

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; 
      } 
     } 

    } 
+1

我不明白,當一個人進入/離開幀中的任何代碼來檢測發送骨架。根據你所說的,圖像的實際保存(你發佈的內容)工作正常。 – Jason 2012-01-11 22:26:37

+0

是的,但是一旦有人進入kinect視圖,即使人離開視圖,它也會開始保存幀,直到它碰到stopFrameNumber。 – 2012-01-13 21:16:17

+0

如何檢測某人何時離開視圖?或者是你想知道該怎麼做? – Jason 2012-01-13 21:20:07

回答

2

竟被我d嘗試這樣的事情。創建一個名爲SkeletonInFrame的bool類變量並將其初始化爲false。每次SkeletonFrameReady觸發時,將此布爾值設置爲true。處理顏色框時,只有在該變量爲true時纔會處理。然後,在處理彩色框架後,將該變量設置爲false。這可以幫助您在不再接收框架事件時停止處理框架。

+0

好吧生病嘗試 – 2012-01-16 17:51:52

+0

我試過這個,然後它說,SkeletonFrameReady是不是一個有效的方法,在那裏的任何幫助? – 2012-01-18 21:24:07

+0

我給你僞代碼。在你的代碼中,你把它稱爲nui_SkeletonFrameReady() – Jason 2012-01-18 21:30:13

3

在我用於骨骼跟蹤事件區域的大多數設置中,檢查if(skeleton!= null)是否需要做的就是在收到null骨架後重置觸發器。

的SDK將在每次事件被觸發左右的時間......

if(skeleton != null) 
{ 
    \\do image taking here 
} 
else 
{ 
    \\reset image counter 
}