private async Task <Tuple <Image <byte>, IList <PointF> > > PrepBitmapAsync(SoftwareBitmap bitmap) { if (bitmap.PixelHeight % 2 != 0) { var resized = new SoftwareBitmap(bitmap.BitmapPixelFormat, bitmap.PixelWidth, bitmap.PixelHeight + 1); bitmap.CopyTo(resized); bitmap = resized; } Rectangle firstFace; try { var detector = await FaceDetector.CreateAsync(); var formats = FaceDetector.GetSupportedBitmapPixelFormats(); var convertedBitmap = SoftwareBitmap.Convert(bitmap, formats.First()); var detected = await detector.DetectFacesAsync(convertedBitmap); var faces = detected .Select(x => x.FaceBox) .Select(x => new Rectangle((int)x.X, (int)x.X + (int)x.Width, (int)x.Y, (int)x.Y + (int)x.Height)); if (!faces.Any()) { return(null); } firstFace = faces.First(); } catch (Exception) { Debugger.Break(); throw; } IList <PointF> points; var image = ConvertTo.Image.FromSoftwareBitmap(bitmap); try { if (alignmentor == null) { using (var stream = ResourceManager.GetStream(ResourceKey.AsmAlignment)) { alignmentor = FaceAlignmentorFactory.Create(FaceAlignmentType.Asm87Points, stream); } } var grayImage = new ImageGray(image); points = alignmentor.Align(grayImage, firstFace).ToList(); if (!points.Any()) { return(null); } } catch (Exception) { Debugger.Break(); throw; } return(new Tuple <Image <byte>, IList <PointF> >(image, points)); }
public AlignmentWrapper(FaceAlignmentType type) { this.alignmentor = FaceAlignmentorFactory.Create(type); this.Type = type; }