Exemple #1
0
        void UseFaceDetectionModel()
        {
            var faceDetector = vision.GetFaceDetector();
            var image        = new VisionImage(ImgSample.Image);

            faceDetector.ProcessImage(image, HandleVisionFaceDetectionCallback);

            void HandleVisionFaceDetectionCallback(VisionFace [] faces, NSError error)
            {
                if (error != null)
                {
                    TxtData.Text = error.Description;
                    return;
                }

                if (faces == null || faces.Length == 0)
                {
                    TxtData.Text = "No faces were found.";
                    return;
                }

                var imageSize = ImgSample.Image.Size;

                UIGraphics.BeginImageContextWithOptions(imageSize, false, 0);
                var context = UIGraphics.GetCurrentContext();

                context.SetStrokeColor(UIColor.Red.CGColor);
                context.SetLineWidth(10);

                ImgSample.Image.Draw(CGPoint.Empty);

                foreach (var face in faces)
                {
                    context.AddRect(face.Frame);
                    context.DrawPath(CGPathDrawingMode.Stroke);
                }

                var newImage = UIGraphics.GetImageFromCurrentImageContext();

                UIGraphics.EndImageContext();

                ImgSample.Image = newImage;
            }
        }