Пример #1
0
        public override void Extract(string outputPath)
        {
            KStudioSeekableEventStream stream = (KStudioSeekableEventStream)_stream;
            int frameCount = _frameAnalysis.FrameCount;

            double[] colorTiming = new double[frameCount];
            var      values      = Enumerable.Range(0, frameCount);

            Parallel.ForEach(values, new ParallelOptions {
                MaxDegreeOfParallelism = 8
            },
                             (value, pls, index) => {
                WriteableBitmap colorBitmap = new WriteableBitmap(ColorWidth, ColorHeight, 96.0, 96.0,
                                                                  PixelFormats.Bgr32,
                                                                  null);
                byte[] colorData = new byte[ColorWidth * ColorHeight * 2];

                // Determine if this frame was a dropped frame
                int frameIndex = _frameAnalysis.FrameMap[(int)index];

                var currEvent = stream.ReadEvent((uint)frameIndex);
                currEvent.CopyEventDataToArray(colorData, 0);

                NativeMethods.YUY2ToBGR(colorData, 1920 * 1080 / 2, colorBitmap.BackBuffer, 1920 * 1080 * 4);
                colorBitmap.WritePixels(new Int32Rect(0, 0, 1920, 1080), colorBitmap.BackBuffer, 1920 * 1080 * 4, colorBitmap.BackBufferStride);

                // create a png bitmap encoder which knows how to save a .png file
                //BitmapEncoder encoder = new PngBitmapEncoder();
                BitmapEncoder encoder = new BmpBitmapEncoder();

                // create frame from the writable bitmap and add to encoder
                encoder.Frames.Add(BitmapFrame.Create(colorBitmap));

                string filePath = outputPath + "/Kinect_Output/Color/";
                Directory.CreateDirectory(filePath);
                string path = Path.Combine(filePath, "ColorFrame_" + index + ".bmp");

                // write the new file to disk
                try {
                    using (FileStream fs = new FileStream(path, FileMode.Create)) {
                        encoder.Save(fs);
                    }
                }
                catch (IOException) { }

                colorTiming[frameIndex] = currEvent.RelativeTime.TotalMilliseconds;

                // Update progress
                OnProgressUpdated(new KinectFileProgressChangedEventArgs {
                    Progress   = (int)((float)((int)index + 1) / frameCount * 100),
                    StatusName = Name
                });
            });

            // Write timings
            string filepath = outputPath + "/Kinect_Output";

            Utils.WriteTimingToFile(filepath + "/color_timing.txt", colorTiming);
        }
Пример #2
0
        public override void Extract(string outputPath)
        {
            KStudioSeekableEventStream stream = (KStudioSeekableEventStream)_stream;

            string filePath = outputPath + "\\Kinect_Output\\Depth\\";

            Utils.ExistOrCreateDirectory(filePath);

            int frameCount = (int)stream.EventCount;

            double[] depthTiming = new double[frameCount];

            var values = Enumerable.Range(0, frameCount);

            Parallel.ForEach(values, (value, pls, index) => {
                WriteableBitmap depthBitmap = new WriteableBitmap(DepthWidth, DepthHeight, 96.0, 96.0,
                                                                  PixelFormats.Gray16, null);
                var currEvent = stream.ReadEvent((uint)index);
                IntPtr buffer = Marshal.AllocHGlobal((int)currEvent.EventDataSize);

                currEvent.CopyEventDataToBuffer(currEvent.EventDataSize, buffer);
                depthBitmap.WritePixels(new System.Windows.Int32Rect(0, 0, DepthWidth, DepthHeight), buffer, (int)currEvent.EventDataSize, depthBitmap.BackBufferStride);

                // create a png bitmap encoder which knows how to save a .png file
                BitmapEncoder encoder = new PngBitmapEncoder();

                // create frame from the writable bitmap and add to encoder
                encoder.Frames.Add(BitmapFrame.Create(depthBitmap));

                string path = Path.Combine(filePath, "DepthFrame_" + index + ".png");

                // write the new file to disk
                try {
                    using (FileStream fs = new FileStream(path, FileMode.Create)) {
                        encoder.Save(fs);
                    }
                }
                catch (IOException ex)
                {
                    Console.WriteLine(ex.StackTrace);
                }

                depthTiming[index] = currEvent.RelativeTime.TotalMilliseconds;

                // Update progress
                OnProgressUpdated(new KinectFileProgressChangedEventArgs {
                    Progress   = (int)((float)(index + 1) / frameCount * 100),
                    StatusName = Name
                });
            });

            // Write timings
            string filepath = outputPath + "\\Kinect_Output";

            Utils.WriteTimingToFile(filepath + "\\depth_timing.txt", depthTiming);
        }
Пример #3
0
        private void DirtyWork(object sender, DoWorkEventArgs e)
        {
            outputData = new ushort[WIDTH * HEIGHT];

            var client = KStudio.CreateClient();

            if (string.IsNullOrEmpty(fileName))
            {
                return;
            }
            var file = client.OpenEventFile(fileName);

            foreach (var item in file.EventStreams)
            {
                if (item.DataTypeName.Equals("Nui Depth"))
                {
                    state = "Depth";

                    KStudioSeekableEventStream stream = item as KStudioSeekableEventStream;
                    this.frameCount = (int)stream.EventCount;
                    timing          = new ushort[frameCount];

                    for (uint i = 0; i < frameCount; i++)
                    {
                        b.ReportProgress((int)((float)i / frameCount * 100));

                        Thread.Sleep(100);
                        var curr_event = stream.ReadEvent(i);
                        //unsafe
                        {
                            int    size = outputData.Length * sizeof(ushort);
                            IntPtr ip   = Marshal.AllocHGlobal(size);

                            uint bufferSize = 0;
                            curr_event.AccessUnderlyingEventDataBuffer(out bufferSize, out ip);

                            Copy(ip, outputData, 0, outputData.Length);
                        }
                        this.timing[i] = (ushort)curr_event.RelativeTime.TotalMilliseconds;
                        string filePath = Environment.CurrentDirectory + "/Xef2Mat_Output/DepthFrame" + i.ToString("D4") + ".mat";
                        MATWriter.ToMatFile("Dep" + i.ToString("D4"), filePath, outputData, HEIGHT, WIDTH);
                    }
                }
                if (item.DataTypeName.Equals("Nui IR"))
                {
                    state = "IR";

                    KStudioSeekableEventStream stream = item as KStudioSeekableEventStream;
                    this.frameCount = (int)stream.EventCount;
                    timing          = new ushort[frameCount];

                    for (uint i = 0; i < frameCount; i++)
                    {
                        b.ReportProgress((int)((float)i / frameCount * 100));

                        var curr_event = stream.ReadEvent(i);
                        //unsafe
                        {
                            int    size = outputData.Length * sizeof(ushort);
                            IntPtr ip   = Marshal.AllocHGlobal(size);

                            uint bufferSize = 0;
                            curr_event.AccessUnderlyingEventDataBuffer(out bufferSize, out ip);

                            Copy(ip, outputData, 0, outputData.Length);
                        }
                        this.timing[i] = (ushort)curr_event.RelativeTime.TotalMilliseconds;
                        string filePath = Environment.CurrentDirectory + "/Xef2Mat_Output/IRFrame" + i.ToString("D4") + ".mat";
                        MATWriter.ToMatFile("IR" + i.ToString("D4"), filePath, outputData, HEIGHT, WIDTH);
                    }
                }
            }
            if (frameCount > 0)
            {
                state = "TimeStamp";
                b.ReportProgress(100);
                string filePath = Environment.CurrentDirectory + "/Xef2Mat_Output/TimeStamp.mat";
                MATWriter.ToMatFile("Time", filePath, this.timing, this.timing.Length, 1);
            }
        }
Пример #4
0
        public override void Extract(string outputPath)
        {
            KStudioSeekableEventStream         stream  = (KStudioSeekableEventStream)_stream;
            IReadOnlyList <KStudioEventHeader> headers = stream.EventHeaders;

            int frameCount = (int)stream.EventCount;

            double[]  audioTiming    = new double[frameCount];
            const int eventFrameSize = 14432; // Size for a single audio event frame
            const int frameSize      = 1024;  // The size of the actual audio data in an event frame
            int       dataSize       = 0;
            int       bytePos        = 0;

            // DEBUG
            int                missedFrames   = 0;
            double             cumulativeTime = 0;
            double             lDiff          = 0;
            KStudioEventHeader lHeader        = null;

            // END DEBUG

            // Hacky counting of total data size
            for (int i = 0; i < frameCount; i++)
            {
                KStudioEventHeader header = headers[i];
                int    j          = eventFrameSize;
                double missedTime = 0;
                double diff       = header.RelativeTime.TotalMilliseconds;

                /* DEBUG */
                if (lHeader != null)
                {
                    diff = header.RelativeTime.TotalMilliseconds - lHeader.RelativeTime.TotalMilliseconds;
                    if (diff > lDiff)
                    {
                        lDiff = diff;
                    }
                }

                lHeader = header;
                /* END DEBUG */

                do
                {
                    dataSize   += frameSize;
                    j          += eventFrameSize - 16;
                    missedTime += 16;
                } while (j <= (int)header.EventDataSize);

                missedTime      = diff - missedTime;
                cumulativeTime += missedTime;
                while (cumulativeTime > 16)
                {
                    cumulativeTime -= 16;
                    missedFrames++;
                }
            }

            // Calculate additional bytes needed
            //int numBytes = (int) totalMissedTime / 16 * 1024;
            int numBytes = missedFrames * frameSize;

            if (numBytes > 0)
            {
                dataSize += numBytes;
            }

            // DEBUG
            missedFrames   = 0;
            cumulativeTime = 0;
            KStudioEvent lEvent = null;

            // END DEBUG

            byte[] rawAudio = new byte[dataSize];

            for (uint i = 0; i < frameCount; i++)
            {
                int    j          = eventFrameSize;
                var    currEvent  = stream.ReadEvent(i);
                byte[] rawBuffer  = new byte[currEvent.EventDataSize];
                int    offset     = 96;
                double diff       = currEvent.RelativeTime.TotalMilliseconds;
                double missedTime = 0;

                if (lEvent != null)
                {
                    diff = currEvent.RelativeTime.TotalMilliseconds - lEvent.RelativeTime.TotalMilliseconds;
                    if (diff > lDiff)
                    {
                        lDiff = diff;
                    }
                }

                lEvent = currEvent;

                audioTiming[i] = currEvent.RelativeTime.TotalMilliseconds;
                currEvent.CopyEventDataToArray(rawBuffer, 0);

                do
                {
                    Array.Copy(rawBuffer, offset, rawAudio, bytePos, frameSize);
                    bytePos    += frameSize;
                    j          += eventFrameSize - 16;
                    offset     += eventFrameSize - 16;
                    missedTime += 16;
                } while (j <= (int)currEvent.EventDataSize);

                // If there is any missed time, add it here.
                missedTime      = diff - missedTime;
                cumulativeTime += missedTime;
                while (cumulativeTime > 16)
                {
                    cumulativeTime -= 16;
                    missedFrames++;
                    bytePos += frameSize;
                }

                // Update progress
                OnProgressUpdated(new KinectFileProgressChangedEventArgs {
                    Progress   = (int)((float)(i + 1) / frameCount * 100),
                    StatusName = Name
                });
            }

            string filepath = outputPath + "/Kinect_Output";

            Directory.CreateDirectory(filepath);
            File.WriteAllBytes(filepath + "/raw_audio.wav", rawAudio);

            // Write timings
            Utils.WriteTimingToFile(filepath + "/audio_timing.txt", audioTiming);
        }