public void TestSecondFrameNewBody() { List<ulong> found = new List<ulong>(); BodyTrackingProcessor processor = new BodyTrackingProcessor(); var firstFrame = new KinectBody[] { FakeBodies.FakeRandomBody(128, true), FakeBodies.FakeRandomBody(256, true), FakeBodies.FakeRandomBody(32, false) }; processor.Next(firstFrame); //Register to event now processor.BodyTrackingStarted += (s, e) => found.Add(e.Body.TrackingId); ulong newBodyId = 17; var secondFrame = new KinectBody[] { FakeBodies.FakeRandomBody(128, true), FakeBodies.FakeRandomBody(newBodyId, true), }; processor.Next(secondFrame); Assert.AreEqual(found.Count, 1); Assert.AreEqual(found.Contains(newBodyId), true); }
public void TestTrackingLostRaised() { List <ulong> found = new List <ulong>(); BodyTrackingProcessor processor = new BodyTrackingProcessor(); ulong lostBodyId = 256; var firstFrame = new KinectBody[] { FakeBodies.FakeRandomBody(128, true), FakeBodies.FakeRandomBody(lostBodyId, true), FakeBodies.FakeRandomBody(32, false) }; processor.Next(firstFrame); //Register to event now processor.BodyTrackingLost += (s, e) => found.Add(e.Body.TrackingId); ulong newBodyId = 17; var secondFrame = new KinectBody[] { FakeBodies.FakeRandomBody(128, true), FakeBodies.FakeRandomBody(newBodyId, true), }; processor.Next(secondFrame); Assert.AreEqual(found.Count, 1); Assert.AreEqual(found.Contains(lostBodyId), true); }
public void TestTrackingFirstRaised() { bool isRaised = false; BodyTrackingProcessor processor = new BodyTrackingProcessor(); processor.BodyTrackingStarted += (s, e) => isRaised = true; var dataSet = new KinectBody[] { FakeBodies.FakeRandomBody(128, true) }; processor.Next(dataSet); Assert.AreEqual(isRaised, true); }
public void TestFirstFrame2in1out() { List<ulong> found = new List<ulong>(); BodyTrackingProcessor processor = new BodyTrackingProcessor(); processor.BodyTrackingStarted += (s, e) => found.Add(e.Body.TrackingId); var dataSet = new KinectBody[] { FakeBodies.FakeRandomBody(128, true), FakeBodies.FakeRandomBody(256, true), FakeBodies.FakeRandomBody(32, false) }; processor.Next(dataSet); Assert.AreEqual(found.Count, 2); Assert.AreEqual(found.Contains(128), true); Assert.AreEqual(found.Contains(256), true); }
public void TestFirstFrame2in1out() { List <ulong> found = new List <ulong>(); BodyTrackingProcessor processor = new BodyTrackingProcessor(); processor.BodyTrackingStarted += (s, e) => found.Add(e.Body.TrackingId); var dataSet = new KinectBody[] { FakeBodies.FakeRandomBody(128, true), FakeBodies.FakeRandomBody(256, true), FakeBodies.FakeRandomBody(32, false) }; processor.Next(dataSet); Assert.AreEqual(found.Count, 2); Assert.AreEqual(found.Contains(128), true); Assert.AreEqual(found.Contains(256), true); }
/// <summary> /// Constructor /// </summary> /// <param name="sensor">Kinect sensor</param> /// <param name="bodyProcessor">Kinect body processor</param> /// <param name="maxFaceCount">Maximum face count</param> public MultipleHdFaceProcessor(KinectSensor sensor, BodyTrackingProcessor bodyProcessor, int maxFaceCount) { if (sensor == null) throw new ArgumentNullException("sensor"); if (bodyProcessor == null) throw new ArgumentNullException("bodyProcessor"); if (maxFaceCount < 1) throw new ArgumentOutOfRangeException("maxFaceCount", "Should be at least 1"); this.bodyProcessor = bodyProcessor; this.bodyProcessor.BodyTrackingStarted += BodyTrackingStarted; this.bodyProcessor.BodyTrackingLost += BodyTrackingLost; this.faceProcessors = new SingleHdFaceProcessor[maxFaceCount]; this.activeProcessors = new Dictionary<ulong, SingleHdFaceProcessor>(); this.idleProcessors = new List<SingleHdFaceProcessor>(maxFaceCount); this.currentResults = new Dictionary<ulong, HdFaceFrameResultEventArgs>(); for (int i = 0; i < maxFaceCount; i++) { this.faceProcessors[i] = new SingleHdFaceProcessor(sensor); this.idleProcessors.Add(this.faceProcessors[i]); } }
static void Main() { Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); RenderForm form = new RenderForm("Kinect multiple hd faces projected to rgb"); RenderDevice device = new RenderDevice(SharpDX.Direct3D11.DeviceCreationFlags.BgraSupport | DeviceCreationFlags.Debug); RenderContext context = new RenderContext(device); DX11SwapChain swapChain = DX11SwapChain.FromHandle(device, form.Handle); VertexShader vertexShader = ShaderCompiler.CompileFromFile<VertexShader>(device, "ProjectedTextureHdFaceView.fx", "VS_Simple"); VertexShader vertexShaderIndexed = ShaderCompiler.CompileFromFile<VertexShader>(device, "ProjectedTextureHdFaceView.fx", "VS_Indexed"); PixelShader pixelShader = ShaderCompiler.CompileFromFile<PixelShader>(device, "ProjectedTextureHdFaceView.fx", "PS"); int maxFaceCount = Consts.MaxBodyCount; int faceVertexCount = (int)Microsoft.Kinect.Face.FaceModel.VertexCount; var vertRgbTempBuffer = new ColorSpacePoint[faceVertexCount]; ColorSpacePoint[] facePoints = new ColorSpacePoint[faceVertexCount * maxFaceCount]; DX11StructuredBuffer lookupBuffer = DX11StructuredBuffer.CreateDynamic<uint>(device, maxFaceCount); //Note : since in this case we use instancing, we only need a buffer for a single face HdFaceIndexBuffer faceIndexBuffer = new HdFaceIndexBuffer(device, 1); DynamicRgbSpaceFaceStructuredBuffer faceRgbBuffer = new DynamicRgbSpaceFaceStructuredBuffer(device, maxFaceCount); KinectSensor sensor = KinectSensor.GetDefault(); sensor.Open(); bool doQuit = false; bool invalidateFace = false; KinectSensorBodyFrameProvider provider = new KinectSensorBodyFrameProvider(sensor); BodyTrackingProcessor bodyTracker = new BodyTrackingProcessor(); MultipleHdFaceProcessor multiFace = new MultipleHdFaceProcessor(sensor, bodyTracker, maxFaceCount); form.KeyDown += (sender, args) => { if (args.KeyCode == Keys.Escape) { doQuit = true; } }; bool uploadColor = false; ColorRGBAFrameData currentData = null; DynamicColorRGBATexture colorTexture = new DynamicColorRGBATexture(device); KinectSensorColorRGBAFrameProvider colorProvider = new KinectSensorColorRGBAFrameProvider(sensor); colorProvider.FrameReceived += (sender, args) => { currentData = args.FrameData; uploadColor = true; }; provider.FrameReceived += (sender, args) => { bodyTracker.Next(args.FrameData); }; multiFace.OnFrameResultsChanged += (sender, args) => { invalidateFace = true; }; RenderLoop.Run(form, () => { if (doQuit) { form.Dispose(); return; } if (invalidateFace) { int offset = 0; foreach (var data in multiFace.CurrentResults) { var vertices = data.FaceModel.CalculateVerticesForAlignment(data.FaceAlignment).ToArray(); sensor.CoordinateMapper.MapCameraPointsToColorSpace(vertices, vertRgbTempBuffer); Array.Copy(vertRgbTempBuffer, 0, facePoints, offset, faceVertexCount); offset += faceVertexCount; } faceRgbBuffer.Copy(context, facePoints, multiFace.CurrentResults.Count * faceVertexCount); invalidateFace = false; } if (uploadColor) { colorTexture.Copy(context, currentData); uploadColor = false; } context.Context.ClearRenderTargetView(swapChain.RenderView, SharpDX.Color.Black); context.RenderTargetStack.Push(swapChain); context.Context.Rasterizer.State = device.RasterizerStates.BackCullSolid; context.Context.OutputMerger.BlendState = device.BlendStates.Disabled; device.Primitives.ApplyFullTri(context, colorTexture.ShaderView); device.Primitives.FullScreenTriangle.Draw(context); if (multiFace.CurrentResults.Count > 0) { context.Context.VertexShader.SetShaderResource(0, faceRgbBuffer.ShaderView); context.Context.PixelShader.SetSampler(0, device.SamplerStates.LinearClamp); context.Context.PixelShader.SetShaderResource(0, colorTexture.ShaderView); if (multiFace.CurrentResults.Count > 1) { uint[] buffer = new uint[multiFace.CurrentResults.Count]; for (uint i = 0; i < multiFace.CurrentResults.Count; i++) { buffer[i] = (uint)((i + 1) % multiFace.CurrentResults.Count); } lookupBuffer.WriteData(context, buffer); context.Context.VertexShader.Set(vertexShaderIndexed); context.Context.VertexShader.SetShaderResource(1, lookupBuffer.ShaderView); } else { context.Context.VertexShader.Set(vertexShader); } context.Context.PixelShader.Set(pixelShader); //Attach index buffer, null topology since we fetch faceIndexBuffer.AttachWithLayout(context); faceIndexBuffer.DrawInstanced(context, multiFace.CurrentResults.Count); } context.RenderTargetStack.Pop(); swapChain.Present(0, SharpDX.DXGI.PresentFlags.None); }); swapChain.Dispose(); context.Dispose(); device.Dispose(); colorProvider.Dispose(); colorTexture.Dispose(); faceIndexBuffer.Dispose(); faceRgbBuffer.Dispose(); provider.Dispose(); pixelShader.Dispose(); vertexShader.Dispose(); vertexShaderIndexed.Dispose(); lookupBuffer.Dispose(); sensor.Close(); }
public void TestTrackingLostRaisedSetFalse() { List<ulong> found = new List<ulong>(); BodyTrackingProcessor processor = new BodyTrackingProcessor(); ulong lostBodyId = 256; var firstFrame = new KinectBody[] { FakeBodies.FakeRandomBody(128, true), FakeBodies.FakeRandomBody(lostBodyId, true), FakeBodies.FakeRandomBody(32, false) }; processor.Next(firstFrame); //Register to event now processor.BodyTrackingLost += (s, e) => found.Add(e.Body.TrackingId); ulong newBodyId = 17; var secondFrame = new KinectBody[] { FakeBodies.FakeRandomBody(128, true), FakeBodies.FakeRandomBody(lostBodyId, false), //Set tracking state to false here FakeBodies.FakeRandomBody(newBodyId, true), }; processor.Next(secondFrame); Assert.AreEqual(found.Count, 1); Assert.AreEqual(found.Contains(lostBodyId), true); }
static void Main() { Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); RenderForm form = new RenderForm("Kinect multiple hd faces projected to rgb"); RenderDevice device = new RenderDevice(SharpDX.Direct3D11.DeviceCreationFlags.BgraSupport | DeviceCreationFlags.Debug); RenderContext context = new RenderContext(device); DX11SwapChain swapChain = DX11SwapChain.FromHandle(device, form.Handle); VertexShader vertexShader = ShaderCompiler.CompileFromFile <VertexShader>(device, "ProjectedHdFaceView.fx", "VS"); PixelShader pixelShader = ShaderCompiler.CompileFromFile <PixelShader>(device, "ProjectedHdFaceView.fx", "PS"); int maxFaceCount = Consts.MaxBodyCount; int faceVertexCount = (int)Microsoft.Kinect.Face.FaceModel.VertexCount; var vertRgbTempBuffer = new ColorSpacePoint[faceVertexCount]; ColorSpacePoint[] facePoints = new ColorSpacePoint[faceVertexCount * maxFaceCount]; HdFaceIndexBuffer faceIndexBuffer = new HdFaceIndexBuffer(device, maxFaceCount); DynamicRgbSpaceFaceStructuredBuffer faceRgbBuffer = new DynamicRgbSpaceFaceStructuredBuffer(device, maxFaceCount); KinectSensor sensor = KinectSensor.GetDefault(); sensor.Open(); bool doQuit = false; bool invalidateFace = false; KinectSensorBodyFrameProvider provider = new KinectSensorBodyFrameProvider(sensor); BodyTrackingProcessor bodyTracker = new BodyTrackingProcessor(); MultipleHdFaceProcessor multiFace = new MultipleHdFaceProcessor(sensor, bodyTracker, maxFaceCount); form.KeyDown += (sender, args) => { if (args.KeyCode == Keys.Escape) { doQuit = true; } }; bool uploadColor = false; ColorRGBAFrameData currentData = null; DynamicColorRGBATexture colorTexture = new DynamicColorRGBATexture(device); KinectSensorColorRGBAFrameProvider colorProvider = new KinectSensorColorRGBAFrameProvider(sensor); colorProvider.FrameReceived += (sender, args) => { currentData = args.FrameData; uploadColor = true; }; provider.FrameReceived += (sender, args) => { bodyTracker.Next(args.FrameData); }; multiFace.OnFrameResultsChanged += (sender, args) => { invalidateFace = true; }; RenderLoop.Run(form, () => { if (doQuit) { form.Dispose(); return; } if (invalidateFace) { int offset = 0; foreach (var data in multiFace.CurrentResults) { var vertices = data.FaceModel.CalculateVerticesForAlignment(data.FaceAlignment).ToArray(); sensor.CoordinateMapper.MapCameraPointsToColorSpace(vertices, vertRgbTempBuffer); Array.Copy(vertRgbTempBuffer, 0, facePoints, offset, faceVertexCount); offset += faceVertexCount; } faceRgbBuffer.Copy(context, facePoints, multiFace.CurrentResults.Count * faceVertexCount); invalidateFace = false; } if (uploadColor) { colorTexture.Copy(context, currentData); uploadColor = false; } context.Context.ClearRenderTargetView(swapChain.RenderView, SharpDX.Color.Black); context.RenderTargetStack.Push(swapChain); context.Context.Rasterizer.State = device.RasterizerStates.BackCullSolid; context.Context.OutputMerger.BlendState = device.BlendStates.Disabled; device.Primitives.ApplyFullTri(context, colorTexture.ShaderView); device.Primitives.FullScreenTriangle.Draw(context); if (multiFace.CurrentResults.Count > 0) { context.Context.Rasterizer.State = device.RasterizerStates.WireFrame; context.Context.OutputMerger.BlendState = device.BlendStates.AlphaBlend; context.Context.VertexShader.SetShaderResource(0, faceRgbBuffer.ShaderView); //Draw lines context.Context.PixelShader.Set(pixelShader); context.Context.VertexShader.Set(vertexShader); //Attach index buffer, null topology since we fetch faceIndexBuffer.AttachWithLayout(context); faceIndexBuffer.Draw(context, multiFace.CurrentResults.Count); } context.RenderTargetStack.Pop(); swapChain.Present(0, SharpDX.DXGI.PresentFlags.None); }); swapChain.Dispose(); context.Dispose(); device.Dispose(); colorProvider.Dispose(); colorTexture.Dispose(); faceIndexBuffer.Dispose(); faceRgbBuffer.Dispose(); provider.Dispose(); pixelShader.Dispose(); vertexShader.Dispose(); sensor.Close(); }