【问题标题】:Capturing video frame without saving image to use Microsoft's Face API在不保存图像的情况下捕获视频帧以使用 Microsoft Face API
【发布时间】:2019-04-11 11:50:49
【问题描述】:

我正在制作一个 C# UWP 项目,它使用网络摄像头并在每次看到一张脸时拍照。这张图片保存在 Image 文件夹中,覆盖之前的一张,然后用于 Microsoft 的 Face API。 我现在要做的不是将图片保存在 Image 文件夹中,而是我想获取框架并在 API 调用中使用它。

已经搜索了一堆东西,但没有找到任何非常具体的东西,找到了一些名为 FrameGrabber 的第三方类,但不太了解如何使用它,也没有找到任何有用的文档来帮助我那个。

想知道是否有人知道这样做的方法,如果有,您是否可以向我提供任何文档或可以帮助我解决此问题的东西。

提前致谢。

【问题讨论】:

    标签: c# uwp frame video-capture face-api


    【解决方案1】:

    在不保存图像的情况下捕获视频帧以使用 Microsoft 的人脸 API

    您可以参考CameraGetPreviewFrame官方代码示例。它使用CaptureElement 显示相机预览框。您可以直接从_mediaCapture 实例中获取SoftwareBitmap。然后将此 SoftwareBitmap 传递给 Microsoft 的 Face API。

    private async Task GetPreviewFrameAsSoftwareBitmapAsync()
    {
        // Get information about the preview
        var previewProperties = _mediaCapture.VideoDeviceController.GetMediaStreamProperties(MediaStreamType.VideoPreview) as VideoEncodingProperties;
    
        // Create the video frame to request a SoftwareBitmap preview frame
        var videoFrame = new VideoFrame(BitmapPixelFormat.Bgra8, (int)previewProperties.Width, (int)previewProperties.Height);
    
        // Capture the preview frame
        using (var currentFrame = await _mediaCapture.GetPreviewFrameAsync(videoFrame))
        {
            // Collect the resulting frame
            SoftwareBitmap previewFrame = currentFrame.SoftwareBitmap;
    
            // Show the frame information
            FrameInfoTextBlock.Text = String.Format("{0}x{1} {2}", previewFrame.PixelWidth, previewFrame.PixelHeight, previewFrame.BitmapPixelFormat);
    
            // Add a simple green filter effect to the SoftwareBitmap
            if (GreenEffectCheckBox.IsChecked == true)
            {
                ApplyGreenFilter(previewFrame);
            }
    
            // Show the frame (as is, no rotation is being applied)
            if (ShowFrameCheckBox.IsChecked == true)
            {
                // Create a SoftwareBitmapSource to display the SoftwareBitmap to the user
                var sbSource = new SoftwareBitmapSource();
                await sbSource.SetBitmapAsync(previewFrame);
    
                // Display it in the Image control
                PreviewFrameImage.Source = sbSource;
            }
    
        }
    }
    

    更新 您可以使用以下代码将SoftwareBitmap转换为IRandomAccessStream,然后将其传递给人脸检测api。

    private async Task<InMemoryRandomAccessStream> EncodedStream(SoftwareBitmap soft, Guid encoderId)
    {
    
        using (var ms = new InMemoryRandomAccessStream())
        {
            BitmapEncoder encoder = await BitmapEncoder.CreateAsync(encoderId, ms);
            encoder.SetSoftwareBitmap(soft);
    
            try
            {
                await encoder.FlushAsync();
            }
            catch (Exception ex)
            {
    
            }
    
            return ms;
        }
    }
    

    【讨论】:

    • 我现在的问题是我似乎找不到将软件位图转换为 IO.Stream 的方法,我需要通过 API 发送它。这就是我所拥有的以及我遇到问题的地方:result = await _faceClient.Face.DetectWithStreamAsync(frameBitmap, true, false, new FaceAttributeType[]
    • @DanielEvaristo 我添加了转换器 SoftwareBitmap 到 IRandomAccessStream 方法的更新。
    • 忘记将其标记为正确。非常感谢,这真的很有帮助。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-09-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多