Xamarin.Forms (Android) 人脸检测器

4

我希望在xamarin.forms中进行人脸检测。

我找到了必要的代码,但是这些代码只适用于xamarin.android。

我想将这些代码设置在xamarin.forms中。

如何使用依赖服务实现此功能?

有谁能提供这方面的信息来帮助我吗?

我的代码:

public class MainActivity : Activity
{
    //Intent code for camera activity
    private static int TAKE_PICTURE_CODE = 100;

    //Max Faces to detect in a picture
    private static int MAX_FACES = 5;

    //Bitmap of a picture taken
    private Bitmap cameraBitmap = null;

    protected override void OnCreate (Bundle bundle)
    {
        base.OnCreate (bundle);

        // Set our view from the "main" layout resource
        SetContentView (Resource.Layout.Main);

        Button take_picture = FindViewById<Button> (Resource.Id.take_picture);

        take_picture.Click  += take_picture_Clicked;

    }

    void take_picture_Clicked (object sender, EventArgs args )
    {
        //call OpenCamera() Event
        openCamera();
    }



    //OnActivityResult
    protected override void OnActivityResult (int requestCode, Result resultCode, Intent data)
    {
        base.OnActivityResult (requestCode, resultCode, data);
        //check if method return takepicturecode
        if(TAKE_PICTURE_CODE == requestCode){
            //Call a method to process image data
            processCameraImage(data);
        }
    }
    /// Open an activity for take a picture
    private void openCamera()
    {
        Intent intent = new Intent (Android.Provider.MediaStore.ActionImageCapture);
        StartActivityForResult (intent, TAKE_PICTURE_CODE);
    }

    /// <summary>
    /// Process picture taken an change UI to detect face
    /// </summary>
    /// <param name="intent">Intent.</param>
    private void processCameraImage(Intent intent)
    {
        //Change layout to main Layout
        SetContentView(Resource.Layout.detectlayout);

        Button detect_face = FindViewById<Button> (Resource.Id.detect_face);

        detect_face.Click += detect_face_Clicked;

        ImageView image_view = FindViewById<ImageView> (Resource.Id.image_view);

        //Set image 
        image_view.SetImageBitmap(cameraBitmap);
    }

    void detect_face_Clicked (object sender, EventArgs args )
    {
        //Detect Face
        detectFaces();
    }

在一张图片上检测人脸,并在每张脸上画一个正方形。
private void detectFaces(){  
//first check if picture has been taken  
if(null != cameraBitmap){  
    //get width of a picture  
    int width = cameraBitmap.Width;  
    //get height of a picture  
    int height = cameraBitmap.Height;  
    //Initialize a facedetector with the picture dimensions and the max number of faces to check  
    FaceDetector detector = new FaceDetector(width, height, MainActivity.MAX_FACES);  
    //Create an array of faces with the number of max faces to check  
    FaceDetector.Face[] faces = new FaceDetector.Face[MainActivity.MAX_FACES];  

    //create a main bitmap  
    Bitmap bitmap565 = Bitmap.CreateBitmap(width, height, Bitmap.Config.Rgb565);  
    //create a dither paint  
    Paint ditherPaint = new Paint();  
    //create a draw paint  
    Paint drawPaint = new Paint();  

    //set true dither to dither paint variable  
    ditherPaint.Dither = true;  
    //set color red for the square  
    drawPaint.Color = Color.Green;  
    //set stroke to style  
    drawPaint.SetStyle(Paint.Style.Stroke);  
    //set stroke width  
    drawPaint.StrokeWidth = 2;  

    //Create a canvas  
    Canvas canvas = new Canvas();  
    //set bitmap to canvas  
    canvas.SetBitmap(bitmap565);  
    //draw bitmap to canvas  
    canvas.DrawBitmap(cameraBitmap, 0, 0, ditherPaint);  

    //get a number of faces detected  
    int facesFound = detector.FindFaces(bitmap565, faces);  
    //mid face point  
    PointF midPoint = new PointF();  
    //eye distance variable  
    float eyeDistance = 0.0f;  
    //confidence variable  
    float confidence = 0.0f;  
    //print numbre of faces found  
    System.Console.WriteLine ("Number of faces found: " + facesFound);  

    //check if found at least one face  
    if(facesFound > 0)  
    {  
        //for each face draw a red squeare  
        for(int index=0; index<facesFound; ++index){  
            // get midpoint of a face  
            faces[index].GetMidPoint(midPoint);  
            //get eye distance  
            eyeDistance = faces[index].EyesDistance();  
            //get confidence  
            confidence = faces [index].Confidence ();  
            //print all parameters  
            System.Console.WriteLine ("Confidence: " + confidence +   
                ", Eye distance: " + eyeDistance +   
                ", Mid Point: (" + midPoint.X + ", " + midPoint.Y + ")");  
            //draw a square in the picture  
            canvas.DrawRect((int)midPoint.X - eyeDistance ,   
                (int)midPoint.Y- eyeDistance ,   
                (int)midPoint.X + eyeDistance,   
                (int)midPoint.Y + eyeDistance, drawPaint);  
        }  
    }  

    //get imageview from layout  
    ImageView imageView = (ImageView)FindViewById(Resource.Id.image_view);  
    //set image with the red squares to imageview  
    imageView.SetImageBitmap(bitmap565);  
}  

}

2个回答

1
你只需要找到如何在Xamarin.Forms中拍照,然后使用FaceDetector处理Android或Ios返回的图片即可。 这里提供了一个简单的示例,使用DependencyService在Android和Ios上拍照,由@Vaka.GopiNadhReddy提供,感谢@Vaka。
下载它,然后在你的detectFaces方法中更改一些内容,并将其添加到MainActivity中。

我已经完成了,你可以参考下面的代码:

private Bitmap detectFaces(Bitmap cameraBitmap)
{
    //first check if picture has been taken  
    if (null != cameraBitmap)
    {
        //get width of a picture  
        int width = cameraBitmap.Width;
        //get height of a picture  
        int height = cameraBitmap.Height;
        //Initialize a facedetector with the picture dimensions and the max number of faces to check  
        FaceDetector detector = new FaceDetector(width, height, MainActivity.MAX_FACES);
        //Create an array of faces with the number of max faces to check  
        FaceDetector.Face[] faces = new FaceDetector.Face[MainActivity.MAX_FACES];

        //create a main bitmap  
        Bitmap bitmap565 = Bitmap.CreateBitmap(width, height, Bitmap.Config.Rgb565);
        //create a dither paint  
        Paint ditherPaint = new Paint();
        //create a draw paint  
        Paint drawPaint = new Paint();

        //set true dither to dither paint variable  
        ditherPaint.Dither = true;
        //set color red for the square  
        drawPaint.Color = Android.Graphics.Color.Green;
        //set stroke to style  
        drawPaint.SetStyle(Paint.Style.Stroke);
        //set stroke width  
        drawPaint.StrokeWidth = 2;

        //Create a canvas  
        Canvas canvas = new Canvas();
        //set bitmap to canvas  
        canvas.SetBitmap(bitmap565);
        //draw bitmap to canvas  
        canvas.DrawBitmap(cameraBitmap, 0, 0, ditherPaint);

        //get a number of faces detected  
        int facesFound = detector.FindFaces(bitmap565, faces);
        //mid face point  
        PointF midPoint = new PointF();
        //eye distance variable  
        float eyeDistance = 0.0f;
        //confidence variable  
        float confidence = 0.0f;
        //print numbre of faces found  
        System.Console.WriteLine("Number of faces found: " + facesFound);

        //check if found at least one face  
        if (facesFound > 0)
        {
            //for each face draw a red squeare  
            for (int index = 0; index < facesFound; ++index)
            {
                // get midpoint of a face  
                faces[index].GetMidPoint(midPoint);
                //get eye distance  
                eyeDistance = faces[index].EyesDistance();
                //get confidence  
                confidence = faces[index].Confidence();
                //print all parameters  
                System.Console.WriteLine("Confidence: " + confidence +
                    ", Eye distance: " + eyeDistance +
                    ", Mid Point: (" + midPoint.X + ", " + midPoint.Y + ")");
                //draw a square in the picture  
                canvas.DrawRect((int)midPoint.X - eyeDistance,
                    (int)midPoint.Y - eyeDistance,
                    (int)midPoint.X + eyeDistance,
                    (int)midPoint.Y + eyeDistance, drawPaint);
            }
        }
        return bitmap565;

    }
    return null;

}

OnActivityResult 中,您需要添加以下代码: AppClass.bitmap = detectFaces(AppClass.bitmap); 请将其添加到这行代码下方。
AppClass.bitmap = AppClass._file.Path.LoadAndResizeBitmap(width, width);

最后,不要忘记添加:

 public static int MAX_FACES = 5;

MainActivity中。

0

我做到了

IDetectFace 接口

 public  interface IDetectFace
    {
        bool FaceDetect(string filename);
    }

detectFaces.cs

    [assembly: Xamarin.Forms.Dependency(typeof(detectFaces))]
namespace Ceptetamir.Mobil.Droid.DependencieServices
{
    class detectFaces : IDetectFace
    {
        static int TAKE_PICTURE_CODE = 100;
        static int MAX_FACES = 5;
        //static string filename = "/storage/sdcard0/Android/data/com.Ceptetamir.Mobil/files/Pictures/Test/";
        //static File file = getLatestFilefromDir(filename);
        //static Bitmap cameraBitmap = BitmapFactory.DecodeFile(file.AbsolutePath);
        public bool FaceDetect(string stream)
        {
            Bitmap cameraBitmap =  BitmapFactory.DecodeFile(stream);
            bool durum = false;
            if (null != cameraBitmap)
            {
                //get width of a picture
                int width = cameraBitmap.Width;
                //get height of a picture
                int height = cameraBitmap.Height;
                //Initialize a facedetector with the picture dimensions and the max number of faces to check
                FaceDetector detector = new FaceDetector(width, height, MAX_FACES);
                //Create an array of faces with the number of max faces to check
                Android.Media.FaceDetector.Face[] faces = new Android.Media.FaceDetector.Face[MAX_FACES];
                //create a main bitmap
                Bitmap bitmap565 = Bitmap.CreateBitmap(width, height, Bitmap.Config.Rgb565);
                //create a dither paint
                Paint ditherPaint = new Paint();
                //create a draw paint
                Paint drawPaint = new Paint();
                //set true dither to dither paint variable
                ditherPaint.Dither = true;
                //set color red for the square
                drawPaint.Color = Color.Green;
                //set stroke to style
                drawPaint.SetStyle(Paint.Style.Stroke);
                //set stroke width
                drawPaint.StrokeWidth = 2;
                //Create a canvas
                Canvas canvas = new Canvas();
                //set bitmap to canvas
                canvas.SetBitmap(bitmap565);
                //draw bitmap to canvas
                canvas.DrawBitmap(cameraBitmap, 0, 0, ditherPaint);
                //get a number of faces detected
                int facesFound = detector.FindFaces(bitmap565, faces);
                //mid face point
                PointF midPoint = new PointF();
                //eye distance variable
                float eyeDistance = 0.0f;
                //confidence variable
                float confidence = 0.0f;
                //print numbre of faces found
                System.Console.WriteLine("Number of faces found: " + facesFound);
                //check if found at least one face
                if (facesFound > 0)
                {
                    //for each face draw a red squeare
                    durum = true;
                }
            }
            return durum;
        }

并使用

DependencyService.Get<IDetectFace>().FaceDetect(file.Path);

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接