Ejemplo n.º 1
0
    private void _OnFrameArrived(object sender, DepthFrameArrivedEventArgs e)
    {
        var frame = _Reader.AcquireLatestFrame();

        if (frame != null)
        {
            frame.CopyFrameDataToArray(_Data);
            frame.Dispose();
            frame = null;
        }
        if (OnDepthFrameArrived != null)
        {
            OnDepthFrameArrived(_Data);
        }
    }
Ejemplo n.º 2
0
    // Update is called once per frame
    void Update()
    {
        //Interval
        //if ((i++ % interval) != 0)
        //return;

        //No reader? return
        if (depthReader == null)
        {
            return;
        }

        //Otherwise read the frame
        var frame = depthReader.AcquireLatestFrame();

        //Frame null? return
        if (frame == null)
        {
            return;
        }

        //Otherwise copy data
        frame.CopyFrameDataToArray(depthBuffer);
        frame.Dispose();

        //Set buffer data
        depthComputeBuffer.SetData(depthBuffer);

        //Dispatch compute shaders
        this.DispatchComputeShaders();
    }
Ejemplo n.º 3
0
    // Update is called once per frame
    override public void Update()
    {
        base.Update();

        if (_Sensor == null)
        {
            return;
        }

        if (_Reader != null)
        {
            var frame = _Reader.AcquireLatestFrame();
            if (frame != null)
            {
                frame.CopyFrameDataToArray(_Data);
                frame.Dispose();
                frame = null;
                RefreshData();
            }
        }

        Material m = GetComponent <Renderer>().material;

        m.SetFloat("_MinX", minX);
        m.SetFloat("_MaxX", maxX);
        m.SetFloat("_MinY", minY);
        m.SetFloat("_MaxY", maxY);
        m.SetFloat("_MinZ", minZ);
        m.SetFloat("_MaxZ", maxZ);
        m.SetFloat("_DistMin", minDist);
        m.SetFloat("_DistMax", maxDist);
        m.SetFloat("_ScaleFactor", scaleFactor);

        //gameObject.GetComponent<Renderer>().material.mainTexture = _ColorManager.GetColorTexture();
    }
Ejemplo n.º 4
0
        private void DispactherTimerTick(object sender, EventArgs e)
        {
            var depthFrame = depthFrameReader.AcquireLatestFrame();

            if (depthFrame == null)
            {
                return;
            }

            depthFrameData = new ushort[depthFrame.FrameDescription.LengthInPixels];
            depthFrame.CopyFrameDataToArray(depthFrameData);

            UpdateRawKinectImage(depthFrameData, 3000);
            if (remaningReferenceSamples > 0)
            {
                CreateReferenceFrame(depthFrameData);
            }
            else
            {
                CreateBinaryImage(depthFrameData);
                DetectPeople(frameWidth, frameHeight, binaryImage);
            }

            depthFrame.Dispose();
        }
Ejemplo n.º 5
0
    // Update is called once per frame
    void Update()
    {
        if (reader != null)
        {
            var frame = reader.AcquireLatestFrame();
            if (frame != null)
            {
                frame.CopyFrameDataToArray(depthData);

                for (var i = 0; i < depthData.Length; i++)
                {
                    depthDataInt[i] = depthData[i];
                }
                depthBuffer.SetData(depthDataInt);

                var kernel = depthTexGen.FindKernel("buildDepthTex");
                depthTexGen.SetBuffer(kernel, "_DepthData", depthBuffer);
                depthTexGen.SetTexture(kernel, "_DepthTex", depthTexture);
                depthTexGen.Dispatch(kernel, width / 8, height / 8, 1);

                frame.Dispose();
                frame = null;
            }
        }
    }
Ejemplo n.º 6
0
    void Update()
    {
        if (_Reader != null)
        {
            var frame     = _Reader.AcquireLatestFrame();
            var frameDesc = _Sensor.DepthFrameSource.FrameDescription;
            if (frame != null)
            {
                frame.CopyFrameDataToArray(_Data);

                // ushort(16bit) のアレーをそのまま byte(8bit)のアレーとしてコピーする
                // https://stackoverflow.com/questions/37213819/convert-ushort-into-byte-and-back
                // https://msdn.microsoft.com/en-us/library/system.buffer.blockcopy(v=vs.110).aspx
                Buffer.BlockCopy(_Data, 0, _RawData, 0, _Data.Length * 2);

                // byteのデータを使ってテクスチャデータを更新する
                // https://docs.unity3d.com/ScriptReference/Texture2D.LoadRawTextureData.html
                _Texture.LoadRawTextureData(_RawData);
                _Texture.Apply();

                frame.Dispose();
                frame = null;
            }
        }
    }
Ejemplo n.º 7
0
    void Update()
    {
        if (_Reader != null)
        {
            var frame = _Reader.AcquireLatestFrame();
            //Debug.Log(frame != null);
            if (frame != null)
            {
                frame.CopyFrameDataToArray(_Data);

                int index = 0;
                foreach (var ir in _Data)
                {
                    byte intensity = (byte)Remap(minDist, maxDist, 255, 0, ir);
                    _RawData[index++] = intensity;
                    _RawData[index++] = intensity;
                    _RawData[index++] = intensity;
                    _RawData[index++] = 255; // Alpha
                }

                _Texture.LoadRawTextureData(_RawData);
                _Texture.Apply();

                frame.Dispose();
                frame = null;
            }
        }
    }
Ejemplo n.º 8
0
 /// <summary>
 /// Acquires the latest depth frame.
 /// It calles the OnDepthFrameReceived only if the acquired frame is not null.
 /// </summary>
 protected void UpdateDepthFrame(bool updateFrameView = true)
 {
     if (depthFrameReader != null)
     {
         using (DepthFrame frame = depthFrameReader.AcquireLatestFrame())
         {
             if (frame != null)
             {
                 if (updateFrameView)
                 {
                     frameView.FrameTexture = frame.ToBitmap();
                 }
                 OnDepthFrameReceived(frame);
             }
         }
     }
 }
Ejemplo n.º 9
0
 void Update()
 {
     if (_Reader != null)
     {
         var frame = _Reader.AcquireLatestFrame();
         ProcessFrame(frame);
     }
 }
Ejemplo n.º 10
0
        public void Update()
        {
            DepthFrame frame = reader.AcquireLatestFrame();

            if (frame != null)
            {
                frame.CopyFrameDataToArray(Data);
                frame.Dispose();
                frame = null;
            }
        }
    // Update is called once per frame
    void Update()
    {
        if (depthReader == null)
        {
            return;
        }
        var frame = depthReader.AcquireLatestFrame();

        if (frame != null)
        {
            int depthFrameWidth      = frame.FrameDescription.Width;
            int depthFrameHeight     = frame.FrameDescription.Height;
            int depthFramePixelCount = depthFrameWidth * depthFrameHeight;

            float horizontalFov = frame.FrameDescription.HorizontalFieldOfView;
            float verticalFov   = frame.FrameDescription.VerticalFieldOfView;
            mapDimensions[0] = depthFrameWidth;
            mapDimensions[1] = depthFrameHeight;
            if (tempPositionTexture != null && (tempPositionTexture.width != depthFrameWidth || tempPositionTexture.height != depthFrameHeight))
            {
                Destroy(tempPositionTexture);
                tempPositionTexture = null;
            }

            if (tempPositionTexture == null)
            {
                tempPositionTexture = new RenderTexture(depthFrameWidth, depthFrameHeight, 0, RenderTextureFormat.ARGBHalf);
                tempPositionTexture.enableRandomWrite = true;
                tempPositionTexture.Create();
            }

            if (positionBuffer == null)
            {
                positionBuffer = new ComputeBuffer(depthFramePixelCount / 2, sizeof(uint));
            }

            using (KinectBuffer depthBuffer = frame.LockImageBuffer())
            {
                positionBuffer.SetData(depthBuffer.UnderlyingBuffer, depthFramePixelCount, sizeof(uint));
                int bakeDepthkernel = pointerCloudBaker.FindKernel("BakeDepth");
                pointerCloudBaker.SetInts("MapDimensions", mapDimensions);
                pointerCloudBaker.SetFloat("NearThreshold", nearThreshold);
                pointerCloudBaker.SetFloat("FarThreshold", farThreshold);
                pointerCloudBaker.SetBuffer(bakeDepthkernel, "PositionBuffer", positionBuffer);
                pointerCloudBaker.SetTexture(bakeDepthkernel, "PositionTexture", tempPositionTexture);
                pointerCloudBaker.Dispatch(bakeDepthkernel, depthFrameHeight / 8, depthFrameWidth / 8, 1);
            }
            Graphics.Blit(tempPositionTexture, pointCloud);

            frame.Dispose();
            frame = null;
        }
    }
Ejemplo n.º 12
0
    void Update()
    {
        if (depthFrameReader != null)
        {
            var frame = depthFrameReader.AcquireLatestFrame();
            if (frame != null)
            {
                frame.CopyFrameDataToArray(depthFrameData);
                int frameWidth  = frame.FrameDescription.Width;
                int frameHeight = frame.FrameDescription.Height;
                frame.Dispose();
                frame = null;

                sensor.CoordinateMapper.MapDepthFrameToCameraSpace(depthFrameData, cameraSpacePoints);

                mapDimensions[0] = frameWidth;
                mapDimensions[1] = frameHeight;

                if (tempPositionTexture != null && (tempPositionTexture.width != frameWidth || tempPositionTexture.height != frameHeight))
                {
                    Destroy(tempPositionTexture);
                    tempPositionTexture = null;
                }

                if (positionBuffer != null && positionBuffer.count != cameraSpacePoints.Length)
                {
                    positionBuffer.Dispose();
                    positionBuffer = null;
                }

                if (tempPositionTexture == null)
                {
                    tempPositionTexture = new RenderTexture(frameWidth, frameHeight, 0, RenderTextureFormat.ARGBHalf);
                    tempPositionTexture.enableRandomWrite = true;
                    tempPositionTexture.Create();
                }

                if (positionBuffer == null)
                {
                    positionBuffer = new ComputeBuffer(cameraSpacePoints.Length, sizeof(float) * 3);
                }
                positionBuffer.SetData(cameraSpacePoints);

                int kernel = PointCloudBaker.FindKernel("BakeDepth");
                PointCloudBaker.SetInts("MapDimensions", mapDimensions);
                PointCloudBaker.SetBuffer(kernel, "PositionBuffer", positionBuffer);
                PointCloudBaker.SetTexture(kernel, "PositionTexture", tempPositionTexture);
                PointCloudBaker.Dispatch(kernel, frameWidth / 8, frameHeight / 8, 1);

                Graphics.CopyTexture(tempPositionTexture, PointCloudMap);
            }
        }
    }
Ejemplo n.º 13
0
 //每個Updata讀取每個畫面的資訊儲存到DATA陣列之中。
 public void UpdateDepth()
 {
     if (_Reader != null)
     {
         var frame = _Reader.AcquireLatestFrame();
         if (frame != null)
         {
             frame.CopyFrameDataToArray(_Data);
             frame.Dispose();
             frame = null;
         }
     }
 }
Ejemplo n.º 14
0
 void Update()
 {
     if (depthReader != null)
     {
         using (DepthFrame frame = depthReader.AcquireLatestFrame()) {
             if (frame != null)
             {
                 frame.CopyFrameDataToArray(Depth);
                 frame.Dispose();
             }
         }
     }
 }
Ejemplo n.º 15
0
 void Update()
 {
     if (_Reader != null)
     {
         var frame = _Reader.AcquireLatestFrame();
         if (frame != null)
         {
             //Debug.Log("depth not null");
             frame.CopyFrameDataToArray(_Data);
             frame.Dispose();
             frame = null;
         }
     }
 }
Ejemplo n.º 16
0
 void Update()
 {
     if (_Reader != null)
     {
         var frame = _Reader.AcquireLatestFrame();
         if (frame != null)
         {
             print("frame");
             frame.CopyFrameDataToArray(_Data);
             frame.Dispose();
             frame = null;
         }
     }
 }
Ejemplo n.º 17
0
 void Update()
 {
     if (colorReader != null)
     {
         updateColor(colorReader.AcquireLatestFrame());
     }
     if (infraredReader != null)
     {
         updateInfrared(infraredReader.AcquireLatestFrame());
     }
     if (depthReader != null)
     {
         updateDepth(depthReader.AcquireLatestFrame());
     }
 }
Ejemplo n.º 18
0
    public bool PollDepthFrame(KinectInterop.SensorData sensorData)
    {
        bool bNewFrame = false;

        if ((multiSourceFrameReader != null && multiSourceFrame != null) ||
            depthFrameReader != null)
        {
            var depthFrame = multiSourceFrame != null?multiSourceFrame.DepthFrameReference.AcquireFrame() :
                                 depthFrameReader.AcquireLatestFrame();

            if (depthFrame != null)
            {
                var pDepthData = GCHandle.Alloc(sensorData.depthImage, GCHandleType.Pinned);
                depthFrame.CopyFrameDataToIntPtr(pDepthData.AddrOfPinnedObject(), (uint)sensorData.depthImage.Length * sizeof(ushort));
                pDepthData.Free();

                sensorData.lastDepthFrameTime = depthFrame.RelativeTime.Ticks;

                depthFrame.Dispose();
                depthFrame = null;

                bNewFrame = true;
            }

            if ((multiSourceFrameReader != null && multiSourceFrame != null) ||
                bodyIndexFrameReader != null)
            {
                var bodyIndexFrame = multiSourceFrame != null?multiSourceFrame.BodyIndexFrameReference.AcquireFrame() :
                                         bodyIndexFrameReader.AcquireLatestFrame();

                if (bodyIndexFrame != null)
                {
                    var pBodyIndexData = GCHandle.Alloc(sensorData.bodyIndexImage, GCHandleType.Pinned);
                    bodyIndexFrame.CopyFrameDataToIntPtr(pBodyIndexData.AddrOfPinnedObject(), (uint)sensorData.bodyIndexImage.Length);
                    pBodyIndexData.Free();

                    sensorData.lastBodyIndexFrameTime = bodyIndexFrame.RelativeTime.Ticks;

                    bodyIndexFrame.Dispose();
                    bodyIndexFrame = null;

                    bNewFrame = true;
                }
            }
        }

        return(bNewFrame);
    }
Ejemplo n.º 19
0
 void Update()
 {
     if (_Reader != null)
     {
         var frame = _Reader.AcquireLatestFrame();
         if (frame != null)
         {
             frame.CopyFrameDataToArray(_Data);
             enhPixelData = DepthToColorImage(_Data);
             _DTexture.LoadRawTextureData(enhPixelData);
             _DTexture.Apply();
             frame.Dispose();
             frame = null;
         }
     }
 }
    // 毎フレームごとの処理
    void Update()
    {
        if (_Reader != null)
        {
            // 最新の深度情報を取得
            var frame = _Reader.AcquireLatestFrame();

            // 現在のフレーム情報更新
            if (frame != null)
            {
                frame.CopyFrameDataToArray(_Data);
                frame.Dispose();
                frame = null;
            }
        }
    }
    /// <summary>
    /// Update Kinect depth map
    /// </summary>
    private void UpdateDepthData()
    {
        if (_isInitialized)
        {
            DepthFrame depthFrame = _depthReader.AcquireLatestFrame();

            if (depthFrame != null)
            {
                depthFrame.CopyFrameDataToArray(_depthData);

                depthFrame.Dispose();

                depthFrame = null;
            }
        }
    }
Ejemplo n.º 22
0
    public override bool pollDepth()
    {
        if (reader != null)
        {
            var frame = reader.AcquireLatestFrame();
            if (frame != null)
            {
                frame.CopyFrameDataToArray(data);
                frame.Dispose();
                frame = null;

                return(true);
            }
        }

        return(false);
    }
Ejemplo n.º 23
0
        public KinectTrackableSource GetDepthFrame()
        {
            using (var depthFrame = depthFrameReader.AcquireLatestFrame())
            {
                if (depthFrame == null)
                {
                    return(null);//throw new ArgumentNullException("Could not get frame from Kinect");
                }
                kinectTrackableSource = new KinectTrackableSource(depthFrame.FrameDescription.LengthInPixels,
                                                                  minimumRealiableTrackingDistance,
                                                                  maximumRealiableTrackingDistance,
                                                                  FrameHeight, FrameWidth);
                depthFrame.CopyFrameDataToArray(kinectTrackableSource.DepthFrameData);
            }

            lastCollectedFrame = kinectTrackableSource;
            return(kinectTrackableSource);
        }
Ejemplo n.º 24
0
 IEnumerator UpdateDepth()
 {
     while (true)
     {
         if (_Reader != null)
         {
             var frame = _Reader.AcquireLatestFrame();
             if (frame != null)
             {
                 frame.CopyFrameDataToArray(_Data);
                 frame.Dispose();
                 DepthFrameUpdateEvent(_Data, Width(), Height());
                 frame = null;
             }
         }
         yield return(new WaitForSeconds(updateTimeMillis / 1000.0f));
     }
 }
Ejemplo n.º 25
0
    public static bool PollDepth(DepthFrameReader reader, bool isNearMode, ref ushort[] depthFrameData)
    {
        bool newDepth = false;

        if (reader != null)
        {
            var frame = reader.AcquireLatestFrame();
            if (frame != null)
            {
                newDepth = true;
                frame.CopyFrameDataToArray(depthFrameData);
                frame.Dispose();
                frame = null;
            }
        }

        return(newDepth);
    }
Ejemplo n.º 26
0
    // Update is called once per frame
    void Update()
    {
        if (_Reader == null)
        {
            return;
        }

        var frame = _Reader.AcquireLatestFrame();

        if (frame == null)
        {
            return;
        }
        frame.CopyFrameDataToArray(_Data);

        frame.Dispose();
        frame = null;
    }
Ejemplo n.º 27
0
    private Boolean updatingTerrain = true; // Flag that keeps the terrain update thread alive


    void updateTerrain()
    {
        while (updatingTerrain)
        {
            // Debug.Log("Updating Terrain...");
            if (_Reader != null)
            {
                var frame = _Reader.AcquireLatestFrame();
                if (frame != null)
                {
                    frame.CopyFrameDataToArray(_Data);
                    frame.Dispose();
                    frame = null;
                }
            }
            UpdateHeightMap();
            Thread.Sleep(100);
        }
    }
Ejemplo n.º 28
0
        public static ushort[] GetDepthData(this DepthFrameReader reader)
        {
            if (reader == null)
            {
                return(null);
            }

            using (var frame = reader.AcquireLatestFrame())
            {
                if (frame == null)
                {
                    return(null);
                }

                var data = new ushort[frame.FrameDescription.LengthInPixels];
                frame.CopyFrameDataToArray(data);
                return(data);
            }
        }
Ejemplo n.º 29
0
    // Update is called once per frame
    void Update()
    {
        //Checks to see if there is a reader, if so grab depth information and
        //put it into the list of ushort called data
        if (reader != null)
        {
            var frame = reader.AcquireLatestFrame();
            if (frame != null)
            {
                frame.CopyFrameDataToArray(data);
                frame.Dispose();
                frame = null;
            }
            else
            {
                state = true;
            }
        }

        //When space is pressed write current data into a file
        if (Input.GetKeyDown(KeyCode.Space))
        {
            print("Space key is pressed");
            if (data != null)
            {
                //Path where the file is written. root location is project root
                var path = ".\\snapShot.txt";
                using (FileStream fs = new FileStream(path, FileMode.Truncate, FileAccess.Write))
                {
                    using (StreamWriter sw = new StreamWriter(fs))
                    {
                        foreach (ushort value in data)
                        {
                            sw.Write(Convert.ToString(value) + ",");
                        }
                    }
                }

                //Prints out a sample, if this is not zero, you are probably detecting something
                print(data[222]);
            }
        }
    }
Ejemplo n.º 30
0
    void Update()
    {
        if (_Reader != null)
        {
            var frame = _Reader.AcquireLatestFrame();

            if (frame != null)
            {
                updateSuccessful = true;
                frame.CopyFrameDataToArray(_Data);
                frame.Dispose();
                frame = null;
            }
            else
            {
                updateSuccessful = false;
            }
        }
    }
Ejemplo n.º 31
0
 static void Main(string[] args)
 {
     unix_base_ts = new TimeSpan(unix_base_ticks);
     // Initialize the kinect sensor
     //kinect IS NOT AN INSTANTIABLE OBJECT - you get a 'device' from the system driver
     try
     {
         kinect = KinectSensor.Default;
     }
     catch (ArgumentOutOfRangeException)
     {
         Console.WriteLine("It appears that no Kinect sensor is connected to your computer!");
         return;
     }
     //device initialization code
     try
     {
         Console.WriteLine("Starting initialization");
         kinect.Open();
         color_reader = kinect.ColorFrameSource.OpenReader();
         depth_reader = kinect.DepthFrameSource.OpenReader();
         Console.WriteLine("Initialization successful");
     }
     catch (InvalidOperationException)
     {
         Console.WriteLine("Failed trying to set up the kinect. Is the device connected, on, and not being used by another application?");
         return;
     }
     // Initialize the socket bridge
     color_tx = new ROS_CS.SocketBridge.SocketTX<ROS_CS.sensor_msgs.Image>(9001);
     depth_tx = new ROS_CS.SocketBridge.SocketTX<ROS_CS.sensor_msgs.Image>(9002);
     //color_rx = new ROS_CS.SocketBridge.SocketRX<ROS_CS.sensor_msgs.Image>("127.0.0.1", 9001, ColorImageCB);
     bool control = true;
     long depth_timestamp = 0;
     long color_timestamp = 0;
     while (control)
     {
         //Get depth data
         DepthFrame latest_depth = depth_reader.AcquireLatestFrame();
         if (latest_depth != null)
         {
             Console.WriteLine("Current depth timestamp: {0:D}, new timestamp: {1:D}", depth_timestamp, latest_depth.RelativeTime);
             if (latest_depth.RelativeTime > depth_timestamp)
             {
                 ROS_CS.sensor_msgs.Image depth_image = GetDepthImageFromRaw(latest_depth);
                 if (depth_image != null)
                 {
                     Console.WriteLine("Sending depth image");
                     depth_tx.Send(depth_image);
                 }
                 else
                 {
                     Console.WriteLine("Null depth image");
                 }
                 depth_timestamp = latest_depth.RelativeTime;
             }
         }
         //Get color data
         ColorFrame latest_color = color_reader.AcquireLatestFrame();
         if (latest_color != null)
         {
             Console.WriteLine("Current color timestamp: {0:D}, new timestamp: {1:D}", color_timestamp, latest_color.RelativeTime);
             if (latest_color.RelativeTime > color_timestamp)
             {
                 ROS_CS.sensor_msgs.Image color_image = GetColorImageFromRaw(latest_color);
                 if (color_image != null)
                 {
                     Console.WriteLine("Sending color image");
                     color_tx.Send(color_image);
                 }
                 else
                 {
                     Console.WriteLine("Null color image");
                 }
                 color_timestamp = latest_color.RelativeTime;
             }
         }
     }
 }