首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >UWP中的人脸检测

问UWP中的人脸检测
EN

Stack Overflow用户
提问于 2021-05-03 17:38:55
回答 1查看 50关注 0票数 0

我正在尝试使用微软在official documentation page上提供的示例代码(人脸检测)。

我正在尝试开发一个UWP应用程序,可以识别视频格式的人脸。

问题

某些方法似乎不存在于源代码中,IDE会将它们标记为

Cannot resolve symbol 'GetLatestFrame'

Cannot resolve symbol 'ProcessNextFrameAsync'

Cannot resolve symbol 'SetupVisualization'

源代码

代码语言:javascript
复制
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上提供的方法

我需要手动添加方法吗?我需要创建另一个类吗?

EN

回答 1

Stack Overflow用户

发布于 2021-05-05 14:13:06

GetLatestFrame()和SetupVisualization()是自定义的方法,GetLatestFrame()用于获取视频,frame.You可以引用this从媒体捕获预览流中获取帧。

SetupVisualization()使用结构件尺寸和面结果创建可视化。

代码语言:javascript
复制
  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, 0),
                    Style = HighlightedFaceBoxStyle
                };
                this.VisualizationCanvas.Children.Add(box);
            }
        }
    }

对于Windows.Media.FaceAnalysis命名空间下的方法ProcessNextFrameAsync(),它异步处理视频帧进行人脸检测。

建议使用BasicFaceDetection做人脸检测,这是官方的示例,你可以尝试一下。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/67366642

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档