我想追踪第一个人,并且只使用这个人的身体。
所以基本上当一个人被跟踪,并且有人走在他身后或正在寻找这个人时,如果他们移动,那么kinect就不会认出其他人。
我在C#中使用SDK 2.0“Body Basics-WPF”的示例代码。我的目标是只识别一个人的几个关节(成功完成)。我找到了一个如何为Kinect v1制作它的线程,但Kinect v2没有。代码如下:
private void Reader_FrameArrived(object sender, BodyFrameArrivedEventArgs e)
{
bool dataReceived = false;
using (BodyFrame bodyFrame = e.FrameReference.AcquireFrame())
{
if (bodyFrame != null)
{
if (this.bodies == null)
{
this.bodies = new Body[bodyFrame.BodyCount];
}
bodyFrame.GetAndRefreshBodyData(this.bodies);
dataReceived = true;
}
}
if (dataReceived)
{
using (DrawingContext dc = this.drawingGroup.Open())
{
dc.DrawRectangle(Brushes.Black, null, new Rect(0.0, 0.0, this.displayWidth, this.displayHeight));
int penIndex = 0;
foreach (Body body in this.bodies)
{
Pen drawPen = this.bodyColors[penIndex++];
if (body.IsTracked)
{
this.DrawClippedEdges(body, dc);
IReadOnlyDictionary<JointType, Joint> joints = body.Joints;
Dictionary<JointType, Point> jointPoints = new Dictionary<JointType, Point>();
foreach (JointType jointType in joints.Keys)
{
if (jointType == JointType.ShoulderRight || jointType == JointType.ElbowRight || jointType == JointType.WristRight || jointType == JointType.HandRight ||
jointType == JointType.ShoulderLeft || jointType == JointType.ElbowLeft || jointType == JointType.WristLeft || jointType == JointType.HandLeft)
{
CameraSpacePoint position = joints[jointType].Position;
if (position.Z < 0)
{
position.Z = InferredZPositionClamp;
}
DepthSpacePoint depthSpacePoint = this.coordinateMapper.MapCameraPointToDepthSpace(position);
jointPoints[jointType] = new Point(depthSpacePoint.X, depthSpacePoint.Y);
}
}
this.DrawBody(joints, jointPoints, dc, drawPen);
}
}
this.drawingGroup.ClipGeometry = new RectangleGeometry(new Rect(0.0, 0.0, this.displayWidth, this.displayHeight));
}
}
}
答案 0 :(得分:1)
前一段时间安静但我通过自己的功能来管理这个问题以保持跟踪一个人。如果被跟踪的人离开Body Array中的下一个Top Body,则获得&#34; Tracked Person&#34;。
private ulong currTrackingId = 0;
private Body GetActiveBody()
{
if (currTrackingId <= 0)
{
foreach (Body body in this.bodies)
{
if (body.IsTracked)
{
currTrackingId = body.TrackingId;
return body;
}
}
return null;
}
else
{
foreach (Body body in this.bodies)
{
if (body.IsTracked && body.TrackingId == currTrackingId)
{
return body;
}
}
}
currTrackingId = 0;
return GetActiveBody();
}
答案 1 :(得分:0)
我使用的是较旧的Kinect,但仍然没有使用新的Kinect,所以这可能会或可能不会起作用: 如果 bodyFrame.BodyCount 只是给你面对Kinect的人数,那么将其替换为1,这样你的代码就变成了:
this.bodies = new Body [1];
我认为kinect通常会挑选最亲近的人。 试试吧,告诉我你得到了什么。
答案 2 :(得分:0)
不要使用foreach来迭代body数组,只需使用列表中的第一个主体。
替换:
foreach (Body body in this.bodies)
{
if (body.IsTracked)
{
...
使用:
var body = this.bodies[0];
if (body.tracked)
{
...
更新:
我刚尝试使用Kinect Studio,我发现即使只有一个跟踪体,也不能保证在数组的第一个插槽中。在我的应用程序中,现在我使用LINQ来获取IsTracked == true的第一个主体。关于如何为生产就绪代码执行此操作,我没有很好的答案。如果我解决了这个问题,我将更新这篇文章。