我有一个Kinect程序,它应该从50帧中获取Skeletons,计算平均值并绘制它。
异步事件处理程序已添加到SkeletonFrameReady
private async void InitSkeletonEvent(object sender,SkeletonFrameReadyEventArgs e) { List<Vector3D> vectors = new List<Vector3D>(); double[,] coordinates = new double[50, 3]; double[,] finalCoordinates = new double[50, 3]; using (SkeletonFrame initSkeletonFrame = e.OpenSkeletonFrame()) { if (initSkeletonFrame != null) { if (initSkeletons == null) { initSkeletons = new Skeleton[initSkeletonFrame.SkeletonArrayLength]; } initSkeletonFrame.CopySkeletonDataTo(initSkeletons); Skeleton initSkeleton = initSkeletons.Where(s => s.TrackingState == SkeletonTrackingState.Tracked).FirstOrDefault(); if (initSkeleton != null) { foreach (Skeleton s in initSkeletons) { // Get all tracked Skeletons for the 50 frames while (frame < maxFrames) { frame += 1; vectors = await getCoordinatesofJoints(initSkeleton); coordinates = await getCoordinatesPerJoint(vectors); } } // Remove the event from SkeletonFrame this.sensor.SkeletonFrameReady -= InitSkeletonEvent; finalCoordinates = await getAverageCoordinateForJoint(coordinates); skeleton = await getAvgSkeleton(finalCoordinates); } } } }
使用await
参数调用了几种方法。但是,他们似乎没有完成他们的工作,并且返回的值不正确。
以下是其中一个功能的示例。其余的都是类比的。
我是否错误地使用了async
和await
?
private Task<List<Vector3D>> getCoordinatesofJoints(Skeleton s) { List<Vector3D> vectors = new List<Vector3D>(); foreach (Joint j in s.Joints) { Vector3D tmp = new Vector3D(j.Position.X, j.Position.Y, j.Position.Z); vectors.Add(tmp); } return Task.Run(() => vectors); }