【问题标题】:How to detect face attributes using Microsoft Cognitive service by providing Windows.Media.FaceAnalysis DetectedFace list?如何通过提供 Windows.Media.FaceAnalysis DetectedFace 列表使用 Microsoft Cognitive 服务检测人脸属性?
【发布时间】:2018-02-28 22:28:18
【问题描述】:

我能够从实时网络摄像头获取面孔作为Windows.Media.FaceAnalysis DetectedFace 对象的列表。现在我想将这些人脸传递给 Microsoft Cognitive Services API 来检测人脸并获取​​人脸属性。我该怎么做?

IList<DetectedFace> faces = null;

// Create a VideoFrame object specifying the pixel format we want our capture image to be (NV12 bitmap in this case).
// GetPreviewFrame will convert the native webcam frame into this format.
const BitmapPixelFormat InputPixelFormat = BitmapPixelFormat.Nv12;
using (VideoFrame previewFrame = new VideoFrame(InputPixelFormat, (int)this.videoProperties.Width, (int)this.videoProperties.Height))
{
    await this.mediaCapture.GetPreviewFrameAsync(previewFrame);

    // The returned VideoFrame should be in the supported NV12 format but we need to verify this.
    if (FaceDetector.IsBitmapPixelFormatSupported(previewFrame.SoftwareBitmap.BitmapPixelFormat))
    {
        faces = await this.faceDetector.DetectFacesAsync(previewFrame.SoftwareBitmap);

        // Now pass this faces to Cognitive services API
        // faceClient.DetectAsync
    }
}

【问题讨论】:

    标签: c# uwp microsoft-cognitive face-api azure-cognitive-services


    【解决方案1】:

    DetectedFace 对象包含实际面部的边界框。因此,您可以使用这些知识创建人脸的内存流并将其发送到Face Client

    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.faceDetector.DetectFacesAsync(destinationPreviewFrame.SoftwareBitmap);
    
                foreach (var face in faces)
                {
                    // convert NV12 to RGBA16 format
                    SoftwareBitmap convertedBitmap = SoftwareBitmap.Convert(destinationPreviewFrame.SoftwareBitmap, BitmapPixelFormat.Rgba16);
    
                    // get the raw bytes of the detected face
                    byte[] rawBytes = await GetBytesFromBitmap(convertedBitmap, BitmapEncoder.BmpEncoderId, face.FaceBox);
    
                    // read the bitmap and send it to the face client
                    using (Stream stream = rawBytes.AsBuffer().AsStream())
                    {
                        var faceAttributesToReturn = new List<FaceAttributeType>()
                        {
                            FaceAttributeType.Age,
                            FaceAttributeType.Emotion,
                            FaceAttributeType.Hair
                        };
    
                        Face[] detectedFaces = await this.faceClient.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);
    
            // apply the bounds of the face
            encoder.BitmapTransform.Bounds = bounds;
    
            await encoder.FlushAsync();
    
            array = new byte[ms.Size];
    
            await ms.ReadAsync(array.AsBuffer(), (uint)ms.Size, InputStreamOptions.None);
        }
    
        return array;
    }
    

    【讨论】:

    • 嗨@Maria Ines Parnisari,谢谢您的回答。您的回答涉及创建临时文件。有什么办法可以避免创建临时文件。我们可以在没有帮助的情况下创建解决方案吗临时文件?
    猜你喜欢
    • 2017-08-22
    • 2017-12-29
    • 2017-06-09
    • 1970-01-01
    • 2020-10-24
    • 2018-12-13
    • 2023-03-24
    • 2017-05-20
    • 2013-10-18
    相关资源
    最近更新 更多