private void FaceSwapInTwoImages( Image <byte> argbImage1, Image <byte> argbImage2, Image <byte> facepointSource, out Image <byte> swapResult1) { if (argbImage1 == null || argbImage1.Channels < Color.RgbChannels) { throw new ArgumentException("Input image 1 should be a valid color image."); } if (argbImage2 == null || argbImage2.Channels < Color.RgbChannels) { throw new ArgumentException("Input image 2 should be a valid color image."); } // face detection Image <byte> imgGray1 = new ImageGray(facepointSource); //argbImage1 Stopwatch sw = Stopwatch.StartNew(); Stopwatch sw2 = Stopwatch.StartNew(); var faces1 = detector.Detect(imgGray1); if (faces1.Length < 1) { throw new InvalidOperationException("Fail to detect face in image 1."); } // pick two largest faces var face1 = faces1.OrderByDescending(y => y.Rect.Area).First(); Image <byte> imgGray2 = new ImageGray(argbImage2); sw.Restart(); var faces2 = detector.Detect(imgGray2); if (faces2.Length < 1) { throw new InvalidOperationException("Fail to detect face in image 2."); } var face2 = faces2.OrderByDescending(y => y.Rect.Area).First(); sw.Restart(); var alignmentShape1 = alignmentor.Align(imgGray1, face1.Rect); sw.Restart(); var alignmentShape2 = alignmentor.Align(imgGray2, face2.Rect); // create the face point file FaceSwapper.SaveTestData("points-Source", argbImage1, alignmentShape1); FaceSwapper.SaveTestData("facepoints-Source", facepointSource, alignmentShape1); FaceSwapper.SaveTestData("points-target", argbImage2, alignmentShape2); swapResult1 = argbImage2.Clone(); sw.Restart(); FaceSwapInternal(argbImage1, argbImage2, alignmentShape1, alignmentShape2, swapResult1); }
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)); }