【问题标题】:Can't get FaceTracker class to work on HoloLens 2无法让 FaceTracker 类在 HoloLens 2 上工作
【发布时间】:2020-07-09 11:47:37
【问题描述】:

我无法让 FaceTracker Class 在 HoloLens 2 上工作。 一旦我尝试使用ProcessNextFrameAsync Method 检测人脸,就会出现以下异常:

System.Runtime.InteropServices.COMException (0x80004005): Unspecified error

这只是错误信息的第一部分,如果需要更多信息,我可以补充。

请参阅此以获取最小示例。

public async void Start()
{
    var selectedGroup = await FindCameraAsync();
    await StartMediaCaptureAsync(selectedGroup);
}
private async Task StartMediaCaptureAsync(MediaFrameSourceGroup sourceGroup)
{
    faceTracker = await FaceTracker.CreateAsync();
    this.mediaCapture = new MediaCapture();
    await this.mediaCapture.InitializeAsync(settings);
    this.frameProcessingTimer = ThreadPoolTimer.CreatePeriodicTimer(ProcessCurrentVideoFrameAsync, timerInterval);
}
private async Task ProcessCurrentVideoFrameAsync()
{
    const BitmapPixelFormat InputPixelFormat = BitmapPixelFormat.Nv12;
    var deviceController = this.mediaCapture.VideoDeviceController;
    this.videoProperties = deviceController.GetMediaStreamProperties(MediaStreamType.VideoPreview) as VideoEncodingProperties;
    VideoFrame videoFrame = new VideoFrame(InputPixelFormat, (int)this.videoProperties.Width (int)this.videoProperties.Height);

    IList<DetectedFace> detectedFaces;
    try
    {
        detectedFaces = await faceTracker.ProcessNextFrameAsync(videoFrame);
    }
    catch (Exception e)
    {
        System.Diagnostics.Debug.WriteLine($"Failed with Exception: {e.ToString()}");
        return;
    }

    videoFrame.Dispose();
}
  1. 我用MediaFrameSourceKind.ColorMediaStreamType.VideoPreviewFindCameraAsync() 得到了一个合适的相机。我认为这很好用。
  2. 启动MediaCaptureStartMediaCaptureAsync() 内的FaceTracker
  3. 尝试在ProcessCurrentVideoFrameAsync()中检测人脸

以下是我测试过的东西和我收到的信息:

  • 我有一张格式为Nv12PixelWidth1504 和PixelHeigt846 的图片
  • 在 Unity 中为网络摄像头、图片库和麦克风授予权限
  • 应用程序使用 Il2CPP 编译
  • 启动应用程序后出现消息No capture devices are available.。在其他文章中提到缺少权限(网络摄像头或麦克风),但事实并非如此。但仍然可以连接。
  • 我使用了Track faces in a sequence of framesBasic Face Tracking sample 作为参考

我非常感谢每一个激励和想法。

2020 年 7 月 14 日更新

我刚刚在 HoloLens 2 上本地存储的几个单独图像上尝试了FaceDetector。效果很好。

尽管FaceDetectorFaceTracker 不相同,但它们非常相似。所以我猜这个问题与MediaCapture有关。

接下来我将尝试使用MediaCapture 捕获图像并使用FaceDetector 处理它。

如果有人在此期间有更多想法,我将不胜感激。

【问题讨论】:

  • 我可以知道你的 HoloLen2 真机的操作系统版本吗? Unity3D版本也很好分享。如果您将 Holographic 面部跟踪示例部署到您的设备,我可以知道您这边会发生什么吗? github.com/microsoft/Windows-universal-samples/tree/master/…
  • @FranklinChen-MSFT HoloLens 2 的操作系统版本是 10.0.19041.1106。我使用的 Unity 版本为 2018.4.22f1。这个例子的想法很好,我已经尝试过了,并且面部跟踪工作。但由于它是一个编译版本,而且我对编程还很陌生,所以我无法使用它对其进行逆向工程。

标签: c# unity3d uwp hololens


【解决方案1】:

这是一个官方示例,展示了如何使用 FaceTracker 类在视频流中查找人脸:Basic face tracking sample。在第 256 行,这是从捕获设备获取预览帧的要点。

但是,根据您的代码,您创建了一个 VideoFrame 对象并为其指定了属性和格式,但是您缺少调用 GetPreviewFrameAsync 以将本机网络摄像头框架转换为 VideoFrame 对象。

您可以尝试以下代码来修复它:

private async Task ProcessCurrentVideoFrameAsync()
{
    const BitmapPixelFormat InputPixelFormat = BitmapPixelFormat.Nv12;
    var deviceController = this.mediaCapture.VideoDeviceController;
    this.videoProperties = deviceController.GetMediaStreamProperties(MediaStreamType.VideoPreview) as VideoEncodingProperties;
    VideoFrame videoFrame = new VideoFrame(InputPixelFormat, (int)this.videoProperties.Width (int)this.videoProperties.Height);

//add this line code.
    await this.mediaCapture.GetPreviewFrameAsync(videoFrame);

【讨论】:

  • 感谢您提供的信息。不幸的是,我已经尝试过这个并收到以下错误消息:“请求在当前状态下无效。”知道这可能是什么原因吗?为了解释,我省略了预览的创建,因为我读过它是数据处理所必需的,但是由于它抛出了上述错误,我只想让 main 构造首先运行。
  • @Azzuen 关于“当前状态下请求无效”错误,请处理MediaCapture.FailedMediaCapture.RecordLimitationExceeded事件。它们将为您提供 MediaCapture 元素的状态信息。如果在录制过程中出现问题,Failed 事件会让您知道出现问题并告诉您发生了什么。
  • RecordLimitationExceeded 事件会在您录制时间过长并停止捕获时告诉您。
  • @Azzuen 是的,GetPreviewFrameAsync 方法需要一个 UI captureElement 来显示预览帧,但这在 Unity 中很难满足。幸运的是,MediaFrameReader 将帮助您在没有 UI 元素的情况下获取视频帧引用,该文档展示了如何使用带有 MediaCapture 的 MediaFrameReader 来获取媒体帧:[使用 MediaFrameReader 处理媒体帧](docs.microsoft.com/en-us/windows/uwp/audio-video-camera/…)。
  • Camera frames sample是本文档的完整代码。我们认为这对您来说是一个很好的方向。
猜你喜欢
  • 1970-01-01
  • 2019-01-12
  • 1970-01-01
  • 1970-01-01
  • 2017-09-15
  • 1970-01-01
  • 2011-12-30
  • 2018-03-25
  • 2012-01-18
相关资源
最近更新 更多