捕获视频帧而不保存图像以使用 Microsoft 的 Face API
Capturing video frame without saving image to use Microsoft's Face API
我正在制作一个 C# UWP 项目,它使用网络摄像头并在每次看到人脸时拍照。这张图片保存在Image文件夹中,覆盖了之前的图片,然后用在微软的Face上API。
我现在要做的不是将图片保存在 Image 文件夹中,而是我想获取框架并在 API 调用中使用它。
已经搜索了一堆东西但没有找到任何非常具体的东西,找到了一些第三方 class 称为 FrameGrabber 但不太了解如何使用它并且没有找到任何东西有用的文档来帮助我。
想知道是否有人有任何想法可以做到这一点,如果有的话,您是否可以向我提供任何文档或可以帮助我解决此问题的东西。
提前致谢。
Capturing video frame without saving image to use Microsoft's Face API
您可以参考CameraGetPreviewFrame
官方代码示例。它使用 CaptureElement
来显示相机预览帧。您可以直接从 _mediaCapture
实例中获取 SoftwareBitmap
。然后把这个SoftwareBitmap
传给微软的FaceAPI.
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;
}
}
我正在制作一个 C# UWP 项目,它使用网络摄像头并在每次看到人脸时拍照。这张图片保存在Image文件夹中,覆盖了之前的图片,然后用在微软的Face上API。 我现在要做的不是将图片保存在 Image 文件夹中,而是我想获取框架并在 API 调用中使用它。
已经搜索了一堆东西但没有找到任何非常具体的东西,找到了一些第三方 class 称为 FrameGrabber 但不太了解如何使用它并且没有找到任何东西有用的文档来帮助我。
想知道是否有人有任何想法可以做到这一点,如果有的话,您是否可以向我提供任何文档或可以帮助我解决此问题的东西。
提前致谢。
Capturing video frame without saving image to use Microsoft's Face API
您可以参考CameraGetPreviewFrame
官方代码示例。它使用 CaptureElement
来显示相机预览帧。您可以直接从 _mediaCapture
实例中获取 SoftwareBitmap
。然后把这个SoftwareBitmap
传给微软的FaceAPI.
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;
}
}