3

Windows.Media.FaceAnalysis DetectedFaceのオブジェクトのリストとしてLive Web Camから顔を取得できました。これらの顔をMicrosoft Cognitive Services APIに渡して顔を検出し、顔の属性を取得したいと考えています。これどうやってするの?Windows.Media.FaceAnalysis DetectedFaceリストを提供してMicrosoft認知サービスを使用して顔の属性を検出する方法?

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 
    } 
} 

答えて

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; 
} 
+0

こんにちは@MariaイネスParnisari、answer.Your回答いただきありがとうございますが、我々は一時的なの助けを借り外でソリューションを作成し、一時的なfile.Canの作成を回避することができます任意の方法を一時的file.Isを作成する必要がファイル? –

+0

@JosephAbraham私の更新された答えを見てください:) –

関連する問題