我正在编写一个代码,用两个Kinect跟踪骨架的所有关节的坐标。这是我到目前为止的代码。它将每个帧的关节坐标写入控制台。然而它只对第二个Kinect这样做。如果打印到两个Kinects的控制台,我会遗漏一些非常简单的代码,但我无法理解。
class Program
{
static void Main(string[] args)
{
var connectedSensors = new List<KinectSensor>();
int sensors = 0;
foreach(KinectSensor sensor in KinectSensor.KinectSensors){
if (sensor == null)
{
Console.WriteLine("No Kinect sensor connected :(");
return;
}
connectedSensors.Add(sensor);
sensors += 1;
}
foreach(KinectSensor s in connectedSensors){
Tracker tracker = new Tracker(s, sensors);
s.SkeletonStream.TrackingMode = SkeletonTrackingMode.Seated;
s.Start();
while (Char.ToLowerInvariant(Console.ReadKey().KeyChar) != 'q') { }
s.Stop();
}
}
}
class Tracker
{
private int skeletonId;
private Skeleton[] skeletons;
public Tracker(KinectSensor sensor, int sensorNr)
{
skeletonId = sensorNr;
sensor.SkeletonFrameReady += SensorSkeletonFrameReady;
sensor.SkeletonStream.Enable();
}
private void SensorSkeletonFrameReady(object sender, SkeletonFrameReadyEventArgs e)
{
using (SkeletonFrame skeletonFrame = e.OpenSkeletonFrame())
{
if (skeletonFrame != null)
{
if (this.skeletons == null)
{
this.skeletons = new Skeleton[skeletonFrame.SkeletonArrayLength];
}
skeletonFrame.CopySkeletonDataTo(this.skeletons);
Skeleton skeleton = this.skeletons.Where(s => s.TrackingState == SkeletonTrackingState.Tracked).FirstOrDefault();
if (skeleton != null)
{
foreach (Joint j in skeleton.Joints)
{
if (j.TrackingState == JointTrackingState.Tracked || j.TrackingState == JointTrackingState.Inferred)
Console.WriteLine("SENSOR "+ skeletonId +"\t "+j.JointType + "\t " + j.Position.X + "\t " + j.Position.Y + "\t " + j.Position.Z);
else
Console.WriteLine("SENSOR " + skeletonId + "\t " + j.JointType + "\t 0\t 0\t 0");
}
}
}
}
}
}
答案 0 :(得分:2)
你的循环
foreach(KinectSensor s in connectedSensors)
{
Tracker tracker = new Tracker(s, sensors);
s.SkeletonStream.TrackingMode = SkeletonTrackingMode.Seated;
s.Start();
while (Char.ToLowerInvariant(Console.ReadKey().KeyChar) != 'q') { }
s.Stop();
}
启动第一个传感器,等待你按下&#34; q&#34;并停止第一个传感器。比第二个传感器启动。那是你要的吗?
如果您想同时运行两个传感器,您应该尝试类似以下内容:
foreach(KinectSensor s in connectedSensors)
{
Tracker tracker = new Tracker(s, sensors);
s.SkeletonStream.TrackingMode = SkeletonTrackingMode.Seated;
s.Start();
}
while (Char.ToLowerInvariant(Console.ReadKey().KeyChar) != 'q') { }
foreach(KinectSensor s in connectedSensors)
{
s.Stop();
}
答案 1 :(得分:0)
您传递的传感器构造函数参数始终是最后一个传感器的编号:
Tracker tracker = new Tracker(s, sensors);
您需要在循环中使用另一个变量或使用常规for循环:
for(int i=0; i < sensors; i += 1){
KinectSensor s = connectedSensors[i];
Tracker tracker = new Tracker(s, i); //the second parameter here is the one you needed to fix.
s.SkeletonStream.TrackingMode = SkeletonTrackingMode.Seated;
s.Start();
while (Char.ToLowerInvariant(Console.ReadKey().KeyChar) != 'q') { }
s.Stop();
}