Ejemplo n.º 1
0
        /// <summary>
        /// Create face alignmentor
        /// </summary>
        /// <param name="type">Face alignment algorithm type</param>
        /// <returns>Face alignmentor</returns>
        public static IFaceAlignmentor CreateAlignmentor(FaceAlignmentType type)
        {
            ResourceKey key = ResourceKey.Unknown;

            switch (type)
            {
            case FaceAlignmentType.Asm87Points:
                key = ResourceKey.AsmAlignment;
                break;

            case FaceAlignmentType.Regression5Points:
                key = ResourceKey.RegressionAlignment;
                break;

            case FaceAlignmentType.Regression27Points:
                key = ResourceKey.RegressionAlignment27Points;
                break;

            default:
                throw new ArgumentOutOfRangeException("type", "Unsupported alignment algorithm type.");
            }

            using (var s = ResourceManager.GetStream(key))
            {
                var alignmentor = FaceAlignmentorFactory.Create(type, s);
                return(alignmentor);
            }
        }
Ejemplo n.º 2
0
        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));
        }
Ejemplo n.º 3
0
 public AlignmentWrapper(FaceAlignmentType type)
 {
     this.alignmentor = FaceAlignmentorFactory.Create(type);
     this.Type        = type;
 }