如何解决UWP 中的人脸检测 问题源代码问题
我正在尝试使用 Microsoft 在 official documentation page 上提供的示例代码(人脸检测)。
我正在尝试开发可识别视频格式人脸的 UWP 应用程序。
问题
Cannot resolve symbol 'GetLatestFrame'
Cannot resolve symbol 'ProcessNextFrameAsync'
Cannot resolve symbol 'SetupVisualization'
源代码
using System;
using System.Collections.Generic;
using System.Threading;
using Windows.Foundation;
using Windows.Graphics.Imaging;
using Windows.Media;
using Windows.Media.FaceAnalysis;
using Windows.System.Threading;
using Windows.UI.Xaml.Controls;
// The Blank Page item template is documented at https://go.microsoft.com/fwlink/?LinkId=402352&clcid=0x409
namespace Network
{
/// <summary>
/// An empty page that can be used on its own or navigated to within a Frame.
/// </summary>
public sealed partial class Network : Page
{
private IAsyncoperation<FaceTracker> faceTracker;
private ThreadPoolTimer frameProcessingTimer;
private SemaphoreSlim frameProcessingSemaphore = new SemaphoreSlim(1);
public Network()
{
this.InitializeComponent();
this.faceTracker = FaceTracker.CreateAsync();
TimeSpan timerInterval = TimeSpan.FromMilliseconds(66); // 15 fps
this.frameProcessingTimer = Windows.System.Threading.ThreadPoolTimer.CreatePeriodicTimer(new Windows.System.Threading.TimerElapsedHandler(ProcessCurrentVideoFrame),timerInterval);
}
public async void ProcessCurrentVideoFrame(ThreadPoolTimer timer)
{
if (!frameProcessingSemaphore.Wait(0))
{
return;
}
VideoFrame currentFrame = await GetLatestFrame();
// Use FaceDetector.GetSupportedBitmapPixelFormats and IsBitmapPixelFormatSupported to dynamically
// determine supported formats
const BitmapPixelFormat faceDetectionPixelFormat = BitmapPixelFormat.Nv12;
if (currentFrame.softwareBitmap.BitmapPixelFormat != faceDetectionPixelFormat)
{
return;
}
try
{
IList<DetectedFace> detectedFaces = await faceTracker.ProcessNextFrameAsync(currentFrame);
var previewFrameSize = new Windows.Foundation.Size(currentFrame.softwareBitmap.PixelWidth,currentFrame.softwareBitmap.PixelHeight);
var ignored = this.dispatcher.RunAsync(Windows.UI.Core.CoredispatcherPriority.normal,() =>
{
this.SetupVisualization(previewFrameSize,detectedFaces);
});
}
catch (Exception e)
{
// Face tracking Failed
}
finally
{
frameProcessingSemaphore.Release();
}
currentFrame.dispose();
}
}
}
问题
我是否错过了添加 documentation page 上提供的方法?
解决方法
GetLatestFrame()
和 SetupVisualization()
是自定义方法,GetLatestFrame()
用于获取视频帧。您可以参考 this 从媒体捕获预览流中获取帧。>
SetupVisualization()
使用框架尺寸和面部结果创建可视化。
private void SetupVisualization(Windows.Foundation.Size framePixelSize,IList<DetectedFace> foundFaces)
{
this.VisualizationCanvas.Children.Clear();
if (this.currentState == ScenarioState.Streaming && framePixelSize.Width != 0.0 && framePixelSize.Height != 0.0)
{
double widthScale = this.VisualizationCanvas.ActualWidth / framePixelSize.Width;
double heightScale = this.VisualizationCanvas.ActualHeight / framePixelSize.Height;
foreach (DetectedFace face in foundFaces)
{
// Create a rectangle element for displaying the face box but since we're using a Canvas
// we must scale the rectangles according to the frames's actual size.
Rectangle box = new Rectangle()
{
Width = face.FaceBox.Width * widthScale,Height = face.FaceBox.Height * heightScale,Margin = new Thickness(face.FaceBox.X * widthScale,face.FaceBox.Y * heightScale,0),Style = HighlightedFaceBoxStyle
};
this.VisualizationCanvas.Children.Add(box);
}
}
}
对于ProcessNextFrameAsync(),它是Windows.Media.FaceAnalysis命名空间下的方法,它异步处理视频帧以进行人脸检测。
推荐使用BasicFaceDetection进行人脸检测,官方提供的样例,你可以试试。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。