Example #1
0
        //<SnippetVideoFrameAvailable>
        private async void mediaPlayer_VideoFrameAvailable(MediaPlayer sender, object args)
        {
            CanvasDevice canvasDevice = CanvasDevice.GetSharedDevice();

            await Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, () =>
            {
                if (frameServerDest == null)
                {
                    // FrameServerImage in this example is a XAML image control
                    frameServerDest = new SoftwareBitmap(BitmapPixelFormat.Rgba8, (int)FrameServerImage.Width, (int)FrameServerImage.Height, BitmapAlphaMode.Ignore);
                }
                if (canvasImageSource == null)
                {
                    canvasImageSource       = new CanvasImageSource(canvasDevice, (int)FrameServerImage.Width, (int)FrameServerImage.Height, DisplayInformation.GetForCurrentView().LogicalDpi);//96);
                    FrameServerImage.Source = canvasImageSource;
                }

                using (CanvasBitmap inputBitmap = CanvasBitmap.CreateFromSoftwareBitmap(canvasDevice, frameServerDest))
                    using (CanvasDrawingSession ds = canvasImageSource.CreateDrawingSession(Windows.UI.Colors.Black))
                    {
                        mediaPlayer.CopyFrameToVideoSurface(inputBitmap);

                        var gaussianBlurEffect = new GaussianBlurEffect
                        {
                            Source       = inputBitmap,
                            BlurAmount   = 5f,
                            Optimization = EffectOptimization.Speed
                        };

                        ds.DrawImage(gaussianBlurEffect);
                    }
            });
        }
Example #2
0
        private async Task mediaPlayer_VideoFrameAvailableToExtractFrames(MediaPlayer mediaPlayer, object args)
        {
            CanvasDevice canvasDevice = CanvasDevice.GetSharedDevice();

            await Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, () =>
            {
                var canvasImageSrc = new CanvasImageSource(canvasDevice, (int)ThumbnailSize.Width, (int)ThumbnailSize.Height, DisplayInformation.GetForCurrentView().LogicalDpi);//96);
                using (SoftwareBitmap softwareBitmap = new SoftwareBitmap(BitmapPixelFormat.Rgba8, (int)ThumbnailSize.Width, (int)ThumbnailSize.Height, BitmapAlphaMode.Ignore))
                    using (CanvasBitmap inputBitmap = CanvasBitmap.CreateFromSoftwareBitmap(canvasDevice, softwareBitmap))
                        using (CanvasDrawingSession ds = canvasImageSrc.CreateDrawingSession(Windows.UI.Colors.Black))
                        {
                            try
                            {
                                Debug.WriteLine(string.Format("...Extract Position:{0} (State={1})", mediaPlayer.PlaybackSession.Position, mediaPlayer.PlaybackSession.PlaybackState.ToString()));

                                mediaPlayer.CopyFrameToVideoSurface(inputBitmap);
                            }
                            catch (Exception e)
                            {
                                Debug.WriteLine(e);
                                return;
                            }
                            ds.DrawImage(inputBitmap);
                            Frames.Add(canvasImageSrc);
                        }
            });
        }
Example #3
0
 private void M_mediaPlayer_VideoFrameAvailable(MediaPlayer sender, object args)
 {
     lock (thisLock)
     {
         m_mediaPlayer.CopyFrameToVideoSurface(d3dInteropSurface);
     }
 }
Example #4
0
        private async Task NextFrame()
        {
            mediaPlayer.StepForwardOneFrame();
            SoftwareBitmap frameServerDest = null;
            SoftwareBitmap sb = null;
            await Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, () =>
            {
                CanvasDevice canvasDevice = CanvasDevice.GetSharedDevice();
                frameServerDest           = new SoftwareBitmap(BitmapPixelFormat.Bgra8, (int)1200, (int)1200, BitmapAlphaMode.Premultiplied);
                var canvasImageSource     = new CanvasImageSource(canvasDevice, (int)1200, (int)1200, DisplayInformation.GetForCurrentView().LogicalDpi);//96);



                using (CanvasBitmap inputBitmap = CanvasBitmap.CreateFromSoftwareBitmap(canvasDevice, frameServerDest))
                    using (CanvasDrawingSession ds = canvasImageSource.CreateDrawingSession(Windows.UI.Colors.Transparent))
                    {
                        mediaPlayer.CopyFrameToVideoSurface(inputBitmap);
                        ds.DrawImage(inputBitmap);



                        frameServerDest = SoftwareBitmap.CreateCopyFromSurfaceAsync(inputBitmap, BitmapAlphaMode.Premultiplied).AsTask().Result;
                    }
                ImagePreview.Source = canvasImageSource;
            });


            VideoFrame inputImage = VideoFrame.CreateWithSoftwareBitmap(frameServerDest);

            // Evaluate the image

            await EvaluateVideoFrameAsync(inputImage);
        }
Example #5
0
        public override void Draw(GameTime gameTime)
        {
            foreach (VideoComponent videoComponent in Components)
            {
                MediaPlayer mediaPlayer = videoComponent.MediaPlayer;

                if (mediaPlayer.PlaybackSession.PlaybackState == MediaPlaybackState.Playing)
                {
                    if (videoComponent.Target != null)
                    {
                        using Vortice.Direct3D11.ID3D11On12Device device11On12 = ((Vortice.Direct3D11.ID3D11Device)GraphicsDevice.Direct3D11Device).QueryInterface <Vortice.Direct3D11.ID3D11On12Device>();

                        var d3D11RenderTarget = device11On12.CreateWrappedResource(
                            videoComponent.Target.NativeResource,
                            new Vortice.Direct3D11.ResourceFlags {
                            BindFlags = (int)Direct3DBindings.ShaderResource
                        },
                            (int)Vortice.Direct3D12.ResourceStates.CopyDestination,
                            (int)Vortice.Direct3D12.ResourceStates.CopyDestination);

                        using (Vortice.DXGI.IDXGISurface dxgiSurface = d3D11RenderTarget.QueryInterface <Vortice.DXGI.IDXGISurface>())
                        {
                            IDirect3DSurface surface = Direct3DInterop.CreateDirect3DSurface(dxgiSurface);
                            mediaPlayer.CopyFrameToVideoSurface(surface);
                        }

                        device11On12.ReleaseWrappedResources(d3D11RenderTarget);
                    }
                }
            }
        }
        private async void MediaPlayer_VideoFrameAvailableAsync(MediaPlayer sender, object args)
        {
            if (!isRenderringFinished)
            {
                return;
            }
            isRenderringFinished = false;
            //CanvasDevice canvasDevice = CanvasDevice.GetSharedDevice();

            if (frameServerDest == null)
            {
                frameServerDest = new SoftwareBitmap(BitmapPixelFormat.Bgra8, (int)sender.PlaybackSession.NaturalVideoWidth, (int)sender.PlaybackSession.NaturalVideoHeight, BitmapAlphaMode.Ignore);
            }
            await Dispatcher.RunAsync(CoreDispatcherPriority.Normal, async() =>
            {
                canvasImageSource  = new CanvasImageSource(canvasDevice, (int)sender.PlaybackSession.NaturalVideoWidth, (int)sender.PlaybackSession.NaturalVideoWidth, DisplayInformation.GetForCurrentView().LogicalDpi);//96);
                OutputImage.Source = canvasImageSource;

                try
                {
                    using (CanvasBitmap inputBitmap = CanvasBitmap.CreateFromSoftwareBitmap(canvasDevice, frameServerDest))
                    //using (CanvasDrawingSession ds = canvasImageSource.CreateDrawingSession(Colors.Black))
                    {
                        sender.CopyFrameToVideoSurface(inputBitmap);

                        using (var cds = canvasRenderTarget.CreateDrawingSession())
                        {
                            cds.DrawImage(inputBitmap, canvasRenderTarget.Bounds);
                        }

                        var pixelBytes = canvasRenderTarget.GetPixelBytes();

                        if (pixelBytes[40] == 0)
                        {
                            System.Diagnostics.Debug.WriteLine("fuckzero");
                        }

                        var boxes = await DetectObjectPoseFromImagePixelsAsync(pixelBytes).ConfigureAwait(true);

                        DrawBoxes(inputBitmap, boxes, canvasImageSource);
                        //ds.DrawImage(inputBitmap);
                    }
                }
                catch (Exception exception)
                {
                    System.Diagnostics.Debug.WriteLine(exception.Message);
                }
            });

            isRenderringFinished = true;
        }
        /// <summary>
        /// MediaFrameReader.FrameArrived callback. Extracts VideoFrame and timestamp and forwards event
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="args"></param>
        private async void MediaPlayer_VideoFrameAvailable(MediaPlayer sender, object args)
        {
            m_mediaPlayer.CopyFrameToVideoSurface(m_videoFrame.Direct3DSurface);

            if (m_desiredImageDescriptor != null)
            {
                await m_videoFrame.CopyToAsync(m_stagingVideoFrame);

                m_stagingVideoFrame.SystemRelativeTime = m_mediaPlayer.PlaybackSession.Position;
                FrameArrived?.Invoke(this, m_stagingVideoFrame);
            }
            else
            {
                m_videoFrame.SystemRelativeTime = m_mediaPlayer.PlaybackSession.Position;
                FrameArrived?.Invoke(this, m_videoFrame);
            }
        }
Example #8
0
        /**
         * 1フレーム抽出
         */
        private void extractFrame(MediaPlayer mediaPlayer)
        {
            CanvasDevice canvasDevice   = CanvasDevice.GetSharedDevice();
            var          canvasImageSrc = new CanvasImageSource(canvasDevice, (int)mThumbnailSize.Width, (int)mThumbnailSize.Height, 96 /*DisplayInformation.GetForCurrentView().LogicalDpi*/);

            using (SoftwareBitmap softwareBitmap = new SoftwareBitmap(BitmapPixelFormat.Rgba8, (int)mThumbnailSize.Width, (int)mThumbnailSize.Height, BitmapAlphaMode.Ignore))
                using (CanvasBitmap inputBitmap = CanvasBitmap.CreateFromSoftwareBitmap(canvasDevice, softwareBitmap))
                    using (CanvasDrawingSession ds = canvasImageSrc.CreateDrawingSession(Windows.UI.Colors.Black))
                    {
                        try
                        {
                            mediaPlayer.CopyFrameToVideoSurface(inputBitmap);
                            ds.DrawImage(inputBitmap);
                        }
                        catch (Exception e)
                        {
                            // 無視する
                            Debug.WriteLine(e.ToString());
                        }
                        CTX.Frames.Add(canvasImageSrc);
                    }
        }
Example #9
0
        public void CopyFrameToVideoSurface(IDirect3DSurface destination)
        {
            if (destination != null)
            {
                try
                {
                    MediaPlayer.CopyFrameToVideoSurface(destination);
                }
                catch (Exception exception)
                {
                    switch (exception.HResult)
                    {
                    //GPU 设备实例已经暂停
                    case -2005270523:
                        break;

                    default:
                        throw new Exception(exception.Message);
                    }
                }
            }
        }
Example #10
0
        /**
         * CustomDrawingモード時のフレーム描画処理
         */
        private async void MP_FrameAvailable(MediaPlayer mediaPlayer, object args)
        {
            if (mGettingFrame)
            {
                return;
            }

            CanvasDevice canvasDevice = CanvasDevice.GetSharedDevice();

            await Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, () =>
            {
                if (mFrameServerDest == null)
                {
                    // FrameServerImage in this example is a XAML image control
                    var playerSize     = CTX.PlayerSize;
                    mFrameServerDest   = new SoftwareBitmap(BitmapPixelFormat.Rgba8, (int)playerSize.Width, (int)playerSize.Height, BitmapAlphaMode.Ignore);
                    mCanvasImageSource = new CanvasImageSource(canvasDevice, (int)playerSize.Width, (int)playerSize.Height, 96 /*DisplayInformation.GetForCurrentView().LogicalDpi*/);
                    mFrameImage.Source = mCanvasImageSource;
                }
                Debug.WriteLine("Frame: {0}", mediaPlayer.PlaybackSession.Position);
                updateSliderPosition(mediaPlayer.PlaybackSession.Position.TotalMilliseconds);

                using (CanvasBitmap inputBitmap = CanvasBitmap.CreateFromSoftwareBitmap(canvasDevice, mFrameServerDest))
                    using (CanvasDrawingSession ds = mCanvasImageSource.CreateDrawingSession(Windows.UI.Colors.Black))
                    {
                        MediaPlayer.CopyFrameToVideoSurface(inputBitmap);
                        if (null != CustomDraw)
                        {
                            CustomDraw(this, ds, inputBitmap);
                        }
                        else
                        {
                            ds.DrawImage(inputBitmap);
                        }
                    }
            });
        }
        private async void MediaPlayer_VideoFrameAvailable(MediaPlayer sender, object args)
        {
            CanvasDevice canvasDevice = CanvasDevice.GetSharedDevice();
            await Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, async() =>
            {
                SoftwareBitmap softwareBitmapImg;
                SoftwareBitmap frameServerDest = new SoftwareBitmap(BitmapPixelFormat.Bgra8, 500, 500, BitmapAlphaMode.Premultiplied);

                using (CanvasBitmap canvasBitmap = CanvasBitmap.CreateFromSoftwareBitmap(canvasDevice, frameServerDest))
                {
                    sender.CopyFrameToVideoSurface(canvasBitmap);
                    softwareBitmapImg = await SoftwareBitmap.CreateCopyFromSurfaceAsync(canvasBitmap, BitmapAlphaMode.Ignore);
                }

                SoftwareBitmapSource imageSource = new SoftwareBitmapSource();
                await imageSource.SetBitmapAsync(softwareBitmapImg);
                UIPreviewImage.Source = imageSource;

                // Encapsulate the image within a VideoFrame to be bound and evaluated
                VideoFrame inputImage = VideoFrame.CreateWithSoftwareBitmap(softwareBitmapImg);

                await EvaluateVideoFrameAsync(inputImage);
            });
        }
Example #12
0
        private async void VideoFrameAvailable(MediaPlayer sender, object args)
        {
            CanvasDevice canvasDevice = CanvasDevice.GetSharedDevice();
            int          width        = (int)sender.PlaybackSession.NaturalVideoWidth;
            int          height       = (int)sender.PlaybackSession.NaturalVideoHeight;

            if (frameBuffer == null)
            {
                frameBuffer = new SoftwareBitmap(BitmapPixelFormat.Rgba8, width, height, BitmapAlphaMode.Premultiplied);
            }

            await window.Dispatcher.RunAsync(CoreDispatcherPriority.Normal, async() => {
                SoftwareBitmap frame;

                using (var inputBitmap = CanvasBitmap.CreateFromSoftwareBitmap(canvasDevice, frameBuffer)) {
                    sender.CopyFrameToVideoSurface(inputBitmap);
                    frame = await SoftwareBitmap.CreateCopyFromSurfaceAsync(inputBitmap);
                }

                Width  = frame.PixelWidth;
                Height = frame.PixelHeight;

                var cam           = camera.View.Inverse();
                var webcamToWorld = new Matrix4(cam.m00, cam.m01, cam.m02, cam.m03,
                                                cam.m10, cam.m11, cam.m12, cam.m13,
                                                cam.m20, cam.m21, cam.m22, cam.m23,
                                                0, 0, 0, 1);

                FrameReady?.Invoke(new FrameData()
                {
                    bitmap = frame,
                    webcamToWorldMatrix = webcamToWorld,
                    projectionMatrix    = camera.Projection
                });
            });
        }
Example #13
0
 /// <summary>
 /// MediaFrameReader.FrameArrived callback. Extracts VideoFrame and timestamp and forwards event
 /// </summary>
 /// <param name="sender"></param>
 /// <param name="args"></param>
 private void mediaPlayer_VideoFrameAvailable(MediaPlayer sender, object args)
 {
     m_mediaPlayer.CopyFrameToVideoSurface(m_videoFrame.Direct3DSurface);
     m_videoFrame.SystemRelativeTime = m_mediaPlayer.PlaybackSession.Position;
     FrameArrived?.Invoke(this, m_videoFrame);
 }
Example #14
0
 private void Player_VideoFrameAvailable(MediaPlayer sender, object args)
 {
     createSourceTarget();
     sender.CopyFrameToVideoSurface(sourceTarget);
     RenderFrame();
 }