我有一个问题我不能使用DetectFacesAsync来检测我从我的电脑网络摄像头获得的预览中的面部。你能帮帮我吗?
private async Task GetPreviewFrameAsSoftwareBitmapAsync()
{
var previewProperties = _mediaCapture.VideoDeviceController.GetMediaStreamProperties(MediaStreamType.VideoPreview) as VideoEncodingProperties;
var videoFrame = new VideoFrame(BitmapPixelFormat.Bgra8, (int)previewProperties.Width, (int)previewProperties.Height);
using (var currentFrame = await _mediaCapture.GetPreviewFrameAsync(videoFrame))
{
SoftwareBitmap previewFrame = currentFrame.SoftwareBitmap;
FrameInfoTextBlock.Text = String.Format("{0}x{1} {2}", previewFrame.PixelWidth, previewFrame.PixelHeight, previewFrame.BitmapPixelFormat);
if (ShowFrameCheckBox.IsChecked == true)
{
var sbSource = new SoftwareBitmapSource();
await sbSource.SetBitmapAsync(previewFrame);
PreviewFrameImage.Source = sbSource;
}
}
}
private async Task DetectAsync()
{
IList<DetectedFace> faces = null;
const BitmapPixelFormat InputPixelFormat = BitmapPixelFormat.Nv12;
using (VideoFrame destinationPreviewFrame = new VideoFrame(InputPixelFormat, 640, 480))
{
await this._mediaCapture.GetPreviewFrameAsync(destinationPreviewFrame);
if(FaceDetector.IsBitmapPixelFormatSupported(InputPixelFormat))
{
faces = await this.DetectFacesAsync(SoftwareBitmap, PreviewFrameImage);
foreach (var face in faces)
{
SoftwareBitmap convertedBitmap = SoftwareBitmap.Convert(destinationPreviewFrame.SoftwareBitmap, BitmapPixelFormat.Rgba16);
Byte[] rawBytes = await GetBytesFromBitmap(convertedBitmap, BitmapEncoder.BmpEncoderId, face.FaceBox);
using (Stream stream = rawBytes.AsBuffer().AsStream())
{
var faceAttributesToReturn = new List<FaceAttributeType>()
{
FaceAttributeType.Age,
FaceAttributeType.Emotion,
FaceAttributeType.Hair
};
Face[] detectedFaces = await this.faceServiceClient.DetectAsync(stream, true, true, faceAttributesToReturn);
Debug.Assert(detectedFaces.Length > 0);
}
}
}
}
}
private async Task<byte[]> GetBytesFromBitmap(SoftwareBitmap soft, Guid encoderId, BitmapBounds bounds)
{
byte[] array = null;
using (var ms = new InMemoryRandomAccessStream())
{
BitmapEncoder encoder = await BitmapEncoder.CreateAsync(encoderId, ms);
encoder.SetSoftwareBitmap(soft);
encoder.BitmapTransform.Bounds = bounds;
await encoder.FlushAsync();
array = new byte[ms.Size];
await ms.ReadAsync(array.AsBuffer(), (uint)ms.Size, InputStreamOptions.None);
}
return array;
}
private async Task GetPreviewFrameAsD3DSurfaceAsync()
{
using (var currentFrame = await _mediaCapture.GetPreviewFrameAsync())
{
if (currentFrame.Direct3DSurface != null)
{
var surface = currentFrame.Direct3DSurface;
FrameInfoTextBlock.Text = String.Format("{0}x{1} {2}", surface.Description.Width, surface.Description.Height, surface.Description.Format);
}
{
SoftwareBitmap previewFrame = currentFrame.SoftwareBitmap;
FrameInfoTextBlock.Text = String.Format("{0}x{1} {2}", previewFrame.PixelWidth, previewFrame.PixelHeight, previewFrame.BitmapPixelFormat);
}
PreviewFrameImage.Source = null;
}
}
我不明白错误的来源,DetecFacesAsync是我可以调用Windows.Media.Face.Analysis的方法。对不起我的近似英语。