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