コード例 #1
0
    void Update()
    {
        using (Capture capture = device.GetCapture())
        {
            tracker.EnqueueCapture(capture);
            var color = capture.Color;
            if (color.WidthPixels > 0)
            {
                Texture2D tex = new Texture2D(color.WidthPixels, color.HeightPixels, TextureFormat.BGRA32, false);
                tex.LoadRawTextureData(color.GetBufferCopy());
                tex.Apply();
                renderer.material.mainTexture = tex;
            }
        }

        using (var frame = tracker.PopResult())
        {
            //Debug.LogFormat("{0} bodies found.", frame.NumBodies);
            if (frame.NumBodies > 0)
            {
                var bodyId = frame.GetBodyId(0);
                //Debug.LogFormat("bodyId={0}", bodyId);
                this.skeleton = frame.GetSkeleton(0);
                Stack <string>  JointNames     = new Stack <string>();
                Stack <Vector3> JointLocations = new Stack <Vector3>();
                for (var i = 0; i < (int)JointId.Count; i++)
                {
                    var joint = this.skeleton.Joints[i];
                    var pos   = joint.Position;
                    var rot   = joint.Orientation;
                    var v     = new Vector3(pos[0], -pos[1], pos[2]) * 0.004f;
                    var r     = new Quaternion(rot[1], rot[2], rot[3], rot[0]);
                    var obj   = debugObjects[i];
                    obj.transform.SetPositionAndRotation(v, r);

                    JointNames.Push(obj.name.ToString());
                    JointLocations.Push(v);
                }

                //handler.UpdateJointLocations(JointNames, JointLocations);
                //if (handler.GetDataCount() == 5000)
                //{
                //    Debug.Log("Wrote JSON!");
                //    handler.WriteJSON();
                //    UnityEditor.EditorApplication.isPlaying = false;
                //}

                string sjl = handler.StreamJointLocations(JointNames, JointLocations);
                streamer.SendMessage(sjl);
            }
        }
    }
コード例 #2
0
ファイル: DebugRenderer.cs プロジェクト: stahlechris/APRLM
    void Update()
    {
        if (canUpdate)
        {
            //this streams camera output as a texture to a plane in the scene
            using (Capture capture = device.GetCapture())
            {
                tracker.EnqueueCapture(capture);
                var color = capture.Color;
                if (color.WidthPixels > 0)
                {
                    Texture2D tex = new Texture2D(color.WidthPixels, color.HeightPixels, TextureFormat.BGRA32, false);
                    tex.LoadRawTextureData(color.GetBufferCopy());
                    tex.Apply();
                    renderer.material.mainTexture = tex;
                }
            }

            //this gets skeleton data from frames and pulls individual joint data from the skeleton to apply to blocks that represent joints
            using (var frame = tracker.PopResult())
            {
                Debug.LogFormat("{0} bodies found.", frame.NumBodies);
                if (frame.NumBodies > 0)
                {
                    var bodyId = frame.GetBodyId(0);
                    //Debug.LogFormat("bodyId={0}", bodyId);
                    this.skeleton = frame.GetSkeleton(0);
                    skeletons.Add(this.skeleton);
                    for (var i = 0; i < (int)JointId.Count; i++)
                    {
                        var joint = this.skeleton.Joints[i];
                        var pos   = joint.Position;
                        //Debug.Log("pos: " + (JointId)i + " " + pos[0] + " " + pos[1] + " " + pos[2]);
                        var rot = joint.Orientation;
                        //Debug.Log("rot " + (JointId)i + " " + rot[0] + " " + rot[1] + " " + rot[2] + " " + rot[3]); // Length 4
                        var v   = new Vector3(pos[0], -pos[1], pos[2]) * 0.004f;
                        var r   = new Quaternion(rot[1], rot[2], rot[3], rot[0]);
                        var obj = debugObjects[i];
                        obj.transform.SetPositionAndRotation(v, r);
                    }
                }
            }
            if (skeletons.Count > 4) // and the current scene is CaptureScene
            {
                Debug.Log("we have enough skeletons");
                GameManager.Instance.currentState = GameState.CaptureCompleted;
                //Disable this Update loop's logic from running
                canUpdate = false;
            }
        } //end if(canUpdate)
    }     //end Update()
コード例 #3
0
 void StreamCameraAsTexture()
 {
     using (Capture capture = device.GetCapture())
     {
         tracker.EnqueueCapture(capture);
         var color = capture.Color;
         if (color.WidthPixels > 0)
         {
             Texture2D tex = new Texture2D(color.WidthPixels, color.HeightPixels, TextureFormat.BGRA32, false);
             tex.LoadRawTextureData(color.GetBufferCopy());
             tex.Apply();
             renderer.material.mainTexture = tex;
         }
     }
 }
コード例 #4
0
    void Update()
    {
        using (Capture capture = device.GetCapture())
        {
            tracker.EnqueueCapture(capture);
            // TODO  DO NOT USE IT!!! It causes memory leak!

            /*var color = capture.Color;
             * if (color != null && color.WidthPixels > 0)
             * {
             *  Texture2D tex = new Texture2D(color.WidthPixels, color.HeightPixels, TextureFormat.BGRA32, false);
             *  tex.LoadRawTextureData(color.GetBufferCopy());
             *  tex.Apply();
             *  renderer.material.mainTexture = tex;
             * }*/
        }

        using (BodyFrame frame = tracker.PopResult())
        {
            Debug.LogFormat("{0} bodies found.", frame.BodyCount);
            if (frame.BodyCount > 0)
            {
                var bodies = frame.Bodies;

                var body = bodies[0];
                for (JointType jt = 0; jt < JointType.Count; jt++)
                {
                    var joint       = body.Skeleton.Joints[(int)jt];
                    var pos         = joint.Position;
                    var orientation = joint.Orientation;
                    var v           = new Vector3(pos.X, -pos.Y, pos.Z) * 0.004f;
                    var r           = new Quaternion(orientation.X, orientation.Y, orientation.Z, orientation.W);
                    var obj         = debugObjects[(int)jt];
                    obj.transform.SetPositionAndRotation(v, r);
                }
            }
        }
    }
コード例 #5
0
    void Update()
    {
        using (Capture capture = device.GetCapture())
        {
            tracker.EnqueueCapture(capture);
            var color = capture.Color;
            if (color.WidthPixels > 0)
            {
                Texture2D tex = new Texture2D(color.WidthPixels, color.HeightPixels, TextureFormat.BGRA32, false);
                tex.LoadRawTextureData(color.GetBufferCopy());
                tex.Apply();
                renderer.material.mainTexture = tex;
            }
        }

        using (var frame = tracker.PopResult())
        {
            Debug.LogFormat("{0} bodies found.", frame.NumBodies);
            if (frame.NumBodies > 0)
            {
                var bodyId = frame.GetBodyId(0);
                Debug.LogFormat("bodyId={0}", bodyId);
                this.skeleton = frame.GetSkeleton(0);
                for (var i = 0; i < (int)JointId.Count; i++)
                {
                    var joint = this.skeleton.Joints[i];
                    var pos   = joint.Position;
                    var rot   = joint.Orientation;
                    var v     = new Vector3(pos[0], -pos[1], pos[2]) * 0.004f;
                    var r     = new Quaternion(rot[1], rot[2], rot[3], rot[0]);
                    var obj   = debugObjects[i];
                    obj.transform.SetPositionAndRotation(v, r);
                }
            }
        }
    }
コード例 #6
0
        private async void Window_Loaded(object sender, RoutedEventArgs e)
        {
            using (Device device = Device.Open(0))
            {
                device.StartCameras(new DeviceConfiguration
                {
                    ColorFormat            = ImageFormat.ColorBGRA32,
                    ColorResolution        = ColorResolution.R720p,
                    DepthMode              = DepthMode.WFOV_2x2Binned,
                    SynchronizedImagesOnly = true,
                    CameraFPS              = FPS.FPS30,
                });

                int colorWidth  = device.GetCalibration().ColorCameraCalibration.ResolutionWidth;
                int colorHeight = device.GetCalibration().ColorCameraCalibration.ResolutionHeight;

                Stopwatch sw         = new Stopwatch();
                int       frameCount = 0;
                sw.Start();

                // Allocate image buffers for us to manipulate
                using (Image transformedDepth = new Image(ImageFormat.Depth16, colorWidth, colorHeight))
                    using (Image outputColorImage = new Image(ImageFormat.ColorBGRA32, colorWidth, colorHeight))
                        using (Transformation transform = device.GetCalibration().CreateTransformation())
                            using (BodyTracker tracker = new BodyTracker(device.GetCalibration()))
                            {
                                tracker.SetTemporalSmoothing(0);

                                while (this.running)
                                {
                                    //if (!Environment.Is64BitProcess)
                                    {
                                        // In 32-bit the BitmapSource memory runs out quickly and we can hit OutOfMemoryException.
                                        // Force garbage collection in each loop iteration to keep memory in check.
                                        GC.Collect();
                                    }

                                    // Wait for a capture on a thread pool thread
                                    using (Capture capture = await Task.Run(() => { return(device.GetCapture()); }).ConfigureAwait(true))
                                    {
                                        // Create a BitmapSource for the unmodified color image.
                                        // Creating the BitmapSource is slow, so do it asynchronously on another thread
                                        Task <BitmapSource> createInputColorBitmapTask = Task.Run(() =>
                                        {
                                            BitmapSource source = capture.Color.CreateBitmapSource();

                                            // Allow the bitmap to move threads
                                            source.Freeze();
                                            return(source);
                                        });

                                        // Compute the colorized output bitmap on a thread pool thread
                                        Task <BitmapSource> createOutputColorBitmapTask = Task.Run(() =>
                                        {
                                            // Transform the depth image to the perspective of the color camera
                                            transform.DepthImageToColorCamera(capture, transformedDepth);

                                            // Get Span<T> references to the pixel buffers for fast pixel access.
                                            Span <ushort> depthBuffer     = transformedDepth.GetPixels <ushort>().Span;
                                            Span <BGRA> colorBuffer       = capture.Color.GetPixels <BGRA>().Span;
                                            Span <BGRA> outputColorBuffer = outputColorImage.GetPixels <BGRA>().Span;

                                            // Create an output color image with data from the depth image
                                            for (int i = 0; i < colorBuffer.Length; i++)
                                            {
                                                // The output image will be the same as the input color image,
                                                // but colorized with Red where there is no depth data, and Green
                                                // where there is depth data at more than 1.5 meters
                                                outputColorBuffer[i] = colorBuffer[i];

                                                if (depthBuffer[i] == 0)
                                                {
                                                    outputColorBuffer[i].R = 255;
                                                }
                                                else if (depthBuffer[i] > 1500)
                                                {
                                                    outputColorBuffer[i].G = 255;
                                                }
                                            }

                                            BitmapSource source = outputColorImage.CreateBitmapSource();

                                            // Allow the bitmap to move threads
                                            source.Freeze();

                                            return(source);
                                        });

                                        // Wait for both bitmaps to be ready and assign them.
                                        BitmapSource inputColorBitmap = await createInputColorBitmapTask.ConfigureAwait(true);

                                        BitmapSource outputColorBitmap = await createOutputColorBitmapTask.ConfigureAwait(true);

                                        this.inputColorImageViewPane.Source  = inputColorBitmap;
                                        this.outputColorImageViewPane.Source = outputColorBitmap;

                                        ++frameCount;

                                        if (sw.Elapsed > TimeSpan.FromSeconds(2))
                                        {
                                            double framesPerSecond = (double)frameCount / sw.Elapsed.TotalSeconds;
                                            this.fps.Content = $"{framesPerSecond:F2} FPS";

                                            frameCount = 0;
                                            sw.Restart();
                                        }

                                        tracker.EnqueueCapture(capture);
                                        using (BodyFrame frame = tracker.PopFrame())
                                        {
                                            for (uint i = 0; i < frame.GetNumBodies(); ++i)
                                            {
                                                Body body = new Body()
                                                {
                                                    id = frame.GetBodyID(i), skeleton = frame.GetBodySkeleton(i)
                                                };
                                                Joint chest = body.skeleton.joints[(int)Joints.SPINE_CHEST];
                                                Console.WriteLine($"User {body.id}: ({chest.position.X}, {chest.position.Y}, {chest.position.Z})");
                                                Vector2?convertedPos = device.GetCalibration().TransformTo2D(chest.position, CalibrationDeviceType.Depth, CalibrationDeviceType.Color);
                                                if (convertedPos.HasValue)
                                                {
                                                    Console.WriteLine($"Converted: ({convertedPos.Value.X}, {convertedPos.Value.Y})");
                                                }
                                            }

                                            frame.GetCapture().Dispose();
                                        }
                                    }
                                }
                            }
            }
        }