如何同时跟踪具有多个关节的关节
本文关键字:何同时 跟踪 | 更新日期: 2023-09-27 17:51:23
我正在编写一个代码来跟踪具有两个Kinects的骨架的所有关节的坐标。这是我目前掌握的代码。它每帧向控制台输出关节的坐标。然而,这只适用于第二款Kinect。我从代码中遗漏了一些非常简单的东西,使其打印到两个kinect的控制台,但我无法弄清楚。
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");
}
}
}
}
}
}
你的循环
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();
}
启动第一个传感器,等待您按"q"并停止第一个传感器。第二个传感器启动。这就是你想要的吗?
如果您想同时运行两个传感器,您应该尝试如下操作:
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();
}
您传递的传感器构造函数参数将始终是最后一个传感器的编号:
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();
}