我需要获取当前预览帧分辨率才能正确显示检测到的面部边界:
_mediaCapture = new MediaCapture();
await _mediaCapture.InitializeAsync();
var definition = new FaceDetectionEffectDefinition();
var faceDetectionEffect = (FaceDetectionEffect)await _mediaCapture.AddVideoEffectAsync(definition, MediaStreamType.VideoPreview);
faceDetectionEffect.DesiredDetectionInterval = TimeSpan.FromMilliseconds(33);
faceDetectionEffect.Enabled = true;
faceDetectionEffect.FaceDetected += FaceDetected;
PreviewControl.Source = _mediaCapture;
await _mediaCapture.StartPreviewAsync();
private async void FaceDetected(object sender, FaceDetectedEventArgs args)
{
double scale = PreviewControl.ActualWidth / ???;
await Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.High, () =>
{
var faces = args.ResultFrame.DetectedFaces;
for (int i = 0; i < faces.Count; i++)
{
double scale = P
var faceRect = faces[i].FaceBox;
Rectangle item;
if (FacesCanvas.Children.Count <= i)
{
item = new Rectangle();
item.Fill = new SolidColorBrush(Colors.AliceBlue);
FacesCanvas.Children.Add(item);
}
else item = (Rectangle)FacesCanvas.Children[i];
item.Height = faceRect.Height * scale;
item.Width = faceRect.Width * scale;
Canvas.SetLeft(item, faceRect.X * scale);
Canvas.SetTop(item, faceRect.Y * scale);
}
});
}
答案 0 :(得分:1)
好的,我明白了:
var props = (VideoEncodingProperties) _mediaCapture.VideoDeviceController
.GetMediaStreamProperties(MediaStreamType.VideoPreview);
double scale = PreviewControl.ActualWidth / props.Width;