2014-02-15 39 views
3

我是kinect sdk v1.7的新手。如何捕捉Kinect SDK V1.7(Avateering XNA)中的运动数据?

我想知道如何从样本采集运动数据。(http://msdn.microsoft.com/en-us/library/jj131041.aspx

所以,我怎么能做出可以捕捉骨架数据转换成文件的程序? (记录)

然后,将文件读回示例程序并对其进行建模(播放)?

我的想法是将骨架数据记录到文件中,然后从文件中获取骨架数据并让头像播放。

我可以做我想要的另一个示例程序。 (http://msdn.microsoft.com/en-us/library/hh855381),导致示例程序只绘制直线和骨架点。

例如,

00001 00:00:[email protected],349,354,332,358,249,353,202,310,278,286,349,269,407,266,430,401,279,425,349,445,408,453,433,332,369,301,460,276,539,269,565,372,370,379,466,387,548,389,575, 

00002 00:00:[email protected],349,353,332,356,249,352,202,309,278,284,349,266,406,263,430,398,279,424,349,445,408,453,433,331,369,301,461,277,541,271,566,371,371,379,466,387,548,390,575, 

[帧号] [时间戳] @ [骨架位置坐标]

在这个例子中

,我假定骨架位置是联合ID顺序。

谢谢(原谅我可怜的英语)。

回答

2

您可以使用StreamWriter,在选定的路径上初始化它,然后为每个帧增加帧计数器,将其写入文件,将时间戳写入文件,然后遍历关节并将它们写入文件。我会做如下:

using System.IO; 

StreamWriter writer = new StreamWriter(@path); 
int frames = 0; 

... 

void AllFramesReady(object sender, AllFramesReadyEventArgs e) 
{ 
    frames++; 
    using (SkeletonFrame sFrame = e.OpenSkeletonFrameData()) 
    { 
     if (sFrame == null) 
      return; 

     skeletonFrame.CopySkeletonDataTo(skeletons); 

     Skeleton skeleton = (from s in skeletons 
           where s.TrackingState == SkeletonTrackingState.Tracked 
           select s); 
     if (skeleton == null) 
      return; 

     if (skeleton.TrackingState == SkeletonTrackingState.Tracked) 
     { 
      writer.Write("{0} {1}@", frames, timestamp);//I dont know how you want to do this 
      foreach (Joint joint in skeleton.Joints) 
      { 
       writer.Write(joint.Position.X + "," + joint.Position.Y + "," joint.Position.Z + ","); 
      } 
      writer.Write(Environment.NewLine); 
     } 
    } 
} 

然后从文件中读取:

StreamReader reader = new StreamReader(@path); 
int frame = -1; 
JointCollection joints; 

... 

string[] lines = reader.ReadAllLines(); 

... 

void AllFramesReady(object sender, AllFramesReadyEventArgs e) 
{ 
    canvas.Children.Clear(); 
    string[] coords = lines[frame].Split('@')[1].Split(','); 
    int jointIndex = 0; 
    for (int i = 0; i < coords.Length; i += 3) 
    { 
     joints[jointIndex].Position.X = int.Parse(coords[i]); 
     joints[jointIndex].Position.Y = int.Parse(coords[i + 1]); 
     joints[jointIndex].Position.X = int.Parse(coords[i + 2]); 
     jointIndex++; 
    } 

    DepthImageFrame depthFrame = e.OpenDepthImageFrame(); 
    canvas.Children.Add(GetBodySegment(joints, brush, new JointType[] { JointType.HipCenter, JointType.Spine, JointType.ShoulderCenter, JointType.Head }, depthFrame, canvas)); 
    canvas.Children.Add(GetBodySegment(joints, brush, new JointType[] { JointType.ShoulderCenter, JointType.ShoulderLeft, JointType.ElbowLeft, JointType.WristLeft, JointType.HandLeft }, depthFrame, canvas)); 
    canvas.Children.Add(GetBodySegment(joints, brush, new JointType[] { JointType.ShoulderCenter, JointType.ShoulderRight, JointType.ElbowRight, JointType.WristRight, JointType.HandRight }, depthFrame, canvas)); 
    canvas.Children.Add(GetBodySegment(joints, brush, new JointType[] { JointType.HipCenter, JointType.HipLeft, JointType.KneeLeft, JointType.AnkleLeft, JointType.FootLeft }, depthFrame, canvas)); 
    canvas.Children.Add(GetBodySegment(joints, brush, new JointType[] { JointType.HipCenter, JointType.HipRight, JointType.KneeRight, JointType.AnkleRight, JointType.FootRight }, depthFrame, canvas)); 
    depthFrame.Dispose(); 

    frame++; 
} 

Point GetDisplayPosition(Joint joint, DepthImageFrame depthFrame, Canvas skeleton) 
{ 
    float depthX, depthY; 
    KinectSensor sensor = KinectSensor.KinectSensors[0]; 
    DepthImageFormat depthImageFormat = sensor.DepthStream.Format; 
    DepthImagePoint depthPoint = sensor.CoordinateMapper.MapSkeletonPointToDepthPoint(joint.Position, depthImageFormat); 

    depthX = depthPoint.X; 
    depthY = depthPoint.Y; 

    depthX = Math.Max(0, Math.Min(depthX * 320, 320)); 
    depthY = Math.Max(0, Math.Min(depthY * 240, 240)); 

    int colorX, colorY; 
    ColorImagePoint colorPoint = sensor.CoordinateMapper.MapDepthPointToColorPoint(depthImageFormat, depthPoint, ColorImageFormat.RgbResolution640x480Fps30); 
    colorX = colorPoint.X; 
    colorY = colorPoint.Y; 

    return new System.Windows.Point((int)(skeleton.Width * colorX/640.0), (int)(skeleton.Height * colorY/480)); 
} 

Polyline GetBodySegment(Joint[] joints, Brush brush, JointType[] ids, DepthImageFrame depthFrame, Canvas canvas) 
{ 
    PointCollection points = new PointCollection(ids.Length); 
    for (int i = 0; i < ids.Length; ++i) 
    { 
     points.Add(GetDisplayPosition(joints[i], depthFrame, canvas)); 
    } 
    Polyline polyline = new Polyline(); 
    polyline.Points = points; 
    polyline.Stroke = brush; 
    polyline.StrokeThickness = 5; 
    return polyline; 
} 

当然,在WPF这仅适用。你只需要使用更改代码:

DepthImageFrame depthFrame = e.OpenDepthImageFrame(); 
    canvas.Children.Add(GetBodySegment(joints, brush, new JointType[] { JointType.HipCenter, JointType.Spine, JointType.ShoulderCenter, JointType.Head }, depthFrame, canvas)); 
    canvas.Children.Add(GetBodySegment(joints, brush, new JointType[] { JointType.ShoulderCenter, JointType.ShoulderLeft, JointType.ElbowLeft, JointType.WristLeft, JointType.HandLeft }, depthFrame, canvas)); 
    canvas.Children.Add(GetBodySegment(joints, brush, new JointType[] { JointType.ShoulderCenter, JointType.ShoulderRight, JointType.ElbowRight, JointType.WristRight, JointType.HandRight }, depthFrame, canvas)); 
    canvas.Children.Add(GetBodySegment(joints, brush, new JointType[] { JointType.HipCenter, JointType.HipLeft, JointType.KneeLeft, JointType.AnkleLeft, JointType.FootLeft }, depthFrame, canvas)); 
    canvas.Children.Add(GetBodySegment(joints, brush, new JointType[] { JointType.HipCenter, JointType.HipRight, JointType.KneeRight, JointType.AnkleRight, JointType.FootRight }, depthFrame, canvas)); 
    depthFrame.Dispose(); 

到avateering样本如何动画模型,你甚至可以创建一个新的Skeleton和复制jointsSkeleton.Joints,那么就传递骨架的“检测”骨架。请注意,您需要更改此示例中使用的函数所需的任何其他必需变量。我对样本不熟悉,因此我不能提供具体的方法名称,但是您可以用开头创建的全局替换全局Skeleton并更新每一帧。因此,我建议这样的:

//in the game class (AvateeringXNA.cs) 
StreamReader reader = new StreamReader(@path); 
int frame = -1; 
JointCollection joints; 
Skeleton recorded = new Skeleton(); 

... 

string[] lines = reader.ReadAllLines(); 

... 

void Update(...) 
{ 
    string[] coords = lines[frame].Split('@')[1].Split(','); 
    int jointIndex = 0; 
    for (int i = 0; i < coords.Length; i += 3) 
    { 
     joints[jointIndex].Position.X = int.Parse(coords[i]); 
     joints[jointIndex].Position.Y = int.Parse(coords[i + 1]); 
     joints[jointIndex].Position.X = int.Parse(coords[i + 2]); 
     jointIndex++; 
    } 

    recorded.Joints = joints; 

    ... 

    //preform necessary methods, except with recorded skeleton instead of detected, I think it is: 
    this.animator.CopySkeleton(recorded); 
    this.animator.FloorClipPlane = skeletonFrame.FloorClipPlane; 

    // Reset the filters if the skeleton was not seen before now 
    if (this.skeletonDetected == false) 
    { 
     this.animator.Reset(); 
    } 

    this.skeletonDetected = true; 
    this.animator.SkeletonVisible = true; 

    ... 

    frame++; 
} 

编辑

当你阅读的起始楼面夹持平面(clipPlanes[0]),它会得到整个帧信息到第一个空间。见下面来看看它是如何将分裂,我将如何读它:

var newFloorClipPlane = Tuple.Create(Single.Parse(clipPlanes[2]), Single.Parse(clipPlanes[3]), Single.Parse(clipPlanes[4]), Single.Parse(clipPlanes[5])); 

这里是你如何布局框架:

frame# [email protected],joint1posy,joint1posz,...jointNPosx,jointNposy,jointNposz floorX floorY floorZ floorW 

这里是'产生的阵列。斯普利特(”“)

["frame#", "[email protected],joint1posy,joint1posz,...jointNPosx,jointNposy,jointNposz", "floorX", "floorY", "floorZ", "floorW"] 

因此,示例输入:

00000002 [email protected],10,10... 11 12 13 14 

与您的代码,你会得到:

[2, 10112101010..., 11, 12] 

从我的代码修正指标:

[11, 12, 13, 14] 

把这一行放入控制台权证申请的真快,看看它输出:

Console.WriteLine(Convert.ToSingle("10,10")); 

输出为1010您正在试图完成的任务,这造成了错误的地板上剪裁平面。你需要适当的索引来实现你想要达到的目标。

注:我改变Convert.ToSingleSingle.Parse,因为它是更好的做法,并在堆栈跟踪他们都瓶坯相同功能

+0

感谢您的回复。 但我想用示例程序(Avatarring) 我在示例程序中找不到任何框架信息。 我想记录骨架信息并将其放回到头像中,然后播放。 但我只能找到关于skeleton的信息是skeleton []对象。如何以txt(字符串)格式记录对象。并转回到头像模型? –

+0

我的意思是我想修改可以捕捉运动数据的示例程序。但我不知道该怎么做。 –

+0

您可以使用我的相同代码来捕捉运动,因为它可以保存关节坐标,并且您可以用相同的方式来读取它,而不是写入画布将其转换为世界点并将头像关节设置为相对...我真的很快编辑@ user255 –

0

嘿你为什么不使用CSV方法写入来讲所有的关节数据excel文件。它会帮助你在稍后阶段分析它们。我已经定制了我的代码,用于将它们放入csv格式,这有助于我在数据的后期阶段进行分析。你可以在你的项目中编写一个单独的文件,导出所有的骨架数据

public void CoordinatesExportToCSV(Skeleton data) 
    { 
     if (!TimeRecorded) 
     { 
      startTime = DateTime.Now; 
      TimeRecorded = true; 
     } 
     recordedSamples[1]++; 
     if (!titles) 
     { 
      sw1.Write("Counter,Time,Clipped Edges,"); 
      foreach (Joint joint in data.Joints) 
      { 
       sw1.Write(joint.JointType.ToString()+","); 
      } 
      titles = true; 
     } 
     else 
     { 
      double a = DateTime.Now.TimeOfDay.TotalSeconds - startTime.TimeOfDay.TotalSeconds; 
      sw1.Write(recordedSamples[1] + "," + a + "," + data.ClippedEdges); 

      foreach (Joint joint in data.Joints) 
      { 
       sw1.Write(joint.Position.X + "|" + joint.Position.Y + "|" + joint.Position.Z+","); 
      } 
     } 
     sw1.WriteLine(); 
    }